events.class.php 8.2 KB

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