1
0

BxDolCronPruning.php 3.3 KB

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