dolibarr 19.0.3
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 * Copyright (C) 2023 William Mead <william.mead@manchenumerique.fr>
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
30class Events // extends CommonObject
31{
35 public $element = 'events';
36
40 public $table_element = 'events';
41
45 public $id;
46
50 public $db;
51
55 public $error = '';
56
60 public $tms;
61
65 public $type;
66
70 public $entity;
71
72 public $dateevent;
73
77 public $ip;
78
82 public $user_agent;
83
87 public $label;
88
92 public $description;
93
97 public $prefix_session;
98
102 public $authentication_method;
103
104
105 // List of all Audit/Security events supported by triggers
106 public $eventstolog = array(
107 array('id'=>'USER_LOGIN', 'test'=>1),
108 array('id'=>'USER_LOGIN_FAILED', 'test'=>1),
109 array('id'=>'USER_LOGOUT', 'test'=>1),
110 array('id'=>'USER_CREATE', 'test'=>1),
111 array('id'=>'USER_MODIFY', 'test'=>1),
112 array('id'=>'USER_NEW_PASSWORD', 'test'=>1),
113 array('id'=>'USER_ENABLEDISABLE', 'test'=>1),
114 array('id'=>'USER_DELETE', 'test'=>1),
115 array('id'=>'USERGROUP_CREATE', 'test'=>1),
116 array('id'=>'USERGROUP_MODIFY', 'test'=>1),
117 array('id'=>'USERGROUP_DELETE', 'test'=>1),
118 );
119
120
121 // BEGIN MODULEBUILDER PROPERTIES
125 public $fields = array(
126 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'noteditable'=>1, 'notnull'=> 1, 'index'=>1, 'position'=>1, 'comment'=>'Id'),
127 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=> 1, 'default'=>1, 'index'=>1, 'position'=>20),
128 'prefix_session'=>array('type'=>'varchar(255)', 'label'=>'PrefixSession', 'enabled'=>1, 'visible'=>-1, 'notnull'=>-1, 'index'=>0, 'position'=>1000),
129 'user_agent' =>array('type'=>'varchar(255)', 'label'=>'UserAgent', 'enabled'=>1, 'visible'=>-1, 'notnull'=> 1, 'default'=>0, 'index'=>1, 'position'=>1000),
130 );
131
132
138 public function __construct($db)
139 {
140 $this->db = $db;
141 }
142
143
150 public function create($user)
151 {
152 global $conf;
153
154 // Clean parameters
155 $this->description = trim($this->description);
156 if (empty($this->user_agent)) {
157 $this->user_agent = (empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT']);
158 }
159
160 // Check parameters
161 if (empty($this->description)) {
162 $this->error = 'ErrorBadValueForParameterCreateEventDesc';
163 return -1;
164 }
165
166 // Insert request
167 $sql = "INSERT INTO ".$this->db->prefix()."events(";
168 $sql .= "type,";
169 $sql .= "entity,";
170 $sql .= "ip,";
171 $sql .= "user_agent,";
172 $sql .= "dateevent,";
173 $sql .= "fk_user,";
174 $sql .= "description,";
175 $sql .= "prefix_session";
176 $sql .= ") VALUES (";
177 $sql .= " '".$this->db->escape($this->type)."',";
178 $sql .= " ".((int) $conf->entity).",";
179 $sql .= " '".$this->db->escape(getUserRemoteIP())."',";
180 $sql .= " ".($this->user_agent ? "'".$this->db->escape(dol_trunc($this->user_agent, 250))."'" : 'NULL').",";
181 $sql .= " '".$this->db->idate($this->dateevent)."',";
182 $sql .= " ".($user->id > 0 ? ((int) $user->id) : 'NULL').",";
183 $sql .= " '".$this->db->escape(dol_trunc($this->description, 250))."',";
184 $sql .= " '".$this->db->escape(dol_getprefix())."'";
185 $sql .= ")";
186
187 dol_syslog(get_class($this)."::create", LOG_DEBUG);
188 $resql = $this->db->query($sql);
189 if ($resql) {
190 $this->id = $this->db->last_insert_id($this->db->prefix()."events");
191 return $this->id;
192 } else {
193 $this->error = "Error ".$this->db->lasterror();
194 return -1;
195 }
196 }
197
198
206 public function update($user = null, $notrigger = 0)
207 {
208 // Clean parameters
209 $this->id = (int) $this->id;
210 $this->type = trim($this->type);
211 $this->description = trim($this->description);
212
213 // Check parameters
214 // Put here code to add control on parameters values
215
216 // Update request
217 $sql = "UPDATE ".$this->db->prefix()."events SET";
218 $sql .= " type='".$this->db->escape($this->type)."',";
219 $sql .= " dateevent='".$this->db->idate($this->dateevent)."',";
220 $sql .= " description='".$this->db->escape($this->description)."'";
221 $sql .= " WHERE rowid=".((int) $this->id);
222
223 dol_syslog(get_class($this)."::update", LOG_DEBUG);
224 $resql = $this->db->query($sql);
225 if (!$resql) {
226 $this->error = "Error ".$this->db->lasterror();
227 return -1;
228 }
229 return 1;
230 }
231
232
240 public function fetch($id, $user = null)
241 {
242 $sql = "SELECT";
243 $sql .= " t.rowid,";
244 $sql .= " t.tms,";
245 $sql .= " t.type,";
246 $sql .= " t.entity,";
247 $sql .= " t.dateevent,";
248 $sql .= " t.description,";
249 $sql .= " t.ip,";
250 $sql .= " t.user_agent,";
251 $sql .= " t.prefix_session";
252 $sql .= " FROM ".$this->db->prefix()."events as t";
253 $sql .= " WHERE t.rowid = ".((int) $id);
254
255 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
256 $resql = $this->db->query($sql);
257 if ($resql) {
258 if ($this->db->num_rows($resql)) {
259 $obj = $this->db->fetch_object($resql);
260
261 $this->id = $obj->rowid;
262 $this->tms = $this->db->jdate($obj->tms);
263 $this->type = $obj->type;
264 $this->entity = $obj->entity;
265 $this->dateevent = $this->db->jdate($obj->dateevent);
266 $this->description = $obj->description;
267 $this->ip = $obj->ip;
268 $this->user_agent = $obj->user_agent;
269 $this->prefix_session = $obj->prefix_session;
270 }
271 $this->db->free($resql);
272
273 return 1;
274 } else {
275 $this->error = "Error ".$this->db->lasterror();
276 return -1;
277 }
278 }
279
280
287 public function delete($user)
288 {
289 $sql = "DELETE FROM ".$this->db->prefix()."events";
290 $sql .= " WHERE rowid=".((int) $this->id);
291
292 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
293 $resql = $this->db->query($sql);
294 if (!$resql) {
295 $this->error = "Error ".$this->db->lasterror();
296 return -1;
297 }
298
299 return 1;
300 }
301
302
310 public function initAsSpecimen()
311 {
312 $this->id = 0;
313
314 $this->tms = time();
315 $this->type = '';
316 $this->dateevent = time();
317 $this->description = 'This is a specimen event';
318 $this->ip = '1.2.3.4';
319 $this->user_agent = 'Mozilla specimen User Agent X.Y';
320 $this->prefix_session = dol_getprefix();
321 }
322}
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.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after 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:121