Advertisement
Guest User

arfooo mailer

a guest
May 25th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. <?php
  2. /**
  3. * Arfooo
  4. *
  5. * @package Arfooo
  6. * @copyright Copyright (c) Arfooo Annuaire (fr) and Arfooo Directory (en)
  7. * by Guillaume Hocine (c) 2007 - 2010
  8. * http://www.arfooo.com/ (fr) and http://www.arfooo.net/ (en)
  9. * @author Guillaume Hocine & Adrian Galewski
  10. * @license http://creativecommons.org/licenses/by/2.0/fr/ Creative Commons
  11. */
  12.  
  13.  
  14. class Mailer extends Model
  15. {
  16. private static $instance = null;
  17. private $adminEmail;
  18. private $inboxDebug = false;
  19. private $sendRealEmail = true;
  20. private $charset = "UTF-8";
  21.  
  22. /**
  23. * @return Mailer
  24. */
  25.  
  26. public static function getInstance()
  27. {
  28. if (self::$instance === null) {
  29. self::$instance = new self();
  30. }
  31.  
  32. return self::$instance;
  33. }
  34.  
  35. function __construct()
  36. {
  37. parent::__construct();
  38.  
  39. $c = new Criteria();
  40. $c->add("role", "administrator");
  41. $c->setLimit(1);
  42. $admin = $this->user->find($c, "email");
  43. $this->adminEmail = $admin->email;
  44. }
  45.  
  46. function getAdminEmail()
  47. {
  48. return $this->adminEmail;
  49. }
  50.  
  51. function sendAdminNotification($site)
  52. {
  53. $message = $this->customMessage->findByPk("submitSite");
  54. $this->sendEmailToAdmin($message->title, $this->replaceTags($message->description, $site));
  55. }
  56.  
  57. function sendEmailToAdmin($subject, $message, $fromEmail = "")
  58. {
  59. return $this->sendEmail($this->adminEmail, $subject, $message, $fromEmail);
  60. }
  61.  
  62. function replaceTags($message, $site = null)
  63. {
  64. $replacements = array("[name of directory]" => Config::get("siteTitle"),
  65. "[url of your directory]" => Config::get("siteRootUrl"));
  66.  
  67. if ($site) {
  68.  
  69. $this->site->attachParents($site);
  70. $detailsUrl = AppRouter::getObjectUrl($site, "siteDetails", true);
  71.  
  72. $category = $this->category->findByPk($site->categoryId, "name");
  73. $siteDescription = (utf8_strlen($site->description) > 100) ? utf8_substr($site->description, 0, 100) : $site->description;
  74.  
  75. $replacements += array(
  76. "[site name]" => $site->siteTitle,
  77. "[name of the category]" => $category->name,
  78. "[url site details]" => $detailsUrl,
  79. "[url site]" => $site->url,
  80. "[description of the site]" => $siteDescription,
  81. "[site type]" => ($site->siteType == "basic") ? _t("Free") : _t("Privilege"));
  82. }
  83.  
  84. $message = strtr($message, $replacements);
  85.  
  86. return $message;
  87. }
  88.  
  89. function sendSiteStateUpdate($webmasterEmail, $site, $subject = false, $description = false)
  90. {
  91. switch ($site['status']) {
  92. case "validated":
  93. $messageId = "validateSite";
  94. break;
  95.  
  96. case "refused":
  97. $messageId = "refuseSite";
  98. break;
  99.  
  100. case "banned":
  101. $messageId = "banSite";
  102. break;
  103. }
  104.  
  105. static $cachedMessages = array();
  106.  
  107. if (!isset($cachedMessages[$messageId])) {
  108. $cachedMessages[$messageId] = $this->customMessage->findByPk($messageId);
  109. }
  110.  
  111. $message = $cachedMessages[$messageId];
  112.  
  113. if (!$description) {
  114. $description = $message->description;
  115. }
  116.  
  117. $description = $this->replaceTags($description, $site);
  118.  
  119. if (!$subject) {
  120. $subject = $message->title;
  121. }
  122.  
  123. $this->sendEmail($webmasterEmail, $subject, $description);
  124.  
  125. }
  126.  
  127. function sendNewsletterTaskFinished()
  128. {
  129. $this->sendEmailToAdmin(_t('Sending the newsletter is finished successfully.'), _t('Sending the newsletter is finished successfully.') . _t('You can go in your administration for further informations.'));
  130. }
  131.  
  132. function sendSiteBacklinkTaskFinished()
  133. {
  134. $this->sendEmailToAdmin(_t('The search for backlinks is finished successfully.'), _t('The search for backlinks is finished successfully.') . _t('You can go in your administration for further informations.'));
  135. }
  136.  
  137. function sendDuplicateContentTaskFinished()
  138. {
  139. $this->sendEmailToAdmin(_t('The search for duplicate content is finished successfully.'), _t('The search for duplicate content is finished successfully.') . _t('You can go in your administration for further informations.'));
  140. }
  141.  
  142. function sendSiteThumbTaskFinished()
  143. {
  144. $this->sendEmailToAdmin(_t('The updating of thumbs is finished successfully.'), _t('The updating of thumbs is finished successfully.') . _t('You can go in your administration for further informations.'));
  145. }
  146.  
  147. function sendWebmasterNotification($site, $webmasterEmail)
  148. {
  149. $message = $this->customMessage->findByPk("webmasterSubmitSite");
  150. $this->sendEmail($webmasterEmail, $message->title, $this->replaceTags($message->description, $site));
  151. }
  152.  
  153. function sendEmailConfirmation($messageId, $email, $confirmLink)
  154. {
  155. $message = $this->customMessage->findByPk($messageId);
  156. $message->description = str_replace("[confirm link]", $confirmLink, $message->description);
  157. $this->sendEmail($email, $message->title, $this->replaceTags($message->description));
  158. }
  159.  
  160. function sendLostPassword($userEmail, $newPassword)
  161. {
  162. $this->sendEmail($userEmail, _t('Annuaire - new webmaster password'), _t('Your new password is') . ' ' . $newPassword);
  163. }
  164.  
  165. function sendContactEmailToUser($email, $title, $text, $fromEmail)
  166. {
  167. $message = $this->customMessage->findByPk("webmasterContact");
  168.  
  169. $text = str_replace(array("[message]", "[sender email]"),
  170. array($text, $fromEmail),
  171. $message->description);
  172.  
  173. $this->sendEmail($email, $title, $text);
  174. }
  175.  
  176. function sendNewCommentNotificationToAdmin($site)
  177. {
  178. $message = $this->customMessage->findByPk("newComment");
  179. $this->sendEmailToAdmin($message->title, $this->replaceTags($message->description, $site));
  180. }
  181.  
  182. function sendNewSiteProblemNotificationToAdmin($site)
  183. {
  184. $message = $this->customMessage->findByPk("newSiteProblem");
  185. $this->sendEmailToAdmin($message->title, $this->replaceTags($message->description, $site));
  186. }
  187.  
  188. function createMailHeaders($from)
  189. {
  190. $mailheaders = "From: $from\n";
  191. $mailheaders .= "Reply-To: $from\n";
  192. $mailheaders .= "MIME-version: 1.0\n";
  193. $mailheaders .= "Content-type: text/html; charset=" . $this->charset . "\n";
  194.  
  195. return $mailheaders;
  196. }
  197.  
  198. function sendEmail($email, $subject, $text, $fromEmail = "")
  199. {
  200. if ($fromEmail == "") {
  201. $fromEmail = $this->adminEmail;
  202. }
  203. $headers = $this->createMailHeaders($fromEmail);
  204.  
  205. $specialChars = array('Ŕ','Á','Â','Ă','Ä','Ĺ','Ć','ŕ','a','á','â','ă','ä','ĺ','ć','Č','É','Ę','Ë','č','e','é','ę','ë','e','Ě','Í','Î','Ď','ě','í','î','ď','Ň','Ó','Ô','Ő','Ö','Ř','ň','ó','ô','ő','ö','ř','Ů','Ú','Ű','Ü','ů','ú','ű','ü','ß','Ç','ç','Đ','đ','Ń','ń','Ţ','ţ','Ý' );
  206. $normalChars = array('A','A','A','A','A','A','A','a','a','a','a','a','a','a','a','E','E','E','E','e','e','e','e','e','e','I','I','I','I','i','i','i','i','O','O','O','O','O','O','o','o','o','o','o','o','U','U','U','U','u','u','u','u','B','C','c','D','d','N','n','P','p','Y' );
  207. $subject = str_replace($specialChars, $normalChars, $subject);
  208.  
  209. if ($this->inboxDebug) {
  210. $str = "<hr>";
  211. $str .= "<b>Time</b> " . date("Y-m-d G.i:s") . "<br>";
  212. $str .= "<b>Email:</b> $email<br>";
  213. $str .= "<b>Topic:</b> $subject<br>";
  214. $str .= "<b>Text:</b> $text<br>";
  215. $str .= "<b>Headers:</b> $headers<br>";
  216.  
  217. $fp = fopen(CODE_ROOT_DIR . "inbox.html", "a");
  218. fwrite($fp, $str);
  219. fclose($fp);
  220. }
  221.  
  222. if ($this->sendRealEmail) {
  223. @mail($email, $subject, $text, $headers);
  224. }
  225.  
  226. return true;
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement