interface_20_all_Logevents.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /* Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2009 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.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/triggers/interface_20_all_Logevents.class.php
  21. * \ingroup core
  22. * \brief Trigger file for
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
  25. /**
  26. * Class of triggers for security events
  27. */
  28. class InterfaceLogevents extends DolibarrTriggers
  29. {
  30. public $picto = 'technic';
  31. public $family = 'core';
  32. public $description = "Triggers of this module allows to add security event records inside Dolibarr.";
  33. public $version = self::VERSION_DOLIBARR;
  34. /**
  35. * Function called when a Dolibarrr business event is done.
  36. * All functions "runTrigger" are triggered if file is inside directory htdocs/core/triggers or htdocs/module/code/triggers (and declared)
  37. *
  38. * @param string $action Event action code
  39. * @param Object $object Object
  40. * @param User $user Object user
  41. * @param Translate $langs Object langs
  42. * @param conf $conf Object conf
  43. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  44. */
  45. public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
  46. {
  47. if (! empty($conf->global->MAIN_LOGEVENTS_DISABLE_ALL)) return 0; // Log events is disabled (hidden features)
  48. $key='MAIN_LOGEVENTS_'.$action;
  49. //dol_syslog("xxxxxxxxxxx".$key);
  50. if (empty($conf->global->$key)) return 0; // Log events not enabled for this action
  51. if (empty($conf->entity)) $conf->entity = $entity; // forcing of the entity if it's not defined (ex: in login form)
  52. $date = dol_now();
  53. // Actions
  54. if ($action == 'USER_LOGIN')
  55. {
  56. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  57. $langs->load("users");
  58. // Initialisation donnees (date,duree,texte,desc)
  59. $text="(UserLogged,".$object->login.")";
  60. $text.=(empty($object->trigger_mesg)?'':' - '.$object->trigger_mesg);
  61. $desc="(UserLogged,".$object->login.")";
  62. $desc.=(empty($object->trigger_mesg)?'':' - '.$object->trigger_mesg);
  63. }
  64. if ($action == 'USER_LOGIN_FAILED')
  65. {
  66. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  67. // Initialisation donnees (date,duree,texte,desc)
  68. $text=$object->trigger_mesg; // Message direct
  69. $desc=$object->trigger_mesg; // Message direct
  70. }
  71. if ($action == 'USER_LOGOUT')
  72. {
  73. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  74. $langs->load("users");
  75. // Initialisation donnees (date,duree,texte,desc)
  76. $text="(UserLogoff,".$object->login.")";
  77. $desc="(UserLogoff,".$object->login.")";
  78. }
  79. if ($action == 'USER_CREATE')
  80. {
  81. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  82. $langs->load("users");
  83. // Initialisation donnees (date,duree,texte,desc)
  84. $text=$langs->transnoentities("NewUserCreated",$object->login);
  85. $desc=$langs->transnoentities("NewUserCreated",$object->login);
  86. }
  87. elseif ($action == 'USER_MODIFY')
  88. {
  89. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  90. $langs->load("users");
  91. // Initialisation donnees (date,duree,texte,desc)
  92. $text=$langs->transnoentities("EventUserModified",$object->login);
  93. $desc=$langs->transnoentities("EventUserModified",$object->login);
  94. }
  95. elseif ($action == 'USER_NEW_PASSWORD')
  96. {
  97. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  98. $langs->load("users");
  99. // Initialisation donnees (date,duree,texte,desc)
  100. $text=$langs->transnoentities("NewUserPassword",$object->login);
  101. $desc=$langs->transnoentities("NewUserPassword",$object->login);
  102. }
  103. elseif ($action == 'USER_ENABLEDISABLE')
  104. {
  105. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  106. $langs->load("users");
  107. // Initialisation donnees (date,duree,texte,desc)
  108. if ($object->statut == 0)
  109. {
  110. $text=$langs->transnoentities("UserEnabled",$object->login);
  111. $desc=$langs->transnoentities("UserEnabled",$object->login);
  112. }
  113. if ($object->statut == 1)
  114. {
  115. $text=$langs->transnoentities("UserDisabled",$object->login);
  116. $desc=$langs->transnoentities("UserDisabled",$object->login);
  117. }
  118. }
  119. elseif ($action == 'USER_DELETE')
  120. {
  121. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  122. $langs->load("users");
  123. // Initialisation donnees (date,duree,texte,desc)
  124. $text=$langs->transnoentities("UserDeleted",$object->login);
  125. $desc=$langs->transnoentities("UserDeleted",$object->login);
  126. }
  127. // Groupes
  128. elseif ($action == 'GROUP_CREATE')
  129. {
  130. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  131. $langs->load("users");
  132. // Initialisation donnees (date,duree,texte,desc)
  133. $text=$langs->transnoentities("NewGroupCreated",$object->name);
  134. $desc=$langs->transnoentities("NewGroupCreated",$object->name);
  135. }
  136. elseif ($action == 'GROUP_MODIFY')
  137. {
  138. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  139. $langs->load("users");
  140. // Initialisation donnees (date,duree,texte,desc)
  141. $text=$langs->transnoentities("GroupModified",$object->name);
  142. $desc=$langs->transnoentities("GroupModified",$object->name);
  143. }
  144. elseif ($action == 'GROUP_DELETE')
  145. {
  146. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  147. $langs->load("users");
  148. // Initialisation donnees (date,duree,texte,desc)
  149. $text=$langs->transnoentities("GroupDeleted",$object->name);
  150. $desc=$langs->transnoentities("GroupDeleted",$object->name);
  151. }
  152. // If not found
  153. /*
  154. else
  155. {
  156. dol_syslog("Trigger '".$this->name."' for action '$action' was ran by ".__FILE__." but no handler found for this action.");
  157. return 0;
  158. }
  159. */
  160. // Add entry in event table
  161. include_once DOL_DOCUMENT_ROOT.'/core/class/events.class.php';
  162. $event=new Events($this->db);
  163. $event->type=$action;
  164. $event->dateevent=$date;
  165. $event->label=$text;
  166. $event->description=$desc;
  167. $event->user_agent=$_SERVER["HTTP_USER_AGENT"];
  168. $result=$event->create($user);
  169. if ($result > 0)
  170. {
  171. return 1;
  172. }
  173. else
  174. {
  175. $error ="Failed to insert security event: ".$event->error;
  176. $this->error=$error;
  177. dol_syslog(get_class($this).": ".$this->error, LOG_ERR);
  178. return -1;
  179. }
  180. return 0;
  181. }
  182. }