cronjob.class.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. <?php
  2. /* Copyright (C) 2007-2019 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file cron/class/cronjob.class.php
  20. * \ingroup cron
  21. */
  22. // Put here all includes required by your class file
  23. require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
  24. /**
  25. * Cron Job class
  26. */
  27. class Cronjob extends CommonObject
  28. {
  29. /**
  30. * @var string ID to identify managed object
  31. */
  32. public $element = 'cronjob';
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element = 'cronjob';
  37. /**
  38. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  39. */
  40. public $picto = 'cron';
  41. /**
  42. * @var int Entity
  43. */
  44. public $entity;
  45. /**
  46. * @var string Job type
  47. */
  48. public $jobtype;
  49. /**
  50. * @var string|int Date for last cron object update
  51. */
  52. public $tms = '';
  53. /**
  54. * @var string|int Date for cron job create
  55. */
  56. public $datec = '';
  57. /**
  58. * @var string Cron Job label
  59. */
  60. public $label;
  61. /**
  62. * @var string Job command
  63. */
  64. public $command;
  65. public $classesname;
  66. public $objectname;
  67. public $methodename;
  68. public $params;
  69. public $md5params;
  70. public $module_name;
  71. public $priority;
  72. /**
  73. * @var string|int Date for last job execution
  74. */
  75. public $datelastrun = '';
  76. /**
  77. * @var string|int Date for next job execution
  78. */
  79. public $datenextrun = '';
  80. /**
  81. * @var string|int Date for end job execution
  82. */
  83. public $dateend = '';
  84. /**
  85. * @var string|int Date for first start job execution
  86. */
  87. public $datestart = '';
  88. /**
  89. * @var string|int Date for last result job execution
  90. */
  91. public $datelastresult = '';
  92. /**
  93. * @var string Last result from end job execution
  94. */
  95. public $lastresult;
  96. /**
  97. * @var string Last output from end job execution
  98. */
  99. public $lastoutput;
  100. /**
  101. * @var string Unit frequency of job execution
  102. */
  103. public $unitfrequency;
  104. /**
  105. * @var int Frequency of job execution
  106. */
  107. public $frequency;
  108. /**
  109. * @var int Status
  110. */
  111. public $status;
  112. /**
  113. * @var int Is job processing
  114. */
  115. public $processing;
  116. /**
  117. * @var int The job current PID
  118. */
  119. public $pid;
  120. /**
  121. * @var int ID
  122. */
  123. public $fk_user_author;
  124. /**
  125. * @var int ID
  126. */
  127. public $fk_user_mod;
  128. /**
  129. * @var int Number of run job execution
  130. */
  131. public $nbrun;
  132. /**
  133. * @var int Maximum run job execution
  134. */
  135. public $maxrun;
  136. /**
  137. * @var string Libname
  138. */
  139. public $libname;
  140. /**
  141. * @var string A test condition to know if job is visible/qualified
  142. */
  143. public $test;
  144. const STATUS_DISABLED = 0;
  145. const STATUS_ENABLED = 1;
  146. const STATUS_ARCHIVED = 2;
  147. /**
  148. * Constructor
  149. *
  150. * @param DoliDb $db Database handler
  151. */
  152. public function __construct($db)
  153. {
  154. $this->db = $db;
  155. }
  156. /**
  157. * Create object into database
  158. *
  159. * @param User $user User that creates
  160. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  161. * @return int <0 if KO, Id of created object if OK
  162. */
  163. public function create($user, $notrigger = 0)
  164. {
  165. global $conf, $langs;
  166. $error = 0;
  167. $now = dol_now();
  168. // Clean parameters
  169. if (isset($this->label)) {
  170. $this->label = trim($this->label);
  171. }
  172. if (isset($this->jobtype)) {
  173. $this->jobtype = trim($this->jobtype);
  174. }
  175. if (isset($this->command)) {
  176. $this->command = trim($this->command);
  177. }
  178. if (isset($this->classesname)) {
  179. $this->classesname = trim($this->classesname);
  180. }
  181. if (isset($this->objectname)) {
  182. $this->objectname = trim($this->objectname);
  183. }
  184. if (isset($this->methodename)) {
  185. $this->methodename = trim($this->methodename);
  186. }
  187. if (isset($this->params)) {
  188. $this->params = trim($this->params);
  189. }
  190. if (isset($this->md5params)) {
  191. $this->md5params = trim($this->md5params);
  192. }
  193. if (isset($this->module_name)) {
  194. $this->module_name = trim($this->module_name);
  195. }
  196. if (isset($this->priority)) {
  197. $this->priority = trim($this->priority);
  198. }
  199. if (isset($this->lastoutput)) {
  200. $this->lastoutput = trim($this->lastoutput);
  201. }
  202. if (isset($this->lastresult)) {
  203. $this->lastresult = trim($this->lastresult);
  204. }
  205. if (isset($this->unitfrequency)) {
  206. $this->unitfrequency = trim($this->unitfrequency);
  207. }
  208. if (isset($this->frequency)) {
  209. $this->frequency = trim($this->frequency);
  210. }
  211. if (isset($this->status)) {
  212. $this->status = trim($this->status);
  213. }
  214. if (isset($this->note_private)) {
  215. $this->note_private = trim($this->note_private);
  216. }
  217. if (isset($this->nbrun)) {
  218. $this->nbrun = (int) $this->nbrun;
  219. }
  220. if (isset($this->maxrun)) {
  221. $this->maxrun = (int) $this->maxrun;
  222. }
  223. if (isset($this->libname)) {
  224. $this->libname = trim($this->libname);
  225. }
  226. if (isset($this->test)) {
  227. $this->test = trim($this->test);
  228. }
  229. // Check parameters
  230. // Put here code to add a control on parameters values
  231. if (dol_strlen($this->datenextrun) == 0) {
  232. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
  233. $error++;
  234. }
  235. if (empty($this->label)) {
  236. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
  237. $error++;
  238. }
  239. if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
  240. $this->errors[] = $langs->trans('CronErrEndDateStartDt');
  241. $error++;
  242. }
  243. if (empty($this->unitfrequency)) {
  244. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
  245. $error++;
  246. }
  247. if (($this->jobtype == 'command') && (empty($this->command))) {
  248. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
  249. $error++;
  250. }
  251. if (($this->jobtype == 'method') && (empty($this->classesname))) {
  252. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
  253. $error++;
  254. }
  255. if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
  256. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
  257. $error++;
  258. }
  259. if (($this->jobtype == 'method') && (empty($this->objectname))) {
  260. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
  261. $error++;
  262. }
  263. if (($this->jobtype == 'function') && (empty($this->libname))) {
  264. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
  265. $error++;
  266. }
  267. // Insert request
  268. $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
  269. $sql .= "entity,";
  270. $sql .= "datec,";
  271. $sql .= "jobtype,";
  272. $sql .= "label,";
  273. $sql .= "command,";
  274. $sql .= "classesname,";
  275. $sql .= "objectname,";
  276. $sql .= "methodename,";
  277. $sql .= "params,";
  278. $sql .= "md5params,";
  279. $sql .= "module_name,";
  280. $sql .= "priority,";
  281. $sql .= "datelastrun,";
  282. $sql .= "datenextrun,";
  283. $sql .= "dateend,";
  284. $sql .= "datestart,";
  285. $sql .= "lastresult,";
  286. $sql .= "datelastresult,";
  287. $sql .= "lastoutput,";
  288. $sql .= "unitfrequency,";
  289. $sql .= "frequency,";
  290. $sql .= "status,";
  291. $sql .= "fk_user_author,";
  292. $sql .= "fk_user_mod,";
  293. $sql .= "note,";
  294. $sql .= "nbrun,";
  295. $sql .= "maxrun,";
  296. $sql .= "libname,";
  297. $sql .= "test";
  298. $sql .= ") VALUES (";
  299. $sql .= " ".(!isset($this->entity) ? $conf->entity : $this->db->escape($this->entity)).",";
  300. $sql .= " '".$this->db->idate($now)."',";
  301. $sql .= " ".(!isset($this->jobtype) ? 'NULL' : "'".$this->db->escape($this->jobtype)."'").",";
  302. $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
  303. $sql .= " ".(!isset($this->command) ? 'NULL' : "'".$this->db->escape($this->command)."'").",";
  304. $sql .= " ".(!isset($this->classesname) ? 'NULL' : "'".$this->db->escape($this->classesname)."'").",";
  305. $sql .= " ".(!isset($this->objectname) ? 'NULL' : "'".$this->db->escape($this->objectname)."'").",";
  306. $sql .= " ".(!isset($this->methodename) ? 'NULL' : "'".$this->db->escape($this->methodename)."'").",";
  307. $sql .= " ".(!isset($this->params) ? 'NULL' : "'".$this->db->escape($this->params)."'").",";
  308. $sql .= " ".(!isset($this->md5params) ? 'NULL' : "'".$this->db->escape($this->md5params)."'").",";
  309. $sql .= " ".(!isset($this->module_name) ? 'NULL' : "'".$this->db->escape($this->module_name)."'").",";
  310. $sql .= " ".(!isset($this->priority) ? '0' : $this->priority).",";
  311. $sql .= " ".(!isset($this->datelastrun) || dol_strlen($this->datelastrun) == 0 ? 'NULL' : "'".$this->db->idate($this->datelastrun)."'").",";
  312. $sql .= " ".(!isset($this->datenextrun) || dol_strlen($this->datenextrun) == 0 ? 'NULL' : "'".$this->db->idate($this->datenextrun)."'").",";
  313. $sql .= " ".(!isset($this->dateend) || dol_strlen($this->dateend) == 0 ? 'NULL' : "'".$this->db->idate($this->dateend)."'").",";
  314. $sql .= " ".(!isset($this->datestart) || dol_strlen($this->datestart) == 0 ? 'NULL' : "'".$this->db->idate($this->datestart)."'").",";
  315. $sql .= " ".(!isset($this->lastresult) ? 'NULL' : "'".$this->db->escape($this->lastresult)."'").",";
  316. $sql .= " ".(!isset($this->datelastresult) || dol_strlen($this->datelastresult) == 0 ? 'NULL' : "'".$this->db->idate($this->datelastresult)."'").",";
  317. $sql .= " ".(!isset($this->lastoutput) ? 'NULL' : "'".$this->db->escape($this->lastoutput)."'").",";
  318. $sql .= " ".(!isset($this->unitfrequency) ? 'NULL' : "'".$this->db->escape($this->unitfrequency)."'").",";
  319. $sql .= " ".(!isset($this->frequency) ? '0' : $this->frequency).",";
  320. $sql .= " ".(!isset($this->status) ? '0' : $this->status).",";
  321. $sql .= " ".$user->id.",";
  322. $sql .= " ".$user->id.",";
  323. $sql .= " ".(!isset($this->note_private) ? 'NULL' : "'".$this->db->escape($this->note_private)."'").",";
  324. $sql .= " ".(!isset($this->nbrun) ? '0' : $this->db->escape($this->nbrun)).",";
  325. $sql .= " ".(empty($this->maxrun) ? '0' : $this->db->escape($this->maxrun)).",";
  326. $sql .= " ".(!isset($this->libname) ? 'NULL' : "'".$this->db->escape($this->libname)."'").",";
  327. $sql .= " ".(!isset($this->test) ? 'NULL' : "'".$this->db->escape($this->test)."'")."";
  328. $sql .= ")";
  329. $this->db->begin();
  330. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  331. $resql = $this->db->query($sql);
  332. if (!$resql) {
  333. $error++;
  334. $this->errors[] = "Error ".$this->db->lasterror();
  335. }
  336. if (!$error) {
  337. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."cronjob");
  338. }
  339. // Commit or rollback
  340. if ($error) {
  341. $this->db->rollback();
  342. return -1 * $error;
  343. } else {
  344. $this->db->commit();
  345. return $this->id;
  346. }
  347. }
  348. /**
  349. * Load object in memory from the database
  350. *
  351. * @param int $id Id object
  352. * @param string $objectname Object name
  353. * @param string $methodname Method name
  354. * @return int <0 if KO, >0 if OK
  355. */
  356. public function fetch($id, $objectname = '', $methodname = '')
  357. {
  358. $sql = "SELECT";
  359. $sql .= " t.rowid,";
  360. $sql .= " t.entity,";
  361. $sql .= " t.tms,";
  362. $sql .= " t.datec,";
  363. $sql .= " t.jobtype,";
  364. $sql .= " t.label,";
  365. $sql .= " t.command,";
  366. $sql .= " t.classesname,";
  367. $sql .= " t.objectname,";
  368. $sql .= " t.methodename,";
  369. $sql .= " t.params,";
  370. $sql .= " t.md5params,";
  371. $sql .= " t.module_name,";
  372. $sql .= " t.priority,";
  373. $sql .= " t.datelastrun,";
  374. $sql .= " t.datenextrun,";
  375. $sql .= " t.dateend,";
  376. $sql .= " t.datestart,";
  377. $sql .= " t.lastresult,";
  378. $sql .= " t.datelastresult,";
  379. $sql .= " t.lastoutput,";
  380. $sql .= " t.unitfrequency,";
  381. $sql .= " t.frequency,";
  382. $sql .= " t.status,";
  383. $sql .= " t.processing,";
  384. $sql .= " t.pid,";
  385. $sql .= " t.fk_user_author,";
  386. $sql .= " t.fk_user_mod,";
  387. $sql .= " t.note as note_private,";
  388. $sql .= " t.nbrun,";
  389. $sql .= " t.maxrun,";
  390. $sql .= " t.libname,";
  391. $sql .= " t.test";
  392. $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
  393. if ($id > 0) {
  394. $sql .= " WHERE t.rowid = ".((int) $id);
  395. } else {
  396. $sql .= " WHERE t.entity IN(0, ".getEntity('cron').")";
  397. $sql .= " AND t.objectname = '".$this->db->escape($objectname)."'";
  398. $sql .= " AND t.methodename = '".$this->db->escape($methodname)."'";
  399. }
  400. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  401. $resql = $this->db->query($sql);
  402. if ($resql) {
  403. if ($this->db->num_rows($resql)) {
  404. $obj = $this->db->fetch_object($resql);
  405. $this->id = $obj->rowid;
  406. $this->ref = $obj->rowid;
  407. $this->entity = $obj->entity;
  408. $this->tms = $this->db->jdate($obj->tms);
  409. $this->datec = $this->db->jdate($obj->datec);
  410. $this->label = $obj->label;
  411. $this->jobtype = $obj->jobtype;
  412. $this->command = $obj->command;
  413. $this->classesname = $obj->classesname;
  414. $this->objectname = $obj->objectname;
  415. $this->methodename = $obj->methodename;
  416. $this->params = $obj->params;
  417. $this->md5params = $obj->md5params;
  418. $this->module_name = $obj->module_name;
  419. $this->priority = $obj->priority;
  420. $this->datelastrun = $this->db->jdate($obj->datelastrun);
  421. $this->datenextrun = $this->db->jdate($obj->datenextrun);
  422. $this->dateend = $this->db->jdate($obj->dateend);
  423. $this->datestart = $this->db->jdate($obj->datestart);
  424. $this->lastresult = $obj->lastresult;
  425. $this->lastoutput = $obj->lastoutput;
  426. $this->datelastresult = $this->db->jdate($obj->datelastresult);
  427. $this->unitfrequency = $obj->unitfrequency;
  428. $this->frequency = $obj->frequency;
  429. $this->status = $obj->status;
  430. $this->processing = $obj->processing;
  431. $this->pid = $obj->pid;
  432. $this->fk_user_author = $obj->fk_user_author;
  433. $this->fk_user_mod = $obj->fk_user_mod;
  434. $this->note_private = $obj->note_private;
  435. $this->nbrun = $obj->nbrun;
  436. $this->maxrun = $obj->maxrun;
  437. $this->libname = $obj->libname;
  438. $this->test = $obj->test;
  439. }
  440. $this->db->free($resql);
  441. return 1;
  442. } else {
  443. $this->error = "Error ".$this->db->lasterror();
  444. return -1;
  445. }
  446. }
  447. /**
  448. * Load list of cron jobs in a memory array from the database
  449. * @TODO Use object CronJob and not CronJobLine.
  450. *
  451. * @param string $sortorder sort order
  452. * @param string $sortfield sort field
  453. * @param int $limit limit page
  454. * @param int $offset page
  455. * @param int $status display active or not
  456. * @param array $filter filter output
  457. * @param int $processing Processing or not
  458. * @return int <0 if KO, >0 if OK
  459. */
  460. public function fetchAll($sortorder = 'DESC', $sortfield = 't.rowid', $limit = 0, $offset = 0, $status = 1, $filter = '', $processing = -1)
  461. {
  462. $this->lines = array();
  463. $sql = "SELECT";
  464. $sql .= " t.rowid,";
  465. $sql .= " t.entity,";
  466. $sql .= " t.tms,";
  467. $sql .= " t.datec,";
  468. $sql .= " t.jobtype,";
  469. $sql .= " t.label,";
  470. $sql .= " t.command,";
  471. $sql .= " t.classesname,";
  472. $sql .= " t.objectname,";
  473. $sql .= " t.methodename,";
  474. $sql .= " t.params,";
  475. $sql .= " t.md5params,";
  476. $sql .= " t.module_name,";
  477. $sql .= " t.priority,";
  478. $sql .= " t.datelastrun,";
  479. $sql .= " t.datenextrun,";
  480. $sql .= " t.dateend,";
  481. $sql .= " t.datestart,";
  482. $sql .= " t.lastresult,";
  483. $sql .= " t.datelastresult,";
  484. $sql .= " t.lastoutput,";
  485. $sql .= " t.unitfrequency,";
  486. $sql .= " t.frequency,";
  487. $sql .= " t.status,";
  488. $sql .= " t.processing,";
  489. $sql .= " t.pid,";
  490. $sql .= " t.fk_user_author,";
  491. $sql .= " t.fk_user_mod,";
  492. $sql .= " t.note as note_private,";
  493. $sql .= " t.nbrun,";
  494. $sql .= " t.libname,";
  495. $sql .= " t.test";
  496. $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
  497. $sql .= " WHERE 1 = 1";
  498. if ($processing >= 0) {
  499. $sql .= " AND t.processing = ".(empty($processing) ? '0' : '1');
  500. }
  501. if ($status >= 0 && $status < 2) {
  502. $sql .= " AND t.status = ".(empty($status) ? '0' : '1');
  503. } elseif ($status == 2) {
  504. $sql .= " AND t.status = 2";
  505. }
  506. // Manage filter
  507. if (is_array($filter) && count($filter) > 0) {
  508. foreach ($filter as $key => $value) {
  509. if ($key == 't.rowid') {
  510. $sql .= " AND ".$key." = ".((int) $value);
  511. } else {
  512. $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
  513. }
  514. }
  515. }
  516. $sql .= $this->db->order($sortfield, $sortorder);
  517. if (!empty($limit) && !empty($offset)) {
  518. $sql .= $this->db->plimit($limit + 1, $offset);
  519. }
  520. $sqlwhere = array();
  521. if (count($sqlwhere) > 0) {
  522. $sql .= " WHERE ".implode(' AND ', $sqlwhere);
  523. }
  524. dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
  525. $resql = $this->db->query($sql);
  526. if ($resql) {
  527. $num = $this->db->num_rows($resql);
  528. $i = 0;
  529. if ($num) {
  530. while ($i < $num) {
  531. $line = new Cronjobline();
  532. $obj = $this->db->fetch_object($resql);
  533. $line->id = $obj->rowid;
  534. $line->ref = $obj->rowid;
  535. $line->entity = $obj->entity;
  536. $line->tms = $this->db->jdate($obj->tms);
  537. $line->datec = $this->db->jdate($obj->datec);
  538. $line->label = $obj->label;
  539. $line->jobtype = $obj->jobtype;
  540. $line->command = $obj->command;
  541. $line->classesname = $obj->classesname;
  542. $line->objectname = $obj->objectname;
  543. $line->methodename = $obj->methodename;
  544. $line->params = $obj->params;
  545. $line->md5params = $obj->md5params;
  546. $line->module_name = $obj->module_name;
  547. $line->priority = $obj->priority;
  548. $line->datelastrun = $this->db->jdate($obj->datelastrun);
  549. $line->datenextrun = $this->db->jdate($obj->datenextrun);
  550. $line->dateend = $this->db->jdate($obj->dateend);
  551. $line->datestart = $this->db->jdate($obj->datestart);
  552. $line->lastresult = $obj->lastresult;
  553. $line->datelastresult = $this->db->jdate($obj->datelastresult);
  554. $line->lastoutput = $obj->lastoutput;
  555. $line->unitfrequency = $obj->unitfrequency;
  556. $line->frequency = $obj->frequency;
  557. $line->status = $obj->status;
  558. $line->processing = $obj->processing;
  559. $line->pid = $obj->pid;
  560. $line->fk_user_author = $obj->fk_user_author;
  561. $line->fk_user_mod = $obj->fk_user_mod;
  562. $line->note_private = $obj->note_private;
  563. $line->nbrun = $obj->nbrun;
  564. $line->libname = $obj->libname;
  565. $line->test = $obj->test;
  566. $this->lines[] = $line;
  567. $i++;
  568. }
  569. }
  570. $this->db->free($resql);
  571. return 1;
  572. } else {
  573. $this->error = "Error ".$this->db->lasterror();
  574. return -1;
  575. }
  576. }
  577. /**
  578. * Update object into database
  579. *
  580. * @param User $user User that modifies
  581. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  582. * @return int <0 if KO, >0 if OK
  583. */
  584. public function update($user = null, $notrigger = 0)
  585. {
  586. global $conf, $langs;
  587. $langs->load('cron');
  588. $error = 0;
  589. // Clean parameters
  590. if (isset($this->label)) {
  591. $this->label = trim($this->label);
  592. }
  593. if (isset($this->jobtype)) {
  594. $this->jobtype = trim($this->jobtype);
  595. }
  596. if (isset($this->command)) {
  597. $this->command = trim($this->command);
  598. }
  599. if (isset($this->classesname)) {
  600. $this->classesname = trim($this->classesname);
  601. }
  602. if (isset($this->objectname)) {
  603. $this->objectname = trim($this->objectname);
  604. }
  605. if (isset($this->methodename)) {
  606. $this->methodename = trim($this->methodename);
  607. }
  608. if (isset($this->params)) {
  609. $this->params = trim($this->params);
  610. }
  611. if (isset($this->md5params)) {
  612. $this->md5params = trim($this->md5params);
  613. }
  614. if (isset($this->module_name)) {
  615. $this->module_name = trim($this->module_name);
  616. }
  617. if (isset($this->priority)) {
  618. $this->priority = trim($this->priority);
  619. }
  620. if (isset($this->lastoutput)) {
  621. $this->lastoutput = trim($this->lastoutput);
  622. }
  623. if (isset($this->lastresult)) {
  624. $this->lastresult = trim($this->lastresult);
  625. }
  626. if (isset($this->unitfrequency)) {
  627. $this->unitfrequency = trim($this->unitfrequency);
  628. }
  629. if (isset($this->frequency)) {
  630. $this->frequency = trim($this->frequency);
  631. }
  632. if (isset($this->status)) {
  633. $this->status = trim($this->status);
  634. }
  635. if (isset($this->note_private)) {
  636. $this->note_private = trim($this->note_private);
  637. }
  638. if (isset($this->nbrun)) {
  639. $this->nbrun = trim($this->nbrun);
  640. }
  641. if (isset($this->libname)) {
  642. $this->libname = trim($this->libname);
  643. }
  644. if (isset($this->test)) {
  645. $this->test = trim($this->test);
  646. }
  647. if (empty($this->maxrun)) {
  648. $this->maxrun = 0;
  649. }
  650. if (empty($this->processing)) {
  651. $this->processing = 0;
  652. }
  653. if (empty($this->pid)) {
  654. $this->pid = null;
  655. }
  656. // Check parameters
  657. // Put here code to add a control on parameters values
  658. if (dol_strlen($this->datenextrun) == 0) {
  659. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
  660. $error++;
  661. }
  662. if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
  663. $this->errors[] = $langs->trans('CronErrEndDateStartDt');
  664. $error++;
  665. }
  666. if (empty($this->label)) {
  667. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
  668. $error++;
  669. }
  670. if (empty($this->unitfrequency)) {
  671. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
  672. $error++;
  673. }
  674. if (($this->jobtype == 'command') && (empty($this->command))) {
  675. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
  676. $error++;
  677. }
  678. if (($this->jobtype == 'method') && (empty($this->classesname))) {
  679. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
  680. $error++;
  681. }
  682. if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
  683. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
  684. $error++;
  685. }
  686. if (($this->jobtype == 'method') && (empty($this->objectname))) {
  687. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
  688. $error++;
  689. }
  690. if (($this->jobtype == 'function') && (empty($this->libname))) {
  691. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
  692. $error++;
  693. }
  694. // Update request
  695. $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
  696. $sql .= " entity=".(isset($this->entity) ? $this->db->escape($this->entity) : $conf->entity).",";
  697. $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
  698. $sql .= " jobtype=".(isset($this->jobtype) ? "'".$this->db->escape($this->jobtype)."'" : "null").",";
  699. $sql .= " command=".(isset($this->command) ? "'".$this->db->escape($this->command)."'" : "null").",";
  700. $sql .= " classesname=".(isset($this->classesname) ? "'".$this->db->escape($this->classesname)."'" : "null").",";
  701. $sql .= " objectname=".(isset($this->objectname) ? "'".$this->db->escape($this->objectname)."'" : "null").",";
  702. $sql .= " methodename=".(isset($this->methodename) ? "'".$this->db->escape($this->methodename)."'" : "null").",";
  703. $sql .= " params=".(isset($this->params) ? "'".$this->db->escape($this->params)."'" : "null").",";
  704. $sql .= " md5params=".(isset($this->md5params) ? "'".$this->db->escape($this->md5params)."'" : "null").",";
  705. $sql .= " module_name=".(isset($this->module_name) ? "'".$this->db->escape($this->module_name)."'" : "null").",";
  706. $sql .= " priority=".(isset($this->priority) ? $this->priority : "null").",";
  707. $sql .= " datelastrun=".(dol_strlen($this->datelastrun) != 0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').",";
  708. $sql .= " datenextrun=".(dol_strlen($this->datenextrun) != 0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').",";
  709. $sql .= " dateend=".(dol_strlen($this->dateend) != 0 ? "'".$this->db->idate($this->dateend)."'" : 'null').",";
  710. $sql .= " datestart=".(dol_strlen($this->datestart) != 0 ? "'".$this->db->idate($this->datestart)."'" : 'null').",";
  711. $sql .= " datelastresult=".(dol_strlen($this->datelastresult) != 0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').",";
  712. $sql .= " lastresult=".(isset($this->lastresult) ? "'".$this->db->escape($this->lastresult)."'" : "null").",";
  713. $sql .= " lastoutput=".(isset($this->lastoutput) ? "'".$this->db->escape($this->lastoutput)."'" : "null").",";
  714. $sql .= " unitfrequency=".(isset($this->unitfrequency) ? $this->unitfrequency : "null").",";
  715. $sql .= " frequency=".(isset($this->frequency) ? $this->frequency : "null").",";
  716. $sql .= " status=".(isset($this->status) ? $this->status : "null").",";
  717. $sql .= " processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing : "0").",";
  718. $sql .= " pid=".(isset($this->pid) ? $this->pid : "null").",";
  719. $sql .= " fk_user_mod=".$user->id.",";
  720. $sql .= " note=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
  721. $sql .= " nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun : "null").",";
  722. $sql .= " maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun : "0").",";
  723. $sql .= " libname=".(isset($this->libname) ? "'".$this->db->escape($this->libname)."'" : "null").",";
  724. $sql .= " test=".(isset($this->test) ? "'".$this->db->escape($this->test)."'" : "null");
  725. $sql .= " WHERE rowid=".((int) $this->id);
  726. $this->db->begin();
  727. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  728. $resql = $this->db->query($sql);
  729. if (!$resql) {
  730. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  731. }
  732. // Commit or rollback
  733. if ($error) {
  734. foreach ($this->errors as $errmsg) {
  735. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  736. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  737. }
  738. $this->db->rollback();
  739. return -1 * $error;
  740. } else {
  741. $this->db->commit();
  742. return 1;
  743. }
  744. }
  745. /**
  746. * Delete object in database
  747. *
  748. * @param User $user User that deletes
  749. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  750. * @return int <0 if KO, >0 if OK
  751. */
  752. public function delete($user, $notrigger = 0)
  753. {
  754. $error = 0;
  755. $this->db->begin();
  756. $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
  757. $sql .= " WHERE rowid=".((int) $this->id);
  758. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  759. $resql = $this->db->query($sql);
  760. if (!$resql) {
  761. $error++;
  762. $this->errors[] = "Error ".$this->db->lasterror();
  763. }
  764. // Commit or rollback
  765. if ($error) {
  766. foreach ($this->errors as $errmsg) {
  767. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  768. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  769. }
  770. $this->db->rollback();
  771. return -1 * $error;
  772. } else {
  773. $this->db->commit();
  774. return 1;
  775. }
  776. }
  777. /**
  778. * Load an object from its id and create a new one in database
  779. *
  780. * @param User $user User making the clone
  781. * @param int $fromid Id of object to clone
  782. * @return int New id of clone
  783. */
  784. public function createFromClone(User $user, $fromid)
  785. {
  786. global $langs;
  787. $error = 0;
  788. $object = new Cronjob($this->db);
  789. $this->db->begin();
  790. // Load source object
  791. $object->fetch($fromid);
  792. $object->id = 0;
  793. // Clear fields
  794. $object->status = self::STATUS_DISABLED;
  795. $object->label = $langs->trans("CopyOf").' '.$langs->trans($object->label);
  796. // Create clone
  797. $object->context['createfromclone'] = 'createfromclone';
  798. $result = $object->create($user);
  799. // Other options
  800. if ($result < 0) {
  801. $this->error = $object->error;
  802. $error++;
  803. }
  804. unset($object->context['createfromclone']);
  805. // End
  806. if (!$error) {
  807. $this->db->commit();
  808. return $object->id;
  809. } else {
  810. $this->db->rollback();
  811. return -1;
  812. }
  813. }
  814. /**
  815. * Initialise object with example values
  816. * Id must be 0 if object instance is a specimen
  817. *
  818. * @return void
  819. */
  820. public function initAsSpecimen()
  821. {
  822. $this->id = 0;
  823. $this->ref = 0;
  824. $this->entity = 0;
  825. $this->tms = '';
  826. $this->datec = '';
  827. $this->label = '';
  828. $this->jobtype = '';
  829. $this->command = '';
  830. $this->classesname = '';
  831. $this->objectname = '';
  832. $this->methodename = '';
  833. $this->params = '';
  834. $this->md5params = '';
  835. $this->module_name = '';
  836. $this->priority = '';
  837. $this->datelastrun = '';
  838. $this->datenextrun = '';
  839. $this->dateend = '';
  840. $this->datestart = '';
  841. $this->datelastresult = '';
  842. $this->lastoutput = '';
  843. $this->lastresult = '';
  844. $this->unitfrequency = '';
  845. $this->frequency = '';
  846. $this->status = 0;
  847. $this->processing = 0;
  848. $this->pid = null;
  849. $this->fk_user_author = 0;
  850. $this->fk_user_mod = 0;
  851. $this->note_private = '';
  852. $this->nbrun = '';
  853. $this->maxrun = 100;
  854. $this->libname = '';
  855. }
  856. /**
  857. * Return a link to the object card (with optionaly the picto)
  858. *
  859. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  860. * @param string $option On what the link point to ('nolink', ...)
  861. * @param int $notooltip 1=Disable tooltip
  862. * @param string $morecss Add more css on link
  863. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  864. * @return string String with URL
  865. */
  866. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  867. {
  868. global $db, $conf, $langs;
  869. global $dolibarr_main_authentication, $dolibarr_main_demo;
  870. global $menumanager;
  871. if (!empty($conf->dol_no_mouse_hover)) {
  872. $notooltip = 1; // Force disable tooltips
  873. }
  874. $result = '';
  875. $label = img_picto('', 'object_'.$this->picto).' <u>'.$langs->trans("CronTask").'</u>';
  876. if (isset($this->status)) {
  877. $label .= ' '.$this->getLibStatut(5);
  878. }
  879. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  880. $label .= '<br><b>'.$langs->trans('Title').':</b> '.$langs->trans($this->label);
  881. if ($this->label != $langs->trans($this->label)) {
  882. $label .= ' <span class="opacitymedium">('.$this->label.')</span>';
  883. }
  884. if (!empty($this->datestart)) {
  885. $label .= '<br><b>'.$langs->trans('CronDtStart').':</b> '.dol_print_date($this->datestart, 'dayhour', 'tzuserrel');
  886. }
  887. if (!empty($this->dateend)) {
  888. $label .= '<br><b>'.$langs->trans('CronDtEnd').':</b> '.dol_print_date($this->dateend, 'dayhour', 'tzuserrel');
  889. }
  890. $url = DOL_URL_ROOT.'/cron/card.php?id='.$this->id;
  891. if ($option != 'nolink') {
  892. // Add param to save lastsearch_values or not
  893. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  894. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  895. $add_save_lastsearch_values = 1;
  896. }
  897. if ($add_save_lastsearch_values) {
  898. $url .= '&save_lastsearch_values=1';
  899. }
  900. }
  901. $linkclose = '';
  902. if (empty($notooltip)) {
  903. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  904. $label = $langs->trans("ShowCronJob");
  905. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  906. }
  907. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  908. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  909. } else {
  910. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  911. }
  912. $linkstart = '<a href="'.$url.'"';
  913. $linkstart .= $linkclose.'>';
  914. $linkend = '</a>';
  915. $result .= $linkstart;
  916. if ($withpicto) {
  917. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  918. }
  919. if ($withpicto != 2) {
  920. $result .= $this->ref;
  921. }
  922. $result .= $linkend;
  923. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  924. return $result;
  925. }
  926. /**
  927. * Load object information
  928. *
  929. * @param int $id ID
  930. * @return int <0 if KO, >0 if OK
  931. */
  932. public function info($id)
  933. {
  934. $sql = "SELECT";
  935. $sql .= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
  936. $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as f";
  937. $sql .= " WHERE f.rowid = ".((int) $id);
  938. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  939. $resql = $this->db->query($sql);
  940. if ($resql) {
  941. if ($this->db->num_rows($resql)) {
  942. $obj = $this->db->fetch_object($resql);
  943. $this->id = $obj->rowid;
  944. $this->user_modification_id = $obj->fk_user_mod;
  945. $this->user_creation_id = $obj->fk_user_author;
  946. $this->date_creation = $this->db->jdate($obj->datec);
  947. $this->date_modification = $this->db->jdate($obj->tms);
  948. }
  949. $this->db->free($resql);
  950. return 1;
  951. } else {
  952. $this->error = "Error ".$this->db->lasterror();
  953. return -1;
  954. }
  955. }
  956. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  957. /**
  958. * Run a job.
  959. * Once job is finished, status and nb of run is updated.
  960. * This function does not plan the next run. This is done by function ->reprogram_jobs
  961. *
  962. * @param string $userlogin User login
  963. * @return int <0 if KO, >0 if OK
  964. */
  965. public function run_jobs($userlogin)
  966. {
  967. // phpcs:enable
  968. global $langs, $conf, $hookmanager;
  969. $hookmanager->initHooks(array('cron'));
  970. $now = dol_now();
  971. $error = 0;
  972. $retval = '';
  973. $langs->load('cron');
  974. if (empty($userlogin)) {
  975. $this->error = "User login is mandatory";
  976. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  977. return -1;
  978. }
  979. // Force the environment of running to the environment declared for job, so jobs launched from command line will run into correct environment
  980. // When job is ran from GUI, the environment should already be same, except if job has entity 0 (visible into all environments)
  981. if ($conf->entity != $this->entity && $this->entity > 0) {
  982. dol_syslog("We try to run a job in entity ".$this->entity." when we are in entity ".$conf->entity, LOG_WARNING);
  983. }
  984. $savcurrententity = $conf->entity;
  985. $conf->setEntityValues($this->db, $this->entity);
  986. dol_syslog(get_class($this)."::run_jobs entity for running job is ".$conf->entity);
  987. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  988. $user = new User($this->db);
  989. $result = $user->fetch('', $userlogin);
  990. if ($result < 0) {
  991. $this->error = "User Error:".$user->error;
  992. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  993. $conf->setEntityValues($this->db, $savcurrententity);
  994. return -1;
  995. } else {
  996. if (empty($user->id)) {
  997. $this->error = " User user login:".$userlogin." do not exists";
  998. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  999. $conf->setEntityValues($this->db, $savcurrententity);
  1000. return -1;
  1001. }
  1002. }
  1003. dol_syslog(get_class($this)."::run_jobs jobtype=".$this->jobtype." userlogin=".$userlogin, LOG_DEBUG);
  1004. // Increase limit of time. Works only if we are not in safe mode
  1005. $ExecTimeLimit = 600;
  1006. if (!empty($ExecTimeLimit)) {
  1007. $err = error_reporting();
  1008. error_reporting(0); // Disable all errors
  1009. //error_reporting(E_ALL);
  1010. @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
  1011. error_reporting($err);
  1012. }
  1013. if (!empty($MemoryLimit)) {
  1014. @ini_set('memory_limit', $MemoryLimit);
  1015. }
  1016. // Update last run date start (to track running jobs)
  1017. $this->datelastrun = $now;
  1018. $this->datelastresult = null;
  1019. $this->lastoutput = '';
  1020. $this->lastresult = '';
  1021. $this->processing = 1; // To know job was started
  1022. $this->pid = dol_getmypid();
  1023. $this->nbrun = $this->nbrun + 1;
  1024. $result = $this->update($user); // This include begin/commit
  1025. if ($result < 0) {
  1026. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1027. $conf->setEntityValues($this->db, $savcurrententity);
  1028. return -1;
  1029. }
  1030. // Run a method
  1031. if ($this->jobtype == 'method') {
  1032. // load classes
  1033. if (!$error) {
  1034. $ret = dol_include_once($this->classesname);
  1035. if ($ret === false || (!class_exists($this->objectname))) {
  1036. if ($ret === false) {
  1037. $this->error = $langs->transnoentitiesnoconv('CronCannotLoadClass', $this->classesname, $this->objectname);
  1038. } else {
  1039. $this->error = $langs->transnoentitiesnoconv('CronCannotLoadObject', $this->classesname, $this->objectname);
  1040. }
  1041. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1042. $this->lastoutput = $this->error;
  1043. $this->lastresult = -1;
  1044. $retval = $this->lastresult;
  1045. $error++;
  1046. }
  1047. }
  1048. // test if method exists
  1049. if (!$error) {
  1050. if (!method_exists($this->objectname, $this->methodename)) {
  1051. $this->error = $langs->transnoentitiesnoconv('CronMethodDoesNotExists', $this->objectname, $this->methodename);
  1052. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1053. $this->lastoutput = $this->error;
  1054. $this->lastresult = -1;
  1055. $retval = $this->lastresult;
  1056. $error++;
  1057. }
  1058. if (in_array(strtolower(trim($this->methodename)), array('executecli'))) {
  1059. $this->error = $langs->transnoentitiesnoconv('CronMethodNotAllowed', $this->methodename, $this->objectname);
  1060. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1061. $this->lastoutput = $this->error;
  1062. $this->lastresult = -1;
  1063. $retval = $this->lastresult;
  1064. $error++;
  1065. }
  1066. }
  1067. // Load langs
  1068. if (!$error) {
  1069. $result = $langs->load($this->module_name);
  1070. $result = $langs->load($this->module_name.'@'.$this->module_name, 0, 0, '', 0, 1);
  1071. if ($result < 0) { // If technical error
  1072. dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
  1073. $this->error = $langs->error;
  1074. $this->lastoutput = $this->error;
  1075. $this->lastresult = -1;
  1076. $retval = $this->lastresult;
  1077. $error++;
  1078. }
  1079. }
  1080. if (!$error) {
  1081. dol_syslog(get_class($this)."::run_jobs START ".$this->objectname."->".$this->methodename."(".$this->params.");", LOG_DEBUG);
  1082. // Create Object for the called module
  1083. $object = new $this->objectname($this->db);
  1084. if ($this->entity > 0) {
  1085. $object->entity = $this->entity; // We work on a dedicated entity
  1086. }
  1087. $params_arr = array();
  1088. if (!empty($this->params) || $this->params === '0') {
  1089. $params_arr = array_map('trim', explode(",", $this->params));
  1090. }
  1091. if (!is_array($params_arr)) {
  1092. $result = call_user_func(array($object, $this->methodename), $this->params);
  1093. } else {
  1094. $result = call_user_func_array(array($object, $this->methodename), $params_arr);
  1095. }
  1096. if ($result === false || (!is_bool($result) && $result != 0)) {
  1097. $langs->load("errors");
  1098. $errmsg = '';
  1099. if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
  1100. $errmsg .= $object->error;
  1101. }
  1102. if (is_array($object->errors) && count($object->errors)) {
  1103. $errmsg .= (($errmsg ? ', ' : '').join(', ', $object->errors));
  1104. }
  1105. if (empty($errmsg)) {
  1106. $errmsg = $langs->trans('ErrorUnknown');
  1107. }
  1108. dol_syslog(get_class($this)."::run_jobs END result=".$result." error=".$errmsg, LOG_ERR);
  1109. $this->error = $errmsg;
  1110. $this->lastoutput = ($object->output ? $object->output."\n" : "").$errmsg;
  1111. $this->lastresult = is_numeric($result) ? $result : -1;
  1112. $retval = $this->lastresult;
  1113. $error++;
  1114. } else {
  1115. dol_syslog(get_class($this)."::run_jobs END");
  1116. $this->lastoutput = $object->output;
  1117. $this->lastresult = var_export($result, true);
  1118. $retval = $this->lastresult;
  1119. }
  1120. }
  1121. }
  1122. if ($this->jobtype == 'function') {
  1123. //load lib
  1124. $libpath = '/'.strtolower($this->module_name).'/lib/'.$this->libname;
  1125. $ret = dol_include_once($libpath);
  1126. if ($ret === false) {
  1127. $this->error = $langs->trans('CronCannotLoadLib').': '.$libpath;
  1128. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1129. $conf->setEntityValues($this->db, $savcurrententity);
  1130. return -1;
  1131. }
  1132. // Load langs
  1133. $result = $langs->load($this->module_name);
  1134. $result = $langs->load($this->module_name.'@'.$this->module_name); // If this->module_name was an existing language file, this will make nothing
  1135. if ($result < 0) { // If technical error
  1136. dol_syslog(get_class($this)."::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
  1137. $conf->setEntityValues($this->db, $savcurrententity);
  1138. return -1;
  1139. }
  1140. dol_syslog(get_class($this)."::run_jobs ".$this->libname."::".$this->methodename."(".$this->params.");", LOG_DEBUG);
  1141. $params_arr = explode(", ", $this->params);
  1142. if (!is_array($params_arr)) {
  1143. $result = call_user_func($this->methodename, $this->params);
  1144. } else {
  1145. $result = call_user_func_array($this->methodename, $params_arr);
  1146. }
  1147. if ($result === false || (!is_bool($result) && $result != 0)) {
  1148. $langs->load("errors");
  1149. dol_syslog(get_class($this)."::run_jobs result=".$result, LOG_ERR);
  1150. $this->error = $langs->trans('ErrorUnknown');
  1151. $this->lastoutput = $this->error;
  1152. $this->lastresult = is_numeric($result) ? $result : -1;
  1153. $retval = $this->lastresult;
  1154. $error++;
  1155. } else {
  1156. $this->lastoutput = var_export($result, true);
  1157. $this->lastresult = var_export($result, true); // Return code
  1158. $retval = $this->lastresult;
  1159. }
  1160. }
  1161. // Run a command line
  1162. if ($this->jobtype == 'command') {
  1163. global $dolibarr_cron_allow_cli;
  1164. if (empty($dolibarr_cron_allow_cli)) {
  1165. $langs->load("errors");
  1166. $this->error = $langs->trans("FailedToExecutCommandJob");
  1167. $this->lastoutput = '';
  1168. $this->lastresult = $langs->trans("ErrorParameterMustBeEnabledToAllwoThisFeature", 'dolibarr_cron_allow_cli');
  1169. } else {
  1170. $outputdir = $conf->cron->dir_temp;
  1171. if (empty($outputdir)) {
  1172. $outputdir = $conf->cronjob->dir_temp;
  1173. }
  1174. if (!empty($outputdir)) {
  1175. dol_mkdir($outputdir);
  1176. $outputfile = $outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method
  1177. // Execute a CLI
  1178. include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
  1179. $utils = new Utils($this->db);
  1180. $arrayresult = $utils->executeCLI($this->command, $outputfile);
  1181. $retval = $arrayresult['result'];
  1182. $this->error = $arrayresult['error'];
  1183. $this->lastoutput = $arrayresult['output'];
  1184. $this->lastresult = $arrayresult['result'];
  1185. }
  1186. }
  1187. }
  1188. dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)");
  1189. $this->datelastresult = dol_now();
  1190. $this->processing = 0;
  1191. $this->pid = null;
  1192. $result = $this->update($user); // This include begin/commit
  1193. if ($result < 0) {
  1194. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1195. $conf->setEntityValues($this->db, $savcurrententity);
  1196. return -1;
  1197. }
  1198. $conf->setEntityValues($this->db, $savcurrententity);
  1199. return $error ?-1 : 1;
  1200. }
  1201. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1202. /**
  1203. * Reprogram a job
  1204. *
  1205. * @param string $userlogin User login
  1206. * @param integer $now Date returned by dol_now()
  1207. * @return int <0 if KO, >0 if OK
  1208. */
  1209. public function reprogram_jobs($userlogin, $now)
  1210. {
  1211. // phpcs:enable
  1212. dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
  1213. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  1214. $user = new User($this->db);
  1215. $result = $user->fetch('', $userlogin);
  1216. if ($result < 0) {
  1217. $this->error = "User Error : ".$user->error;
  1218. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1219. return -1;
  1220. } else {
  1221. if (empty($user->id)) {
  1222. $this->error = " User user login:".$userlogin." do not exists";
  1223. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1224. return -1;
  1225. }
  1226. }
  1227. dol_syslog(get_class($this)."::reprogram_jobs datenextrun=".$this->datenextrun." ".dol_print_date($this->datenextrun, 'dayhourrfc')." frequency=".$this->frequency." unitfrequency=".$this->unitfrequency, LOG_DEBUG);
  1228. if (empty($this->datenextrun)) {
  1229. if (empty($this->datestart)) {
  1230. $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
  1231. } else {
  1232. $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
  1233. }
  1234. }
  1235. if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0) {
  1236. // Loop until date is after future
  1237. while ($this->datenextrun < $now) {
  1238. $this->datenextrun += ($this->frequency * $this->unitfrequency);
  1239. // TODO For exact frequency (every month, every year, ...), use instead a dol_time_plus_duree($time, $duration_value, $duration_unit)
  1240. }
  1241. } else {
  1242. //$this->datenextrun=$this->datenextrun + ($this->frequency * $this->unitfrequency);
  1243. dol_syslog(get_class($this)."::reprogram_jobs datenextrun is already in future, we do not change it");
  1244. }
  1245. // Archive job
  1246. if ($this->autodelete == 2) {
  1247. if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
  1248. || ($this->dateend && ($this->datenextrun > $this->dateend))) {
  1249. $this->status = self::STATUS_ARCHIVED;
  1250. dol_syslog(get_class($this)."::reprogram_jobs Job will be set to archived", LOG_ERR);
  1251. }
  1252. }
  1253. $result = $this->update($user);
  1254. if ($result < 0) {
  1255. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1256. return -1;
  1257. }
  1258. return 1;
  1259. }
  1260. /**
  1261. * Return label of status of user (active, inactive)
  1262. *
  1263. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  1264. * @return string Label of status
  1265. */
  1266. public function getLibStatut($mode = 0)
  1267. {
  1268. return $this->LibStatut($this->status, $mode, $this->processing, $this->lastresult);
  1269. }
  1270. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1271. /**
  1272. * Renvoi le libelle d'un statut donne
  1273. *
  1274. * @param int $status Id statut
  1275. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  1276. * @param int $processing 0=Not running, 1=Running
  1277. * @param int $lastresult Value of last result (0=no error, error otherwise)
  1278. * @return string Label of status
  1279. */
  1280. public function LibStatut($status, $mode = 0, $processing = 0, $lastresult = 0)
  1281. {
  1282. // phpcs:enable
  1283. $this->labelStatus = array(); // Force reset o array because label depends on other fields
  1284. $this->labelStatusShort = array();
  1285. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  1286. global $langs;
  1287. $langs->load('users');
  1288. $moretext = '';
  1289. if ($processing) {
  1290. $moretext = ' ('.$langs->trans("Running").')';
  1291. } elseif ($lastresult) {
  1292. $moretext .= ' ('.$langs->trans("Error").')';
  1293. }
  1294. $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled').$moretext;
  1295. $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled').$moretext;
  1296. $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
  1297. $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled');
  1298. }
  1299. $statusType = 'status4';
  1300. if ($status == 1 && $processing) {
  1301. $statusType = 'status1';
  1302. }
  1303. if ($status == 0) {
  1304. $statusType = 'status5';
  1305. }
  1306. if ($this->lastresult) {
  1307. $statusType = 'status8';
  1308. }
  1309. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  1310. }
  1311. }
  1312. /**
  1313. * Crob Job line class
  1314. */
  1315. class Cronjobline
  1316. {
  1317. /**
  1318. * @var int ID
  1319. */
  1320. public $id;
  1321. public $entity;
  1322. /**
  1323. * @var string Ref
  1324. */
  1325. public $ref;
  1326. public $tms = '';
  1327. public $datec = '';
  1328. /**
  1329. * @var string Cron Job Line label
  1330. */
  1331. public $label;
  1332. public $jobtype;
  1333. public $command;
  1334. public $classesname;
  1335. public $objectname;
  1336. public $methodename;
  1337. public $params;
  1338. public $md5params;
  1339. public $module_name;
  1340. public $priority;
  1341. public $datelastrun = '';
  1342. public $datenextrun = '';
  1343. public $dateend = '';
  1344. public $datestart = '';
  1345. public $lastresult = '';
  1346. public $lastoutput;
  1347. public $unitfrequency;
  1348. public $frequency;
  1349. /**
  1350. * @var int Status
  1351. */
  1352. public $status;
  1353. /**
  1354. * @var int ID
  1355. */
  1356. public $fk_user_author;
  1357. /**
  1358. * @var int ID
  1359. */
  1360. public $fk_user_mod;
  1361. public $note;
  1362. public $nbrun;
  1363. public $libname;
  1364. /**
  1365. * Constructor
  1366. *
  1367. */
  1368. public function __construct()
  1369. {
  1370. return 1;
  1371. }
  1372. }