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