interfaces.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 <https://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. public $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. public function __construct($db)
  45. {
  46. $this->db = $db;
  47. }
  48. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  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. public 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. $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);
  66. dol_syslog(get_class($this).'::run_triggers '.$this->error, LOG_ERR);
  67. $this->errors[] = $this->error;
  68. return -1;
  69. }
  70. if (!is_object($langs)) { // Warning
  71. 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);
  72. }
  73. if (!is_object($user)) { // Warning
  74. 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);
  75. $user = new User($this->db);
  76. }
  77. //dol_syslog(get_class($this)."::run_triggers action=".$action." Launch run_triggers", LOG_DEBUG);
  78. $nbfile = $nbtotal = $nbok = $nbko = 0;
  79. $files = array();
  80. $modules = array();
  81. $orders = array();
  82. $i = 0;
  83. $dirtriggers = array_merge(array('/core/triggers'), $conf->modules_parts['triggers']);
  84. foreach ($dirtriggers as $reldir) {
  85. $dir = dol_buildpath($reldir, 0);
  86. $newdir = dol_osencode($dir);
  87. //print "xx".$dir;exit;
  88. // Check if directory exists (we do not use dol_is_dir to avoir loading files.lib.php at each call)
  89. if (!is_dir($newdir)) {
  90. continue;
  91. }
  92. $handle = opendir($newdir);
  93. if (is_resource($handle)) {
  94. $fullpathfiles = array();
  95. while (($file = readdir($handle)) !== false) {
  96. $reg = array();
  97. if (is_readable($newdir."/".$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i', $file, $reg)) {
  98. $part1 = $reg[1];
  99. $part2 = $reg[2];
  100. $part3 = $reg[3];
  101. $nbfile++;
  102. // Check if trigger file is disabled by name
  103. if (preg_match('/NORUN$/i', $file)) {
  104. continue;
  105. }
  106. // Check if trigger file is for a particular module
  107. $qualified = true;
  108. if (strtolower($reg[2]) != 'all') {
  109. $module = preg_replace('/^mod/i', '', $reg[2]);
  110. $constparam = 'MAIN_MODULE_'.strtoupper($module);
  111. if (empty($conf->global->$constparam)) {
  112. $qualified = false;
  113. }
  114. }
  115. if (!$qualified) {
  116. //dol_syslog(get_class($this)."::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled", LOG_DEBUG);
  117. continue;
  118. }
  119. $modName = "Interface".ucfirst($reg[3]);
  120. //print "file=$file - modName=$modName\n";
  121. if (in_array($modName, $modules)) { // $modules = list of modName already loaded
  122. $langs->load("errors");
  123. dol_syslog(get_class($this)."::run_triggers action=".$action." ".$langs->trans("ErrorDuplicateTrigger", $newdir."/".$file, $fullpathfiles[$modName]), LOG_WARNING);
  124. continue;
  125. }
  126. try {
  127. //print 'Todo for '.$modName." : ".$newdir.'/'.$file."\n";
  128. include_once $newdir.'/'.$file;
  129. //print 'Done for '.$modName."\n";
  130. } catch (Exception $e) {
  131. dol_syslog('ko for '.$modName." ".$e->getMessage()."\n", LOG_ERR);
  132. }
  133. $modules[$i] = $modName;
  134. $files[$i] = $file;
  135. $fullpathfiles[$modName] = $newdir.'/'.$file;
  136. $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
  137. $i++;
  138. }
  139. }
  140. closedir($handle);
  141. }
  142. }
  143. asort($orders);
  144. // Loop on each trigger
  145. foreach ($orders as $key => $value) {
  146. $modName = $modules[$key];
  147. if (empty($modName)) {
  148. continue;
  149. }
  150. $objMod = new $modName($this->db);
  151. if ($objMod) {
  152. $dblevelbefore = $this->db->transaction_opened;
  153. $result = 0;
  154. if (method_exists($objMod, 'runTrigger')) { // New method to implement
  155. //dol_syslog(get_class($this)."::run_triggers action=".$action." Launch runTrigger for file '".$files[$key]."'", LOG_DEBUG);
  156. $result = $objMod->runTrigger($action, $object, $user, $langs, $conf);
  157. } elseif (method_exists($objMod, 'run_trigger')) { // Deprecated method
  158. 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);
  159. $result = $objMod->run_trigger($action, $object, $user, $langs, $conf);
  160. } else {
  161. 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);
  162. }
  163. $dblevelafter = $this->db->transaction_opened;
  164. if ($dblevelbefore != $dblevelafter) {
  165. $errormessage = "Error, the balance begin/close of db transactions has been broken into trigger ".$modName." with action=".$action." before=".$dblevelbefore." after=".$dblevelafter;
  166. $this->errors[] = $errormessage;
  167. dol_syslog($errormessage, LOG_ERR);
  168. $result = -1;
  169. }
  170. if ($result > 0) {
  171. // Action OK
  172. $nbtotal++;
  173. $nbok++;
  174. }
  175. if ($result == 0) {
  176. // Aucune action faite
  177. $nbtotal++;
  178. }
  179. if ($result < 0) {
  180. // Action KO
  181. //dol_syslog("Error in trigger ".$action." - result = ".$result." - Nb of error string returned = ".count($objMod->errors), LOG_ERR);
  182. $nbtotal++;
  183. $nbko++;
  184. if (!empty($objMod->errors)) {
  185. $this->errors = array_merge($this->errors, $objMod->errors);
  186. } elseif (!empty($objMod->error)) {
  187. $this->errors[] = $objMod->error;
  188. }
  189. //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($this->errors), LOG_ERR);
  190. }
  191. } else {
  192. dol_syslog(get_class($this)."::run_triggers action=".$action." Failed to instantiate trigger for file '".$files[$key]."'", LOG_ERR);
  193. }
  194. }
  195. if ($nbko) {
  196. 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);
  197. return -$nbko;
  198. } else {
  199. //dol_syslog(get_class($this)."::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG);
  200. return $nbok;
  201. }
  202. }
  203. /**
  204. * Return list of triggers. Function used by admin page htdoc/admin/triggers.
  205. * List is sorted by trigger filename so by priority to run.
  206. *
  207. * @param array $forcedirtriggers null=All default directories. This parameter is used by modulebuilder module only.
  208. * @return array Array list of triggers
  209. */
  210. public function getTriggersList($forcedirtriggers = null)
  211. {
  212. global $conf, $langs, $db;
  213. $files = array();
  214. $fullpath = array();
  215. $relpath = array();
  216. $iscoreorexternal = array();
  217. $modules = array();
  218. $orders = array();
  219. $i = 0;
  220. $dirtriggers = array_merge(array('/core/triggers/'), $conf->modules_parts['triggers']);
  221. if (is_array($forcedirtriggers)) {
  222. $dirtriggers = $forcedirtriggers;
  223. }
  224. foreach ($dirtriggers as $reldir) {
  225. $dir = dol_buildpath($reldir, 0);
  226. $newdir = dol_osencode($dir);
  227. // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
  228. if (!is_dir($newdir)) {
  229. continue;
  230. }
  231. $handle = opendir($newdir);
  232. if (is_resource($handle)) {
  233. while (($file = readdir($handle)) !== false) {
  234. $reg = array();
  235. if (is_readable($newdir.'/'.$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/', $file, $reg)) {
  236. if (preg_match('/\.back$/', $file)) {
  237. continue;
  238. }
  239. $part1 = $reg[1];
  240. $part2 = $reg[2];
  241. $part3 = $reg[3];
  242. $modName = 'Interface'.ucfirst($reg[3]);
  243. //print "file=$file"; print "modName=$modName"; exit;
  244. if (in_array($modName, $modules)) {
  245. $langs->load("errors");
  246. print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateTrigger", $modName, "/htdocs/core/triggers/").'</div>';
  247. } else {
  248. include_once $newdir.'/'.$file;
  249. }
  250. $files[$i] = $file;
  251. $fullpath[$i] = $dir.'/'.$file;
  252. $relpath[$i] = preg_replace('/^\//', '', $reldir).'/'.$file;
  253. $iscoreorexternal[$i] = ($reldir == '/core/triggers/' ? 'internal' : 'external');
  254. $modules[$i] = $modName;
  255. $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
  256. $i++;
  257. }
  258. }
  259. closedir($handle);
  260. }
  261. }
  262. asort($orders);
  263. $triggers = array();
  264. $j = 0;
  265. // Loop on each trigger
  266. foreach ($orders as $key => $value) {
  267. $modName = $modules[$key];
  268. if (empty($modName)) {
  269. continue;
  270. }
  271. if (!class_exists($modName)) {
  272. print 'Error: A trigger file was found but its class "'.$modName.'" was not found.'."<br>\n";
  273. continue;
  274. }
  275. $text = '';
  276. try {
  277. $objMod = new $modName($db);
  278. if (is_subclass_of($objMod, 'DolibarrTriggers')) {
  279. // Define disabledbyname and disabledbymodule
  280. $disabledbyname = 0;
  281. $disabledbymodule = 1;
  282. $module = '';
  283. // Check if trigger file is disabled by name
  284. if (preg_match('/NORUN$/i', $files[$key])) {
  285. $disabledbyname = 1;
  286. }
  287. // Check if trigger file is for a particular module
  288. if (preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i', $files[$key], $reg)) {
  289. $module = preg_replace('/^mod/i', '', $reg[2]);
  290. $constparam = 'MAIN_MODULE_'.strtoupper($module);
  291. if (strtolower($module) == 'all') {
  292. $disabledbymodule = 0;
  293. } elseif (empty($conf->global->$constparam)) {
  294. $disabledbymodule = 2;
  295. }
  296. $triggers[$j]['module'] = strtolower($module);
  297. }
  298. // We set info of modules
  299. $triggers[$j]['picto'] = (!empty($objMod->picto)) ? img_object('', $objMod->picto, 'class="valignmiddle pictomodule "') : img_object('', 'generic', 'class="valignmiddle pictomodule "');
  300. $triggers[$j]['file'] = $files[$key];
  301. $triggers[$j]['fullpath'] = $fullpath[$key];
  302. $triggers[$j]['relpath'] = $relpath[$key];
  303. $triggers[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
  304. $triggers[$j]['version'] = $objMod->getVersion();
  305. $triggers[$j]['status'] = img_picto($langs->trans("Active"), 'tick');
  306. if ($disabledbyname > 0 || $disabledbymodule > 1) {
  307. $triggers[$j]['status'] = '';
  308. }
  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. $text .= $langs->trans("TriggerDisabledByName").'<br>';
  314. if ($disabledbymodule == 2) {
  315. $text .= $langs->trans("TriggerDisabledAsModuleDisabled", $module).'<br>';
  316. }
  317. } else {
  318. if ($disabledbymodule == 0) {
  319. $text .= $langs->trans("TriggerAlwaysActive").'<br>';
  320. }
  321. if ($disabledbymodule == 1) {
  322. $text .= $langs->trans("TriggerActiveAsModuleActive", $module).'<br>';
  323. }
  324. if ($disabledbymodule == 2) {
  325. $text .= $langs->trans("TriggerDisabledAsModuleDisabled", $module).'<br>';
  326. }
  327. }
  328. } else {
  329. $triggers[$j]['picto'] = (!empty($objMod->picto)) ? img_object('', $objMod->picto, 'class="valignmiddle pictomodule "') : img_object('', 'generic', 'class="valignmiddle pictomodule "');
  330. $triggers[$j]['file'] = $files[$key];
  331. $triggers[$j]['fullpath'] = $fullpath[$key];
  332. $triggers[$j]['relpath'] = $relpath[$key];
  333. $triggers[$j]['status'] = img_picto('Error: Trigger '.$modName.' does not extends DolibarrTriggers', 'warning');
  334. //print 'Error: Trigger '.$modName.' does not extends DolibarrTriggers<br>';
  335. $text = 'Error: Trigger '.$modName.' does not extends DolibarrTriggers';
  336. }
  337. } catch (Exception $e) {
  338. print $e->getMessage();
  339. }
  340. $triggers[$j]['info'] = $text;
  341. $j++;
  342. }
  343. return $triggers;
  344. }
  345. }