dolibarr  17.0.3
interface_20_all_Logevents.class.php
Go to the documentation of this file.
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 
26 require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
27 
28 
33 {
39  public function __construct($db)
40  {
41  $this->db = $db;
42 
43  $this->name = preg_replace('/^Interface/i', '', get_class($this));
44  $this->family = "core";
45  $this->description = "Triggers of this module allows to add security event records inside Dolibarr.";
46  // 'development', 'experimental', 'dolibarr' or version
47  $this->version = self::VERSION_DOLIBARR;
48  $this->picto = 'technic';
49  }
50 
62  public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
63  {
64  if (!empty($conf->global->MAIN_LOGEVENTS_DISABLE_ALL)) {
65  return 0; // Log events is disabled (hidden features)
66  }
67 
68  $key = 'MAIN_LOGEVENTS_'.$action;
69  //dol_syslog("xxxxxxxxxxx".$key);
70  if (empty($conf->global->$key)) {
71  return 0; // Log events not enabled for this action
72  }
73 
74  if (empty($conf->entity)) {
75  $conf->entity = $entity; // forcing of the entity if it's not defined (ex: in login form)
76  }
77 
78  $date = dol_now();
79 
80  // Actions
81  if ($action == 'USER_LOGIN') {
82  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
83 
84  $langs->load("users");
85  // Initialisation donnees (date,duree,texte,desc)
86  $text = "(UserLogged,".$object->login.")";
87  $text .= (empty($object->trigger_mesg) ? '' : ' - '.$object->trigger_mesg);
88  $desc = "(UserLogged,".$object->login.")";
89  $desc .= (empty($object->trigger_mesg) ? '' : ' - '.$object->trigger_mesg);
90  } elseif ($action == 'USER_LOGIN_FAILED') {
91  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
92 
93  // Initialisation donnees (date,duree,texte,desc)
94  $text = $object->trigger_mesg; // Message direct
95  $desc = $object->trigger_mesg; // Message direct
96  } elseif ($action == 'USER_LOGOUT') {
97  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
98 
99  $langs->load("users");
100  // Initialisation donnees (date,duree,texte,desc)
101  $text = "(UserLogoff,".$object->login.")";
102  $desc = "(UserLogoff,".$object->login.")";
103  } elseif ($action == 'USER_CREATE') {
104  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
105  $langs->load("users");
106 
107  // Initialisation donnees (date,duree,texte,desc)
108  $text = $langs->transnoentities("NewUserCreated", $object->login);
109  $desc = $langs->transnoentities("NewUserCreated", $object->login);
110  } elseif ($action == 'USER_MODIFY') {
111  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
112  $langs->load("users");
113 
114  // Initialisation donnees (date,duree,texte,desc)
115  $text = $langs->transnoentities("EventUserModified", $object->login);
116  $desc = $langs->transnoentities("EventUserModified", $object->login);
117  } elseif ($action == 'USER_NEW_PASSWORD') {
118  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
119  $langs->load("users");
120 
121  // Initialisation donnees (date,duree,texte,desc)
122  $text = $langs->transnoentities("NewUserPassword", $object->login);
123  $desc = $langs->transnoentities("NewUserPassword", $object->login);
124  } elseif ($action == 'USER_ENABLEDISABLE') {
125  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
126  $langs->load("users");
127  // Initialisation donnees (date,duree,texte,desc)
128  if ($object->statut == 0) {
129  $text = $langs->transnoentities("UserEnabled", $object->login);
130  $desc = $langs->transnoentities("UserEnabled", $object->login);
131  }
132  if ($object->statut == 1) {
133  $text = $langs->transnoentities("UserDisabled", $object->login);
134  $desc = $langs->transnoentities("UserDisabled", $object->login);
135  }
136  } elseif ($action == 'USER_DELETE') {
137  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
138  $langs->load("users");
139  // Initialisation donnees (date,duree,texte,desc)
140  $text = $langs->transnoentities("UserDeleted", $object->login);
141  $desc = $langs->transnoentities("UserDeleted", $object->login);
142  } elseif ($action == 'USERGROUP_CREATE') {
143  // Groups
144  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
145  $langs->load("users");
146  // Initialisation donnees (date,duree,texte,desc)
147  $text = $langs->transnoentities("NewGroupCreated", $object->name);
148  $desc = $langs->transnoentities("NewGroupCreated", $object->name);
149  } elseif ($action == 'USERGROUP_MODIFY') {
150  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
151  $langs->load("users");
152  // Initialisation donnees (date,duree,texte,desc)
153  $text = $langs->transnoentities("GroupModified", $object->name);
154  $desc = $langs->transnoentities("GroupModified", $object->name);
155  } elseif ($action == 'USERGROUP_DELETE') {
156  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
157  $langs->load("users");
158  // Initialisation donnees (date,duree,texte,desc)
159  $text = $langs->transnoentities("GroupDeleted", $object->name);
160  $desc = $langs->transnoentities("GroupDeleted", $object->name);
161  }
162 
163  // If not found
164  /*
165  else
166  {
167  dol_syslog("Trigger '".$this->name."' for action '$action' was ran by ".__FILE__." but no handler found for this action.");
168  return 0;
169  }
170  */
171 
172  // Add more information into desc from the context property
173  if (!empty($desc) && !empty($object->context['audit'])) {
174  $desc .= ' - '.$object->context['audit'];
175  }
176 
177  // Add entry in event table
178  include_once DOL_DOCUMENT_ROOT.'/core/class/events.class.php';
179 
180  $event = new Events($this->db);
181  $event->type = $action;
182  $event->dateevent = $date;
183  $event->label = $text;
184  $event->description = $desc;
185  $event->user_agent = (empty($_SERVER["HTTP_USER_AGENT"]) ? '' : $_SERVER["HTTP_USER_AGENT"]);
186 
187  $result = $event->create($user);
188  if ($result > 0) {
189  return 1;
190  } else {
191  $error = "Failed to insert security event: ".$event->error;
192  $this->errors[] = $error;
193  $this->error = $error;
194 
195  dol_syslog(get_class($this).": ".$error, LOG_ERR);
196  return -1;
197  }
198  }
199 }
db
$conf db
API class for accounts.
Definition: inc.php:41
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
Translate
Class to manage translations.
Definition: translate.class.php:30
InterfaceLogevents\runTrigger
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarrr security audit event is done.
Definition: interface_20_all_Logevents.class.php:62
Events
Events class.
Definition: events.class.php:29
name
$conf db name
Definition: repair.php:122
Conf
Class to stock current configuration.
Definition: conf.class.php:33
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
InterfaceLogevents
Class of triggers for security audit events.
Definition: interface_20_all_Logevents.class.php:32
User
Class to manage Dolibarr users.
Definition: user.class.php:46
DolibarrTriggers
Class that all the triggers must extend.
Definition: dolibarrtriggers.class.php:21
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2951
InterfaceLogevents\__construct
__construct($db)
Constructor.
Definition: interface_20_all_Logevents.class.php:39