cron_run_jobs_by_url.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /* Copyright (C) 2012 Nicolas Villa aka Boyquotes http://informetic.fr
  3. * Copyright (C) 2013 Florian Henry <forian.henry@open-cocnept.pro>
  4. * Copyright (C) 2013-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2017 Regis Houssin <regis.houssin@inodbox.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/public/cron/cron_run_jobs_by_url.php
  22. * \ingroup cron
  23. * \brief Execute pendings jobs
  24. */
  25. if (!defined('NOTOKENRENEWAL')) {
  26. define('NOTOKENRENEWAL', '1'); // Disables token renewal
  27. }
  28. if (!defined('NOREQUIREMENU')) {
  29. define('NOREQUIREMENU', '1');
  30. }
  31. if (!defined('NOREQUIREHTML')) {
  32. define('NOREQUIREHTML', '1');
  33. }
  34. if (!defined('NOREQUIREAJAX')) {
  35. define('NOREQUIREAJAX', '1');
  36. }
  37. if (!defined('NOLOGIN')) {
  38. define('NOLOGIN', '1');
  39. }
  40. if (!defined('NOIPCHECK')) {
  41. define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  42. }
  43. // So log file will have a suffix
  44. if (!defined('USESUFFIXINLOG')) {
  45. define('USESUFFIXINLOG', '_cron');
  46. }
  47. // For MultiCompany module.
  48. // Do not use GETPOST here, function is not defined and define must be done before including main.inc.php
  49. $entity = (!empty($_GET['entity']) ? (int) $_GET['entity'] : (!empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
  50. if (is_numeric($entity)) {
  51. define("DOLENTITY", $entity);
  52. }
  53. // Error if CLI mode
  54. if (php_sapi_name() == "cli") {
  55. echo "Error: This page can't be used as a CLI script. For the CLI version of script, launch cron_run_job.php available into scripts/cron/ directory.\n";
  56. exit(-1);
  57. }
  58. // librarie core
  59. // Dolibarr environment
  60. require '../../main.inc.php';
  61. // librarie jobs
  62. dol_include_once("/cron/class/cronjob.class.php");
  63. global $langs, $conf;
  64. // Language Management
  65. $langs->loadLangs(array("admin", "cron", "dict"));
  66. // Security check
  67. if (empty($conf->cron->enabled)) {
  68. httponly_accessforbidden('Module Cron not enabled');
  69. }
  70. /*
  71. * View
  72. */
  73. // current date
  74. $now = dol_now();
  75. // Check the key, avoid that a stranger starts cron
  76. $key = GETPOST('securitykey', 'alpha');
  77. if (empty($key)) {
  78. echo 'Securitykey is required. Check setup of cron jobs module.';
  79. exit;
  80. }
  81. if ($key != getDolGlobalString('CRON_KEY')) {
  82. echo 'Securitykey is wrong.';
  83. exit;
  84. }
  85. // Check the key, avoid that a stranger starts cron
  86. $userlogin = GETPOST('userlogin', 'alpha');
  87. if (empty($userlogin)) {
  88. echo 'Userlogin is required.';
  89. exit;
  90. }
  91. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  92. $user = new User($db);
  93. $result = $user->fetch('', $userlogin);
  94. if ($result < 0) {
  95. echo "User Error:".$user->error;
  96. dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
  97. exit;
  98. } else {
  99. if (empty($user->id)) {
  100. echo " User login:".$userlogin." do not exists";
  101. dol_syslog(" User login:".$userlogin." do not exists", LOG_ERR);
  102. exit;
  103. }
  104. }
  105. $user->getrights();
  106. $id = GETPOST('id', 'alpha'); // We accept non numeric id. We will filter later.
  107. // create a jobs object
  108. $object = new Cronjob($db);
  109. $filter = array();
  110. if (!empty($id)) {
  111. if (!is_numeric($id)) {
  112. echo "Error: Bad value for parameter job id";
  113. dol_syslog("cron_run_jobs.php Bad value for parameter job id", LOG_WARNING);
  114. exit;
  115. }
  116. $filter['t.rowid'] = $id;
  117. }
  118. $result = $object->fetchAll('ASC,ASC,ASC', 't.priority,t.entity,t.rowid', 0, 0, 1, $filter, 0);
  119. if ($result < 0) {
  120. echo "Error: ".$object->error;
  121. dol_syslog("cron_run_jobs.php fetch Error".$object->error, LOG_ERR);
  122. exit;
  123. }
  124. $qualifiedjobs = array();
  125. foreach ($object->lines as $val) {
  126. if (!verifCond($val->test)) {
  127. continue;
  128. }
  129. $qualifiedjobs[] = $val;
  130. }
  131. // TODO Duplicate code. This sequence of code must be shared with code into cron_run_jobs.php script.
  132. // current date
  133. $nbofjobs = count($qualifiedjobs);
  134. $nbofjobslaunchedok = 0;
  135. $nbofjobslaunchedko = 0;
  136. if (is_array($qualifiedjobs) && (count($qualifiedjobs) > 0)) {
  137. $savconf = dol_clone($conf);
  138. // Loop over job
  139. foreach ($qualifiedjobs as $line) {
  140. dol_syslog("cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label, LOG_DEBUG);
  141. echo "cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label;
  142. // Force reload of setup for the current entity
  143. if ($line->entity != $conf->entity) {
  144. dol_syslog("cron_run_jobs.php we work on another entity so we reload user and conf", LOG_DEBUG);
  145. echo " -> we change entity so we reload user and conf";
  146. $conf->entity = (empty($line->entity) ? 1 : $line->entity);
  147. $conf->setValues($db); // This make also the $mc->setValues($conf); that reload $mc->sharings
  148. // Force recheck that user is ok for the entity to process and reload permission for entity
  149. if ($conf->entity != $user->entity && $user->entity != 0) {
  150. $result = $user->fetch('', $userlogin, '', 0, $conf->entity);
  151. if ($result < 0) {
  152. echo "\nUser Error: ".$user->error."\n";
  153. dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
  154. exit(-1);
  155. } else {
  156. if ($result == 0) {
  157. echo "\nUser login: ".$userlogin." does not exists for entity ".$conf->entity."\n";
  158. dol_syslog("User login:".$userlogin." does not exists", LOG_ERR);
  159. exit(-1);
  160. }
  161. }
  162. $user->getrights();
  163. }
  164. }
  165. //If date_next_jobs is less of current date, execute the program, and store the execution time of the next execution in database
  166. if (($line->datenextrun < $now) && (empty($line->datestart) || $line->datestart <= $now) && (empty($line->dateend) || $line->dateend >= $now)) {
  167. echo " - qualified";
  168. dol_syslog("cron_run_jobs.php line->datenextrun:".dol_print_date($line->datenextrun, 'dayhourrfc')." line->datestart:".dol_print_date($line->datestart, 'dayhourrfc')." line->dateend:".dol_print_date($line->dateend, 'dayhourrfc')." now:".dol_print_date($now, 'dayhourrfc'));
  169. $cronjob = new Cronjob($db);
  170. $result = $cronjob->fetch($line->id);
  171. if ($result < 0) {
  172. echo "Error cronjobid: ".$line->id." cronjob->fetch: ".$cronjob->error."\n";
  173. echo "Failed to fetch job ".$line->id."\n";
  174. dol_syslog("cron_run_jobs.php::fetch Error".$cronjob->error, LOG_ERR);
  175. exit;
  176. }
  177. // Execute job
  178. $result = $cronjob->run_jobs($userlogin);
  179. if ($result < 0) {
  180. echo "Error cronjobid: ".$line->id." cronjob->run_job: ".$cronjob->error."\n";
  181. echo "At least one job failed. Go on menu Home-Setup-Admin tools to see result for each job.\n";
  182. echo "You can also enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
  183. dol_syslog("cron_run_jobs.php::run_jobs Error".$cronjob->error, LOG_ERR);
  184. $nbofjobslaunchedko++;
  185. } else {
  186. $nbofjobslaunchedok++;
  187. }
  188. echo " - result of run_jobs = ".$result;
  189. // We re-program the next execution and stores the last execution time for this job
  190. $result = $cronjob->reprogram_jobs($userlogin, $now);
  191. if ($result < 0) {
  192. echo "Error cronjobid: ".$line->id." cronjob->reprogram_job: ".$cronjob->error."\n";
  193. echo "Enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
  194. dol_syslog("cron_run_jobs.php::reprogram_jobs Error".$cronjob->error, LOG_ERR);
  195. exit;
  196. }
  197. echo " - reprogrammed\n";
  198. } else {
  199. echo " - not qualified\n";
  200. dol_syslog("cron_run_jobs.php job not qualified line->datenextrun:".dol_print_date($line->datenextrun, 'dayhourrfc')." line->datestart:".dol_print_date($line->datestart, 'dayhourrfc')." line->dateend:".dol_print_date($line->dateend, 'dayhourrfc')." now:".dol_print_date($now, 'dayhourrfc'));
  201. }
  202. }
  203. $conf = $savconf;
  204. echo "Result: ".($nbofjobs)." jobs - ".($nbofjobslaunchedok + $nbofjobslaunchedko)." launched = ".$nbofjobslaunchedok." OK + ".$nbofjobslaunchedko." KO";
  205. } else {
  206. echo "Result: No active jobs found.";
  207. }
  208. $db->close();