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