dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
31class Events // extends CommonObject
32{
36 public $element = 'events';
37
41 public $table_element = 'events';
42
46 public $id;
47
51 public $db;
52
56 public $error = '';
57
61 public $tms;
62
66 public $type;
67
71 public $entity;
72
73 public $dateevent;
74
78 public $ip;
79
83 public $user_agent;
84
88 public $label;
89
93 public $description;
94
98 public $prefix_session;
99
103 public $authentication_method;
104
105
106 // List of all Audit/Security events supported by triggers
107 public $eventstolog = array(
108 array('id'=>'USER_LOGIN', 'test'=>1),
109 array('id'=>'USER_LOGIN_FAILED', 'test'=>1),
110 array('id'=>'USER_LOGOUT', 'test'=>1),
111 array('id'=>'USER_CREATE', 'test'=>1),
112 array('id'=>'USER_MODIFY', 'test'=>1),
113 array('id'=>'USER_NEW_PASSWORD', 'test'=>1),
114 array('id'=>'USER_ENABLEDISABLE', 'test'=>1),
115 array('id'=>'USER_DELETE', 'test'=>1),
116 array('id'=>'USERGROUP_CREATE', 'test'=>1),
117 array('id'=>'USERGROUP_MODIFY', 'test'=>1),
118 array('id'=>'USERGROUP_DELETE', 'test'=>1),
119 );
120
121
122 // BEGIN MODULEBUILDER PROPERTIES
126 public $fields = array(
127 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'noteditable'=>1, 'notnull'=> 1, 'index'=>1, 'position'=>1, 'comment'=>'Id'),
128 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=> 1, 'default'=>1, 'index'=>1, 'position'=>20),
129 'prefix_session'=>array('type'=>'varchar(255)', 'label'=>'PrefixSession', 'enabled'=>1, 'visible'=>-1, 'notnull'=>-1, 'index'=>0, 'position'=>1000),
130 'user_agent' =>array('type'=>'varchar(255)', 'label'=>'UserAgent', 'enabled'=>1, 'visible'=>-1, 'notnull'=> 1, 'default'=>0, 'index'=>1, 'position'=>1000),
131 );
132
133
139 public function __construct($db)
140 {
141 $this->db = $db;
142 }
143
144
151 public function create($user)
152 {
153 global $conf;
154
155 // Clean parameters
156 $this->description = trim($this->description);
157 if (empty($this->user_agent)) {
158 $this->user_agent = (empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT']);
159 }
160
161 // Check parameters
162 if (empty($this->description)) {
163 $this->error = 'ErrorBadValueForParameterCreateEventDesc';
164 return -1;
165 }
166
167 // Insert request
168 $sql = "INSERT INTO ".$this->db->prefix()."events(";
169 $sql .= "type,";
170 $sql .= "entity,";
171 $sql .= "ip,";
172 $sql .= "user_agent,";
173 $sql .= "dateevent,";
174 $sql .= "fk_user,";
175 $sql .= "description,";
176 $sql .= "prefix_session";
177 $sql .= ") VALUES (";
178 $sql .= " '".$this->db->escape($this->type)."',";
179 $sql .= " ".((int) $conf->entity).",";
180 $sql .= " '".$this->db->escape(getUserRemoteIP())."',";
181 $sql .= " ".($this->user_agent ? "'".$this->db->escape(dol_trunc($this->user_agent, 250))."'" : 'NULL').",";
182 $sql .= " '".$this->db->idate($this->dateevent)."',";
183 $sql .= " ".($user->id > 0 ? ((int) $user->id) : 'NULL').",";
184 $sql .= " '".$this->db->escape(dol_trunc($this->description, 250))."',";
185 $sql .= " '".$this->db->escape(dol_getprefix())."'";
186 $sql .= ")";
187
188 dol_syslog(get_class($this)."::create", LOG_DEBUG);
189 $resql = $this->db->query($sql);
190 if ($resql) {
191 $this->id = $this->db->last_insert_id($this->db->prefix()."events");
192 return $this->id;
193 } else {
194 $this->error = "Error ".$this->db->lasterror();
195 return -1;
196 }
197 }
198
199
207 public function update($user = null, $notrigger = 0)
208 {
209 // Clean parameters
210 $this->id = (int) $this->id;
211 $this->type = trim($this->type);
212 $this->description = trim($this->description);
213
214 // Check parameters
215 // Put here code to add control on parameters values
216
217 // Update request
218 $sql = "UPDATE ".$this->db->prefix()."events SET";
219 $sql .= " type='".$this->db->escape($this->type)."',";
220 $sql .= " dateevent='".$this->db->idate($this->dateevent)."',";
221 $sql .= " description='".$this->db->escape($this->description)."'";
222 $sql .= " WHERE rowid=".((int) $this->id);
223
224 dol_syslog(get_class($this)."::update", LOG_DEBUG);
225 $resql = $this->db->query($sql);
226 if (!$resql) {
227 $this->error = "Error ".$this->db->lasterror();
228 return -1;
229 }
230 return 1;
231 }
232
233
241 public function fetch($id, $user = null)
242 {
243 $sql = "SELECT";
244 $sql .= " t.rowid,";
245 $sql .= " t.tms,";
246 $sql .= " t.type,";
247 $sql .= " t.entity,";
248 $sql .= " t.dateevent,";
249 $sql .= " t.description,";
250 $sql .= " t.ip,";
251 $sql .= " t.user_agent,";
252 $sql .= " t.prefix_session";
253 $sql .= " FROM ".$this->db->prefix()."events as t";
254 $sql .= " WHERE t.rowid = ".((int) $id);
255
256 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
257 $resql = $this->db->query($sql);
258 if ($resql) {
259 if ($this->db->num_rows($resql)) {
260 $obj = $this->db->fetch_object($resql);
261
262 $this->id = $obj->rowid;
263 $this->tms = $this->db->jdate($obj->tms);
264 $this->type = $obj->type;
265 $this->entity = $obj->entity;
266 $this->dateevent = $this->db->jdate($obj->dateevent);
267 $this->description = $obj->description;
268 $this->ip = $obj->ip;
269 $this->user_agent = $obj->user_agent;
270 $this->prefix_session = $obj->prefix_session;
271 }
272 $this->db->free($resql);
273
274 return 1;
275 } else {
276 $this->error = "Error ".$this->db->lasterror();
277 return -1;
278 }
279 }
280
281
288 public function delete($user)
289 {
290 $sql = "DELETE FROM ".$this->db->prefix()."events";
291 $sql .= " WHERE rowid=".((int) $this->id);
292
293 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
294 $resql = $this->db->query($sql);
295 if (!$resql) {
296 $this->error = "Error ".$this->db->lasterror();
297 return -1;
298 }
299
300 return 1;
301 }
302
303
311 public function initAsSpecimen()
312 {
313 $this->id = 0;
314
315 $this->tms = time();
316 $this->type = '';
317 $this->dateevent = time();
318 $this->description = 'This is a specimen event';
319 $this->ip = '1.2.3.4';
320 $this->user_agent = 'Mozilla specimen User Agent X.Y';
321 $this->prefix_session = dol_getprefix();
322
323 return 1;
324 }
325}
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:139