interfaces.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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@capnetworks.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. var $db;
  31. var $dir; // Directory with all core and external triggers files
  32. var $errors = array(); // Array for errors
  33. /**
  34. * Constructor
  35. *
  36. * @param DoliDB $db Database handler
  37. */
  38. function __construct($db)
  39. {
  40. $this->db = $db;
  41. }
  42. /**
  43. * Function called when a Dolibarr business event occurs
  44. * This function call all qualified triggers.
  45. *
  46. * @param string $action Trigger event code
  47. * @param object $object Objet concerned. Some context information may also be provided into array property object->context.
  48. * @param User $user Objet user
  49. * @param Lang $langs Objet lang
  50. * @param Conf $conf Objet conf
  51. * @return int Nb of triggers ran if no error, -Nb of triggers with errors otherwise.
  52. */
  53. function run_triggers($action,$object,$user,$langs,$conf)
  54. {
  55. // Check parameters
  56. if (! is_object($object) || ! is_object($conf)) // Error
  57. {
  58. $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);
  59. dol_syslog(get_class($this).'::run_triggers '.$this->error, LOG_ERR);
  60. $this->errors[]=$this->error;
  61. return -1;
  62. }
  63. if (! is_object($langs)) // Warning
  64. {
  65. 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);
  66. }
  67. if (! is_object($user)) // Warning
  68. {
  69. 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);
  70. global $db;
  71. $user = new User($db);
  72. }
  73. $nbfile = $nbtotal = $nbok = $nbko = 0;
  74. $files = array();
  75. $modules = array();
  76. $orders = array();
  77. $i=0;
  78. $dirtriggers=array_merge(array('/core/triggers'),$conf->modules_parts['triggers']);
  79. foreach($dirtriggers as $reldir)
  80. {
  81. $dir=dol_buildpath($reldir,0);
  82. $newdir=dol_osencode($dir);
  83. //print "xx".$dir;exit;
  84. // Check if directory exists (we do not use dol_is_dir to avoir loading files.lib.php at each call)
  85. if (! is_dir($newdir)) continue;
  86. $handle=opendir($newdir);
  87. if (is_resource($handle))
  88. {
  89. while (($file = readdir($handle))!==false)
  90. {
  91. if (is_readable($newdir."/".$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i',$file,$reg))
  92. {
  93. $part1=$reg[1];
  94. $part2=$reg[2];
  95. $part3=$reg[3];
  96. $nbfile++;
  97. // Check if trigger file is disabled by name
  98. if (preg_match('/NORUN$/i',$file)) continue;
  99. // Check if trigger file is for a particular module
  100. $qualified=true;
  101. if (strtolower($reg[2]) != 'all')
  102. {
  103. $module=preg_replace('/^mod/i','',$reg[2]);
  104. $constparam='MAIN_MODULE_'.strtoupper($module);
  105. if (empty($conf->global->$constparam)) $qualified=false;
  106. }
  107. if (! $qualified)
  108. {
  109. dol_syslog(get_class($this)."::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled", LOG_DEBUG);
  110. continue;
  111. }
  112. $modName = "Interface".ucfirst($reg[3]);
  113. //print "file=$file - modName=$modName\n";
  114. if (in_array($modName,$modules)) // $modules = list of modName already loaded
  115. {
  116. $langs->load("errors");
  117. dol_syslog(get_class($this)."::run_triggers action=".$action." ".$langs->trans("ErrorDuplicateTrigger", $newdir."/".$file, $fullpathfiles[$modName]), LOG_WARNING);
  118. continue;
  119. }
  120. try {
  121. //print 'Todo for '.$modName." : ".$newdir.'/'.$file."\n";
  122. include_once $newdir.'/'.$file;
  123. //print 'Done for '.$modName."\n";
  124. }
  125. catch(Exception $e)
  126. {
  127. dol_syslog('ko for '.$modName." ".$e->getMessage()."\n", LOG_ERR);
  128. }
  129. $modules[$i] = $modName;
  130. $files[$i] = $file;
  131. $fullpathfiles[$modName] = $newdir.'/'.$file;
  132. $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
  133. $i++;
  134. }
  135. }
  136. }
  137. }
  138. asort($orders);
  139. // Loop on each trigger
  140. foreach ($orders as $key => $value)
  141. {
  142. $modName = $modules[$key];
  143. if (empty($modName)) continue;
  144. $objMod = new $modName($this->db);
  145. if ($objMod)
  146. {
  147. $result=0;
  148. if (method_exists($objMod, 'runTrigger')) // New method to implement
  149. {
  150. dol_syslog(get_class($this)."::run_triggers action=".$action." Launch runTrigger for file '".$files[$key]."'", LOG_INFO);
  151. $result=$objMod->runTrigger($action,$object,$user,$langs,$conf);
  152. }
  153. elseif (method_exists($objMod, 'run_trigger')) // Deprecated method
  154. {
  155. dol_syslog(get_class($this)."::run_triggers action=".$action." Launch run_trigger for file '".$files[$key]."'", LOG_INFO);
  156. $result=$objMod->run_trigger($action,$object,$user,$langs,$conf);
  157. }
  158. else
  159. {
  160. 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);
  161. }
  162. if ($result > 0)
  163. {
  164. // Action OK
  165. $nbtotal++;
  166. $nbok++;
  167. }
  168. if ($result == 0)
  169. {
  170. // Aucune action faite
  171. $nbtotal++;
  172. }
  173. if ($result < 0)
  174. {
  175. // Action KO
  176. //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($objMod->errors), LOG_ERR);
  177. $nbtotal++;
  178. $nbko++;
  179. if (! empty($objMod->errors)) $this->errors=array_merge($this->errors,$objMod->errors);
  180. else if (! empty($objMod->error)) $this->errors[]=$objMod->error;
  181. //dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($this->errors), LOG_ERR);
  182. }
  183. }
  184. else
  185. {
  186. dol_syslog(get_class($this)."::run_triggers action=".$action." Failed to instantiate trigger for file '".$files[$key]."'", LOG_ERR);
  187. }
  188. }
  189. if ($nbko)
  190. {
  191. 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);
  192. return -$nbko;
  193. }
  194. else
  195. {
  196. //dol_syslog(get_class($this)."::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG);
  197. return $nbok;
  198. }
  199. }
  200. /**
  201. * Return list of triggers. Function used by admin page htdoc/admin/triggers.
  202. * List is sorted by trigger filename so by priority to run.
  203. *
  204. * @param array $forcedirtriggers null=All default directories. This parameter is used by modulebuilder module only.
  205. * @return array Array list of triggers
  206. */
  207. function getTriggersList($forcedirtriggers=null)
  208. {
  209. global $conf, $langs;
  210. $files = array();
  211. $fullpath = array();
  212. $relpath = array();
  213. $iscoreorexternal = array();
  214. $modules = array();
  215. $orders = array();
  216. $i = 0;
  217. $dirtriggers=array_merge(array('/core/triggers/'),$conf->modules_parts['triggers']);
  218. if (is_array($forcedirtriggers))
  219. {
  220. $dirtriggers=$forcedirtriggers;
  221. }
  222. foreach($dirtriggers as $reldir)
  223. {
  224. $dir=dol_buildpath($reldir,0);
  225. $newdir=dol_osencode($dir);
  226. // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call)
  227. if (! is_dir($newdir)) continue;
  228. $handle=opendir($newdir);
  229. if (is_resource($handle))
  230. {
  231. while (($file = readdir($handle))!==false)
  232. {
  233. if (is_readable($newdir.'/'.$file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/',$file,$reg))
  234. {
  235. $part1=$reg[1];
  236. $part2=$reg[2];
  237. $part3=$reg[3];
  238. $modName = 'Interface'.ucfirst($reg[3]);
  239. //print "file=$file"; print "modName=$modName"; exit;
  240. if (in_array($modName,$modules))
  241. {
  242. $langs->load("errors");
  243. print '<div class="error">'.$langs->trans("Error").' : '.$langs->trans("ErrorDuplicateTrigger",$modName,"/htdocs/core/triggers/").'</div>';
  244. }
  245. else
  246. {
  247. include_once $newdir.'/'.$file;
  248. }
  249. $files[$i] = $file;
  250. $fullpath[$i] = $dir.'/'.$file;
  251. $relpath[$i] = preg_replace('/^\//','',$reldir).'/'.$file;
  252. $iscoreorexternal[$i] = ($reldir == '/core/triggers/'?'internal':'external');
  253. $modules[$i] = $modName;
  254. $orders[$i] = $part1.'_'.$part2.'_'.$part3; // Set sort criteria value
  255. $i++;
  256. }
  257. }
  258. closedir($handle);
  259. }
  260. }
  261. asort($orders);
  262. $triggers = array();
  263. $j = 0;
  264. // Loop on each trigger
  265. foreach ($orders as $key => $value)
  266. {
  267. $modName = $modules[$key];
  268. if (empty($modName)) continue;
  269. if (! class_exists($modName))
  270. {
  271. print 'Error: A trigger file was found but its class "'.$modName.'" was not found.'."<br>\n";
  272. continue;
  273. }
  274. $objMod = new $modName($this->db);
  275. // Define disabledbyname and disabledbymodule
  276. $disabledbyname=0;
  277. $disabledbymodule=1;
  278. $module='';
  279. // Check if trigger file is disabled by name
  280. if (preg_match('/NORUN$/i',$files[$key])) $disabledbyname=1;
  281. // Check if trigger file is for a particular module
  282. if (preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i',$files[$key],$reg))
  283. {
  284. $module=preg_replace('/^mod/i','',$reg[2]);
  285. $constparam='MAIN_MODULE_'.strtoupper($module);
  286. if (strtolower($reg[2]) == 'all') $disabledbymodule=0;
  287. else if (empty($conf->global->$constparam)) $disabledbymodule=2;
  288. }
  289. // We set info of modules
  290. $triggers[$j]['picto'] = $objMod->picto?img_object('',$objMod->picto):img_object('','generic');
  291. $triggers[$j]['file'] = $files[$key];
  292. $triggers[$j]['fullpath'] = $fullpath[$key];
  293. $triggers[$j]['relpath'] = $relpath[$key];
  294. $triggers[$j]['iscoreorexternal'] = $iscoreorexternal[$key];
  295. $triggers[$j]['version'] = $objMod->getVersion();
  296. $triggers[$j]['status'] = img_picto($langs->trans("Active"),'tick');
  297. if ($disabledbyname > 0 || $disabledbymodule > 1) $triggers[$j]['status'] = "&nbsp;";
  298. $text ='<b>'.$langs->trans("Description").':</b><br>';
  299. $text.=$objMod->getDesc().'<br>';
  300. $text.='<br><b>'.$langs->trans("Status").':</b><br>';
  301. if ($disabledbyname == 1)
  302. {
  303. $text.=$langs->trans("TriggerDisabledByName").'<br>';
  304. if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
  305. }
  306. else
  307. {
  308. if ($disabledbymodule == 0) $text.=$langs->trans("TriggerAlwaysActive").'<br>';
  309. if ($disabledbymodule == 1) $text.=$langs->trans("TriggerActiveAsModuleActive",$module).'<br>';
  310. if ($disabledbymodule == 2) $text.=$langs->trans("TriggerDisabledAsModuleDisabled",$module).'<br>';
  311. }
  312. $triggers[$j]['info'] = $text;
  313. $j++;
  314. }
  315. return $triggers;
  316. }
  317. }