dolibarr  18.0.6
events.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2019 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
29 class Events // extends CommonObject
30 {
34  public $element = 'events';
35 
39  public $table_element = 'events';
40 
44  public $id;
45 
49  public $db;
50 
54  public $error = '';
55 
59  public $tms;
60 
64  public $type;
65 
69  public $entity;
70 
71  public $dateevent;
72 
76  public $ip;
77 
81  public $user_agent;
82 
86  public $description;
87 
91  public $prefix_session;
92 
96  public $authentication_method;
97 
98 
99  // List of all Audit/Security events supported by triggers
100  public $eventstolog = array(
101  array('id'=>'USER_LOGIN', 'test'=>1),
102  array('id'=>'USER_LOGIN_FAILED', 'test'=>1),
103  array('id'=>'USER_LOGOUT', 'test'=>1),
104  array('id'=>'USER_CREATE', 'test'=>1),
105  array('id'=>'USER_MODIFY', 'test'=>1),
106  array('id'=>'USER_NEW_PASSWORD', 'test'=>1),
107  array('id'=>'USER_ENABLEDISABLE', 'test'=>1),
108  array('id'=>'USER_DELETE', 'test'=>1),
109  array('id'=>'USERGROUP_CREATE', 'test'=>1),
110  array('id'=>'USERGROUP_MODIFY', 'test'=>1),
111  array('id'=>'USERGROUP_DELETE', 'test'=>1),
112  );
113 
114 
115  // BEGIN MODULEBUILDER PROPERTIES
119  public $fields = array(
120  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'noteditable'=>1, 'notnull'=> 1, 'index'=>1, 'position'=>1, 'comment'=>'Id'),
121  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=> 1, 'default'=>1, 'index'=>1, 'position'=>20),
122  'prefix_session'=>array('type'=>'varchar(255)', 'label'=>'PrefixSession', 'enabled'=>1, 'visible'=>-1, 'notnull'=>-1, 'index'=>0, 'position'=>1000),
123  'user_agent' =>array('type'=>'varchar(255)', 'label'=>'UserAgent', 'enabled'=>1, 'visible'=>-1, 'notnull'=> 1, 'default'=>0, 'index'=>1, 'position'=>1000),
124  );
125 
126 
132  public function __construct($db)
133  {
134  $this->db = $db;
135  }
136 
137 
144  public function create($user)
145  {
146  global $conf;
147 
148  // Clean parameters
149  $this->description = trim($this->description);
150  if (empty($this->user_agent)) {
151  $this->user_agent = (empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT']);
152  }
153 
154  // Check parameters
155  if (empty($this->description)) {
156  $this->error = 'ErrorBadValueForParameterCreateEventDesc';
157  return -1;
158  }
159 
160  // Insert request
161  $sql = "INSERT INTO ".$this->db->prefix()."events(";
162  $sql .= "type,";
163  $sql .= "entity,";
164  $sql .= "ip,";
165  $sql .= "user_agent,";
166  $sql .= "dateevent,";
167  $sql .= "fk_user,";
168  $sql .= "description,";
169  $sql .= "prefix_session";
170  $sql .= ") VALUES (";
171  $sql .= " '".$this->db->escape($this->type)."',";
172  $sql .= " ".((int) $conf->entity).",";
173  $sql .= " '".$this->db->escape(getUserRemoteIP())."',";
174  $sql .= " ".($this->user_agent ? "'".$this->db->escape(dol_trunc($this->user_agent, 250))."'" : 'NULL').",";
175  $sql .= " '".$this->db->idate($this->dateevent)."',";
176  $sql .= " ".($user->id > 0 ? ((int) $user->id) : 'NULL').",";
177  $sql .= " '".$this->db->escape(dol_trunc($this->description, 250))."',";
178  $sql .= " '".$this->db->escape(dol_getprefix())."'";
179  $sql .= ")";
180 
181  dol_syslog(get_class($this)."::create", LOG_DEBUG);
182  $resql = $this->db->query($sql);
183  if ($resql) {
184  $this->id = $this->db->last_insert_id($this->db->prefix()."events");
185  return $this->id;
186  } else {
187  $this->error = "Error ".$this->db->lasterror();
188  return -1;
189  }
190  }
191 
192 
200  public function update($user = null, $notrigger = 0)
201  {
202  // Clean parameters
203  $this->id = (int) $this->id;
204  $this->type = trim($this->type);
205  $this->description = trim($this->description);
206 
207  // Check parameters
208  // Put here code to add control on parameters values
209 
210  // Update request
211  $sql = "UPDATE ".$this->db->prefix()."events SET";
212  $sql .= " type='".$this->db->escape($this->type)."',";
213  $sql .= " dateevent='".$this->db->idate($this->dateevent)."',";
214  $sql .= " description='".$this->db->escape($this->description)."'";
215  $sql .= " WHERE rowid=".((int) $this->id);
216 
217  dol_syslog(get_class($this)."::update", LOG_DEBUG);
218  $resql = $this->db->query($sql);
219  if (!$resql) {
220  $this->error = "Error ".$this->db->lasterror();
221  return -1;
222  }
223  return 1;
224  }
225 
226 
234  public function fetch($id, $user = null)
235  {
236  $sql = "SELECT";
237  $sql .= " t.rowid,";
238  $sql .= " t.tms,";
239  $sql .= " t.type,";
240  $sql .= " t.entity,";
241  $sql .= " t.dateevent,";
242  $sql .= " t.description,";
243  $sql .= " t.ip,";
244  $sql .= " t.user_agent,";
245  $sql .= " t.prefix_session";
246  $sql .= " FROM ".$this->db->prefix()."events as t";
247  $sql .= " WHERE t.rowid = ".((int) $id);
248 
249  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
250  $resql = $this->db->query($sql);
251  if ($resql) {
252  if ($this->db->num_rows($resql)) {
253  $obj = $this->db->fetch_object($resql);
254 
255  $this->id = $obj->rowid;
256  $this->tms = $this->db->jdate($obj->tms);
257  $this->type = $obj->type;
258  $this->entity = $obj->entity;
259  $this->dateevent = $this->db->jdate($obj->dateevent);
260  $this->description = $obj->description;
261  $this->ip = $obj->ip;
262  $this->user_agent = $obj->user_agent;
263  $this->prefix_session = $obj->prefix_session;
264  }
265  $this->db->free($resql);
266 
267  return 1;
268  } else {
269  $this->error = "Error ".$this->db->lasterror();
270  return -1;
271  }
272  }
273 
274 
281  public function delete($user)
282  {
283  $sql = "DELETE FROM ".$this->db->prefix()."events";
284  $sql .= " WHERE rowid=".((int) $this->id);
285 
286  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
287  $resql = $this->db->query($sql);
288  if (!$resql) {
289  $this->error = "Error ".$this->db->lasterror();
290  return -1;
291  }
292 
293  return 1;
294  }
295 
296 
304  public function initAsSpecimen()
305  {
306  $this->id = 0;
307 
308  $this->tms = time();
309  $this->type = '';
310  $this->dateevent = time();
311  $this->description = 'This is a specimen event';
312  $this->ip = '1.2.3.4';
313  $this->user_agent = 'Mozilla specimen User Agent X.Y';
314  $this->prefix_session = dol_getprefix();
315  }
316 }
Events class.
initAsSpecimen()
Initialise an instance with random values.
__construct($db)
Constructor.
create($user)
Create in database.
fetch($id, $user=null)
Load object in memory from database.
update($user=null, $notrigger=0)
Update database.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getUserRemoteIP()
Return the IP of remote user.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:120