dolibarr  17.0.4
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 
93  // List of all Audit/Security events supported by triggers
94  public $eventstolog = array(
95  array('id'=>'USER_LOGIN', 'test'=>1),
96  array('id'=>'USER_LOGIN_FAILED', 'test'=>1),
97  array('id'=>'USER_LOGOUT', 'test'=>1),
98  array('id'=>'USER_CREATE', 'test'=>1),
99  array('id'=>'USER_MODIFY', 'test'=>1),
100  array('id'=>'USER_NEW_PASSWORD', 'test'=>1),
101  array('id'=>'USER_ENABLEDISABLE', 'test'=>1),
102  array('id'=>'USER_DELETE', 'test'=>1),
103  array('id'=>'USERGROUP_CREATE', 'test'=>1),
104  array('id'=>'USERGROUP_MODIFY', 'test'=>1),
105  array('id'=>'USERGROUP_DELETE', 'test'=>1),
106  );
107 
108 
109  // BEGIN MODULEBUILDER PROPERTIES
113  public $fields = array(
114  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'noteditable'=>1, 'notnull'=> 1, 'index'=>1, 'position'=>1, 'comment'=>'Id'),
115  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=> 1, 'default'=>1, 'index'=>1, 'position'=>20),
116  'prefix_session'=>array('type'=>'varchar(255)', 'label'=>'PrefixSession', 'enabled'=>1, 'visible'=>-1, 'notnull'=>-1, 'index'=>0, 'position'=>1000),
117  'user_agent' =>array('type'=>'varchar(255)', 'label'=>'UserAgent', 'enabled'=>1, 'visible'=>-1, 'notnull'=> 1, 'default'=>0, 'index'=>1, 'position'=>1000),
118  );
119 
120 
126  public function __construct($db)
127  {
128  $this->db = $db;
129  }
130 
131 
138  public function create($user)
139  {
140  global $conf;
141 
142  // Clean parameters
143  $this->description = trim($this->description);
144  if (empty($this->user_agent)) {
145  $this->user_agent = (empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT']);
146  }
147 
148  // Check parameters
149  if (empty($this->description)) {
150  $this->error = 'ErrorBadValueForParameterCreateEventDesc';
151  return -1;
152  }
153 
154  // Insert request
155  $sql = "INSERT INTO ".$this->db->prefix()."events(";
156  $sql .= "type,";
157  $sql .= "entity,";
158  $sql .= "ip,";
159  $sql .= "user_agent,";
160  $sql .= "dateevent,";
161  $sql .= "fk_user,";
162  $sql .= "description,";
163  $sql .= "prefix_session";
164  $sql .= ") VALUES (";
165  $sql .= " '".$this->db->escape($this->type)."',";
166  $sql .= " ".((int) $conf->entity).",";
167  $sql .= " '".$this->db->escape(getUserRemoteIP())."',";
168  $sql .= " ".($this->user_agent ? "'".$this->db->escape(dol_trunc($this->user_agent, 250))."'" : 'NULL').",";
169  $sql .= " '".$this->db->idate($this->dateevent)."',";
170  $sql .= " ".($user->id > 0 ? ((int) $user->id) : 'NULL').",";
171  $sql .= " '".$this->db->escape(dol_trunc($this->description, 250))."',";
172  $sql .= " '".$this->db->escape(dol_getprefix())."'";
173  $sql .= ")";
174 
175  dol_syslog(get_class($this)."::create", LOG_DEBUG);
176  $resql = $this->db->query($sql);
177  if ($resql) {
178  $this->id = $this->db->last_insert_id($this->db->prefix()."events");
179  return $this->id;
180  } else {
181  $this->error = "Error ".$this->db->lasterror();
182  return -1;
183  }
184  }
185 
186 
194  public function update($user = null, $notrigger = 0)
195  {
196  // Clean parameters
197  $this->id = (int) $this->id;
198  $this->type = trim($this->type);
199  $this->description = trim($this->description);
200 
201  // Check parameters
202  // Put here code to add control on parameters values
203 
204  // Update request
205  $sql = "UPDATE ".$this->db->prefix()."events SET";
206  $sql .= " type='".$this->db->escape($this->type)."',";
207  $sql .= " dateevent='".$this->db->idate($this->dateevent)."',";
208  $sql .= " description='".$this->db->escape($this->description)."'";
209  $sql .= " WHERE rowid=".((int) $this->id);
210 
211  dol_syslog(get_class($this)."::update", LOG_DEBUG);
212  $resql = $this->db->query($sql);
213  if (!$resql) {
214  $this->error = "Error ".$this->db->lasterror();
215  return -1;
216  }
217  return 1;
218  }
219 
220 
228  public function fetch($id, $user = null)
229  {
230  $sql = "SELECT";
231  $sql .= " t.rowid,";
232  $sql .= " t.tms,";
233  $sql .= " t.type,";
234  $sql .= " t.entity,";
235  $sql .= " t.dateevent,";
236  $sql .= " t.description,";
237  $sql .= " t.ip,";
238  $sql .= " t.user_agent,";
239  $sql .= " t.prefix_session";
240  $sql .= " FROM ".$this->db->prefix()."events as t";
241  $sql .= " WHERE t.rowid = ".((int) $id);
242 
243  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
244  $resql = $this->db->query($sql);
245  if ($resql) {
246  if ($this->db->num_rows($resql)) {
247  $obj = $this->db->fetch_object($resql);
248 
249  $this->id = $obj->rowid;
250  $this->tms = $this->db->jdate($obj->tms);
251  $this->type = $obj->type;
252  $this->entity = $obj->entity;
253  $this->dateevent = $this->db->jdate($obj->dateevent);
254  $this->description = $obj->description;
255  $this->ip = $obj->ip;
256  $this->user_agent = $obj->user_agent;
257  $this->prefix_session = $obj->prefix_session;
258  }
259  $this->db->free($resql);
260 
261  return 1;
262  } else {
263  $this->error = "Error ".$this->db->lasterror();
264  return -1;
265  }
266  }
267 
268 
275  public function delete($user)
276  {
277  $sql = "DELETE FROM ".$this->db->prefix()."events";
278  $sql .= " WHERE rowid=".((int) $this->id);
279 
280  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
281  $resql = $this->db->query($sql);
282  if (!$resql) {
283  $this->error = "Error ".$this->db->lasterror();
284  return -1;
285  }
286 
287  return 1;
288  }
289 
290 
298  public function initAsSpecimen()
299  {
300  $this->id = 0;
301 
302  $this->tms = time();
303  $this->type = '';
304  $this->dateevent = time();
305  $this->description = 'This is a specimen event';
306  $this->ip = '1.2.3.4';
307  $this->user_agent = 'Mozilla specimen User Agent X.Y';
308  $this->prefix_session = dol_getprefix();
309  }
310 }
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') &&!empty($user->rights->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') &&!empty($user->rights->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)) $resql
Social contributions to pay.
Definition: index.php:745
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:119
$conf db
API class for accounts.
Definition: inc.php:41