cronjob.class.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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. /**
  141. * @var string Autodelete
  142. */
  143. public $autodelete;
  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.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. /**
  446. * Load list of cron jobs in a memory array from the database
  447. * @TODO Use object CronJob and not CronJobLine.
  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 fetchAll($sortorder = 'DESC', $sortfield = 't.rowid', $limit = 0, $offset = 0, $status = 1, $filter = '', $processing = -1)
  459. {
  460. $this->lines = array();
  461. $sql = "SELECT";
  462. $sql .= " t.rowid,";
  463. $sql .= " t.entity,";
  464. $sql .= " t.tms,";
  465. $sql .= " t.datec,";
  466. $sql .= " t.jobtype,";
  467. $sql .= " t.label,";
  468. $sql .= " t.command,";
  469. $sql .= " t.classesname,";
  470. $sql .= " t.objectname,";
  471. $sql .= " t.methodename,";
  472. $sql .= " t.params,";
  473. $sql .= " t.md5params,";
  474. $sql .= " t.module_name,";
  475. $sql .= " t.priority,";
  476. $sql .= " t.datelastrun,";
  477. $sql .= " t.datenextrun,";
  478. $sql .= " t.dateend,";
  479. $sql .= " t.datestart,";
  480. $sql .= " t.lastresult,";
  481. $sql .= " t.datelastresult,";
  482. $sql .= " t.lastoutput,";
  483. $sql .= " t.unitfrequency,";
  484. $sql .= " t.frequency,";
  485. $sql .= " t.status,";
  486. $sql .= " t.processing,";
  487. $sql .= " t.fk_user_author,";
  488. $sql .= " t.fk_user_mod,";
  489. $sql .= " t.note as note_private,";
  490. $sql .= " t.nbrun,";
  491. $sql .= " t.libname,";
  492. $sql .= " t.test";
  493. $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
  494. $sql .= " WHERE 1 = 1";
  495. if ($processing >= 0) {
  496. $sql .= " AND t.processing = ".(empty($processing) ? '0' : '1');
  497. }
  498. if ($status >= 0 && $status < 2) {
  499. $sql .= " AND t.status = ".(empty($status) ? '0' : '1');
  500. } elseif ($status == 2) {
  501. $sql .= " AND t.status = 2";
  502. }
  503. // Manage filter
  504. if (is_array($filter) && count($filter) > 0) {
  505. foreach ($filter as $key => $value) {
  506. if ($key == 't.rowid') {
  507. $sql .= " AND ".$key." = ".((int) $value);
  508. } else {
  509. $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
  510. }
  511. }
  512. }
  513. $sql .= $this->db->order($sortfield, $sortorder);
  514. if (!empty($limit) && !empty($offset)) {
  515. $sql .= $this->db->plimit($limit + 1, $offset);
  516. }
  517. $sqlwhere = array();
  518. if (count($sqlwhere) > 0) {
  519. $sql .= " WHERE ".implode(' AND ', $sqlwhere);
  520. }
  521. dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
  522. $resql = $this->db->query($sql);
  523. if ($resql) {
  524. $num = $this->db->num_rows($resql);
  525. $i = 0;
  526. if ($num) {
  527. while ($i < $num) {
  528. $line = new Cronjobline();
  529. $obj = $this->db->fetch_object($resql);
  530. $line->id = $obj->rowid;
  531. $line->ref = $obj->rowid;
  532. $line->entity = $obj->entity;
  533. $line->tms = $this->db->jdate($obj->tms);
  534. $line->datec = $this->db->jdate($obj->datec);
  535. $line->label = $obj->label;
  536. $line->jobtype = $obj->jobtype;
  537. $line->command = $obj->command;
  538. $line->classesname = $obj->classesname;
  539. $line->objectname = $obj->objectname;
  540. $line->methodename = $obj->methodename;
  541. $line->params = $obj->params;
  542. $line->md5params = $obj->md5params;
  543. $line->module_name = $obj->module_name;
  544. $line->priority = $obj->priority;
  545. $line->datelastrun = $this->db->jdate($obj->datelastrun);
  546. $line->datenextrun = $this->db->jdate($obj->datenextrun);
  547. $line->dateend = $this->db->jdate($obj->dateend);
  548. $line->datestart = $this->db->jdate($obj->datestart);
  549. $line->lastresult = $obj->lastresult;
  550. $line->datelastresult = $this->db->jdate($obj->datelastresult);
  551. $line->lastoutput = $obj->lastoutput;
  552. $line->unitfrequency = $obj->unitfrequency;
  553. $line->frequency = $obj->frequency;
  554. $line->status = $obj->status;
  555. $line->processing = $obj->processing;
  556. $line->fk_user_author = $obj->fk_user_author;
  557. $line->fk_user_mod = $obj->fk_user_mod;
  558. $line->note_private = $obj->note_private;
  559. $line->nbrun = $obj->nbrun;
  560. $line->libname = $obj->libname;
  561. $line->test = $obj->test;
  562. $this->lines[] = $line;
  563. $i++;
  564. }
  565. }
  566. $this->db->free($resql);
  567. return 1;
  568. } else {
  569. $this->error = "Error ".$this->db->lasterror();
  570. return -1;
  571. }
  572. }
  573. /**
  574. * Update object into database
  575. *
  576. * @param User $user User that modifies
  577. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  578. * @return int <0 if KO, >0 if OK
  579. */
  580. public function update($user = null, $notrigger = 0)
  581. {
  582. global $conf, $langs;
  583. $langs->load('cron');
  584. $error = 0;
  585. // Clean parameters
  586. if (isset($this->label)) {
  587. $this->label = trim($this->label);
  588. }
  589. if (isset($this->jobtype)) {
  590. $this->jobtype = trim($this->jobtype);
  591. }
  592. if (isset($this->command)) {
  593. $this->command = trim($this->command);
  594. }
  595. if (isset($this->classesname)) {
  596. $this->classesname = trim($this->classesname);
  597. }
  598. if (isset($this->objectname)) {
  599. $this->objectname = trim($this->objectname);
  600. }
  601. if (isset($this->methodename)) {
  602. $this->methodename = trim($this->methodename);
  603. }
  604. if (isset($this->params)) {
  605. $this->params = trim($this->params);
  606. }
  607. if (isset($this->md5params)) {
  608. $this->md5params = trim($this->md5params);
  609. }
  610. if (isset($this->module_name)) {
  611. $this->module_name = trim($this->module_name);
  612. }
  613. if (isset($this->priority)) {
  614. $this->priority = trim($this->priority);
  615. }
  616. if (isset($this->lastoutput)) {
  617. $this->lastoutput = trim($this->lastoutput);
  618. }
  619. if (isset($this->lastresult)) {
  620. $this->lastresult = trim($this->lastresult);
  621. }
  622. if (isset($this->unitfrequency)) {
  623. $this->unitfrequency = trim($this->unitfrequency);
  624. }
  625. if (isset($this->frequency)) {
  626. $this->frequency = trim($this->frequency);
  627. }
  628. if (isset($this->status)) {
  629. $this->status = trim($this->status);
  630. }
  631. if (isset($this->note_private)) {
  632. $this->note_private = trim($this->note_private);
  633. }
  634. if (isset($this->nbrun)) {
  635. $this->nbrun = trim($this->nbrun);
  636. }
  637. if (isset($this->libname)) {
  638. $this->libname = trim($this->libname);
  639. }
  640. if (isset($this->test)) {
  641. $this->test = trim($this->test);
  642. }
  643. if (empty($this->maxrun)) {
  644. $this->maxrun = 0;
  645. }
  646. if (empty($this->processing)) {
  647. $this->processing = 0;
  648. }
  649. // Check parameters
  650. // Put here code to add a control on parameters values
  651. if (dol_strlen($this->datenextrun) == 0) {
  652. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
  653. $error++;
  654. }
  655. if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
  656. $this->errors[] = $langs->trans('CronErrEndDateStartDt');
  657. $error++;
  658. }
  659. if (empty($this->label)) {
  660. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
  661. $error++;
  662. }
  663. if (empty($this->unitfrequency)) {
  664. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
  665. $error++;
  666. }
  667. if (($this->jobtype == 'command') && (empty($this->command))) {
  668. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
  669. $error++;
  670. }
  671. if (($this->jobtype == 'method') && (empty($this->classesname))) {
  672. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
  673. $error++;
  674. }
  675. if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
  676. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
  677. $error++;
  678. }
  679. if (($this->jobtype == 'method') && (empty($this->objectname))) {
  680. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
  681. $error++;
  682. }
  683. if (($this->jobtype == 'function') && (empty($this->libname))) {
  684. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
  685. $error++;
  686. }
  687. // Update request
  688. $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
  689. $sql .= " entity=".(isset($this->entity) ? $this->db->escape($this->entity) : $conf->entity).",";
  690. $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
  691. $sql .= " jobtype=".(isset($this->jobtype) ? "'".$this->db->escape($this->jobtype)."'" : "null").",";
  692. $sql .= " command=".(isset($this->command) ? "'".$this->db->escape($this->command)."'" : "null").",";
  693. $sql .= " classesname=".(isset($this->classesname) ? "'".$this->db->escape($this->classesname)."'" : "null").",";
  694. $sql .= " objectname=".(isset($this->objectname) ? "'".$this->db->escape($this->objectname)."'" : "null").",";
  695. $sql .= " methodename=".(isset($this->methodename) ? "'".$this->db->escape($this->methodename)."'" : "null").",";
  696. $sql .= " params=".(isset($this->params) ? "'".$this->db->escape($this->params)."'" : "null").",";
  697. $sql .= " md5params=".(isset($this->md5params) ? "'".$this->db->escape($this->md5params)."'" : "null").",";
  698. $sql .= " module_name=".(isset($this->module_name) ? "'".$this->db->escape($this->module_name)."'" : "null").",";
  699. $sql .= " priority=".(isset($this->priority) ? $this->priority : "null").",";
  700. $sql .= " datelastrun=".(dol_strlen($this->datelastrun) != 0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').",";
  701. $sql .= " datenextrun=".(dol_strlen($this->datenextrun) != 0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').",";
  702. $sql .= " dateend=".(dol_strlen($this->dateend) != 0 ? "'".$this->db->idate($this->dateend)."'" : 'null').",";
  703. $sql .= " datestart=".(dol_strlen($this->datestart) != 0 ? "'".$this->db->idate($this->datestart)."'" : 'null').",";
  704. $sql .= " datelastresult=".(dol_strlen($this->datelastresult) != 0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').",";
  705. $sql .= " lastresult=".(isset($this->lastresult) ? "'".$this->db->escape($this->lastresult)."'" : "null").",";
  706. $sql .= " lastoutput=".(isset($this->lastoutput) ? "'".$this->db->escape($this->lastoutput)."'" : "null").",";
  707. $sql .= " unitfrequency=".(isset($this->unitfrequency) ? $this->unitfrequency : "null").",";
  708. $sql .= " frequency=".(isset($this->frequency) ? $this->frequency : "null").",";
  709. $sql .= " status=".(isset($this->status) ? $this->status : "null").",";
  710. $sql .= " processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing : "0").",";
  711. $sql .= " fk_user_mod=".$user->id.",";
  712. $sql .= " note=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
  713. $sql .= " nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun : "null").",";
  714. $sql .= " maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun : "0").",";
  715. $sql .= " libname=".(isset($this->libname) ? "'".$this->db->escape($this->libname)."'" : "null").",";
  716. $sql .= " test=".(isset($this->test) ? "'".$this->db->escape($this->test)."'" : "null");
  717. $sql .= " WHERE rowid=".((int) $this->id);
  718. $this->db->begin();
  719. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  720. $resql = $this->db->query($sql);
  721. if (!$resql) {
  722. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  723. }
  724. // Commit or rollback
  725. if ($error) {
  726. foreach ($this->errors as $errmsg) {
  727. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  728. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  729. }
  730. $this->db->rollback();
  731. return -1 * $error;
  732. } else {
  733. $this->db->commit();
  734. return 1;
  735. }
  736. }
  737. /**
  738. * Delete object in database
  739. *
  740. * @param User $user User that deletes
  741. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  742. * @return int <0 if KO, >0 if OK
  743. */
  744. public function delete($user, $notrigger = 0)
  745. {
  746. $error = 0;
  747. $this->db->begin();
  748. $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
  749. $sql .= " WHERE rowid=".((int) $this->id);
  750. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  751. $resql = $this->db->query($sql);
  752. if (!$resql) {
  753. $error++;
  754. $this->errors[] = "Error ".$this->db->lasterror();
  755. }
  756. // Commit or rollback
  757. if ($error) {
  758. foreach ($this->errors as $errmsg) {
  759. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  760. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  761. }
  762. $this->db->rollback();
  763. return -1 * $error;
  764. } else {
  765. $this->db->commit();
  766. return 1;
  767. }
  768. }
  769. /**
  770. * Load an object from its id and create a new one in database
  771. *
  772. * @param User $user User making the clone
  773. * @param int $fromid Id of object to clone
  774. * @return int New id of clone
  775. */
  776. public function createFromClone(User $user, $fromid)
  777. {
  778. global $langs;
  779. $error = 0;
  780. $object = new Cronjob($this->db);
  781. $this->db->begin();
  782. // Load source object
  783. $object->fetch($fromid);
  784. $object->id = 0;
  785. // Clear fields
  786. $object->status = self::STATUS_DISABLED;
  787. $object->label = $langs->trans("CopyOf").' '.$langs->trans($object->label);
  788. // Create clone
  789. $object->context['createfromclone'] = 'createfromclone';
  790. $result = $object->create($user);
  791. // Other options
  792. if ($result < 0) {
  793. $this->error = $object->error;
  794. $error++;
  795. }
  796. unset($object->context['createfromclone']);
  797. // End
  798. if (!$error) {
  799. $this->db->commit();
  800. return $object->id;
  801. } else {
  802. $this->db->rollback();
  803. return -1;
  804. }
  805. }
  806. /**
  807. * Initialise object with example values
  808. * Id must be 0 if object instance is a specimen
  809. *
  810. * @return void
  811. */
  812. public function initAsSpecimen()
  813. {
  814. $this->id = 0;
  815. $this->ref = 0;
  816. $this->entity = 0;
  817. $this->tms = '';
  818. $this->datec = '';
  819. $this->label = '';
  820. $this->jobtype = '';
  821. $this->command = '';
  822. $this->classesname = '';
  823. $this->objectname = '';
  824. $this->methodename = '';
  825. $this->params = '';
  826. $this->md5params = '';
  827. $this->module_name = '';
  828. $this->priority = '';
  829. $this->datelastrun = '';
  830. $this->datenextrun = '';
  831. $this->dateend = '';
  832. $this->datestart = '';
  833. $this->datelastresult = '';
  834. $this->lastoutput = '';
  835. $this->lastresult = '';
  836. $this->unitfrequency = '';
  837. $this->frequency = '';
  838. $this->status = 0;
  839. $this->processing = 0;
  840. $this->fk_user_author = 0;
  841. $this->fk_user_mod = 0;
  842. $this->note_private = '';
  843. $this->nbrun = '';
  844. $this->maxrun = 100;
  845. $this->libname = '';
  846. }
  847. /**
  848. * Return a link to the object card (with optionaly the picto)
  849. *
  850. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  851. * @param string $option On what the link point to ('nolink', ...)
  852. * @param int $notooltip 1=Disable tooltip
  853. * @param string $morecss Add more css on link
  854. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  855. * @return string String with URL
  856. */
  857. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  858. {
  859. global $db, $conf, $langs;
  860. global $dolibarr_main_authentication, $dolibarr_main_demo;
  861. global $menumanager;
  862. if (!empty($conf->dol_no_mouse_hover)) {
  863. $notooltip = 1; // Force disable tooltips
  864. }
  865. $result = '';
  866. $label = img_picto('', 'object_'.$this->picto).' <u>'.$langs->trans("CronTask").'</u>';
  867. if (isset($this->status)) {
  868. $label .= ' '.$this->getLibStatut(5);
  869. }
  870. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  871. $label .= '<br><b>'.$langs->trans('Title').':</b> '.$langs->trans($this->label);
  872. if ($this->label != $langs->trans($this->label)) {
  873. $label .= ' <span class="opacitymedium">('.$this->label.')</span>';
  874. }
  875. if (!empty($this->datestart)) {
  876. $label .= '<br><b>'.$langs->trans('CronDtStart').':</b> '.dol_print_date($this->datestart, 'dayhour', 'tzuserrel');
  877. }
  878. if (!empty($this->dateend)) {
  879. $label .= '<br><b>'.$langs->trans('CronDtEnd').':</b> '.dol_print_date($this->dateend, 'dayhour', 'tzuserrel');
  880. }
  881. $url = DOL_URL_ROOT.'/cron/card.php?id='.$this->id;
  882. if ($option != 'nolink') {
  883. // Add param to save lastsearch_values or not
  884. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  885. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  886. $add_save_lastsearch_values = 1;
  887. }
  888. if ($add_save_lastsearch_values) {
  889. $url .= '&save_lastsearch_values=1';
  890. }
  891. }
  892. $linkclose = '';
  893. if (empty($notooltip)) {
  894. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  895. $label = $langs->trans("ShowCronJob");
  896. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  897. }
  898. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  899. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  900. } else {
  901. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  902. }
  903. $linkstart = '<a href="'.$url.'"';
  904. $linkstart .= $linkclose.'>';
  905. $linkend = '</a>';
  906. $result .= $linkstart;
  907. if ($withpicto) {
  908. $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);
  909. }
  910. if ($withpicto != 2) {
  911. $result .= $this->ref;
  912. }
  913. $result .= $linkend;
  914. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  915. return $result;
  916. }
  917. /**
  918. * Load object information
  919. *
  920. * @param int $id ID
  921. * @return int <0 if KO, >0 if OK
  922. */
  923. public function info($id)
  924. {
  925. $sql = "SELECT";
  926. $sql .= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
  927. $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as f";
  928. $sql .= " WHERE f.rowid = ".((int) $id);
  929. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  930. $resql = $this->db->query($sql);
  931. if ($resql) {
  932. if ($this->db->num_rows($resql)) {
  933. $obj = $this->db->fetch_object($resql);
  934. $this->id = $obj->rowid;
  935. $this->user_modification_id = $obj->fk_user_mod;
  936. $this->user_creation_id = $obj->fk_user_author;
  937. $this->date_creation = $this->db->jdate($obj->datec);
  938. $this->date_modification = $this->db->jdate($obj->tms);
  939. }
  940. $this->db->free($resql);
  941. return 1;
  942. } else {
  943. $this->error = "Error ".$this->db->lasterror();
  944. return -1;
  945. }
  946. }
  947. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  948. /**
  949. * Run a job.
  950. * Once job is finished, status and nb of run is updated.
  951. * This function does not plan the next run. This is done by function ->reprogram_jobs
  952. *
  953. * @param string $userlogin User login
  954. * @return int <0 if KO, >0 if OK
  955. */
  956. public function run_jobs($userlogin)
  957. {
  958. // phpcs:enable
  959. global $langs, $conf, $hookmanager;
  960. $hookmanager->initHooks(array('cron'));
  961. $now = dol_now();
  962. $error = 0;
  963. $retval = '';
  964. $langs->load('cron');
  965. if (empty($userlogin)) {
  966. $this->error = "User login is mandatory";
  967. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  968. return -1;
  969. }
  970. // Force the environment of running to the environment declared for job, so jobs launched from command line will run into correct environment
  971. // When job is ran from GUI, the environment should already be same, except if job has entity 0 (visible into all environments)
  972. if ($conf->entity != $this->entity && $this->entity > 0) {
  973. dol_syslog("We try to run a job in entity ".$this->entity." when we are in entity ".$conf->entity, LOG_WARNING);
  974. }
  975. $savcurrententity = $conf->entity;
  976. $conf->setEntityValues($this->db, $this->entity);
  977. dol_syslog(get_class($this)."::run_jobs entity for running job is ".$conf->entity);
  978. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  979. $user = new User($this->db);
  980. $result = $user->fetch('', $userlogin);
  981. if ($result < 0) {
  982. $this->error = "User Error:".$user->error;
  983. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  984. $conf->setEntityValues($this->db, $savcurrententity);
  985. return -1;
  986. } else {
  987. if (empty($user->id)) {
  988. $this->error = " User user login:".$userlogin." do not exists";
  989. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  990. $conf->setEntityValues($this->db, $savcurrententity);
  991. return -1;
  992. }
  993. }
  994. dol_syslog(get_class($this)."::run_jobs jobtype=".$this->jobtype." userlogin=".$userlogin, LOG_DEBUG);
  995. // Increase limit of time. Works only if we are not in safe mode
  996. $ExecTimeLimit = 600;
  997. if (!empty($ExecTimeLimit)) {
  998. $err = error_reporting();
  999. error_reporting(0); // Disable all errors
  1000. //error_reporting(E_ALL);
  1001. @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
  1002. error_reporting($err);
  1003. }
  1004. if (!empty($MemoryLimit)) {
  1005. @ini_set('memory_limit', $MemoryLimit);
  1006. }
  1007. // Update last run date start (to track running jobs)
  1008. $this->datelastrun = $now;
  1009. $this->datelastresult = null;
  1010. $this->lastoutput = '';
  1011. $this->lastresult = '';
  1012. $this->processing = 1; // To know job was started
  1013. $this->nbrun = $this->nbrun + 1;
  1014. $result = $this->update($user); // This include begin/commit
  1015. if ($result < 0) {
  1016. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1017. $conf->setEntityValues($this->db, $savcurrententity);
  1018. return -1;
  1019. }
  1020. // Run a method
  1021. if ($this->jobtype == 'method') {
  1022. // load classes
  1023. if (!$error) {
  1024. $ret = dol_include_once($this->classesname);
  1025. if ($ret === false || (!class_exists($this->objectname))) {
  1026. if ($ret === false) {
  1027. $this->error = $langs->transnoentitiesnoconv('CronCannotLoadClass', $this->classesname, $this->objectname);
  1028. } else {
  1029. $this->error = $langs->transnoentitiesnoconv('CronCannotLoadObject', $this->classesname, $this->objectname);
  1030. }
  1031. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1032. $this->lastoutput = $this->error;
  1033. $this->lastresult = -1;
  1034. $retval = $this->lastresult;
  1035. $error++;
  1036. }
  1037. }
  1038. // test if method exists
  1039. if (!$error) {
  1040. if (!method_exists($this->objectname, $this->methodename)) {
  1041. $this->error = $langs->transnoentitiesnoconv('CronMethodDoesNotExists', $this->objectname, $this->methodename);
  1042. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1043. $this->lastoutput = $this->error;
  1044. $this->lastresult = -1;
  1045. $retval = $this->lastresult;
  1046. $error++;
  1047. }
  1048. if (in_array(strtolower(trim($this->methodename)), array('executecli'))) {
  1049. $this->error = $langs->transnoentitiesnoconv('CronMethodNotAllowed', $this->methodename, $this->objectname);
  1050. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1051. $this->lastoutput = $this->error;
  1052. $this->lastresult = -1;
  1053. $retval = $this->lastresult;
  1054. $error++;
  1055. }
  1056. }
  1057. // Load langs
  1058. if (!$error) {
  1059. $result = $langs->load($this->module_name);
  1060. $result = $langs->load($this->module_name.'@'.$this->module_name, 0, 0, '', 0, 1);
  1061. if ($result < 0) { // If technical error
  1062. dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
  1063. $this->error = $langs->error;
  1064. $this->lastoutput = $this->error;
  1065. $this->lastresult = -1;
  1066. $retval = $this->lastresult;
  1067. $error++;
  1068. }
  1069. }
  1070. if (!$error) {
  1071. dol_syslog(get_class($this)."::run_jobs START ".$this->objectname."->".$this->methodename."(".$this->params.");", LOG_DEBUG);
  1072. // Create Object for the called module
  1073. $nameofclass = $this->objectname;
  1074. $object = new $nameofclass($this->db);
  1075. if ($this->entity > 0) {
  1076. $object->entity = $this->entity; // We work on a dedicated entity
  1077. }
  1078. $params_arr = array();
  1079. if (!empty($this->params) || $this->params === '0') {
  1080. $params_arr = array_map('trim', explode(",", $this->params));
  1081. }
  1082. if (!is_array($params_arr)) {
  1083. $result = call_user_func(array($object, $this->methodename), $this->params);
  1084. } else {
  1085. $result = call_user_func_array(array($object, $this->methodename), $params_arr);
  1086. }
  1087. if ($result === false || (!is_bool($result) && $result != 0)) {
  1088. $langs->load("errors");
  1089. $errmsg = '';
  1090. if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
  1091. $errmsg .= $object->error;
  1092. }
  1093. if (is_array($object->errors) && count($object->errors)) {
  1094. $errmsg .= (($errmsg ? ', ' : '').join(', ', $object->errors));
  1095. }
  1096. if (empty($errmsg)) {
  1097. $errmsg = $langs->trans('ErrorUnknown');
  1098. }
  1099. dol_syslog(get_class($this)."::run_jobs END result=".$result." error=".$errmsg, LOG_ERR);
  1100. $this->error = $errmsg;
  1101. $this->lastoutput = ($object->output ? $object->output."\n" : "").$errmsg;
  1102. $this->lastresult = is_numeric($result) ? $result : -1;
  1103. $retval = $this->lastresult;
  1104. $error++;
  1105. } else {
  1106. dol_syslog(get_class($this)."::run_jobs END");
  1107. $this->lastoutput = $object->output;
  1108. $this->lastresult = var_export($result, true);
  1109. $retval = $this->lastresult;
  1110. }
  1111. }
  1112. }
  1113. if ($this->jobtype == 'function') {
  1114. //load lib
  1115. $libpath = '/'.strtolower($this->module_name).'/lib/'.$this->libname;
  1116. $ret = dol_include_once($libpath);
  1117. if ($ret === false) {
  1118. $this->error = $langs->trans('CronCannotLoadLib').': '.$libpath;
  1119. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1120. $conf->setEntityValues($this->db, $savcurrententity);
  1121. return -1;
  1122. }
  1123. // Load langs
  1124. $result = $langs->load($this->module_name);
  1125. $result = $langs->load($this->module_name.'@'.$this->module_name); // If this->module_name was an existing language file, this will make nothing
  1126. if ($result < 0) { // If technical error
  1127. dol_syslog(get_class($this)."::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
  1128. $conf->setEntityValues($this->db, $savcurrententity);
  1129. return -1;
  1130. }
  1131. dol_syslog(get_class($this)."::run_jobs ".$this->libname."::".$this->methodename."(".$this->params.");", LOG_DEBUG);
  1132. $params_arr = explode(", ", $this->params);
  1133. if (!is_array($params_arr)) {
  1134. $result = call_user_func($this->methodename, $this->params);
  1135. } else {
  1136. $result = call_user_func_array($this->methodename, $params_arr);
  1137. }
  1138. if ($result === false || (!is_bool($result) && $result != 0)) {
  1139. $langs->load("errors");
  1140. dol_syslog(get_class($this)."::run_jobs result=".$result, LOG_ERR);
  1141. $this->error = $langs->trans('ErrorUnknown');
  1142. $this->lastoutput = $this->error;
  1143. $this->lastresult = is_numeric($result) ? $result : -1;
  1144. $retval = $this->lastresult;
  1145. $error++;
  1146. } else {
  1147. $this->lastoutput = var_export($result, true);
  1148. $this->lastresult = var_export($result, true); // Return code
  1149. $retval = $this->lastresult;
  1150. }
  1151. }
  1152. // Run a command line
  1153. if ($this->jobtype == 'command') {
  1154. global $dolibarr_cron_allow_cli;
  1155. if (empty($dolibarr_cron_allow_cli)) {
  1156. $langs->load("errors");
  1157. $this->error = $langs->trans("FailedToExecutCommandJob");
  1158. $this->lastoutput = '';
  1159. $this->lastresult = $langs->trans("ErrorParameterMustBeEnabledToAllwoThisFeature", 'dolibarr_cron_allow_cli');
  1160. } else {
  1161. $outputdir = $conf->cron->dir_temp;
  1162. if (empty($outputdir)) {
  1163. $outputdir = $conf->cronjob->dir_temp;
  1164. }
  1165. if (!empty($outputdir)) {
  1166. dol_mkdir($outputdir);
  1167. $outputfile = $outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method
  1168. // Execute a CLI
  1169. include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
  1170. $utils = new Utils($this->db);
  1171. $arrayresult = $utils->executeCLI($this->command, $outputfile);
  1172. $retval = $arrayresult['result'];
  1173. $this->error = $arrayresult['error'];
  1174. $this->lastoutput = $arrayresult['output'];
  1175. $this->lastresult = $arrayresult['result'];
  1176. }
  1177. }
  1178. }
  1179. dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)");
  1180. $this->datelastresult = dol_now();
  1181. $this->processing = 0;
  1182. $result = $this->update($user); // This include begin/commit
  1183. if ($result < 0) {
  1184. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1185. $conf->setEntityValues($this->db, $savcurrententity);
  1186. return -1;
  1187. }
  1188. $conf->setEntityValues($this->db, $savcurrententity);
  1189. return $error ?-1 : 1;
  1190. }
  1191. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1192. /**
  1193. * Reprogram a job
  1194. *
  1195. * @param string $userlogin User login
  1196. * @param integer $now Date returned by dol_now()
  1197. * @return int <0 if KO, >0 if OK
  1198. */
  1199. public function reprogram_jobs($userlogin, $now)
  1200. {
  1201. // phpcs:enable
  1202. dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
  1203. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  1204. $user = new User($this->db);
  1205. $result = $user->fetch('', $userlogin);
  1206. if ($result < 0) {
  1207. $this->error = "User Error : ".$user->error;
  1208. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1209. return -1;
  1210. } else {
  1211. if (empty($user->id)) {
  1212. $this->error = " User user login:".$userlogin." do not exists";
  1213. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1214. return -1;
  1215. }
  1216. }
  1217. dol_syslog(get_class($this)."::reprogram_jobs datenextrun=".$this->datenextrun." ".dol_print_date($this->datenextrun, 'dayhourrfc')." frequency=".$this->frequency." unitfrequency=".$this->unitfrequency, LOG_DEBUG);
  1218. if (empty($this->datenextrun)) {
  1219. if (empty($this->datestart)) {
  1220. $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
  1221. } else {
  1222. $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
  1223. }
  1224. }
  1225. if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0) {
  1226. // Loop until date is after future
  1227. while ($this->datenextrun < $now) {
  1228. $this->datenextrun += ($this->frequency * $this->unitfrequency);
  1229. // TODO For exact frequency (every month, every year, ...), use instead a dol_time_plus_duree($time, $duration_value, $duration_unit)
  1230. }
  1231. } else {
  1232. //$this->datenextrun=$this->datenextrun + ($this->frequency * $this->unitfrequency);
  1233. dol_syslog(get_class($this)."::reprogram_jobs datenextrun is already in future, we do not change it");
  1234. }
  1235. // Archive job
  1236. if ($this->autodelete == 2) {
  1237. if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
  1238. || ($this->dateend && ($this->datenextrun > $this->dateend))) {
  1239. $this->status = self::STATUS_ARCHIVED;
  1240. dol_syslog(get_class($this)."::reprogram_jobs Job will be set to archived", LOG_ERR);
  1241. }
  1242. }
  1243. $result = $this->update($user);
  1244. if ($result < 0) {
  1245. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1246. return -1;
  1247. }
  1248. return 1;
  1249. }
  1250. /**
  1251. * Return label of status of user (active, inactive)
  1252. *
  1253. * @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
  1254. * @return string Label of status
  1255. */
  1256. public function getLibStatut($mode = 0)
  1257. {
  1258. return $this->LibStatut($this->status, $mode, $this->processing, $this->lastresult);
  1259. }
  1260. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1261. /**
  1262. * Renvoi le libelle d'un statut donne
  1263. *
  1264. * @param int $status Id statut
  1265. * @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
  1266. * @param int $processing 0=Not running, 1=Running
  1267. * @param int $lastresult Value of last result (0=no error, error otherwise)
  1268. * @return string Label of status
  1269. */
  1270. public function LibStatut($status, $mode = 0, $processing = 0, $lastresult = 0)
  1271. {
  1272. // phpcs:enable
  1273. $this->labelStatus = array(); // Force reset o array because label depends on other fields
  1274. $this->labelStatusShort = array();
  1275. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  1276. global $langs;
  1277. $langs->load('users');
  1278. $moretext = '';
  1279. if ($processing) {
  1280. $moretext = ' ('.$langs->trans("Running").')';
  1281. } elseif ($lastresult) {
  1282. $moretext .= ' ('.$langs->trans("Error").')';
  1283. }
  1284. $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled').$moretext;
  1285. $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled').$moretext;
  1286. $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
  1287. $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled');
  1288. }
  1289. $statusType = 'status4';
  1290. if ($status == 1 && $processing) {
  1291. $statusType = 'status1';
  1292. }
  1293. if ($status == 0) {
  1294. $statusType = 'status5';
  1295. }
  1296. if ($this->lastresult) {
  1297. $statusType = 'status8';
  1298. }
  1299. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  1300. }
  1301. }
  1302. /**
  1303. * Crob Job line class
  1304. */
  1305. class Cronjobline
  1306. {
  1307. /**
  1308. * @var int ID
  1309. */
  1310. public $id;
  1311. public $entity;
  1312. /**
  1313. * @var string Ref
  1314. */
  1315. public $ref;
  1316. public $tms = '';
  1317. public $datec = '';
  1318. /**
  1319. * @var string Cron Job Line label
  1320. */
  1321. public $label;
  1322. public $jobtype;
  1323. public $command;
  1324. public $classesname;
  1325. public $objectname;
  1326. public $methodename;
  1327. public $params;
  1328. public $md5params;
  1329. public $module_name;
  1330. public $priority;
  1331. public $datelastrun = '';
  1332. public $datenextrun = '';
  1333. public $dateend = '';
  1334. public $datestart = '';
  1335. public $lastresult = '';
  1336. public $lastoutput;
  1337. public $unitfrequency;
  1338. public $frequency;
  1339. /**
  1340. * @var int Status
  1341. */
  1342. public $status;
  1343. /**
  1344. * @var int ID
  1345. */
  1346. public $fk_user_author;
  1347. /**
  1348. * @var int ID
  1349. */
  1350. public $fk_user_mod;
  1351. public $note;
  1352. public $nbrun;
  1353. public $libname;
  1354. /**
  1355. * Constructor
  1356. *
  1357. */
  1358. public function __construct()
  1359. {
  1360. return 1;
  1361. }
  1362. }