events.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. /**
  19. * \file htdocs/core/class/events.class.php
  20. * \ingroup core
  21. * \brief File of class to manage security events.
  22. */
  23. /**
  24. * Events class
  25. */
  26. class Events // extends CommonObject
  27. {
  28. /**
  29. * @var string ID to identify managed object
  30. */
  31. public $element = 'events';
  32. /**
  33. * @var string Name of table without prefix where object is stored
  34. */
  35. public $table_element = 'events';
  36. /**
  37. * @var int ID
  38. */
  39. public $id;
  40. /**
  41. * @var DoliDB Database handler.
  42. */
  43. public $db;
  44. /**
  45. * @var string Error code (or message)
  46. */
  47. public $error = '';
  48. /**
  49. * @var int timestamp
  50. */
  51. public $tms;
  52. /**
  53. * @var string Type
  54. */
  55. public $type;
  56. /**
  57. * @var int Entity
  58. */
  59. public $entity;
  60. public $dateevent;
  61. /**
  62. * @var string IP
  63. */
  64. public $ip;
  65. /**
  66. * @var string User agent
  67. */
  68. public $user_agent;
  69. /**
  70. * @var string description
  71. */
  72. public $description;
  73. /**
  74. * @var string Prefix session obtained with method dol_getprefix()
  75. */
  76. public $prefix_session;
  77. // List of all Audit/Security events supported by triggers
  78. public $eventstolog = array(
  79. array('id'=>'USER_LOGIN', 'test'=>1),
  80. array('id'=>'USER_LOGIN_FAILED', 'test'=>1),
  81. array('id'=>'USER_LOGOUT', 'test'=>1),
  82. array('id'=>'USER_CREATE', 'test'=>1),
  83. array('id'=>'USER_MODIFY', 'test'=>1),
  84. array('id'=>'USER_NEW_PASSWORD', 'test'=>1),
  85. array('id'=>'USER_ENABLEDISABLE', 'test'=>1),
  86. array('id'=>'USER_DELETE', 'test'=>1),
  87. array('id'=>'USERGROUP_CREATE', 'test'=>1),
  88. array('id'=>'USERGROUP_MODIFY', 'test'=>1),
  89. array('id'=>'USERGROUP_DELETE', 'test'=>1),
  90. );
  91. // BEGIN MODULEBUILDER PROPERTIES
  92. /**
  93. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  94. */
  95. public $fields = array(
  96. 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'noteditable'=>1, 'notnull'=> 1, 'index'=>1, 'position'=>1, 'comment'=>'Id'),
  97. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=> 1, 'default'=>1, 'index'=>1, 'position'=>20),
  98. 'prefix_session'=>array('type'=>'varchar(255)', 'label'=>'PrefixSession', 'enabled'=>1, 'visible'=>-1, 'notnull'=>-1, 'index'=>0, 'position'=>1000),
  99. 'user_agent' =>array('type'=>'varchar(255)', 'label'=>'UserAgent', 'enabled'=>1, 'visible'=>-1, 'notnull'=> 1, 'default'=>0, 'index'=>1, 'position'=>1000),
  100. );
  101. /**
  102. * Constructor
  103. *
  104. * @param DoliDB $db Database handler
  105. */
  106. public function __construct($db)
  107. {
  108. $this->db = $db;
  109. }
  110. /**
  111. * Create in database
  112. *
  113. * @param User $user User that create
  114. * @return int <0 if KO, >0 if OK
  115. */
  116. public function create($user)
  117. {
  118. global $conf;
  119. // Clean parameters
  120. $this->description = trim($this->description);
  121. if (empty($this->user_agent)) {
  122. $this->user_agent = (empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT']);
  123. }
  124. // Check parameters
  125. if (empty($this->description)) {
  126. $this->error = 'ErrorBadValueForParameterCreateEventDesc';
  127. return -1;
  128. }
  129. // Insert request
  130. $sql = "INSERT INTO ".$this->db->prefix()."events(";
  131. $sql .= "type,";
  132. $sql .= "entity,";
  133. $sql .= "ip,";
  134. $sql .= "user_agent,";
  135. $sql .= "dateevent,";
  136. $sql .= "fk_user,";
  137. $sql .= "description,";
  138. $sql .= "prefix_session";
  139. $sql .= ") VALUES (";
  140. $sql .= " '".$this->db->escape($this->type)."',";
  141. $sql .= " ".((int) $conf->entity).",";
  142. $sql .= " '".$this->db->escape(getUserRemoteIP())."',";
  143. $sql .= " ".($this->user_agent ? "'".$this->db->escape(dol_trunc($this->user_agent, 250))."'" : 'NULL').",";
  144. $sql .= " '".$this->db->idate($this->dateevent)."',";
  145. $sql .= " ".($user->id > 0 ? ((int) $user->id) : 'NULL').",";
  146. $sql .= " '".$this->db->escape(dol_trunc($this->description, 250))."',";
  147. $sql .= " '".$this->db->escape(dol_getprefix())."'";
  148. $sql .= ")";
  149. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  150. $resql = $this->db->query($sql);
  151. if ($resql) {
  152. $this->id = $this->db->last_insert_id($this->db->prefix()."events");
  153. return $this->id;
  154. } else {
  155. $this->error = "Error ".$this->db->lasterror();
  156. return -1;
  157. }
  158. }
  159. /**
  160. * Update database
  161. *
  162. * @param User $user User that modify
  163. * @param int $notrigger 0=no, 1=yes (no update trigger)
  164. * @return int <0 if KO, >0 if OK
  165. */
  166. public function update($user = null, $notrigger = 0)
  167. {
  168. // Clean parameters
  169. $this->id = (int) $this->id;
  170. $this->type = trim($this->type);
  171. $this->description = trim($this->description);
  172. // Check parameters
  173. // Put here code to add control on parameters values
  174. // Update request
  175. $sql = "UPDATE ".$this->db->prefix()."events SET";
  176. $sql .= " type='".$this->db->escape($this->type)."',";
  177. $sql .= " dateevent='".$this->db->idate($this->dateevent)."',";
  178. $sql .= " description='".$this->db->escape($this->description)."'";
  179. $sql .= " WHERE rowid=".((int) $this->id);
  180. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  181. $resql = $this->db->query($sql);
  182. if (!$resql) {
  183. $this->error = "Error ".$this->db->lasterror();
  184. return -1;
  185. }
  186. return 1;
  187. }
  188. /**
  189. * Load object in memory from database
  190. *
  191. * @param int $id Id object
  192. * @param User $user User that load
  193. * @return int <0 if KO, >0 if OK
  194. */
  195. public function fetch($id, $user = null)
  196. {
  197. $sql = "SELECT";
  198. $sql .= " t.rowid,";
  199. $sql .= " t.tms,";
  200. $sql .= " t.type,";
  201. $sql .= " t.entity,";
  202. $sql .= " t.dateevent,";
  203. $sql .= " t.description,";
  204. $sql .= " t.ip,";
  205. $sql .= " t.user_agent,";
  206. $sql .= " t.prefix_session";
  207. $sql .= " FROM ".$this->db->prefix()."events as t";
  208. $sql .= " WHERE t.rowid = ".((int) $id);
  209. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  210. $resql = $this->db->query($sql);
  211. if ($resql) {
  212. if ($this->db->num_rows($resql)) {
  213. $obj = $this->db->fetch_object($resql);
  214. $this->id = $obj->rowid;
  215. $this->tms = $this->db->jdate($obj->tms);
  216. $this->type = $obj->type;
  217. $this->entity = $obj->entity;
  218. $this->dateevent = $this->db->jdate($obj->dateevent);
  219. $this->description = $obj->description;
  220. $this->ip = $obj->ip;
  221. $this->user_agent = $obj->user_agent;
  222. $this->prefix_session = $obj->prefix_session;
  223. }
  224. $this->db->free($resql);
  225. return 1;
  226. } else {
  227. $this->error = "Error ".$this->db->lasterror();
  228. return -1;
  229. }
  230. }
  231. /**
  232. * Delete object in database
  233. *
  234. * @param User $user User that delete
  235. * @return int <0 if KO, >0 if OK
  236. */
  237. public function delete($user)
  238. {
  239. $sql = "DELETE FROM ".$this->db->prefix()."events";
  240. $sql .= " WHERE rowid=".((int) $this->id);
  241. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  242. $resql = $this->db->query($sql);
  243. if (!$resql) {
  244. $this->error = "Error ".$this->db->lasterror();
  245. return -1;
  246. }
  247. return 1;
  248. }
  249. /**
  250. * Initialise an instance with random values.
  251. * Used to build previews or test instances.
  252. * id must be 0 if object instance is a specimen.
  253. *
  254. * @return void
  255. */
  256. public function initAsSpecimen()
  257. {
  258. $this->id = 0;
  259. $this->tms = time();
  260. $this->type = '';
  261. $this->dateevent = time();
  262. $this->description = 'This is a specimen event';
  263. $this->ip = '1.2.3.4';
  264. $this->user_agent = 'Mozilla specimen User Agent X.Y';
  265. $this->prefix_session = dol_getprefix();
  266. }
  267. }