PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cron.php

https://github.com/CasperGemini/e107
PHP | 197 lines | 104 code | 37 blank | 56 comment | 20 complexity | 33b05b4281ca484eb3d63ee3a2f3f705 MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/php -q
  2. <?php
  3. /*
  4. + ----------------------------------------------------------------------------+
  5. || e107 website system
  6. |
  7. | Copyright (C) 2008-2009 e107 Inc
  8. | http://e107.org
  9. |
  10. |
  11. | Released under the terms and conditions of the
  12. | GNU General Public License (http://gnu.org).
  13. |
  14. | $Source: /cvs_backup/e107_0.8/cron.php,v $
  15. | $Revision$
  16. | $Date$
  17. | $Author$
  18. +----------------------------------------------------------------------------+
  19. */
  20. // Usage: [full path to this script]cron.php --u=admin --p=password // use your admin login.
  21. // test
  22. $_E107['cli'] = TRUE;
  23. $_E107['debug'] = false;
  24. $_E107['no_online'] = TRUE;
  25. $_E107['no_forceuserupdate'] = TRUE;
  26. $_E107['no_menus'] = TRUE;
  27. // we allow theme init as cron jobs might need to access current theme templates (e.g. custom email templates)
  28. require_once(realpath(dirname(__FILE__)."/class2.php"));
  29. $pwd = ($_E107['debug'] && $_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : trim($_SERVER['argv'][1]);
  30. if($pref['e_cron_pwd'] != $pwd)
  31. {
  32. require_once(e_HANDLER."mail.php");
  33. $message = "Your Cron Schedule is not configured correctly. Your passwords do not match.
  34. <br /><br />
  35. Sent from cron: ".$pwd."<br />
  36. Stored in e107: ".$pref['e_cron_pwd']."<br /><br />
  37. You should regenerate the cron command in admin and enter it again in your server configuration.
  38. ";
  39. sendemail($pref['siteadminemail'], "e107 - Cron Schedule Misconfigured.", $message, $pref['siteadmin'],$pref['siteadminemail'], $pref['siteadmin']);
  40. exit;
  41. }
  42. e107::getCache()->CachePageMD5 = '_';
  43. e107::getCache()->set('cronLastLoad',time(),TRUE,FALSE,TRUE);
  44. // from the plugin directory:
  45. // realpath(dirname(__FILE__)."/../../")."/";
  46. $list = array();
  47. $sql = e107::getDb();
  48. if($sql->select("cron",'cron_function,cron_tab','cron_active =1'))
  49. {
  50. while($row = $sql->fetch(MYSQL_ASSOC))
  51. {
  52. list($class,$function) = explode("::",$row['cron_function'],2);
  53. $key = $class."__".$function;
  54. $list[$key] = array(
  55. 'path' => $class,
  56. 'active' => 1,
  57. 'tab' => $row['cron_tab'],
  58. 'function' => $function,
  59. 'class' => $class
  60. );
  61. }
  62. }
  63. // foreach($pref['e_cron_pref'] as $func=>$cron)
  64. // {
  65. // if($cron['active']==1)
  66. // {
  67. // $list[$func] = $cron;
  68. // }
  69. // }
  70. if($_E107['debug'] && $_SERVER['QUERY_STRING'])
  71. {
  72. echo "<h1>Cron Lists</h1>";
  73. print_a($list);
  74. }
  75. require_once(e_HANDLER."cron_class.php");
  76. $cron = new CronParser();
  77. require_once(e_HANDLER."mail.php");
  78. foreach($list as $func=>$val)
  79. {
  80. $cron->calcLastRan($val['tab']);
  81. $due = $cron->getLastRanUnix();
  82. if($_E107['debug'])
  83. {
  84. echo "<br />Cron: ".$val['function'];
  85. }
  86. if($due > (time()-45))
  87. {
  88. if($_E107['debug']) { echo "<br />Running Now...<br />path: ".$val['path']; }
  89. if(($val['path']=='_system') || is_readable(e_PLUGIN.$val['path']."/e_cron.php"))
  90. {
  91. if($val['path'] != '_system') // this is correct.
  92. {
  93. include_once(e_PLUGIN.$val['path']."/e_cron.php");
  94. }
  95. $classname = $val['class']."_cron";
  96. if(class_exists($classname, false))
  97. {
  98. $obj = new $classname;
  99. if(method_exists($obj,$val['function']))
  100. {
  101. // $mes->add("Executing config function <b>".$key." : ".$method_name."()</b>", E_MESSAGE_DEBUG);
  102. if($_E107['debug']) { echo "<br />Method Found: ".$classname."::".$val['function']."()"; }
  103. // Exception handling
  104. $methodname = $val['function'];
  105. $status = false;
  106. try
  107. {
  108. $status = $obj->$methodname();
  109. }
  110. catch (Exception $e)
  111. {
  112. $errorMData = $e->getFile().' '.$e->getLine();
  113. $errorMData .= "\n\n".$e->getCode().''.$e->getMessage();
  114. $errorMData .= "\n\n".implode("\n", $e->getTrace());
  115. //TODO log error in admin log. Pref for sending email to Administator
  116. sendemail($pref['siteadminemail'], $pref['siteadmin'].": Cron Schedule Exception", $errorMData, $pref['siteadmin'],$pref['siteadminemail'], $pref['siteadmin']);
  117. }
  118. // $status = call_user_func(array($obj,$val['function']));
  119. // If task returns value which is not boolean (bc), it'll be used as a message (send email, logs)
  120. if($status && true !== $status)
  121. {
  122. //TODO log error in admin log. Pref for sending email to Administator
  123. // echo "\nerror running the function ".$func.".\n"; // log the error.
  124. if($_E107['debug']) { echo "<br />Method returned message: [{$classname}::".$val['function'].'] '.$status; }
  125. sendemail($pref['siteadminemail'], $pref['siteadmin'].": Cron Schedule Task Report", "Method returned message: [{$classname}::".$val['function'].'] '.$status, $pref['siteadmin'], $pref['siteadminemail'], $pref['siteadmin']);
  126. }
  127. }
  128. else
  129. {
  130. if($_E107['debug']) { echo "<br />Couldn't find method: ".$val['function']; }
  131. }
  132. }
  133. else
  134. {
  135. if($_E107['debug']) { echo "<br />Couldn't find class: ".$classname; }
  136. }
  137. }
  138. }
  139. // echo "Cron Unix = ". $cron->getLastRanUnix();
  140. // echo "<br />Now = ".time();
  141. }
  142. // echo "<br />Cron '$cron_str0' last due at: " . date('r', $cron->getLastRanUnix()) . "<p>";
  143. // $cron->getLastRan() returns last due time in an array
  144. // print_a($cron->getLastRan());
  145. // echo "Debug:<br />" . nl2br($cron->getDebug());
  146. /*
  147. $cron_str1 = "3 12 * * *";
  148. if ($cron->calcLastRan($cron_str1))
  149. {
  150. echo "<p>Cron '$cron_str1' last due at: " . date('r', $cron->getLastRanUnix()) . "<p>";
  151. print_r($cron->getLastRan());
  152. }
  153. else
  154. {
  155. echo "Error parsing";
  156. }
  157. echo "Debug:<br />" . nl2br($cron->getDebug());
  158. */
  159. exit;
  160. ?>