dolibarr 20.0.0
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// Because 2 entities can have the same ref
54$entity = (!empty($_GET['entity']) ? (int) $_GET['entity'] : (!empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
55if (is_numeric($entity)) {
56 define("DOLENTITY", $entity);
57}
58
59// Error if CLI mode
60if (php_sapi_name() == "cli") {
61 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";
62 exit(-1);
63}
64
65// core library
66// Dolibarr environment
67require '../../main.inc.php';
68
69// cron jobs library
70dol_include_once("/cron/class/cronjob.class.php");
71
72global $langs, $conf;
73
74// Language Management
75$langs->loadLangs(array("admin", "cron", "dict"));
76
77// Security check
78if (empty($conf->cron->enabled)) {
79 httponly_accessforbidden('Module Cron not enabled');
80}
81
82
83
84/*
85 * View
86 */
87
88// current date
89$now = dol_now();
90
91// Check the key, avoid that a stranger starts cron
92$key = GETPOST('securitykey', 'alpha');
93if (empty($key)) {
94 echo 'Securitykey is required. Check setup of cron jobs module.';
95 exit;
96}
97if ($key != getDolGlobalString('CRON_KEY')) {
98 echo 'Securitykey is wrong.';
99 exit;
100}
101// Check the key, avoid that a stranger starts cron
102$userlogin = GETPOST('userlogin', 'alpha');
103if (empty($userlogin)) {
104 echo 'Userlogin is required.';
105 exit;
106}
107require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
108$user = new User($db);
109$result = $user->fetch('', $userlogin);
110if ($result < 0) {
111 echo "User Error:".$user->error;
112 dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
113 exit;
114} else {
115 if (empty($user->id)) {
116 echo " User login:".$userlogin." do not exists";
117 dol_syslog(" User login:".$userlogin." do not exists", LOG_ERR);
118 exit;
119 }
120}
121$user->getrights();
122
123$id = GETPOST('id', 'alpha'); // We accept non numeric id. We will filter later.
124
125
126// create a jobs object
127$object = new Cronjob($db);
128
129$filter = array();
130if (!empty($id)) {
131 if (!is_numeric($id)) {
132 echo "Error: Bad value for parameter job id";
133 dol_syslog("cron_run_jobs.php Bad value for parameter job id", LOG_WARNING);
134 exit;
135 }
136 $filter['t.rowid'] = $id;
137}
138
139$result = $object->fetchAll('ASC,ASC,ASC', 't.priority,t.entity,t.rowid', 0, 0, 1, $filter, 0);
140if ($result < 0) {
141 echo "Error: ".$object->error;
142 dol_syslog("cron_run_jobs.php fetch Error".$object->error, LOG_ERR);
143 exit;
144}
145
146// TODO Duplicate code. This sequence of code must be shared with code into cron_run_jobs.php script.
147
148// current date
149$nbofjobs = count($object->lines);
150$nbofjobslaunchedok = 0;
151$nbofjobslaunchedko = 0;
152
153if (is_array($object->lines) && (count($object->lines) > 0)) {
154 $savconf = dol_clone($conf, 0);
155
156 // Loop over job
157 foreach ($object->lines as $line) {
158 dol_syslog("cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label, LOG_DEBUG);
159 echo "cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label;
160
161 // Force reload of setup for the current entity
162 if ($line->entity != $conf->entity) {
163 dol_syslog("cron_run_jobs.php we work on another entity so we reload user and conf", LOG_DEBUG);
164 echo " -> we change entity so we reload user and conf";
165
166 $conf->entity = (empty($line->entity) ? 1 : $line->entity);
167 $conf->setValues($db); // This make also the $mc->setValues($conf); that reload $mc->sharings
168
169 // Force recheck that user is ok for the entity to process and reload permission for entity
170 if ($conf->entity != $user->entity && $user->entity != 0) {
171 $result = $user->fetch('', $userlogin, '', 0, $conf->entity);
172 if ($result < 0) {
173 echo "\nUser Error: ".$user->error."\n";
174 dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
175 exit(-1);
176 } else {
177 if ($result == 0) {
178 echo "\nUser login: ".$userlogin." does not exists for entity ".$conf->entity."\n";
179 dol_syslog("User login:".$userlogin." does not exists", LOG_ERR);
180 exit(-1);
181 }
182 }
183 $user->getrights();
184 }
185 }
186
187 if (!verifCond($line->test)) {
188 continue;
189 }
190
191 //If date_next_jobs is less of current date, execute the program, and store the execution time of the next execution in database
192 $datenextrunok = (empty($line->datenextrun) || (int) $line->datenextrun < $now);
193 $datestartok = (empty($line->datestart) || $line->datestart <= $now);
194 $dateendok = (empty($line->dateend) || $line->dateend >= $now);
195 if ($datenextrunok && $datestartok && $dateendok) {
196 echo " - qualified";
197
198 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'));
199
200 $cronjob = new Cronjob($db);
201 $result = $cronjob->fetch($line->id);
202 if ($result < 0) {
203 echo "Error cronjobid: ".$line->id." cronjob->fetch: ".$cronjob->error."\n";
204 echo "Failed to fetch job ".$line->id."\n";
205 dol_syslog("cron_run_jobs.php::fetch Error".$cronjob->error, LOG_ERR);
206 exit;
207 }
208 // Execute job
209 $result = $cronjob->run_jobs($userlogin);
210 if ($result < 0) {
211 echo "Error cronjobid: ".$line->id." cronjob->run_job: ".$cronjob->error."\n";
212 echo "At least one job failed. Go on menu Home-Setup-Admin tools to see result for each job.\n";
213 echo "You can also enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
214 dol_syslog("cron_run_jobs.php::run_jobs Error".$cronjob->error, LOG_ERR);
215 $nbofjobslaunchedko++;
216 $resultstring = 'KO';
217 } else {
218 $nbofjobslaunchedok++;
219 $resultstring = 'OK';
220 }
221
222 echo "Result of run_jobs = ".$resultstring." result = ".$result;
223
224 // We re-program the next execution and stores the last execution time for this job
225 $result = $cronjob->reprogram_jobs($userlogin, $now);
226 if ($result < 0) {
227 echo "Error cronjobid: ".$line->id." cronjob->reprogram_job: ".$cronjob->error."\n";
228 echo "Enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
229 dol_syslog("cron_run_jobs.php::reprogram_jobs Error".$cronjob->error, LOG_ERR);
230 exit;
231 }
232
233 echo "Job re-scheduled\n";
234 } else {
235 echo " - not qualified (datenextrunok=".($datenextrunok ?: 0).", datestartok=".($datestartok ?: 0).", dateendok=".($dateendok ?: 0).")\n";
236
237 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'));
238 }
239 }
240
241 $conf = $savconf;
242
243 echo "Result: ".($nbofjobs)." jobs - ".($nbofjobslaunchedok + $nbofjobslaunchedko)." launched = ".$nbofjobslaunchedok." OK + ".$nbofjobslaunchedko." KO";
244} else {
245 echo "Result: No active jobs found.";
246}
247
248$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.
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.
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.