cronjob.class.php 47 KB

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