events.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /* Copyright (C) 2007-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.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 <http://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. * \author Laurent Destailleur
  23. */
  24. // Put here all includes required by your class file
  25. //require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  26. //require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  27. //require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  28. /**
  29. * Events class
  30. */
  31. class Events // extends CommonObject
  32. {
  33. /**
  34. * @var string ID to identify managed object
  35. */
  36. public $element='events';
  37. /**
  38. * @var string Name of table without prefix where object is stored
  39. */
  40. public $table_element='events';
  41. /**
  42. * @var int ID
  43. */
  44. public $id;
  45. /**
  46. * @var DoliDB Database handler.
  47. */
  48. public $db;
  49. /**
  50. * @var string Error code (or message)
  51. */
  52. public $error='';
  53. var $tms;
  54. var $type;
  55. var $entity;
  56. var $dateevent;
  57. /**
  58. * @var string description
  59. */
  60. public $description;
  61. // List of all Audit/Security events supported by triggers
  62. var $eventstolog=array(
  63. /*array('id'=>'USER_LOGIN', 'test'=>1),
  64. array('id'=>'USER_LOGIN_FAILED', 'test'=>1),
  65. array('id'=>'USER_LOGOUT', 'test'=>1),*/
  66. array('id'=>'USER_CREATE', 'test'=>1),
  67. array('id'=>'USER_MODIFY', 'test'=>1),
  68. array('id'=>'USER_NEW_PASSWORD', 'test'=>1),
  69. array('id'=>'USER_ENABLEDISABLE', 'test'=>1),
  70. array('id'=>'USER_DELETE', 'test'=>1),
  71. /* array('id'=>'USER_SETINGROUP', 'test'=>1), deprecated. Replace with USER_MODIFY
  72. array('id'=>'USER_REMOVEFROMGROUP', 'test'=>1), deprecated. Replace with USER_MODIFY */
  73. array('id'=>'GROUP_CREATE', 'test'=>1),
  74. array('id'=>'GROUP_MODIFY', 'test'=>1),
  75. array('id'=>'GROUP_DELETE', 'test'=>1),
  76. /* array('id'=>'ACTION_CREATE', 'test'=>$conf->societe->enabled),
  77. array('id'=>'COMPANY_CREATE', 'test'=>$conf->societe->enabled),
  78. array('id'=>'CONTRACT_VALIDATE', 'test'=>$conf->contrat->enabled),
  79. array('id'=>'PROPAL_VALIDATE', 'test'=>$conf->propal->enabled),
  80. array('id'=>'PROPAL_CLOSE_SIGNED', 'test'=>$conf->propal->enabled),
  81. array('id'=>'PROPAL_CLOSE_REFUSED', 'test'=>$conf->propal->enabled),
  82. array('id'=>'PROPAL_SENTBYMAIL', 'test'=>$conf->propal->enabled),
  83. array('id'=>'ORDER_VALIDATE', 'test'=>$conf->commande->enabled),
  84. array('id'=>'ORDER_SENTBYMAIL', 'test'=>$conf->commande->enabled),
  85. array('id'=>'BILL_VALIDATE', 'test'=>$conf->facture->enabled),
  86. array('id'=>'BILL_PAYED', 'test'=>$conf->facture->enabled),
  87. array('id'=>'BILL_CANCEL', 'test'=>$conf->facture->enabled),
  88. array('id'=>'BILL_SENTBYMAIL', 'test'=>$conf->facture->enabled),
  89. array('id'=>'PAYMENT_CUSTOMER_CREATE','test'=>$conf->facture->enabled),
  90. array('id'=>'PAYMENT_SUPPLIER_CREATE','test'=>$conf->fournisseur->enabled),
  91. array('id'=>'MEMBER_CREATE', 'test'=>$conf->adherent->enabled),
  92. array('id'=>'MEMBER_VALIDATE', 'test'=>$conf->adherent->enabled),
  93. array('id'=>'MEMBER_SUBSCRIPTION', 'test'=>$conf->adherent->enabled),
  94. array('id'=>'MEMBER_MODIFY', 'test'=>$conf->adherent->enabled),
  95. array('id'=>'MEMBER_RESILIATE', 'test'=>$conf->adherent->enabled),
  96. array('id'=>'MEMBER_DELETE', 'test'=>$conf->adherent->enabled),
  97. */
  98. );
  99. /**
  100. * Constructor
  101. *
  102. * @param DoliDB $db Database handler
  103. */
  104. function __construct($db)
  105. {
  106. $this->db = $db;
  107. }
  108. /**
  109. * Create in database
  110. *
  111. * @param User $user User that create
  112. * @return int <0 if KO, >0 if OK
  113. */
  114. function create($user)
  115. {
  116. global $conf, $langs;
  117. // Clean parameters
  118. $this->description=trim($this->description);
  119. // Check parameters
  120. if (empty($this->description)) { $this->error='ErrorBadValueForParameterCreateEventDesc'; return -1; }
  121. // Insert request
  122. $sql = "INSERT INTO ".MAIN_DB_PREFIX."events(";
  123. $sql.= "type,";
  124. $sql.= "entity,";
  125. $sql.= "ip,";
  126. $sql.= "user_agent,";
  127. $sql.= "dateevent,";
  128. $sql.= "fk_user,";
  129. $sql.= "description";
  130. $sql.= ") VALUES (";
  131. $sql.= " '".$this->db->escape($this->type)."',";
  132. $sql.= " ".$conf->entity.",";
  133. $sql.= " '".$this->db->escape($_SERVER['REMOTE_ADDR'])."',";
  134. $sql.= " ".($_SERVER['HTTP_USER_AGENT']?"'".$this->db->escape(dol_trunc($_SERVER['HTTP_USER_AGENT'],250))."'":'NULL').",";
  135. $sql.= " '".$this->db->idate($this->dateevent)."',";
  136. $sql.= " ".($user->id?"'".$this->db->escape($user->id)."'":'NULL').",";
  137. $sql.= " '".$this->db->escape(dol_trunc($this->description,250))."'";
  138. $sql.= ")";
  139. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  140. $resql=$this->db->query($sql);
  141. if ($resql)
  142. {
  143. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."events");
  144. return $this->id;
  145. }
  146. else
  147. {
  148. $this->error="Error ".$this->db->lasterror();
  149. return -1;
  150. }
  151. }
  152. /**
  153. * Update database
  154. *
  155. * @param User $user User that modify
  156. * @param int $notrigger 0=no, 1=yes (no update trigger)
  157. * @return int <0 if KO, >0 if OK
  158. */
  159. function update($user=null, $notrigger=0)
  160. {
  161. global $conf, $langs;
  162. // Clean parameters
  163. $this->id=trim($this->id);
  164. $this->type=trim($this->type);
  165. $this->description=trim($this->description);
  166. // Check parameters
  167. // Put here code to add control on parameters values
  168. // Update request
  169. $sql = "UPDATE ".MAIN_DB_PREFIX."events SET";
  170. $sql.= " type='".$this->db->escape($this->type)."',";
  171. $sql.= " dateevent='".$this->db->idate($this->dateevent)."',";
  172. $sql.= " description='".$this->db->escape($this->description)."'";
  173. $sql.= " WHERE rowid=".$this->id;
  174. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  175. $resql = $this->db->query($sql);
  176. if (! $resql)
  177. {
  178. $this->error="Error ".$this->db->lasterror();
  179. return -1;
  180. }
  181. return 1;
  182. }
  183. /**
  184. * Load object in memory from database
  185. *
  186. * @param int $id Id object
  187. * @param User $user User that load
  188. * @return int <0 if KO, >0 if OK
  189. */
  190. function fetch($id, $user=null)
  191. {
  192. global $langs;
  193. $sql = "SELECT";
  194. $sql.= " t.rowid,";
  195. $sql.= " t.tms,";
  196. $sql.= " t.type,";
  197. $sql.= " t.entity,";
  198. $sql.= " t.dateevent,";
  199. $sql.= " t.description,";
  200. $sql.= " t.ip,";
  201. $sql.= " t.user_agent";
  202. $sql.= " FROM ".MAIN_DB_PREFIX."events as t";
  203. $sql.= " WHERE t.rowid = ".$id;
  204. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  205. $resql=$this->db->query($sql);
  206. if ($resql)
  207. {
  208. if ($this->db->num_rows($resql))
  209. {
  210. $obj = $this->db->fetch_object($resql);
  211. $this->id = $obj->rowid;
  212. $this->tms = $this->db->jdate($obj->tms);
  213. $this->type = $obj->type;
  214. $this->entity = $obj->entity;
  215. $this->dateevent = $this->db->jdate($obj->dateevent);
  216. $this->description = $obj->description;
  217. $this->ip = $obj->ip;
  218. $this->user_agent = $obj->user_agent;
  219. }
  220. $this->db->free($resql);
  221. return 1;
  222. }
  223. else
  224. {
  225. $this->error="Error ".$this->db->lasterror();
  226. return -1;
  227. }
  228. }
  229. /**
  230. * Delete object in database
  231. *
  232. * @param User $user User that delete
  233. * @return int <0 if KO, >0 if OK
  234. */
  235. function delete($user)
  236. {
  237. global $conf, $langs;
  238. $sql = "DELETE FROM ".MAIN_DB_PREFIX."events";
  239. $sql.= " WHERE rowid=".$this->id;
  240. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  241. $resql = $this->db->query($sql);
  242. if (! $resql)
  243. {
  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. 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. }
  264. }