BxDolCronPruning.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. class BxDolCronPruning extends BxDolCron
  10. {
  11. protected function start()
  12. {
  13. set_time_limit(0);
  14. ignore_user_abort();
  15. ob_start();
  16. }
  17. protected function finish()
  18. {
  19. bx_alert('system', 'pruning', 0);
  20. if (!($sOutput = ob_get_clean()))
  21. return;
  22. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Pruning', array('pruning_output' => $sOutput, 'site_title' => getParam('site_title')), 0, 0);
  23. if ($aTemplate)
  24. sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_NOTIFY);
  25. }
  26. /**
  27. * Clean database by deleting some expired data
  28. */
  29. protected function cleanDatabase()
  30. {
  31. // clean expired membership levels
  32. $oAcl = BxDolAcl::getInstance();
  33. $iDeleteMemLevels = $oAcl ? $oAcl->maintenance() : 0;
  34. //--- Clean sessions ---//
  35. $oSession = BxDolSession::getInstance();
  36. $iSessions = $oSession ? $oSession->maintenance() : 0;
  37. // clean old views
  38. $iDeletedViews = BxDolView::maintenance ();
  39. // clean storage engine expired private file tokens
  40. $iDeletedExpiredTokens = BxDolStorage::pruning();
  41. // clean outdated transcoded images
  42. $iDeletedTranscodedImages = BxDolTranscoderImage::pruning();
  43. // clean expired keys
  44. $oKey = BxDolKey::getInstance();
  45. $iDeletedKeys = $oKey ? $oKey->prune() : 0;
  46. // clean old votes
  47. $iDeletedVotes = BxDolVote::maintenance();
  48. echo _t('_sys_pruning_db', $iDeleteMemLevels, $iSessions, $iDeletedViews, $iDeletedVotes, $iDeletedKeys, $iDeletedExpiredTokens, $iDeletedTranscodedImages);
  49. }
  50. /**
  51. * Clean tmp folders (tmp, cache) by deleting old files (by default older than 1 month)
  52. */
  53. protected function cleanTmpFolders()
  54. {
  55. $iTmpFileLife = 2592000; // one month
  56. $aDirsToClean = array(
  57. BX_DIRECTORY_PATH_TMP,
  58. BX_DIRECTORY_PATH_CACHE,
  59. BX_DIRECTORY_PATH_CACHE_PUBLIC,
  60. );
  61. $iNumTmp = 0;
  62. $iNumDel = 0;
  63. foreach ($aDirsToClean as $sDir) {
  64. if (!($h = opendir($sDir)))
  65. continue;
  66. while ($sFile = readdir($h)) {
  67. if ('.' == $sFile || '..' == $sFile || '.' == $sFile[0])
  68. continue;
  69. ++$iNumTmp;
  70. $iDiff = time() - filemtime($sDir . $sFile);
  71. if ($iDiff < $iTmpFileLife)
  72. continue;
  73. if (is_file($sDir . $sFile))
  74. @unlink ($sDir . $sFile);
  75. else
  76. @bx_rrmdir($sDir . $sFile);
  77. ++$iNumDel;
  78. }
  79. closedir($h);
  80. }
  81. echo _t('_sys_pruning_files', $iNumTmp, $iNumDel);
  82. }
  83. public function processing()
  84. {
  85. $this->start();
  86. $this->cleanTmpFolders();
  87. $this->cleanDatabase();
  88. $this->finish();
  89. }
  90. }
  91. /** @} */