1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class NewsletterCollection extends ItemCollection
25: {
26: 27: 28: 29:
30: public function __construct()
31: {
32: global $cfg;
33: parent::__construct($cfg["tab"]["news"], "idnews");
34: $this->_setItemClass("Newsletter");
35: }
36:
37: 38: 39: 40:
41: public function create($sName)
42: {
43: global $client, $lang, $auth;
44:
45: $sName = cSecurity::escapeDB($sName, null);
46: $client = cSecurity::toInteger($client);
47: $lang = cSecurity::toInteger($lang);
48:
49:
50: $this->resetQuery;
51: $this->setWhere("idclient", $client);
52: $this->setWhere("idlang", $lang);
53: $this->setWhere("name", $sName);
54: $this->query();
55:
56: if ($this->next()) {
57: return $this->create($sName."_".substr(md5(rand()), 0, 10));
58: }
59:
60: $oItem = parent::createNewItem();
61: $oItem->set("idclient", $client);
62: $oItem->set("idlang", $lang);
63: $oItem->set("name", $sName);
64: $oItem->set("created", date('Y-m-d H:i:s'), false);
65: $oItem->set("author", cSecurity::escapeDB($auth->auth["uid"], null));
66:
67: $oItem->store();
68:
69: return $oItem;
70: }
71:
72: 73: 74: 75:
76: public function duplicate($iItemID)
77: {
78: global $client, $lang, $auth;
79:
80: $client = cSecurity::toInteger($client);
81: $lang = cSecurity::toInteger($lang);
82:
83: cInclude("includes", "functions.con.php");
84:
85: $oBaseItem = new Newsletter();
86: $oBaseItem->loadByPrimaryKey($iItemID);
87:
88: $oItem = parent::createNewItem();
89: $oItem->set("name", $oBaseItem->get("name")."_".substr(md5(rand()), 0, 10));
90:
91: $iIDArt = 0;
92: if ($oBaseItem->get("type") == "html" && $oBaseItem->get("idart") > 0 && $oBaseItem->get("template_idart") > 0) {
93: $oClientLang = new cApiClientLanguage(false, $client, $lang);
94:
95: if ($oClientLang->getProperty("newsletter", "html_newsletter") == "true") {
96: $iIDArt = conCopyArticle($oBaseItem->get("idart"),
97: $oClientLang->getProperty("newsletter", "html_newsletter_idcat"),
98: sprintf(i18n("Newsletter: %s"), $oItem->get("name")));
99: conMakeOnline($iIDArt, $lang);
100: }
101: unset($oClientLang);
102: }
103: $oItem->set("idart", $iIDArt);
104: $oItem->set("template_idart", $oBaseItem->get("template_idart"));
105: $oItem->set("idclient", $client);
106: $oItem->set("idlang", $lang);
107: $oItem->set("welcome", 0);
108: $oItem->set("type", $oBaseItem->get("type"));
109: $oItem->set("subject", $oBaseItem->get("subject"));
110: $oItem->set("message", $oBaseItem->get("message"));
111: $oItem->set("newsfrom", $oBaseItem->get("newsfrom"));
112: $oItem->set("newsfromname", $oBaseItem->get("newsfromname"));
113: $oItem->set("newsdate", date("Y-m-d H:i:s"), false);
114: $oItem->set("use_cronjob", $oBaseItem->get("use_cronjob"));
115: $oItem->set("send_to", $oBaseItem->get("send_to"));
116: $oItem->set("send_ids", $oBaseItem->get("send_ids"));
117: $oItem->set("dispatch", $oBaseItem->get("dispatch"));
118: $oItem->set("dispatch_count", $oBaseItem->get("dispatch_count"));
119: $oItem->set("dispatch_delay", $oBaseItem->get("dispatch_delay"));
120: $oItem->set("author", $auth->auth["uid"]);
121: $oItem->set("created", date('Y-m-d H:i:s'), false);
122:
123:
124: if (!is_object($this->properties)) {
125: $this->properties = new cApiPropertyCollection();
126: }
127: $this->properties->setWhere("idclient", $client);
128: $this->properties->setWhere("itemtype", $this->primaryKey);
129: $this->properties->setWhere("itemid", $iItemID);
130: $this->properties->query();
131:
132: while ($oPropertyItem = $this->properties->next()) {
133: $oItem->setProperty($oPropertyItem->get("type"), $oPropertyItem->get("name"), $oPropertyItem->get("value"), $client);
134: }
135:
136: $oItem->store();
137:
138: return $oItem;
139: }
140: }
141:
142: 143: 144:
145: class Newsletter extends Item
146: {
147: 148: 149: 150:
151: protected $_sError;
152:
153: 154: 155: 156:
157: public function __construct($mId = false)
158: {
159: global $cfg;
160: parent::__construct($cfg["tab"]["news"], "idnews");
161: $this->_sError = "";
162: if ($mId !== false) {
163: $this->loadByPrimaryKey($mId);
164: }
165: }
166:
167: 168: 169: 170:
171: public function store()
172: {
173: global $client, $lang, $auth;
174:
175: $client = cSecurity::toInteger($client);
176: $lang = cSecurity::toInteger($lang);
177:
178: $this->set("modified", date('Y-m-d H:i:s'), false);
179: $this->set("modifiedby", $auth->auth["uid"]);
180:
181: if ($this->get("welcome") == 1) {
182: $oItems = new NewsletterCollection();
183: $oItems->setWhere("idclient", $client);
184: $oItems->setWhere("idlang", $lang);
185: $oItems->setWhere("welcome", 1);
186: $oItems->setWhere("idnews", $this->get("idnews"), "<>");
187: $oItems->query();
188:
189: while ($oItem = $oItems->next()) {
190: $oItem->set("welcome", 0);
191: $oItem->store();
192: }
193: unset($oItem);
194: unset($oItems);
195: }
196:
197: return parent::store();
198: }
199:
200: 201: 202: 203: 204: 205: 206: 207: 208:
209: public function _replaceTag(&$sCode, $bIsHTML, $sField, $sData)
210: {
211: if ($sCode && !$bIsHTML) {
212: $sCode = str_replace("MAIL_".strtoupper($sField), $sData, $sCode);
213: } else if ($sCode) {
214:
215: $sRegExp = '/\[mail\s*([^]]+)\s*name=(?:"|")'.$sField.'(?:"|")\s*(.*?)\s*\]((?:.|\s)+?)\[\/mail\]/i';
216: $aMatch = array();
217: $iMatches = preg_match($sRegExp, $sCode, $aMatch) ;
218:
219: if ($iMatches > 0) {
220:
221: $sParameter = $aMatch[1] . $aMatch[2];
222: $sMessage = $aMatch[3];
223: $sRegExp = '/\s*(.*?)\s*=\s*(?:"|")(.*?)(?:"|")\s*/i';
224: $aMatch = array();
225:
226: if (preg_match_all($sRegExp, $sParameter, $aMatch) > 0) {
227:
228: $aParameter = array_combine($aMatch[1], $aMatch[2]);
229: unset($aMatch);
230:
231: if (!array_key_exists("type", $aParameter)) {
232: $aParameter["type"] = "text";
233: }
234:
235: switch ($aParameter["type"]) {
236: case "link":
237:
238:
239:
240:
241:
242:
243:
244:
245: $sText = $aParameter["text"];
246:
247: if ($sText == "") {
248: $sText = $sData;
249: }
250: if ($sMessage == "") {
251: $sMessage = $sData;
252: }
253:
254:
255:
256: unset($aParameter["type"]);
257: unset($aParameter["text"]);
258:
259: $sParameter = "";
260: if (count($aParameter) > 0) {
261: foreach ($aParameter as $sKey => $sValue) {
262: $sParameter .= ' '.$sKey . '="' . $sValue . '"';
263: }
264: }
265: $sMessage = str_replace("MAIL_".strtoupper($sField), '<a href="'.conHtmlentities($sData).'"'.$sParameter.'>'.$sText.'</a>', $sMessage);
266:
267: break;
268: default:
269: $sMessage = str_replace("MAIL_".strtoupper($sField), $sData, $sMessage);
270:
271: }
272:
273: $sRegExp = '/\[mail[^]]+name=(?:"|")'.$sField.'(?:"|").*?\].*?\[\/mail\]/is';
274: $sCode = preg_replace($sRegExp, $sMessage, $sCode, -1);
275:
276: $sCode = str_replace("MAIL_".strtoupper($sField), $sData, $sCode);
277: }
278: }
279: }
280: }
281:
282:
283: protected function _getNewsletterTagData($sHTML, $sTag)
284: {
285:
286:
287:
288:
289:
290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309:
310:
311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321:
322:
323:
324:
325:
326:
327:
328: }
329:
330: protected function _deChunkHTTPBody($sHeader, $sBody, $sEOL = "\r\n")
331: {
332:
333:
334:
335:
336: $aParts = preg_split("/\r?\n/", $sHeader, -1, PREG_SPLIT_NO_EMPTY);
337:
338: $aHeader = array();
339: for ($i = 0;$i < sizeof ($aParts); $i++) {
340: if ($i != 0) {
341: $iPos = strpos($aParts[$i], ':');
342: $sParameter = strtolower (str_replace(' ', '', substr ($aParts[$i], 0, $iPos)));
343: $sValue = trim(substr($aParts[$i], ($iPos + 1)));
344: } else {
345: $sField = 'status';
346: $aParameters = explode(' ', $aParts[$i]);
347: $sParameter = $aParameters[1];
348: }
349:
350: if ($sParameter == 'set-cookie') {
351: $aHeader['cookies'][] = $sValue;
352: } else if ($sParameter == 'content-type') {
353: if (($iPos = strpos($sValue, ';')) !== false) {
354: $aHeader[$sParameter] = substr($sValue, 0, $iPos);
355: } else {
356: $aHeader[$sParameter] = $sValue;
357: }
358: } else {
359: $aHeader[$sParameter] = $sValue;
360: }
361: }
362:
363:
364: $iEOLLen = strlen($sEOL);
365:
366: $sBuffer = '';
367: if (isset($aHeader['transfer-encoding']) && $aHeader['transfer-encoding'] == 'chunked') {
368: do {
369: $sBody = ltrim ($sBody);
370: $iPos = strpos($sBody, $sEOL);
371: $iDataLen = hexdec (substr($sBody, 0, $iPos));
372:
373: if (isset($aHeader['content-encoding'])) {
374: $sBuffer .= gzinflate(substr($sBody, ($iPos + $iEOLLen + 10), $iDataLen));
375: } else {
376: $sBuffer .= substr($sBody, ($iPos + $iEOLLen), $iDataLen);
377: }
378:
379: $sBody = substr ($sBody, ($iPos + $iDataLen + $iEOLLen));
380: $sRemaining = trim ($sBody);
381:
382: } while (!empty($sRemaining));
383: } else if (isset($aHeader['content-encoding'])) {
384: $sBuffer = gzinflate(substr($sBody, 10));
385: } else {
386: $sBuffer = $sBody;
387: }
388:
389: return $sBuffer;
390: }
391:
392: 393: 394: 395: 396:
397: public function getHTMLMessage()
398: {
399: global $lang, $client, $contenido;
400: $frontendURL = cRegistry::getFrontendUrl();
401:
402: if ($this->get("type") == "html" && $this->get("idart") > 0 && $this->htmlArticleExists()) {
403:
404:
405: $iIDArt = $this->get("idart");
406:
407:
408: $oClientLang = new cApiClientLanguage(false, $client, $lang);
409: $iIDCat = $oClientLang->getProperty("newsletter", "html_newsletter_idcat");
410: unset($oClientLang);
411:
412:
413: $oClient = new cApiClient($client);
414: $sHTTPUserName = $oClient->getProperty("newsletter", "html_username");
415: $sHTTPPassword = $oClient->getProperty("newsletter", "html_password");
416: unset($oClient);
417:
418:
419: if ($iIDArt > 0 && $iIDCat > 0) {
420:
421: $bSetOffline = false;
422: $oArticles = new cApiArticleLanguageCollection;
423: $oArticles->setWhere("idlang", $this->get("idlang"));
424: $oArticles->setWhere("idart", $this->get("idart"));
425: $oArticles->query();
426:
427: if ($oArticle = $oArticles->next()) {
428: if ($oArticle->get("online") == 0) {
429: $bSetOffline = true;
430: $oArticle->set("online", 1);
431: $oArticle->store();
432: }
433: unset($oArticle);
434: }
435: unset($oArticles);
436:
437: $sFile = "front_content.php?client=$client&lang=$lang&idcat=$iIDCat&idart=$iIDArt&noex=1&send=1";
438: $aURL = parse_url($frontendURL);
439:
440:
441: if ($aURL["scheme"] == "https") {
442: $iPort = 443;
443: $sTarget = "ssl://".$aURL["host"];
444: } else {
445: $iPort = 80;
446: $sTarget = $aURL["host"];
447: }
448: if ($aURL["port"]) {
449: $iPort = $aURL["port"];
450: }
451:
452: $iErrorNo = 0;
453: $sErrorMsg = "";
454: if ($iHandler = fsockopen($sTarget, $iPort, $iErrorNo, $sErrorMsg, 30)) {
455:
456:
457:
458: fputs($iHandler, "GET ".$aURL["path"].$sFile." HTTP/1.1\r\n");
459: fputs($iHandler, "Host: ".$aURL["host"]."\r\n");
460:
461:
462: if ($sHTTPUserName != "" && $sHTTPPassword != "") {
463: fputs($iHandler, "Authorization: Basic " . base64_encode("$sHTTPUserName:$sHTTPPassword") . "\r\n");
464: }
465:
466: fputs($iHandler, "Referer: http://".$aURL["host"]."\r\n");
467: fputs($iHandler, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n");
468: fputs($iHandler, "Connection: close\r\n\r\n");
469:
470:
471: $sHTML = "";
472: $sHeader = "";
473: $bBody = false;
474: while (!feof($iHandler)) {
475:
476: $sLine = fgets($iHandler, 1024);
477: if ($bBody) {
478: $sHTML .= $sLine;
479: } else if ($sLine == "\r\n") {
480: $bBody = true;
481: } else {
482: $sHeader .= $sLine;
483: }
484: }
485: fclose ($iHandler);
486:
487: $sHTML = $this->_deChunkHTTPBody($sHeader, $sHTML);
488:
489:
490:
491:
492: if (getEffectiveSetting('newsletter', 'remove_base_tag', "false") == "true") {
493:
494: $sHTML = preg_replace('/<base href=(.*?)>/is', '', $sHTML, 1);
495:
496:
497:
498: $sHTML = preg_replace('/[sS[rR][cC][ ]*=[ ]*"([^h][^t][^t][^p][^:].*)"/', 'rc="'.$frontendURL.'$1"', $sHTML);
499: $sHTML = preg_replace('/[hH][rR][eE][fF][ ]*=[ ]*"([^h][^t][^t][^p][^:][A-Za-z0-9#\.?\-=_&]*)"/', 'href="'.$frontendURL.'$1"', $sHTML);
500: $sHTML = preg_replace('/url\((.*)\)/', 'url('. $frontendURL.'$1)', $sHTML);
501:
502:
503: $sHTML = str_replace($frontendURL . "front_content.php?idart=".$iIDArt."#", "#", $sHTML);
504: }
505:
506: $sReturn = $sHTML;
507: } else {
508: if ($contenido) {
509: $sErrorText = i18n("There was a problem getting the newsletter article using http. Error: %s (%s)");
510: } else {
511: $sErrorText = "There was a problem getting the newsletter article using http. Error: %s (%s)";
512: }
513:
514: $this->_sError = sprintf($sErrorText, $sErrorMsg, $iErrorNo);
515: $sReturn = false;
516: }
517:
518:
519: if ($bSetOffline) {
520: $oArticles = new cApiArticleLanguageCollection();
521: $oArticles->setWhere("idlang", $this->get("idlang"));
522: $oArticles->setWhere("idart", $this->get("idart"));
523: $oArticles->query();
524:
525: if ($oArticle = $oArticles->next()) {
526: $oArticle->set("online", 0);
527: $oArticle->store();
528: }
529: unset($oArticle);
530: unset($oArticles);
531: }
532:
533: return $sReturn;
534: } else {
535: return false;
536: }
537: } else {
538: return false;
539: }
540: }
541:
542: 543: 544: 545:
546: public function htmlArticleExists()
547: {
548: if ($this->get("idart") > 0) {
549: $oArticles = new cApiArticleLanguageCollection();
550: $oArticles->setWhere("idlang", $this->get("idlang"));
551: $oArticles->setWhere("idart", $this->get("idart"));
552: $oArticles->query();
553:
554: if ($oArticles->count() > 0) {
555: $bReturn = true;
556: } else {
557: $bReturn = false;
558: }
559:
560: unset($oArticles);
561: } else {
562: $bReturn = false;
563: }
564:
565: return $bReturn;
566: }
567:
568: 569: 570: 571: 572: 573: 574: 575: 576:
577: public function sendEMail($iIDCatArt, $sEMail, $sName = "", $bSimulatePlugins = true, $sEncoding = "iso-8859-1")
578: {
579: global $lang, $client, $cfg, $cfgClient, $contenido;
580:
581:
582: if ($sName == "") {
583: $sName = $sEMail;
584: }
585:
586: $oLanguage = new cApiLanguage($lang);
587: $sFormatDate = $oLanguage->getProperty("dateformat", "date");
588: $sFormatTime = $oLanguage->getProperty("dateformat", "time");
589: unset($oLanguage);
590:
591: if ($sFormatDate == "") {
592: $sFormatDate = "%d.%m.%Y";
593: }
594: if ($sFormatTime == "") {
595: $sFormatTime = "%H:%M";
596: }
597:
598:
599: $sFrom = $this->get("newsfrom");
600: $sFromName = $this->get("newsfromname");
601: if ($sFromName == "") {
602: $sFromName = $sFrom;
603: }
604: $sSubject = $this->get("subject");
605: $sMessageText = $this->get("message");
606:
607: $bIsHTML = false;
608: if ($this->get("type") == "html") {
609: $sMessageHTML = $this->getHTMLMessage();
610:
611: if ($sMessageHTML === false) {
612:
613:
614:
615: if ($contenido) {
616: $sError = i18n("Newsletter to %s could not be sent: No html message available");
617: } else {
618: $sError = "Newsletter to %s could not be sent: No html message available";
619: }
620: $this->_sError = $sName." (".$sEMail."): ".sprintf($sError, $sEMail);
621: return false;
622: } else {
623: $bIsHTML = true;
624: }
625: }
626:
627:
628: if (!getSystemProperty("newsletter", "disable-rn-replacement")) {
629: $sMessageText = str_replace("\r\n", "\n", $sMessageText);
630: }
631:
632:
633: $sKey = str_repeat("key", 10);
634: $sPath = cRegistry::getFrontendUrl() . "front_content.php?changelang=".$lang."&idcatart=".$iIDCatArt."&";
635:
636:
637: $this->_replaceTag($sMessageText, false, "name", $sName);
638: $this->_replaceTag($sMessageText, false, "number", 1);
639: $this->_replaceTag($sMessageText, false, "date", strftime($sFormatDate));
640: $this->_replaceTag($sMessageText, false, "time", strftime($sFormatTime));
641: $this->_replaceTag($sMessageText, false, "unsubscribe", $sPath."unsubscribe=".$sKey);
642: $this->_replaceTag($sMessageText, false, "change", $sPath."change=".$sKey);
643: $this->_replaceTag($sMessageText, false, "stop", $sPath."stop=".$sKey);
644: $this->_replaceTag($sMessageText, false, "goon", $sPath."goon=".$sKey);
645:
646:
647: if ($bIsHTML) {
648: $this->_replaceTag($sMessageHTML, true, "name", $sName);
649: $this->_replaceTag($sMessageHTML, true, "number", 1);
650: $this->_replaceTag($sMessageHTML, true, "date", strftime($sFormatDate));
651: $this->_replaceTag($sMessageHTML, true, "time", strftime($sFormatTime));
652: $this->_replaceTag($sMessageHTML, true, "unsubscribe", $sPath."unsubscribe=".$sKey);
653: $this->_replaceTag($sMessageHTML, true, "change", $sPath."change=".$sKey);
654: $this->_replaceTag($sMessageHTML, true, "stop", $sPath."stop=".$sKey);
655: $this->_replaceTag($sMessageHTML, true, "goon", $sPath."goon=".$sKey);
656: }
657:
658: if ($bSimulatePlugins) {
659:
660: if (getSystemProperty("newsletter", "newsletter-recipients-plugin") == "true") {
661: if (is_array($cfg['plugins']['recipients'])) {
662: foreach ($cfg['plugins']['recipients'] as $sPlugin) {
663: plugin_include("recipients", $sPlugin."/".$sPlugin.".php");
664: if (function_exists("recipients_".$sPlugin."_wantedVariables")) {
665: $aPluginVars = array();
666: $aPluginVars = call_user_func("recipients_".$sPlugin."_wantedVariables");
667:
668: foreach ($aPluginVars as $sPluginVar) {
669:
670: $this->_replaceTag($sMessageText, false, $sPluginVar, ":: ".$sPlugin.": ".$sPluginVar." ::");
671:
672: if ($bIsHTML) {
673: $this->_replaceTag($sMessageHTML, true, $sPluginVar, ":: ".$sPlugin.": ".$sPluginVar." ::");
674: }
675: }
676: }
677: }
678: }
679: } else {
680: setSystemProperty("newsletter", "newsletter-recipients-plugin", "false");
681: }
682: }
683:
684: if (!isValidMail($sEMail) || strtolower($sEMail) == "sysadmin@ihresite.de") {
685:
686: if ($contenido) {
687: $sError = i18n("Newsletter to %s could not be sent: No valid e-mail address");
688: } else {
689: $sError = "Newsletter to %s could not be sent: No valid e-mail address";
690: }
691: $this->_sError = $sName." (".$sEMail."): ".sprintf($sError, $sEMail);
692: return false;
693: } else {
694: if ($bIsHTML) {
695: $body = $sMessageHTML;
696: } else {
697: $body = $sMessageText."\n\n";
698: }
699: if ($bIsHTML) {
700: $contentType = 'text/html';
701: } else {
702: $contentType = 'text/plain';
703: }
704:
705: $mailer = new cMailer();
706: $message = Swift_Message::newInstance($sSubject, $body, $contentType, $sEncoding);
707: $message->setFrom($sFrom, $sFromName);
708: $message->setTo($sEMail);
709: $result = $mailer->send($message);
710:
711: if (!$result) {
712: if ($contenido) {
713: $sError = i18n("Newsletter to %s could not be sent");
714: } else {
715: $sError = "Newsletter to %s could not be sent";
716: }
717: $this->_sError = $sName." (".$sEMail."): ".sprintf($sError, $sEMail);
718: return false;
719: } else {
720: return true;
721: }
722: }
723: }
724:
725: 726: 727: 728: 729: 730: 731: 732: 733: 734: 735: 736:
737: public function sendDirect($iIDCatArt, $iIDNewsRcp = false, $iIDNewsGroup = false, &$aSendRcps, $sEncoding = "iso-8859-1")
738: {
739: global $lang, $client, $cfg, $cfgClient, $contenido, $recipient;
740:
741:
742: $aMessages = array();
743:
744: $oLanguage = new cApiLanguage($lang);
745: unset($oLanguage);
746:
747: if ($sFormatDate == "") {
748: $sFormatDate = "%d.%m.%Y";
749: }
750: if ($sFormatTime == "") {
751: $sFormatTime = "%H:%M";
752: }
753:
754: $sPath = cRegistry::getFrontendUrl() . "front_content.php?changelang=".$lang."&idcatart=".$iIDCatArt."&";
755:
756:
757: $sFrom = $this->get("newsfrom");
758: $sFromName = $this->get("newsfromname");
759: if ($sFromName == "") {
760: $sFromName = $sFrom;
761: }
762: $sSubject = $this->get("subject");
763: $sMessageText = $this->get("message");
764:
765: $bIsHTML = false;
766: if ($this->get("type") == "html") {
767: $sMessageHTML = $this->getHTMLMessage();
768:
769: if ($sMessageHTML === false) {
770:
771:
772:
773: if ($contenido) {
774: $sError = i18n("Newsletter could not be sent: No html message available");
775: } else {
776: $sError = "Newsletter could not be sent: No html message available";
777: }
778: $this->_sError = $sError;
779: return false;
780: } else {
781: $bIsHTML = true;
782: }
783: }
784:
785:
786: if (!getSystemProperty("newsletter", "disable-rn-replacement")) {
787: $sMessageText = str_replace("\r\n", "\n", $sMessageText);
788: }
789:
790:
791:
792: $this->_replaceTag($sMessageText, false, "date", strftime($sFormatDate));
793: $this->_replaceTag($sMessageText, false, "time", strftime($sFormatTime));
794:
795:
796: if ($bIsHTML) {
797: $this->_replaceTag($sMessageHTML, true, "date", strftime($sFormatDate));
798: $this->_replaceTag($sMessageHTML, true, "time", strftime($sFormatTime));
799: }
800:
801:
802: if (getSystemProperty("newsletter", "newsletter-recipients-plugin") == "true") {
803: $bPluginEnabled = true;
804: $aPlugins = array();
805:
806: if (is_array($cfg['plugins']['recipients'])) {
807: foreach ($cfg['plugins']['recipients'] as $sPlugin) {
808: plugin_include("recipients", $sPlugin."/".$sPlugin.".php");
809: if (function_exists("recipients_".$sPlugin."_wantedVariables")) {
810: $aPlugins[$sPlugin] = call_user_func("recipients_".$sPlugin."_wantedVariables");
811: }
812: }
813: }
814: } else {
815: setSystemProperty("newsletter", "newsletter-recipients-plugin", "false");
816: $bPluginEnabled = false;
817: }
818:
819: $aRecipients = array();
820: if ($iIDNewsGroup !== false) {
821: $oGroupMembers = new RecipientGroupMemberCollection;
822: $aRecipients = $oGroupMembers->getRecipientsInGroup ($iIDNewsGroup, false);
823: } else if ($iIDNewsRcp !== false) {
824: $aRecipients[] = $iIDNewsRcp;
825: }
826:
827: $iCount = count($aRecipients);
828: if ($iCount > 0) {
829: $this->_replaceTag($sMessageText, false, "number", $iCount);
830:
831:
832: if ($bIsHTML) {
833: $this->_replaceTag($sMessageHTML, true, "number", $iCount);
834: }
835:
836: foreach ($aRecipients as $iID) {
837: $sRcpMsgText = $sMessageText;
838: $sRcpMsgHTML = $sMessageHTML;
839:
840:
841: $recipient = new Recipient;
842: $recipient->loadByPrimaryKey($iID);
843:
844: $sEMail = $recipient->get("email");
845: $sName = $recipient->get("name");
846: if (empty ($sName)) {
847: $sName = $sEMail;
848: }
849: $sKey = $recipient->get("hash");
850:
851: $bSendHTML = false;
852: if ($recipient->get("news_type") == 1) {
853: $bSendHTML = true;
854: }
855:
856: $this->_replaceTag($sRcpMsgText, false, "name", $sName);
857: $this->_replaceTag($sRcpMsgText, false, "unsubscribe", $sPath."unsubscribe=".$sKey);
858: $this->_replaceTag($sRcpMsgText, false, "change", $sPath."change=".$sKey);
859: $this->_replaceTag($sRcpMsgText, false, "stop", $sPath."stop=".$sKey);
860: $this->_replaceTag($sRcpMsgText, false, "goon", $sPath."goon=".$sKey);
861:
862:
863: if ($bIsHTML && $bSendHTML) {
864: $this->_replaceTag($sRcpMsgHTML, true, "name", $sName);
865: $this->_replaceTag($sRcpMsgHTML, true, "unsubscribe", $sPath."unsubscribe=".$sKey);
866: $this->_replaceTag($sRcpMsgHTML, true, "change", $sPath."change=".$sKey);
867: $this->_replaceTag($sRcpMsgHTML, true, "stop", $sPath."stop=".$sKey);
868: $this->_replaceTag($sRcpMsgHTML, true, "goon", $sPath."goon=".$sKey);
869: }
870:
871: if ($bPluginEnabled) {
872: foreach ($aPlugins as $sPlugin => $aPluginVar) {
873: foreach ($aPluginVar as $sPluginVar) {
874:
875: $this->_replaceTag($sRcpMsgText, false, $sPluginVar, call_user_func("recipients_".$sPlugin."_getvalue", $sPluginVar));
876:
877: if ($bIsHTML && $bSendHTML) {
878: $this->_replaceTag($sRcpMsgHTML, true, $sPluginVar, call_user_func("recipients_".$sPlugin."_getvalue", $sPluginVar));
879: }
880: }
881: }
882: }
883:
884: if (strlen($sKey) != 30) {
885: if ($contenido) {
886: $sError = i18n("Newsletter to %s could not be sent: Recipient has an incompatible or empty key");
887: } else {
888: $sError = "Newsletter to %s could not be sent: Recipient has an incompatible or empty key";
889: }
890: $aMessages[] = $sName." (".$sEMail."): ".sprintf($sError, $sEMail);
891: } else if (!isValidMail($sEMail)) {
892: if ($contenido) {
893: $sError = i18n("Newsletter to %s could not be sent: No valid e-mail address specified");
894: } else {
895: $sError = "Newsletter to %s could not be sent: No valid e-mail address specified";
896: }
897: $aMessages[] = $sName." (".$sEMail."): ".sprintf($sError, $sEMail);
898: } else {
899: if ($bIsHTML && $bSendHTML) {
900: $body = $sRcpMsgHTML;
901: } else {
902: $body = $sRcpMsgText."\n\n";
903: }
904:
905: if ($bIsHTML && $bSendHTML) {
906: $contentType = 'text/html';
907: } else {
908: $contentType = 'text/plain';
909: }
910:
911: $mailer = new cMailer();
912: $message = Swift_Message::newInstance($sSubject, $body, $contentType, $sEncoding);
913: $message->setFrom($sFrom, $sFromName);
914: $message->setTo($sEMail);
915: $result = $mailer->send($message);
916:
917: if ($result) {
918: $aSendRcps[] = $sName." (".$sEMail.")";
919: } else {
920: if ($contenido) {
921: $sError = i18n("Newsletter to %s could not be sent");
922: } else {
923: $sError = "Newsletter to %s could not be sent";
924: }
925: $aMessages[] = $sName." (".$sEMail."): ".sprintf($sError, $sEMail);
926: }
927: }
928: }
929: } else {
930: if ($contenido) {
931: $sError = i18n("No recipient with specified recipient/group id %s/%s found");
932: } else {
933: $sError = "No recipient with specified recpient/group id %s/%s found";
934: }
935: $aMessages[] = sprintf($sError, $iIDNewsRcp, $iIDNewsGroup);
936: }
937:
938: if (count($aMessages) > 0) {
939: $this->_sError = implode("<br>", $aMessages);
940: return false;
941: } else {
942: return true;
943: }
944: }
945: }
946: