interfaces.class.php 14 KB

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