events.class.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. * Initialy built by build_class_from_table on 2008-02-28 17:25
  31. */
  32. class Events // extends CommonObject
  33. {
  34. public $element='events'; //!< Id that identify managed objects
  35. public $table_element='events'; //!< Name of table without prefix where object is stored
  36. var $id;
  37. var $db;
  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'=>'GROUP_CREATE', 'test'=>1),
  54. array('id'=>'GROUP_MODIFY', 'test'=>1),
  55. array('id'=>'GROUP_DELETE', 'test'=>1),
  56. /* array('id'=>'ACTION_CREATE', 'test'=>$conf->societe->enabled),
  57. array('id'=>'COMPANY_CREATE', 'test'=>$conf->societe->enabled),
  58. array('id'=>'CONTRACT_VALIDATE', 'test'=>$conf->contrat->enabled),
  59. array('id'=>'PROPAL_VALIDATE', 'test'=>$conf->propal->enabled),
  60. array('id'=>'PROPAL_CLOSE_SIGNED', 'test'=>$conf->propal->enabled),
  61. array('id'=>'PROPAL_CLOSE_REFUSED', 'test'=>$conf->propal->enabled),
  62. array('id'=>'PROPAL_SENTBYMAIL', 'test'=>$conf->propal->enabled),
  63. array('id'=>'ORDER_VALIDATE', 'test'=>$conf->commande->enabled),
  64. array('id'=>'ORDER_SENTBYMAIL', 'test'=>$conf->commande->enabled),
  65. array('id'=>'BILL_VALIDATE', 'test'=>$conf->facture->enabled),
  66. array('id'=>'BILL_PAYED', 'test'=>$conf->facture->enabled),
  67. array('id'=>'BILL_CANCEL', 'test'=>$conf->facture->enabled),
  68. array('id'=>'BILL_SENTBYMAIL', 'test'=>$conf->facture->enabled),
  69. array('id'=>'PAYMENT_CUSTOMER_CREATE','test'=>$conf->facture->enabled),
  70. array('id'=>'PAYMENT_SUPPLIER_CREATE','test'=>$conf->fournisseur->enabled),
  71. array('id'=>'MEMBER_CREATE', 'test'=>$conf->adherent->enabled),
  72. array('id'=>'MEMBER_VALIDATE', 'test'=>$conf->adherent->enabled),
  73. array('id'=>'MEMBER_SUBSCRIPTION', 'test'=>$conf->adherent->enabled),
  74. array('id'=>'MEMBER_MODIFY', 'test'=>$conf->adherent->enabled),
  75. array('id'=>'MEMBER_RESILIATE', 'test'=>$conf->adherent->enabled),
  76. array('id'=>'MEMBER_DELETE', 'test'=>$conf->adherent->enabled),
  77. */
  78. );
  79. /**
  80. * Constructor
  81. *
  82. * @param DoliDB $db Database handler
  83. */
  84. function __construct($db)
  85. {
  86. $this->db = $db;
  87. return 1;
  88. }
  89. /**
  90. * Create in database
  91. *
  92. * @param User $user User that create
  93. * @return int <0 if KO, >0 if OK
  94. */
  95. function create($user)
  96. {
  97. global $conf, $langs;
  98. // Clean parameters
  99. $this->description=trim($this->description);
  100. // Check parameters
  101. if (! $this->description) { $this->error='ErrorBadValueForParameter'; return -1; }
  102. // Insert request
  103. $sql = "INSERT INTO ".MAIN_DB_PREFIX."events(";
  104. $sql.= "type,";
  105. $sql.= "entity,";
  106. $sql.= "ip,";
  107. $sql.= "user_agent,";
  108. $sql.= "dateevent,";
  109. $sql.= "fk_user,";
  110. $sql.= "description";
  111. $sql.= ") VALUES (";
  112. $sql.= " '".$this->type."',";
  113. $sql.= " ".$conf->entity.",";
  114. $sql.= " '".$_SERVER['REMOTE_ADDR']."',";
  115. $sql.= " ".($_SERVER['HTTP_USER_AGENT']?"'".dol_trunc($_SERVER['HTTP_USER_AGENT'],250)."'":'NULL').",";
  116. $sql.= " '".$this->db->idate($this->dateevent)."',";
  117. $sql.= " ".($user->id?"'".$user->id."'":'NULL').",";
  118. $sql.= " '".$this->db->escape($this->description)."'";
  119. $sql.= ")";
  120. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  121. $resql=$this->db->query($sql);
  122. if ($resql)
  123. {
  124. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."events");
  125. return $this->id;
  126. }
  127. else
  128. {
  129. $this->error="Error ".$this->db->lasterror();
  130. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  131. return -1;
  132. }
  133. }
  134. /**
  135. * Update database
  136. *
  137. * @param User $user User that modify
  138. * @param int $notrigger 0=no, 1=yes (no update trigger)
  139. * @return int <0 if KO, >0 if OK
  140. */
  141. function update($user=0, $notrigger=0)
  142. {
  143. global $conf, $langs;
  144. // Clean parameters
  145. $this->id=trim($this->id);
  146. $this->type=trim($this->type);
  147. $this->description=trim($this->description);
  148. // Check parameters
  149. // Put here code to add control on parameters values
  150. // Update request
  151. $sql = "UPDATE ".MAIN_DB_PREFIX."events SET";
  152. $sql.= " type='".$this->type."',";
  153. $sql.= " dateevent=".$this->db->idate($this->dateevent).",";
  154. $sql.= " description='".$this->db->escape($this->description)."'";
  155. $sql.= " WHERE rowid=".$this->id;
  156. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  157. $resql = $this->db->query($sql);
  158. if (! $resql)
  159. {
  160. $this->error="Error ".$this->db->lasterror();
  161. dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
  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=0)
  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 sql=".$sql, 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. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  210. return -1;
  211. }
  212. }
  213. /**
  214. * Delete object in database
  215. *
  216. * @param User $user User that delete
  217. * @return int <0 if KO, >0 if OK
  218. */
  219. function delete($user)
  220. {
  221. global $conf, $langs;
  222. $sql = "DELETE FROM ".MAIN_DB_PREFIX."events";
  223. $sql.= " WHERE rowid=".$this->id;
  224. dol_syslog(get_class($this)."::delete sql=".$sql);
  225. $resql = $this->db->query($sql);
  226. if (! $resql)
  227. {
  228. $this->error="Error ".$this->db->lasterror();
  229. dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
  230. return -1;
  231. }
  232. return 1;
  233. }
  234. /**
  235. * Initialise an instance with random values.
  236. * Used to build previews or test instances.
  237. * id must be 0 if object instance is a specimen.
  238. *
  239. * @return void
  240. */
  241. function initAsSpecimen()
  242. {
  243. $this->id=0;
  244. $this->tms=time();
  245. $this->type='';
  246. $this->dateevent=time();
  247. $this->description='This is a specimen event';
  248. }
  249. }
  250. ?>