dolibarr  13.0.2
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)) $this->user_agent = (empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT']);
145 
146  // Check parameters
147  if (empty($this->description)) { $this->error = 'ErrorBadValueForParameterCreateEventDesc'; return -1; }
148 
149  // Insert request
150  $sql = "INSERT INTO ".MAIN_DB_PREFIX."events(";
151  $sql .= "type,";
152  $sql .= "entity,";
153  $sql .= "ip,";
154  $sql .= "user_agent,";
155  $sql .= "dateevent,";
156  $sql .= "fk_user,";
157  $sql .= "description,";
158  $sql .= "prefix_session";
159  $sql .= ") VALUES (";
160  $sql .= " '".$this->db->escape($this->type)."',";
161  $sql .= " ".$conf->entity.",";
162  $sql .= " '".$this->db->escape(getUserRemoteIP())."',";
163  $sql .= " ".($this->user_agent ? "'".$this->db->escape(dol_trunc($this->user_agent, 250))."'" : 'NULL').",";
164  $sql .= " '".$this->db->idate($this->dateevent)."',";
165  $sql .= " ".($user->id ? "'".$this->db->escape($user->id)."'" : 'NULL').",";
166  $sql .= " '".$this->db->escape(dol_trunc($this->description, 250))."',";
167  $sql .= " '".$this->db->escape(dol_getprefix())."'";
168  $sql .= ")";
169 
170  dol_syslog(get_class($this)."::create", LOG_DEBUG);
171  $resql = $this->db->query($sql);
172  if ($resql)
173  {
174  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."events");
175  return $this->id;
176  } else {
177  $this->error = "Error ".$this->db->lasterror();
178  return -1;
179  }
180  }
181 
182 
190  public function update($user = null, $notrigger = 0)
191  {
192  // Clean parameters
193  $this->id = (int) $this->id;
194  $this->type = trim($this->type);
195  $this->description = trim($this->description);
196 
197  // Check parameters
198  // Put here code to add control on parameters values
199 
200  // Update request
201  $sql = "UPDATE ".MAIN_DB_PREFIX."events SET";
202  $sql .= " type='".$this->db->escape($this->type)."',";
203  $sql .= " dateevent='".$this->db->idate($this->dateevent)."',";
204  $sql .= " description='".$this->db->escape($this->description)."'";
205  $sql .= " WHERE rowid=".$this->id;
206 
207  dol_syslog(get_class($this)."::update", LOG_DEBUG);
208  $resql = $this->db->query($sql);
209  if (!$resql)
210  {
211  $this->error = "Error ".$this->db->lasterror();
212  return -1;
213  }
214  return 1;
215  }
216 
217 
225  public function fetch($id, $user = null)
226  {
227  $sql = "SELECT";
228  $sql .= " t.rowid,";
229  $sql .= " t.tms,";
230  $sql .= " t.type,";
231  $sql .= " t.entity,";
232  $sql .= " t.dateevent,";
233  $sql .= " t.description,";
234  $sql .= " t.ip,";
235  $sql .= " t.user_agent,";
236  $sql .= " t.prefix_session";
237  $sql .= " FROM ".MAIN_DB_PREFIX."events as t";
238  $sql .= " WHERE t.rowid = ".$id;
239 
240  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
241  $resql = $this->db->query($sql);
242  if ($resql)
243  {
244  if ($this->db->num_rows($resql))
245  {
246  $obj = $this->db->fetch_object($resql);
247 
248  $this->id = $obj->rowid;
249  $this->tms = $this->db->jdate($obj->tms);
250  $this->type = $obj->type;
251  $this->entity = $obj->entity;
252  $this->dateevent = $this->db->jdate($obj->dateevent);
253  $this->description = $obj->description;
254  $this->ip = $obj->ip;
255  $this->user_agent = $obj->user_agent;
256  $this->prefix_session = $obj->prefix_session;
257  }
258  $this->db->free($resql);
259 
260  return 1;
261  } else {
262  $this->error = "Error ".$this->db->lasterror();
263  return -1;
264  }
265  }
266 
267 
274  public function delete($user)
275  {
276  $sql = "DELETE FROM ".MAIN_DB_PREFIX."events";
277  $sql .= " WHERE rowid=".$this->id;
278 
279  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
280  $resql = $this->db->query($sql);
281  if (!$resql)
282  {
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 }
getUserRemoteIP()
Return the IP of remote user.
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:750
fetch($id, $user=null)
Load object in memory from database.
initAsSpecimen()
Initialise an instance with random values.
$conf db
API class for accounts.
Definition: inc.php:54
__construct($db)
Constructor.
create($user)
Create in database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
print $_SERVER["PHP_SELF"]
Edit parameters.
update($user=null, $notrigger=0)
Update database.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_trunc($string, $size=40, $trunc= 'right', $stringencoding= 'UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
Events class.