interface_20_all_Logevents.class.php 7.8 KB

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