BxDolCronCmd.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. // TODO: make alert for database pruning
  9. // TODO: move some functionality to modules, like banners, etc
  10. // TODO: remove command line options
  11. // TODO: db funtions 2 class function + prepare statements
  12. // TODO: other refactoring
  13. bx_import('BxDolCron');
  14. class BxDolCronCmd extends BxDolCron {
  15. // - Functions -
  16. function finish() {
  17. if ($output = ob_get_clean())
  18. sendMail(getParam('site_email'), getParam('site_title') . ": Periodic Report", $output, 0, array(), BX_EMAIL_NOTIFY, 'text'); // TODO: email template
  19. }
  20. function clean_database()
  21. {
  22. $oDb = BxDolDb::getInstance();
  23. $db_clean_mem_levels = (int) getParam("db_clean_mem_levels");
  24. //clear from `sys_acl_levels_members`
  25. if ($oDb->query("DELETE FROM `sys_acl_levels_members` WHERE `DateExpires` < NOW() - INTERVAL $db_clean_mem_levels DAY"))
  26. $oDb->query("OPTIMIZE TABLE `sys_acl_levels_members`");
  27. //--- Clean sessions ---//
  28. bx_import('BxDolSession');
  29. $oSession = BxDolSession::getInstance();
  30. $iSessions = $oSession->oDb->deleteExpired();
  31. // clean old views
  32. bx_import('BxDolView');
  33. $iDeletedViews = BxDolView::maintenance ();
  34. // clean old votes
  35. bx_import('BxDolVote');
  36. BxDolVote::maintenance();
  37. // clean comments ratings
  38. //bx_import('BxDolCmts');
  39. //BxDolCmts::maintenance(); // TODO: fix it
  40. // clean storage engine expired private file tokens
  41. bx_import('BxDolStorage');
  42. $iDeletedExpiredTokens = BxDolStorage::pruning();
  43. // clean outdated transcoded images
  44. bx_import('BxDolImageTranscoder');
  45. $iDeletedTranscodedImages = BxDolImageTranscoder::pruning();
  46. // clean expired keys
  47. bx_import('BxDolKey');
  48. $oKey = BxDolKey::getInstance();
  49. $iDeletedKeys = $oKey ? $oKey->prune() : 0;
  50. echo "\n- Database cleaning -\n";
  51. echo "Deleted sessions: $iSessions\n";
  52. echo "Deleted views: $iDeletedViews\n";
  53. echo "Storage expired tokens: $iDeletedExpiredTokens\n";
  54. echo "Deleted outdated transcoded images: $iDeletedTranscodedImages\n";
  55. echo "Deleted expired keys: $iDeletedKeys\n";
  56. }
  57. function del_old_all_files() {
  58. $num_tmp = 0;
  59. $num_del = 0;
  60. $file_life = 86400; // one day
  61. $dirToClean = array();
  62. $dirToClean[] = BxDolConfig::getInstance()->get('path_dynamic', 'tmp');
  63. $dirToClean[] = BX_DIRECTORY_PATH_CACHE;
  64. foreach( $dirToClean as $value )
  65. {
  66. if ( !( $lang_dir = opendir( $value ) ) )
  67. {
  68. continue;
  69. }
  70. else
  71. {
  72. while ($lang_file = readdir( $lang_dir ))
  73. {
  74. $diff = time() - filectime( $value . $lang_file);
  75. if ( $diff > $file_life && '.' != $lang_file && '..' != $lang_file && '.htaccess' !== $lang_file )
  76. {
  77. @unlink ($value . $lang_file);
  78. ++$num_del;
  79. }
  80. ++$num_tmp;
  81. }
  82. closedir( $lang_dir );
  83. }
  84. }
  85. echo "\n- Temporary files check -\n";
  86. echo "Total temp files: $num_tmp\n";
  87. echo "Deleted temp files: $num_del\n";
  88. }
  89. function processing() {
  90. set_time_limit(0);
  91. ignore_user_abort();
  92. ob_start();
  93. $iDay = date( "d" );
  94. if (getParam( "cmdDay" ) == $iDay) {
  95. echo "Already done today, bailing out\n";
  96. $this->finish();
  97. return;
  98. }
  99. setParam("cmdDay", $iDay);
  100. // clear tmp folder
  101. $this->del_old_all_files();
  102. $this->clean_database();
  103. $this->finish();
  104. }
  105. }