interfaces.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /* Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2010 Regis Houssin <regis.houssin@inodbox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/interfaces.class.php
  21. * \ingroup core
  22. * \brief Fichier de la classe de gestion des triggers
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
  25. /**
  26. * Class to manage triggers
  27. */
  28. class Interfaces
  29. {
  30. /**
  31. * @var DoliDB Database handler.
  32. */
  33. public $db;
  34. var $dir; // Directory with all core and external triggers files
  35. /**
  36. * @var string[] Error codes (or messages)
  37. */
  38. public $errors = array();
  39. /**
  40. * Constructor
  41. *
  42. * @param DoliDB $db Database handler
  43. */
  44. function __construct($db)
  45. {
  46. $this->db = $db;
  47. }
  48. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  49. /**
  50. * Function called when a Dolibarr business event occurs
  51. * This function call all qualified triggers.
  52. *
  53. * @param string $action Trigger event code
  54. * @param object $object Objet concerned. Some context information may also be provided into array property object->context.
  55. * @param User $user Objet user
  56. * @param Translate $langs Objet lang
  57. * @param Conf $conf Objet conf
  58. * @return int Nb of triggers ran if no error, -Nb of triggers with errors otherwise.
  59. */
  60. function run_triggers($action,$object,$user,$langs,$conf)
  61. {
  62. // phpcs:enable
  63. // Check parameters
  64. if (! is_object($object) || ! is_object($conf)) // Error
  65. {
  66. $this->error='function run_triggers called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf);
  67. dol_syslog(get_class($this).'::run_triggers '.$this->error, LOG_ERR);
  68. $this->errors[]=$this->error;
  69. return -1;
  70. }
  71. if (! is_object($langs)) // Warning
  72. {
  73. dol_syslog(get_class($this).'::run_triggers was called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf), LOG_WARNING);
  74. }
  75. if (! is_object($user)) // Warning
  76. {
  77. dol_syslog(get_class($this).'::run_triggers was called with wrong parameters action='.$action.' object='.is_object($object).' user='.is_object($user).' langs='.is_object($langs).' conf='.is_object($conf), LOG_WARNING);
  78. global $db;
  79. $user = new User($db);
  80. }
  81. //dol_syslog(get_class($this)."::run_triggers action=".$action." Launch run_triggers", LOG_DEBUG);
  82. $nbfile = $nbtotal = $nbok = $nbko = 0;
  83. $files = array();
  84. $modules = array();
  85. $orders = array();
  86. $i=0;
  87. $dirtriggers=array_merge(array('/core/triggers'),$conf->modules_parts['triggers']);
  88. foreach($dirtriggers as $reldir)
  89. {
  90. $dir=dol_buildpath($reldir,0);
  91. $newdir=dol_osencode($dir);
  92. //print "xx".$dir;exit;
  93. // Check if directory exists (we do not use dol_is_dir to avoir loading files.lib.php at each call)
  94. if (! is_dir($newdir)) continue;
  95. $handle=opendir($newdir);
  96. if (is_resource($handle))
  97. {
  98. while (($file = readdir($handle))!==false)
  99. {
  100. if (is_readable($newdir."/".$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i',$file,$reg))
  101. {
  102. $part1=$reg[1];
  103. $part2=$reg[2];
  104. $part3=$reg[3];
  105. $nbfile++;
  106. // Check if trigger file is disabled by name
  107. if (preg_match('/NORUN$/i',$file)) continue;
  108. // Check if trigger file is for a particular module
  109. $qualified=true;
  110. if (strtolower($reg[2]) != 'all')
  111. {
  112. $module=preg_replace('/^mod/i','',$reg[2]);
  113. $constparam='MAIN_MODULE_'.strtoupper($module);
  114. if (empty($conf->global->$constparam)) $qualified=false;
  115. }
  116. if (! $qualified)
  117. {
  118. //dol_syslog(get_class($this)."::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled", LOG_DEBUG);
  119. continue;
  120. }
  121. $modName = "Interface".ucfirst($reg[3]);
  122. //print "file=$file - modName=$modName\n";
  123. if (in_array($modName,$modules)) // $modules = list of modName already loaded
  124. {
  125. $langs->load("errors");
  126. dol_syslog(get_class($this)."::run_triggers action=".$action." ".$langs->trans("ErrorDuplicateTrigger", $newdir."/".$file, $fullpathfiles[$modName]), LOG_WARNING);
  127. continue;
  128. }
  129. try {
  130. //print 'Todo for '.$modName." : ".$newdir.'/'.$file."\n";
  131. include_once $newdir.'/'.$file;
  132. //print 'Done for '.$modName."\n";
  133. }
  134. catch(Exception $e)
  135. {
  136. dol_syslog('ko for '.$modName." ".$e->getMessage()."\n", LOG_ERR);
  137. }
  138. $modules[$i] = $modName;
  139. $files[$i] = $file;
  140. $fullpathfiles[$modName] = $newdir.'/'.$file;
  141. $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
  142. $i++;
  143. }
  144. }
  145. }
  146. }
  147. asort($orders);
  148. // Loop on each trigger
  149. foreach ($orders as $key => $value)
  150. {
  151. $modName = $modules[$key];
  152. if (empty($modName)) continue;
  153. $objMod = new $modName($this->db);
  154. if ($objMod)
  155. {
  156. $result=0;
  157. if (method_exists($objMod, 'runTrigger')) // New method to implement
  158. {
  159. //dol_syslog(get_class($this)."::run_triggers action=".$action." Launch runTrigger for file '".$files[$key]."'", LOG_DEBUG);
  160. $result=$objMod->runTrigger($action,$object,$user,$langs,$conf);
  161. }
  162. elseif (method_exists($objMod, 'run_trigger')) // Deprecated method
  163. {
  164. dol_syslog(get_class($this)."::run_triggers action=".$action." Launch old method run_trigger (rename your trigger into runTrigger) for file '".$files[$key]."'", LOG_WARNING);
  165. $result=$objMod->run_trigger($action,$object,$user,$langs,$conf);
  166. }
  167. else
  168. {
  169. dol_syslog(get_class($this)."::run_triggers action=".$action." A trigger was declared for class ".get_class($objMod)." but method runTrigger was not found", LOG_ERR);
  170. }
  171. if ($result > 0)
  172. {
  173. // Action OK
  174. $nbtotal++;
  175. $nbok++;
  176. }
  177. if ($result == 0)
  178. {
  179. // Aucune action faite
  180. $nbtotal++;
  181. }
  182. if ($result < 0)
  183. {
  184. // Action KO
  185. //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($objMod->errors), LOG_ERR);
  186. $nbtotal++;
  187. $nbko++;
  188. if (! empty($objMod->errors)) $this->errors=array_merge($this->errors,$objMod->errors);
  189. else if (! empty($objMod->error)) $this->errors[]=$objMod->error;
  190. //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($this->errors), LOG_ERR);
  191. }
  192. }
  193. else
  194. {
  195. dol_syslog(get_class($this)."::run_triggers action=".$action." Failed to instantiate trigger for file '".$files[$key]."'", LOG_ERR);
  196. }
  197. }
  198. if ($nbko)
  199. {
  200. dol_syslog(get_class($this)."::run_triggers action=".$action." Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko." - Nb of error string returned in this->errors = ".count($this->errors), LOG_ERR);
  201. return -$nbko;
  202. }
  203. else
  204. {
  205. //dol_syslog(get_class($this)."::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG);
  206. return $nbok;
  207. }
  208. }
  209. /**
  210. * Return list of triggers. Function used by admin page htdoc/admin/triggers.
  211. * List is sorted by trigger filename so by priority to run.
  212. *
  213. * @param array $forcedirtriggers null=All default directories. This parameter is used by modulebuilder module only.
  214. * @return array Array list of triggers
  215. */
  216. function getTriggersList($forcedirtriggers=null)
  217. {
  218. global $conf, $langs, $db;
  219. $files = array();
  220. $fullpath = array();
  221. $relpath = array();
  222. $iscoreorexternal = array();
  223. $modules = array();
  224. $orders = array();
  225. $i = 0;
  226. $dirtriggers=array_merge(array('/core/triggers/'),$conf->modules_parts['triggers']);
  227. if (is_array($forcedirtriggers))
  228. {
  229. $dirtriggers=$forcedirtriggers;
  230. }
  231. foreach($dirtriggers as $reldir)
  232. {
  233. $dir=dol_buildpath($reldir,0);
  234. $newdir=dol_osencode($dir);
  235. // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
  236. if (! is_dir($newdir)) continue;
  237. $handle=opendir($newdir);
  238. if (is_resource($handle))
  239. {
  240. while (($file = readdir($handle))!==false)
  241. {
  242. if (is_readable($newdir.'/'.$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/',$file,$reg))
  243. {
  244. if (preg_match('/\.back$/',$file)) continue;
  245. $part1=$reg[1];
  246. $part2=$reg[2];
  247. $part3=$reg[3];
  248. $modName = 'Interface'.ucfirst($reg[3]);
  249. //print "file=$file"; print "modName=$modName"; exit;
  250. if (in_array($modName,$modules))
  251. {
  252. $langs->load("errors");
  253. print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateTrigger",$modName,"/htdocs/core/triggers/").'</div>';
  254. }
  255. else
  256. {
  257. include_once $newdir.'/'.$file;
  258. }
  259. $files[$i] = $file;
  260. $fullpath[$i] = $dir.'/'.$file;
  261. $relpath[$i] = preg_replace('/^\//','',$reldir).'/'.$file;
  262. $iscoreorexternal[$i] = ($reldir == '/core/triggers/'?'internal':'external');
  263. $modules[$i] = $modName;
  264. $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
  265. $i++;
  266. }
  267. }
  268. closedir($handle);
  269. }
  270. }
  271. asort($orders);
  272. $triggers = array();
  273. $j = 0;
  274. // Loop on each trigger
  275. foreach ($orders as $key => $value)
  276. {
  277. $modName = $modules[$key];
  278. if (empty($modName)) continue;
  279. if (! class_exists($modName))
  280. {
  281. print 'Error: A trigger file was found but its class "'.$modName.'" was not found.'."<br>\n";
  282. continue;
  283. }
  284. $objMod = new $modName($db);
  285. // Define disabledbyname and disabledbymodule
  286. $disabledbyname=0;
  287. $disabledbymodule=1;
  288. $module='';
  289. // Check if trigger file is disabled by name
  290. if (preg_match('/NORUN$/i',$files[$key])) $disabledbyname=1;
  291. // Check if trigger file is for a particular module
  292. if (preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i',$files[$key],$reg))
  293. {
  294. $module=preg_replace('/^mod/i','',$reg[2]);
  295. $constparam='MAIN_MODULE_'.strtoupper($module);
  296. if (strtolower($module) == 'all') $disabledbymodule=0;
  297. else if (empty($conf->global->$constparam)) $disabledbymodule=2;
  298. $triggers[$j]['module']=strtolower($module);
  299. }
  300. // We set info of modules
  301. $triggers[$j]['picto'] = $objMod->picto?img_object('',$objMod->picto):img_object('','generic');
  302. $triggers[$j]['file'] = $files[$key];
  303. $triggers[$j]['fullpath'] = $fullpath[$key];
  304. $triggers[$j]['relpath'] = $relpath[$key];
  305. $triggers[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
  306. $triggers[$j]['version'] = $objMod->getVersion();
  307. $triggers[$j]['status'] = img_picto($langs->trans("Active"),'tick');
  308. if ($disabledbyname > 0 || $disabledbymodule > 1) $triggers[$j]['status'] = '';
  309. $text ='<b>'.$langs->trans("Description").':</b><br>';
  310. $text.=$objMod->getDesc().'<br>';
  311. $text.='<br><b>'.$langs->trans("Status").':</b><br>';
  312. if ($disabledbyname == 1)
  313. {
  314. $text.=$langs->trans("TriggerDisabledByName").'<br>';
  315. if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
  316. }
  317. else
  318. {
  319. if ($disabledbymodule == 0) $text.=$langs->trans("TriggerAlwaysActive").'<br>';
  320. if ($disabledbymodule == 1) $text.=$langs->trans("TriggerActiveAsModuleActive",$module).'<br>';
  321. if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
  322. }
  323. $triggers[$j]['info'] = $text;
  324. $j++;
  325. }
  326. return $triggers;
  327. }
  328. }