dolibarr 21.0.0-beta
cron_run_jobs_by_url.php
Go to the documentation of this file.
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28if (!defined('NOTOKENRENEWAL')) {
29 define('NOTOKENRENEWAL', '1'); // Disables token renewal
30}
31if (!defined('NOREQUIREMENU')) {
32 define('NOREQUIREMENU', '1');
33}
34if (!defined('NOREQUIREHTML')) {
35 define('NOREQUIREHTML', '1');
36}
37if (!defined('NOREQUIREAJAX')) {
38 define('NOREQUIREAJAX', '1');
39}
40if (!defined('NOLOGIN')) {
41 define('NOLOGIN', '1');
42}
43if (!defined('NOIPCHECK')) {
44 define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
45}
46
47// So log file will have a suffix
48if (!defined('USESUFFIXINLOG')) {
49 define('USESUFFIXINLOG', '_cron');
50}
51
52// For MultiCompany module.
53// Do not use GETPOST here, function is not defined and define must be done before including main.inc.php
54// Because 2 entities can have the same ref
55$entity = (!empty($_GET['entity']) ? (int) $_GET['entity'] : (!empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
56if (is_numeric($entity)) {
57 define("DOLENTITY", $entity);
58}
59
60// Error if CLI mode
61if (php_sapi_name() == "cli") {
62 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";
63 exit(-1);
64}
65
66// core library
67// Dolibarr environment
68require '../../main.inc.php';
69
70// cron jobs library
71dol_include_once("/cron/class/cronjob.class.php");
72
73global $langs, $conf;
74
75// Language Management
76$langs->loadLangs(array("admin", "cron", "dict"));
77
78// Security check
79if (empty($conf->cron->enabled)) {
80 httponly_accessforbidden('Module Cron not enabled');
81}
82
83
84
85/*
86 * View
87 */
88
89// current date
90$now = dol_now();
91
92// Check the key, avoid that a stranger starts cron
93$key = GETPOST('securitykey', 'alpha');
94if (empty($key)) {
95 echo 'Securitykey is required. Check setup of cron jobs module.';
96 exit;
97}
98if ($key != getDolGlobalString('CRON_KEY')) {
99 echo 'Securitykey is wrong.';
100 exit;
101}
102// Check the key, avoid that a stranger starts cron
103$userlogin = GETPOST('userlogin', 'alpha');
104if (empty($userlogin)) {
105 echo 'Userlogin is required.';
106 exit;
107}
108require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
109$user = new User($db);
110$result = $user->fetch(0, $userlogin);
111if ($result < 0) {
112 echo "User Error:".$user->error;
113 dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
114 exit;
115} else {
116 if (empty($user->id)) {
117 echo " User login:".$userlogin." do not exists";
118 dol_syslog(" User login:".$userlogin." do not exists", LOG_ERR);
119 exit;
120 }
121}
122$user->loadRights();
123
124$id = GETPOST('id', 'alpha'); // We accept non numeric id. We will filter later.
125
126
127// create a jobs object
128$object = new Cronjob($db);
129
130$filter = array();
131if (!empty($id)) {
132 if (!is_numeric($id)) {
133 echo "Error: Bad value for parameter job id";
134 dol_syslog("cron_run_jobs.php Bad value for parameter job id", LOG_WARNING);
135 exit;
136 }
137 $filter['t.rowid'] = $id;
138}
139
140$result = $object->fetchAll('ASC,ASC,ASC', 't.priority,t.entity,t.rowid', 0, 0, 1, $filter, 0);
141if ($result < 0) {
142 echo "Error: ".$object->error;
143 dol_syslog("cron_run_jobs.php fetch Error".$object->error, LOG_ERR);
144 exit;
145}
146
147// TODO Duplicate code. This sequence of code must be shared with code into cron_run_jobs.php script.
148
149// current date
150$nbofjobs = count($object->lines);
151$nbofjobslaunchedok = 0;
152$nbofjobslaunchedko = 0;
153
154if (is_array($object->lines) && (count($object->lines) > 0)) {
155 $savconf = dol_clone($conf, 0);
156
157 // Loop over job
158 foreach ($object->lines as $line) {
159 dol_syslog("cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label, LOG_DEBUG);
160 echo "cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label;
161
162 // Force reload of setup for the current entity
163 if ($line->entity != $conf->entity) {
164 dol_syslog("cron_run_jobs.php we work on another entity so we reload user and conf", LOG_DEBUG);
165 echo " -> we change entity so we reload user and conf";
166
167 $conf->entity = (empty($line->entity) ? 1 : $line->entity);
168 $conf->setValues($db); // This make also the $mc->setValues($conf); that reload $mc->sharings
169
170 // Force recheck that user is ok for the entity to process and reload permission for entity
171 if ($conf->entity != $user->entity && $user->entity != 0) {
172 $result = $user->fetch(0, $userlogin, '', 0, $conf->entity);
173 if ($result < 0) {
174 echo "\nUser Error: ".$user->error."\n";
175 dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
176 exit(-1);
177 } else {
178 if ($result == 0) {
179 echo "\nUser login: ".$userlogin." does not exists for entity ".$conf->entity."\n";
180 dol_syslog("User login:".$userlogin." does not exists", LOG_ERR);
181 exit(-1);
182 }
183 }
184 $user->loadRights();
185 }
186 }
187
188 if (!verifCond($line->test)) {
189 continue;
190 }
191
192 //If date_next_jobs is less of current date, execute the program, and store the execution time of the next execution in database
193 $datenextrunok = (empty($line->datenextrun) || (int) $line->datenextrun < $now);
194 $datestartok = (empty($line->datestart) || $line->datestart <= $now);
195 $dateendok = (empty($line->dateend) || $line->dateend >= $now);
196 if ($datenextrunok && $datestartok && $dateendok) {
197 echo " - qualified";
198
199 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'));
200
201 $cronjob = new Cronjob($db);
202 $result = $cronjob->fetch($line->id);
203 if ($result < 0) {
204 echo "Error cronjobid: ".$line->id." cronjob->fetch: ".$cronjob->error."\n";
205 echo "Failed to fetch job ".$line->id."\n";
206 dol_syslog("cron_run_jobs.php::fetch Error".$cronjob->error, LOG_ERR);
207 exit;
208 }
209 // Execute job
210 $result = $cronjob->run_jobs($userlogin);
211 if ($result < 0) {
212 echo "Error cronjobid: ".$line->id." cronjob->run_job: ".$cronjob->error."\n";
213 echo "At least one job failed. Go on menu Home-Setup-Admin tools to see result for each job.\n";
214 echo "You can also enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
215 dol_syslog("cron_run_jobs.php::run_jobs Error".$cronjob->error, LOG_ERR);
216 $nbofjobslaunchedko++;
217 $resultstring = 'KO';
218 } else {
219 $nbofjobslaunchedok++;
220 $resultstring = 'OK';
221 }
222
223 echo "Result of run_jobs = ".$resultstring." result = ".$result;
224
225 // We re-program the next execution and stores the last execution time for this job
226 $result = $cronjob->reprogram_jobs($userlogin, $now);
227 if ($result < 0) {
228 echo " - Error cronjobid: ".$line->id." cronjob->reprogram_job: ".$cronjob->error."\n";
229 echo "Enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
230 dol_syslog("cron_run_jobs.php::reprogram_jobs Error".$cronjob->error, LOG_ERR);
231 exit(1);
232 }
233
234 echo " - Job re-scheduled\n";
235 } else {
236 echo " - not qualified (datenextrunok=".($datenextrunok ?: 0).", datestartok=".($datestartok ?: 0).", dateendok=".($dateendok ?: 0).")\n";
237
238 dol_syslog("cron_run_jobs.php job ".$line->id." 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'));
239 }
240 }
241
242 $conf = $savconf;
243
244 echo "Result: ".($nbofjobs)." jobs - ".($nbofjobslaunchedok + $nbofjobslaunchedko)." launched = ".$nbofjobslaunchedok." OK + ".$nbofjobslaunchedko." KO";
245} else {
246 echo "Result: No active jobs found.";
247}
248
249$db->close();
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Cron Job class.
Class to manage Dolibarr users.
verifCond($strToEvaluate, $onlysimplestring='1')
Verify if condition in string is ok or not.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_clone($object, $native=2)
Create a clone of instance of object (new instance with same value for each properties) With native =...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.