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 * 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 dol_syslog("cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label, LOG_DEBUG);
165 echo "cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label;
166
167 // Force reload of setup for the current entity
168 if ($line->entity != $conf->entity) {
169 dol_syslog("cron_run_jobs.php we work on another entity so we reload user and conf", LOG_DEBUG);
170 echo " -> we change entity so we reload user and conf";
171
172 $conf->entity = (empty($line->entity) ? 1 : $line->entity);
173 $conf->setValues($db); // This make also the $mc->setValues($conf); that reload $mc->sharings
174
175 // Force recheck that user is ok for the entity to process and reload permission for entity
176 if ($conf->entity != $user->entity && $user->entity != 0) {
177 $result = $user->fetch(0, $userlogin, '', 0, $conf->entity);
178 if ($result < 0) {
179 echo "\nUser Error: ".$user->error."\n";
180 dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
181 exit(-1);
182 } else {
183 if ($result == 0) {
184 echo "\nUser login: ".$userlogin." does not exists for entity ".$conf->entity."\n";
185 dol_syslog("User login:".$userlogin." does not exists", LOG_ERR);
186 exit(-1);
187 }
188 }
189 $user->loadRights();
190 }
191 }
192
193 if (!verifCond($line->test)) {
194 continue;
195 }
196
197 //If date_next_jobs is less of current date, execute the program, and store the execution time of the next execution in database
198 $datenextrunok = (empty($line->datenextrun) || (int) $line->datenextrun < $now);
199 $datestartok = (empty($line->datestart) || $line->datestart <= $now);
200 $dateendok = (empty($line->dateend) || $line->dateend >= $now);
201 if ($datenextrunok && $datestartok && $dateendok) {
202 echo " - qualified";
203
204 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'));
205
206 $cronjob = new Cronjob($db);
207 $result = $cronjob->fetch($line->id);
208 if ($result < 0) {
209 echo "Error cronjobid: ".$line->id." cronjob->fetch: ".$cronjob->error."\n";
210 echo "Failed to fetch job ".$line->id."\n";
211 dol_syslog("cron_run_jobs.php::fetch Error".$cronjob->error, LOG_ERR);
212 exit;
213 }
214 // Execute job
215 $result = $cronjob->run_jobs($userlogin);
216 if ($result < 0) {
217 echo "Error cronjobid: ".$line->id." cronjob->run_job: ".$cronjob->error."\n";
218 echo "At least one job failed. Go on menu Home-Setup-Admin tools to see result for each job.\n";
219 echo "You can also enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
220 dol_syslog("cron_run_jobs.php::run_jobs Error".$cronjob->error, LOG_ERR);
221 $nbofjobslaunchedko++;
222 $resultstring = 'KO';
223 } else {
224 $nbofjobslaunchedok++;
225 $resultstring = 'OK';
226 }
227
228 echo "Result of run_jobs = ".$resultstring." result = ".$result;
229
230 // We re-program the next execution and stores the last execution time for this job
231 $result = $cronjob->reprogram_jobs($userlogin, $now);
232 if ($result < 0) {
233 echo " - Error cronjobid: ".$line->id." cronjob->reprogram_job: ".$cronjob->error."\n";
234 echo "Enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
235 dol_syslog("cron_run_jobs.php::reprogram_jobs Error".$cronjob->error, LOG_ERR);
236 exit(1);
237 }
238
239 echo " - Job re-scheduled\n";
240 } else {
241 echo " - not qualified (datenextrunok=".($datenextrunok ?: 0).", datestartok=".($datestartok ?: 0).", dateendok=".($dateendok ?: 0).")\n";
242
243 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'));
244 }
245 }
246
247 $conf = $savconf;
248
249 echo "Result: ".($nbofjobs)." jobs - ".($nbofjobslaunchedok + $nbofjobslaunchedko)." launched = ".$nbofjobslaunchedok." OK + ".$nbofjobslaunchedko." KO";
250} else {
251 echo "Result: No active jobs found.";
252}
253
254$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.