dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
32class Events // extends CommonObject
33{
37 public $element = 'events';
38
42 public $table_element = 'events';
43
47 public $id;
48
52 public $db;
53
57 public $error = '';
58
62 public $tms;
63
67 public $type;
68
72 public $entity;
73
77 public $dateevent;
78
82 public $ip;
83
87 public $user_agent;
88
92 public $label;
93
97 public $description;
98
102 public $prefix_session;
103
107 public $authentication_method;
108
109
113 public $eventstolog = array(
114 array('id' => 'USER_LOGIN', 'test' => 1),
115 array('id' => 'USER_LOGIN_FAILED', 'test' => 1),
116 array('id' => 'USER_LOGOUT', 'test' => 1),
117 array('id' => 'USER_CREATE', 'test' => 1),
118 array('id' => 'USER_MODIFY', 'test' => 1),
119 array('id' => 'USER_NEW_PASSWORD', 'test' => 1),
120 array('id' => 'USER_ENABLEDISABLE', 'test' => 1),
121 array('id' => 'USER_DELETE', 'test' => 1),
122 array('id' => 'USERGROUP_CREATE', 'test' => 1),
123 array('id' => 'USERGROUP_MODIFY', 'test' => 1),
124 array('id' => 'USERGROUP_DELETE', 'test' => 1),
125 );
126
127
128 // BEGIN MODULEBUILDER PROPERTIES
132 public $fields = array(
133 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -2, 'noteditable' => 1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id'),
134 'entity' => array('type' => 'integer', 'label' => 'Entity', 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'default' => '1', 'index' => 1, 'position' => 20),
135 'prefix_session' => array('type' => 'varchar(255)', 'label' => 'PrefixSession', 'enabled' => 1, 'visible' => -1, 'notnull' => -1, 'index' => 0, 'position' => 1000),
136 'user_agent' => array('type' => 'varchar(255)', 'label' => 'UserAgent', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'default' => '0', 'index' => 1, 'position' => 1000),
137 );
138
139
145 public function __construct($db)
146 {
147 $this->db = $db;
148 }
149
150
157 public function create($user)
158 {
159 global $conf;
160
161 // Clean parameters
162 $this->description = trim($this->description);
163 if (empty($this->user_agent)) {
164 $this->user_agent = (empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT']);
165 }
166
167 // Check parameters
168 if (empty($this->description)) {
169 $this->error = 'ErrorBadValueForParameterCreateEventDesc';
170 return -1;
171 }
172
173 // Insert request
174 $sql = "INSERT INTO ".$this->db->prefix()."events(";
175 $sql .= "type,";
176 $sql .= "entity,";
177 $sql .= "ip,";
178 $sql .= "user_agent,";
179 $sql .= "dateevent,";
180 $sql .= "fk_user,";
181 $sql .= "description,";
182 $sql .= "prefix_session";
183 $sql .= ") VALUES (";
184 $sql .= " '".$this->db->escape($this->type)."',";
185 $sql .= " ".((int) $conf->entity).",";
186 $sql .= " '".$this->db->escape(getUserRemoteIP())."',";
187 $sql .= " ".($this->user_agent ? "'".$this->db->escape(dol_trunc($this->user_agent, 250))."'" : 'NULL').",";
188 $sql .= " '".$this->db->idate($this->dateevent)."',";
189 $sql .= " ".($user->id > 0 ? ((int) $user->id) : 'NULL').",";
190 $sql .= " '".$this->db->escape(dol_trunc($this->description, 250))."',";
191 $sql .= " '".$this->db->escape(dol_getprefix())."'";
192 $sql .= ")";
193
194 dol_syslog(get_class($this)."::create", LOG_DEBUG);
195 $resql = $this->db->query($sql);
196 if ($resql) {
197 $this->id = $this->db->last_insert_id($this->db->prefix()."events");
198 return $this->id;
199 } else {
200 $this->error = "Error ".$this->db->lasterror();
201 return -1;
202 }
203 }
204
205
213 public function update($user = null, $notrigger = 0)
214 {
215 // Clean parameters
216 $this->id = (int) $this->id;
217 $this->type = trim($this->type);
218 $this->description = trim($this->description);
219
220 // Check parameters
221 // Put here code to add control on parameters values
222
223 // Update request
224 $sql = "UPDATE ".$this->db->prefix()."events SET";
225 $sql .= " type='".$this->db->escape($this->type)."',";
226 $sql .= " dateevent='".$this->db->idate($this->dateevent)."',";
227 $sql .= " description='".$this->db->escape($this->description)."'";
228 $sql .= " WHERE rowid=".((int) $this->id);
229
230 dol_syslog(get_class($this)."::update", LOG_DEBUG);
231 $resql = $this->db->query($sql);
232 if (!$resql) {
233 $this->error = "Error ".$this->db->lasterror();
234 return -1;
235 }
236 return 1;
237 }
238
239
247 public function fetch($id, $user = null)
248 {
249 $sql = "SELECT";
250 $sql .= " t.rowid,";
251 $sql .= " t.tms,";
252 $sql .= " t.type,";
253 $sql .= " t.entity,";
254 $sql .= " t.dateevent,";
255 $sql .= " t.description,";
256 $sql .= " t.ip,";
257 $sql .= " t.user_agent,";
258 $sql .= " t.prefix_session";
259 $sql .= " FROM ".$this->db->prefix()."events as t";
260 $sql .= " WHERE t.rowid = ".((int) $id);
261
262 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
263 $resql = $this->db->query($sql);
264 if ($resql) {
265 if ($this->db->num_rows($resql)) {
266 $obj = $this->db->fetch_object($resql);
267
268 $this->id = $obj->rowid;
269 $this->tms = $this->db->jdate($obj->tms);
270 $this->type = $obj->type;
271 $this->entity = $obj->entity;
272 $this->dateevent = $this->db->jdate($obj->dateevent);
273 $this->description = $obj->description;
274 $this->ip = $obj->ip;
275 $this->user_agent = $obj->user_agent;
276 $this->prefix_session = $obj->prefix_session;
277 }
278 $this->db->free($resql);
279
280 return 1;
281 } else {
282 $this->error = "Error ".$this->db->lasterror();
283 return -1;
284 }
285 }
286
287
294 public function delete($user)
295 {
296 $sql = "DELETE FROM ".$this->db->prefix()."events";
297 $sql .= " WHERE rowid=".((int) $this->id);
298
299 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
300 $resql = $this->db->query($sql);
301 if (!$resql) {
302 $this->error = "Error ".$this->db->lasterror();
303 return -1;
304 }
305
306 return 1;
307 }
308
309
317 public function initAsSpecimen()
318 {
319 $this->id = 0;
320
321 $this->tms = time();
322 $this->type = '';
323 $this->dateevent = time();
324 $this->description = 'This is a specimen event';
325 $this->ip = '1.2.3.4';
326 $this->user_agent = 'Mozilla specimen User Agent X.Y';
327 $this->prefix_session = dol_getprefix();
328
329 return 1;
330 }
331}
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:149