PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/notificationmail.class.php

https://github.com/ardowz/Thesis-SideB
PHP | 250 lines | 185 code | 24 blank | 41 comment | 19 complexity | 34f3474d28e398d8da331aea57b51b26 MD5 | raw file
  1. <?php
  2. /*
  3. * @version $Id: notificationmail.class.php 14684 2011-06-11 06:32:40Z remi $
  4. -------------------------------------------------------------------------
  5. GLPI - Gestionnaire Libre de Parc Informatique
  6. Copyright (C) 2003-2011 by the INDEPNET Development Team.
  7. http://indepnet.net/ http://glpi-project.org
  8. -------------------------------------------------------------------------
  9. LICENSE
  10. This file is part of GLPI.
  11. GLPI is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. GLPI is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with GLPI; if not, write to the Free Software Foundation, Inc.,
  21. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. --------------------------------------------------------------------------
  23. */
  24. // ----------------------------------------------------------------------
  25. // Original Author of file:
  26. // Purpose of file:
  27. // ----------------------------------------------------------------------
  28. if (!defined('GLPI_ROOT')) {
  29. die("Sorry. You can't access directly to this file");
  30. }
  31. require_once(GLPI_PHPMAILER_DIR . "/class.phpmailer.php");
  32. /**
  33. * NotificationMail class extends phpmail and implements the NotificationInterface
  34. **/
  35. class NotificationMail extends phpmailer implements NotificationInterface {
  36. //! mailing type (new,attrib,followup,finish)
  37. var $mailtype = NULL;
  38. /** Job class variable - job to be mailed
  39. * @see Job
  40. */
  41. var $job = NULL;
  42. /** User class variable - user who make changes
  43. * @see User
  44. */
  45. var $user = NULL;
  46. /// Is the followupadded private ?
  47. var $followupisprivate = NULL;
  48. /// Set default variables for all new objects
  49. var $WordWrap = 80;
  50. /// Defaut charset
  51. var $CharSet = "utf-8";
  52. /**
  53. * Constructor
  54. **/
  55. function __construct() {
  56. global $CFG_GLPI;
  57. // Comes from config
  58. $this->SetLanguage("en", GLPI_PHPMAILER_DIR . "/language/");
  59. if ($CFG_GLPI['smtp_mode'] != MAIL_MAIL) {
  60. $this->Mailer = "smtp";
  61. $this->Host = $CFG_GLPI['smtp_host'].':'.$CFG_GLPI['smtp_port'];
  62. if ($CFG_GLPI['smtp_username'] != '') {
  63. $this->SMTPAuth = true;
  64. $this->Username = $CFG_GLPI['smtp_username'];
  65. $this->Password = decrypt($CFG_GLPI['smtp_passwd'], GLPIKEY);
  66. }
  67. if ($CFG_GLPI['smtp_mode'] == MAIL_SMTPSSL) {
  68. $this->SMTPSecure = "ssl";
  69. }
  70. if ($CFG_GLPI['smtp_mode'] == MAIL_SMTPTLS) {
  71. $this->SMTPSecure = "tls";
  72. }
  73. }
  74. if ($_SESSION['glpi_use_mode'] == DEBUG_MODE) {
  75. $this->do_debug = 3;
  76. }
  77. }
  78. /**
  79. * Determine if email is valid
  80. *
  81. * @param $address email to check
  82. * @param $options array options used
  83. * - checkdns :check dns entry
  84. *
  85. * @return boolean
  86. * from http://www.linuxjournal.com/article/9585
  87. **/
  88. static function isUserAddressValid($address, $options=array('checkdns'=>false)) {
  89. $checkdns = $options['checkdns'];
  90. $isValid = true;
  91. $atIndex = strrpos($address, "@");
  92. if (is_bool($atIndex) && !$atIndex) {
  93. $isValid = false;
  94. } else {
  95. $domain = substr($address, $atIndex+1);
  96. $local = substr($address, 0, $atIndex);
  97. $localLen = strlen($local);
  98. $domainLen = strlen($domain);
  99. if ($localLen < 1 || $localLen > 64) {
  100. // local part length exceeded
  101. $isValid = false;
  102. } else if ($domainLen < 1 || $domainLen > 255) {
  103. // domain part length exceeded
  104. $isValid = false;
  105. } else if ($local[0] == '.' || $local[$localLen-1] == '.') {
  106. // local part starts or ends with '.'
  107. $isValid = false;
  108. } else if (preg_match('/\\.\\./', $local)) {
  109. // local part has two consecutive dots
  110. $isValid = false;
  111. } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
  112. // character not valid in domain part
  113. $isValid = false;
  114. } else if (preg_match('/\\.\\./', $domain)) {
  115. // domain part has two consecutive dots
  116. $isValid = false;
  117. } else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
  118. str_replace("\\\\","",$local))) {
  119. // character not valid in local part unless
  120. // local part is quoted
  121. if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) {
  122. $isValid = false;
  123. }
  124. }
  125. if ($checkdns) {
  126. if ($isValid
  127. && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
  128. // domain not found in DNS
  129. $isValid = false;
  130. }
  131. } else if (!preg_match('/\\./', $domain) || !preg_match("/[a-zA-Z0-9]$/", $domain)) {
  132. // domain has no dots or do not end by alphenum char
  133. $isValid = false;
  134. }
  135. }
  136. return $isValid;
  137. }
  138. static function testNotification() {
  139. global $CFG_GLPI,$LANG;
  140. $mmail = new NotificationMail;
  141. $mmail->AddCustomHeader("Auto-Submitted: auto-generated");
  142. $mmail->SetFrom($CFG_GLPI["admin_email"], $CFG_GLPI["admin_email_name"]);
  143. $mmail->AddAddress($CFG_GLPI["admin_email"], $CFG_GLPI["admin_email_name"]);
  144. $mmail->Subject = "[GLPI] ".$LANG['mailing'][32];
  145. $mmail->Body = $LANG['mailing'][31]."\n-- \n".$CFG_GLPI["mailing_signature"];
  146. if (!$mmail->Send()) {
  147. addMessageAfterRedirect($LANG['setup'][206], false, ERROR);
  148. } else {
  149. addMessageAfterRedirect($LANG['setup'][205]);
  150. }
  151. }
  152. /**
  153. * Format the mail sender to send
  154. *
  155. * @return mail sender email string
  156. */
  157. function getEntityAdminAddress() {
  158. global $CFG_GLPI,$DB;
  159. $query = "SELECT `admin_email` AS email
  160. FROM `glpi_entitydatas`
  161. WHERE `entities_id` = '".$this->job->fields["entities_id"]."'";
  162. if ($result = $DB->query($query)) {
  163. if ($DB->numrows($result)) {
  164. $data = $DB->fetch_assoc($result);
  165. if (self::isUserAddressValid($data["email"])) {
  166. return $data["email"];
  167. }
  168. }
  169. }
  170. return $CFG_GLPI["admin_email"];
  171. }
  172. function sendNotification($options=array()) {
  173. global $LANG;
  174. $mmail = new self();
  175. $mmail->AddCustomHeader("Auto-Submitted: auto-generated");
  176. $mmail->SetFrom($options['from'], $options['fromname']);
  177. if ($options['replyto']) {
  178. $mmail->AddReplyTo($options['replyto'], $options['replytoname']);
  179. }
  180. $mmail->Subject = $options['subject'];
  181. if (empty($options['content_html'])) {
  182. $mmail->isHTML(false);
  183. $mmail->Body = $options['content_text'];
  184. } else {
  185. $mmail->isHTML(true);
  186. $mmail->Body = $options['content_html'];
  187. $mmail->AltBody = $options['content_text'];
  188. }
  189. $mmail->AddAddress($options['to'], $options['toname']);
  190. $mmail->MessageID = "<GLPI-".$options["items_id"].".".time().".".rand(). "@".php_uname('n').">";
  191. $messageerror = $LANG['mailing'][47];
  192. if (!$mmail->Send()) {
  193. $senderror = true;
  194. addMessageAfterRedirect($messageerror."<br>".$mmail->ErrorInfo, true);
  195. } else {
  196. logInFile("mail", $LANG['tracking'][38]." ".$options['to']." : ".$options['subject']."\n");
  197. }
  198. $mmail->ClearAddresses();
  199. return true;
  200. }
  201. }
  202. ?>