PageRenderTime 35ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/main/class/notify/notify.php

http://github.com/FSB/Fire-Soft-Board-2
PHP | 325 lines | 190 code | 34 blank | 101 comment | 30 complexity | 626a0991af59ae34f7b1ef3007361cc9 MD5 | raw file
  1. <?php
  2. /**
  3. * Fire-Soft-Board version 2
  4. *
  5. * @package FSB2
  6. * @author Genova <genova@fire-soft-board.com>
  7. * @version $Id$
  8. * @license http://opensource.org/licenses/gpl-2.0.php GNU GPL 2
  9. */
  10. /**
  11. * Gestion d'envoie de messages de notification en differe.
  12. * Mail / SMTP supportés, MSN et Jabber sont mis de côtés pour le moment et ne fonctionnent pas correctement.
  13. */
  14. class Notify extends Fsb_model
  15. {
  16. /**
  17. * Nombre maximum d'essaie de renvoie d'une notification si elle echoue
  18. */
  19. const MAX_TRY = 3;
  20. /**
  21. * Methode d'envoie de notification
  22. *
  23. * @var int
  24. */
  25. private $method = NOTIFY_MAIL;
  26. /**
  27. * Liste des destinataires
  28. *
  29. * @var array
  30. */
  31. private $bcc = array();
  32. /**
  33. * Sujet de la notification
  34. *
  35. * @var string
  36. */
  37. private $subject = '';
  38. /**
  39. * Contenu de la notification
  40. *
  41. * @var string
  42. */
  43. private $body = '';
  44. /**
  45. * Variables a parser pour le message de notification
  46. *
  47. * @var string
  48. */
  49. private $vars = array();
  50. /**
  51. * Methodes d'envoie de notification
  52. *
  53. * @var array
  54. */
  55. private $ext = array(NOTIFY_MAIL => true, NOTIFY_MSN => true, NOTIFY_JABBER => true);
  56. /**
  57. * Constructeur, determine la methode de notification
  58. *
  59. * @param int $method Choix de la methode de notification
  60. */
  61. public function __construct($method = NOTIFY_MAIL)
  62. {
  63. if (!Fsb::$cfg->get('jabber_notify_enabled') || !function_exists('fsockopen'))
  64. {
  65. unset($this->ext[NOTIFY_JABBER]);
  66. }
  67. if (!Fsb::$cfg->get('msn_notify_enabled') || !function_exists('fsockopen') || !extension_loaded('curl'))
  68. {
  69. unset($this->ext[NOTIFY_MSN]);
  70. }
  71. $this->method = (isset($this->ext[$method])) ? $method : NOTIFY_MAIL;
  72. }
  73. /**
  74. * Ajout de destinataires
  75. *
  76. * @param string $addr Adresse du destinataire
  77. */
  78. public function add_bcc($addr)
  79. {
  80. $this->bcc[] = $addr;
  81. }
  82. /**
  83. * Selection du template pour le contenu du message
  84. *
  85. * @param string $template Chemin vers le template
  86. */
  87. public function set_template($template)
  88. {
  89. // On regarde si une modification du template existe
  90. $updated_template = dirname($template) . '/' . get_file_data(basename($template), 'filename') . '.updated';
  91. if (file_exists($updated_template))
  92. {
  93. $template = $updated_template;
  94. }
  95. if (!file_exists($template))
  96. {
  97. trigger_error('Le template ' . $template . ' n\'existe pas', FSB_ERROR);
  98. }
  99. // Recuperation du contenu du template
  100. $this->body = file_get_contents($template);
  101. }
  102. /**
  103. * Ajout de variables de template
  104. *
  105. * @param array $vars Liste des variables de template
  106. */
  107. public function set_vars($vars)
  108. {
  109. $this->vars = array_merge($this->vars, $vars);
  110. }
  111. /**
  112. * Assigne le sujet de la notification
  113. *
  114. * @param string $subject
  115. */
  116. public function set_subject($subject)
  117. {
  118. if (!$subject)
  119. {
  120. $subject = Fsb::$session->lang('no_subject');
  121. }
  122. $this->subject = $subject;
  123. }
  124. /**
  125. * Parse le contenu du message avec les variables de template
  126. */
  127. public function parse_body()
  128. {
  129. foreach ($this->vars AS $key => $value)
  130. {
  131. $this->body = str_replace('{' . $key . '}', $value, $this->body);
  132. }
  133. // Pas de HTML pour MSN / Jabber
  134. if ($this->method == NOTIFY_MSN || $this->method == NOTIFY_JABBER)
  135. {
  136. $this->body = preg_replace('#</?[^>]+?>#si', '', $this->body);
  137. $this->body = str_replace(array("\r\n", "\r"), array("\n", "\n"), $this->body);
  138. $this->body = str_replace("\n", "\r\n", $this->body);
  139. if ($this->method == NOTIFY_JABBER)
  140. {
  141. $this->body = htmlspecialchars($this->body);
  142. }
  143. }
  144. }
  145. /**
  146. * Envoie le message
  147. *
  148. * @param bool $parse_body true si on doit parser le texte a partir des variables de templates
  149. * @return bool Succes ou non de l'envoie du message
  150. */
  151. public function send($parse_body = true)
  152. {
  153. if ($parse_body)
  154. {
  155. $this->parse_body();
  156. }
  157. $result = false;
  158. // Envoie par Email via la classe PHPmailer ...
  159. if ($this->method == NOTIFY_MAIL)
  160. {
  161. $mail = new Notify_mail();
  162. foreach ($this->bcc AS $bcc)
  163. {
  164. if ($bcc)
  165. {
  166. $mail->AddBCC($bcc);
  167. }
  168. }
  169. $mail->Subject = $this->subject;
  170. $mail->Body = $this->body;
  171. $result = $mail->Send();
  172. $mail->SmtpClose();
  173. unset($mail);
  174. }
  175. // Envoie via le protocole Jabber ...
  176. if ($this->method == NOTIFY_JABBER)
  177. {
  178. // Connexion au serveur Jabber
  179. $jabber = new Notify_jabber();
  180. $jabber->server = Fsb::$cfg->get('jabber_notify_server');
  181. $jabber->port = Fsb::$cfg->get('jabber_notify_port');
  182. $jabber->username = Fsb::$cfg->get('jabber_notify_email');
  183. $jabber->password = Fsb::$cfg->get('jabber_notify_password');
  184. if ($jabber->Connect() && $jabber->SendAuth())
  185. {
  186. $jabber->SendPresence(null, null, "online");
  187. // Envoie du message
  188. foreach ($this->bcc AS $bcc)
  189. {
  190. $result = $jabber->SendMessage($bcc, 'normal', null, array(
  191. 'body' => $this->body,
  192. ));
  193. }
  194. $jabber->Disconnect();
  195. }
  196. else
  197. {
  198. $result = false;
  199. }
  200. }
  201. // Envoie via le protocole MSN ...
  202. if ($this->method == NOTIFY_MSN)
  203. {
  204. $msn = new Notify_msn(Fsb::$cfg->get('msn_notify_email'), Fsb::$cfg->get('msn_notify_password'));
  205. $result = $msn->connect();
  206. // On envoie une notification MSN message, compte par compte
  207. if ($result)
  208. {
  209. foreach ($this->bcc AS $bcc)
  210. {
  211. $result = $msn->send_message($this->subject . " :\r\n" . $this->body, $bcc);
  212. }
  213. }
  214. $msn->close();
  215. unset($msn);
  216. }
  217. return ($result);
  218. }
  219. /**
  220. * Ajout du message a la liste des notifications
  221. */
  222. public function put()
  223. {
  224. $this->parse_body();
  225. Fsb::$db->insert('notify', array(
  226. 'notify_method' => $this->method,
  227. 'notify_time' => CURRENT_TIME,
  228. 'notify_subject' => $this->subject,
  229. 'notify_body' => $this->body,
  230. 'notify_bcc' => implode("\n", $this->bcc),
  231. 'notify_try' => 0,
  232. ));
  233. Fsb::$db->destroy_cache('notify_');
  234. }
  235. /**
  236. * Remise a zero des informations
  237. */
  238. public function reset()
  239. {
  240. $this->method = NOTIFY_MAIL;
  241. $this->body = '';
  242. $this->subject = '';
  243. $this->bcc = array();
  244. $this->vars = array();
  245. }
  246. /**
  247. * Envoie toutes les notifications en attente
  248. */
  249. public function send_queue()
  250. {
  251. $sql = 'SELECT *
  252. FROM ' . SQL_PREFIX . 'notify
  253. ORDER BY notify_time';
  254. $result = Fsb::$db->query($sql, 'notify_');
  255. if ($row = Fsb::$db->row($result))
  256. {
  257. // Suppression des elements directement, afin d'eviter un double envoie
  258. $sql = 'DELETE FROM ' . SQL_PREFIX . 'notify';
  259. Fsb::$db->query($sql);
  260. Fsb::$db->destroy_cache('notify_');
  261. // Envoie du message
  262. do
  263. {
  264. $this->method = (isset($this->ext[$row['notify_method']])) ? $row['notify_method'] : NOTIFY_MAIL;
  265. $this->subject = $row['notify_subject'];
  266. $this->body = $row['notify_body'];
  267. $this->bcc = explode("\n", $row['notify_bcc']);
  268. $return = $this->send(false);
  269. $this->reset();
  270. // En cas d'echec du message on le reinsere dans la base de donnee
  271. if (!$return && $row['notify_try'] < Notify::MAX_TRY)
  272. {
  273. $row['notify_try']++;
  274. unset($row['notify_id']);
  275. foreach ($row AS $k => $v)
  276. {
  277. if (is_numeric($k))
  278. {
  279. unset($row[$k]);
  280. }
  281. }
  282. Fsb::$db->insert('notify', $row, 'INSERT', true);
  283. }
  284. }
  285. while ($row = Fsb::$db->row($result));
  286. Fsb::$db->query_multi_insert();
  287. }
  288. Fsb::$db->free($result);
  289. }
  290. }
  291. /* EOF */