dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
18  */
19 
26 require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
27 
28 
33 {
37  public $picto = 'technic';
38 
39  public $family = 'core';
40 
41  public $description = "Triggers of this module allows to add security event records inside Dolibarr.";
42 
47  public $version = self::VERSION_DOLIBARR;
48 
60  public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
61  {
62  if (! empty($conf->global->MAIN_LOGEVENTS_DISABLE_ALL)) return 0; // Log events is disabled (hidden features)
63 
64  $key='MAIN_LOGEVENTS_'.$action;
65  //dol_syslog("xxxxxxxxxxx".$key);
66  if (empty($conf->global->$key)) return 0; // Log events not enabled for this action
67 
68  if (empty($conf->entity)) $conf->entity = $entity; // forcing of the entity if it's not defined (ex: in login form)
69 
70  $date = dol_now();
71 
72  // Actions
73  if ($action == 'USER_LOGIN')
74  {
75  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
76 
77  $langs->load("users");
78  // Initialisation donnees (date,duree,texte,desc)
79  $text="(UserLogged,".$object->login.")";
80  $text.=(empty($object->trigger_mesg)?'':' - '.$object->trigger_mesg);
81  $desc="(UserLogged,".$object->login.")";
82  $desc.=(empty($object->trigger_mesg)?'':' - '.$object->trigger_mesg);
83  }
84  if ($action == 'USER_LOGIN_FAILED')
85  {
86  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
87 
88  // Initialisation donnees (date,duree,texte,desc)
89  $text=$object->trigger_mesg; // Message direct
90  $desc=$object->trigger_mesg; // Message direct
91  }
92  if ($action == 'USER_LOGOUT')
93  {
94  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
95 
96  $langs->load("users");
97  // Initialisation donnees (date,duree,texte,desc)
98  $text="(UserLogoff,".$object->login.")";
99  $desc="(UserLogoff,".$object->login.")";
100  }
101  if ($action == 'USER_CREATE')
102  {
103  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
104  $langs->load("users");
105 
106  // Initialisation donnees (date,duree,texte,desc)
107  $text=$langs->transnoentities("NewUserCreated",$object->login);
108  $desc=$langs->transnoentities("NewUserCreated",$object->login);
109  }
110  elseif ($action == 'USER_MODIFY')
111  {
112  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
113  $langs->load("users");
114 
115  // Initialisation donnees (date,duree,texte,desc)
116  $text=$langs->transnoentities("EventUserModified",$object->login);
117  $desc=$langs->transnoentities("EventUserModified",$object->login);
118  }
119  elseif ($action == 'USER_NEW_PASSWORD')
120  {
121  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
122  $langs->load("users");
123 
124  // Initialisation donnees (date,duree,texte,desc)
125  $text=$langs->transnoentities("NewUserPassword",$object->login);
126  $desc=$langs->transnoentities("NewUserPassword",$object->login);
127  }
128  elseif ($action == 'USER_ENABLEDISABLE')
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  if ($object->statut == 0)
134  {
135  $text=$langs->transnoentities("UserEnabled",$object->login);
136  $desc=$langs->transnoentities("UserEnabled",$object->login);
137  }
138  if ($object->statut == 1)
139  {
140  $text=$langs->transnoentities("UserDisabled",$object->login);
141  $desc=$langs->transnoentities("UserDisabled",$object->login);
142  }
143  }
144  elseif ($action == 'USER_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("UserDeleted",$object->login);
150  $desc=$langs->transnoentities("UserDeleted",$object->login);
151  }
152 
153  // Groupes
154  elseif ($action == 'GROUP_CREATE')
155  {
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("NewGroupCreated",$object->name);
160  $desc=$langs->transnoentities("NewGroupCreated",$object->name);
161  }
162  elseif ($action == 'GROUP_MODIFY')
163  {
164  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
165  $langs->load("users");
166  // Initialisation donnees (date,duree,texte,desc)
167  $text=$langs->transnoentities("GroupModified",$object->name);
168  $desc=$langs->transnoentities("GroupModified",$object->name);
169  }
170  elseif ($action == 'GROUP_DELETE')
171  {
172  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
173  $langs->load("users");
174  // Initialisation donnees (date,duree,texte,desc)
175  $text=$langs->transnoentities("GroupDeleted",$object->name);
176  $desc=$langs->transnoentities("GroupDeleted",$object->name);
177  }
178 
179  // If not found
180 /*
181  else
182  {
183  dol_syslog("Trigger '".$this->name."' for action '$action' was ran by ".__FILE__." but no handler found for this action.");
184  return 0;
185  }
186 */
187 
188  // Add more information into desc from the context property
189  if (! empty($desc) && ! empty($object->context['audit'])) $desc.=' - '.$object->context['audit'];
190 
191  // Add entry in event table
192  include_once DOL_DOCUMENT_ROOT.'/core/class/events.class.php';
193 
194  $event=new Events($this->db);
195  $event->type=$action;
196  $event->dateevent=$date;
197  $event->label=$text;
198  $event->description=$desc;
199  $event->user_agent=$_SERVER["HTTP_USER_AGENT"];
200 
201  $result=$event->create($user);
202  if ($result > 0)
203  {
204  return 1;
205  }
206  else
207  {
208  $error ="Failed to insert security event: ".$event->error;
209  $this->error=$error;
210 
211  dol_syslog(get_class($this).": ".$this->error, LOG_ERR);
212  return -1;
213  }
214  }
215 }
transnoentities($key, $param1='', $param2='', $param3='', $param4='', $param5='')
Return translated value of a text string Si il n&#39;y a pas de correspondance pour ce texte...
Class to stock current configuration.
Definition: conf.class.php:33
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarrr business event is done.
Class to manage Dolibarr users.
Definition: user.class.php:41
Class of triggers for security events.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:103
load($domain, $alt=0, $stopafterdirection=0, $forcelangdir='', $loadfromfileonly=0)
Load translation key-value for a particular file, into a memory array.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage translations.
dol_now($mode='gmt')
Return date for now.
Class that all the triggers must extend.
Events class.