events.class.php 8.4 KB

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