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 NewsletterJobCollection extends ItemCollection {
25:
26: 27: 28: 29: 30:
31: public function __construct() {
32: global $cfg;
33: parent::__construct($cfg["tab"]["news_jobs"], "idnewsjob");
34: $this->_setItemClass("NewsletterJob");
35: }
36:
37: 38: 39: 40: 41: 42: 43:
44: public function create($iIDNews, $iIDCatArt, $sName = "") {
45: global $client, $lang, $cfg, $cfgClient, $auth;
46:
47: $oNewsletter = new Newsletter();
48: if ($oNewsletter->loadByPrimaryKey($iIDNews)) {
49: $iIDNews = cSecurity::toInteger($iIDNews);
50: $iIDCatArt = cSecurity::toInteger($iIDCatArt);
51: $lang = cSecurity::toInteger($lang);
52: $client = cSecurity::toInteger($client);
53: $sName = $this->escape($sName);
54:
55: $oItem = $this->createNewItem();
56:
57: $oItem->set("idnews", $iIDNews);
58: $oItem->set("idclient", $client);
59: $oItem->set("idlang", $lang);
60:
61: if ($sName == "") {
62: $oItem->set("name", $oNewsletter->get("name"));
63: } else {
64: $oItem->set("name", $sName);
65: }
66: $oItem->set("type", $oNewsletter->get("type"));
67: $oItem->set("use_cronjob", $oNewsletter->get("use_cronjob"));
68:
69: $oLang = new cApiLanguage($lang);
70: $oItem->set("encoding", $oLang->get("encoding"));
71: unset($oLang);
72:
73: $oItem->set("idart", $oNewsletter->get("idart"));
74: $oItem->set("subject", $oNewsletter->get("subject"));
75:
76:
77: $sPath = cRegistry::getFrontendUrl() . "front_content.php?changelang=" . $lang . "&idcatart=" . $iIDCatArt . "&";
78:
79: $sMessageText = $oNewsletter->get("message");
80:
81:
82:
83: if (!getSystemProperty("newsletter", "disable-rn-replacement")) {
84: $sMessageText = str_replace("\r\n", "\n", $sMessageText);
85: }
86:
87: $oNewsletter->_replaceTag($sMessageText, false, "unsubscribe", $sPath . "unsubscribe={KEY}");
88: $oNewsletter->_replaceTag($sMessageText, false, "change", $sPath . "change={KEY}");
89: $oNewsletter->_replaceTag($sMessageText, false, "stop", $sPath . "stop={KEY}");
90: $oNewsletter->_replaceTag($sMessageText, false, "goon", $sPath . "goon={KEY}");
91:
92: $oItem->set("message_text", $sMessageText);
93:
94: if ($oNewsletter->get("type") == "text") {
95:
96: $sMessageHTML = "";
97: } else {
98:
99: $sMessageHTML = $oNewsletter->getHTMLMessage();
100:
101: if ($sMessageHTML) {
102: $oNewsletter->_replaceTag($sMessageHTML, true, "name", "MAIL_NAME");
103: $oNewsletter->_replaceTag($sMessageHTML, true, "number", "MAIL_NUMBER");
104: $oNewsletter->_replaceTag($sMessageHTML, true, "date", "MAIL_DATE");
105: $oNewsletter->_replaceTag($sMessageHTML, true, "time", "MAIL_TIME");
106:
107: $oNewsletter->_replaceTag($sMessageHTML, true, "unsubscribe", $sPath . "unsubscribe={KEY}");
108: $oNewsletter->_replaceTag($sMessageHTML, true, "change", $sPath . "change={KEY}");
109: $oNewsletter->_replaceTag($sMessageHTML, true, "stop", $sPath . "stop={KEY}");
110: $oNewsletter->_replaceTag($sMessageHTML, true, "goon", $sPath . "goon={KEY}");
111:
112:
113: if (getSystemProperty("newsletter", "newsletter-recipients-plugin") == "true") {
114: if (is_array($cfg['plugins']['recipients'])) {
115: foreach ($cfg['plugins']['recipients'] as $sPlugin) {
116: plugin_include("recipients", $sPlugin . "/" . $sPlugin . ".php");
117: if (function_exists("recipients_" . $sPlugin . "_wantedVariables")) {
118: $aPluginVars = array();
119: $aPluginVars = call_user_func("recipients_" . $sPlugin . "_wantedVariables");
120:
121: foreach ($aPluginVars as $sPluginVar) {
122: $oNewsletter->_replaceTag($sMessageHTML, true, $sPluginVar, "MAIL_" . strtoupper($sPluginVar));
123: }
124: }
125: }
126: }
127: }
128: } else {
129:
130:
131:
132: return false;
133: }
134: }
135:
136: $oItem->set("message_html", $sMessageHTML);
137:
138: $oItem->set("newsfrom", $oNewsletter->get("newsfrom"));
139: if ($oNewsletter->get("newsfromname") == "") {
140: $oItem->set("newsfromname", $oNewsletter->get("newsfrom"));
141: } else {
142: $oItem->set("newsfromname", $oNewsletter->get("newsfromname"));
143: }
144: $oItem->set("newsdate", date("Y-m-d H:i:s"), false);
145: $oItem->set("dispatch", $oNewsletter->get("dispatch"));
146: $oItem->set("dispatch_count", $oNewsletter->get("dispatch_count"));
147: $oItem->set("dispatch_delay", $oNewsletter->get("dispatch_delay"));
148:
149:
150: $aSendInfo = array();
151: $aSendInfo[] = $oNewsletter->get("send_to");
152:
153: switch ($oNewsletter->get("send_to")) {
154: case "selection":
155: $oGroups = new NewsletterRecipientGroupCollection();
156: $oGroups->setWhere("idnewsgroup", unserialize($oNewsletter->get("send_ids")), "IN");
157: $oGroups->setOrder("groupname");
158: $oGroups->query();
159:
160:
161:
162:
163: while (($oGroup = $oGroups->next()) !== false) {
164: $aSendInfo[] = $oGroup->get("groupname");
165: }
166:
167: unset($oGroup);
168: unset($oGroups);
169: break;
170: case "single":
171: if (is_numeric($oNewsletter->get("send_ids"))) {
172: $oRcp = new NewsletterRecipient($oNewsletter->get("send_ids"));
173:
174: if ($oRcp->get("name") == "") {
175: $aSendInfo[] = $oRcp->get("email");
176: } else {
177: $aSendInfo[] = $oRcp->get("name");
178: }
179: $aSendInfo[] = $oRcp->get("email");
180:
181: unset($oRcp);
182: }
183: break;
184: default:
185: }
186: $oItem->set("send_to", serialize($aSendInfo), false);
187:
188: $oItem->set("created", date('Y-m-d H:i:s'), false);
189: $oItem->set("author", $auth->auth["uid"]);
190: $oItem->set("authorname", $auth->auth["uname"]);
191: unset($oNewsletter);
192:
193:
194: $oLogs = new NewsletterLogCollection();
195: $iRecipientCount = $oLogs->initializeJob($oItem->get($oItem->getPrimaryKeyName()), $iIDNews);
196: unset($oLogs);
197:
198: $oItem->set("rcpcount", $iRecipientCount);
199: $oItem->set("sendcount", 0);
200: $oItem->set("status", 1);
201:
202:
203:
204: $oItem->store();
205:
206: return $oItem;
207: } else {
208: return false;
209: }
210: }
211:
212: 213: 214: 215: 216: 217: 218:
219: public function delete($iItemID) {
220: $oLogs = new NewsletterLogCollection();
221: $oLogs->delete($iItemID);
222:
223: parent::delete($iItemID);
224: }
225:
226: }
227:
228: 229: 230:
231: class NewsletterJob extends Item {
232:
233: 234: 235: 236: 237:
238: public function __construct($mId = false) {
239: global $cfg;
240: parent::__construct($cfg["tab"]["news_jobs"], "idnewsjob");
241: if ($mId !== false) {
242: $this->loadByPrimaryKey($mId);
243: }
244: }
245:
246: public function runJob() {
247: global $cfg, $recipient;
248:
249: $iCount = 0;
250: if ($this->get("status") == 2) {
251:
252:
253: $dStart = strtotime($this->get("started"));
254: $dNow = time();
255:
256: if (($dNow - $dStart) > (5 * 60)) {
257: $this->set("status", 1);
258: $this->set("started", "0000-00-00 00:00:00", false);
259:
260: $oLogs = new NewsletterLogCollection();
261: $oLogs->setWhere("idnewsjob", $this->get($this->getPrimaryKeyName()));
262: $oLogs->setWhere("status", "sending");
263: $oLogs->query();
264:
265: while (($oLog = $oLogs->next()) !== false) {
266: $oLog->set("status", "error (sending)");
267: $oLog->store();
268: }
269: }
270: }
271:
272: if ($this->get("status") == 1) {
273:
274: $this->set("status", 2);
275: $this->set("started", date("Y-m-d H:i:s"), false);
276: $this->store();
277:
278:
279: $aMessages = array();
280:
281: $oLanguage = new cApiLanguage($this->get("idlang"));
282: $sFormatDate = $oLanguage->getProperty("dateformat", "date");
283: $sFormatTime = $oLanguage->getProperty("dateformat", "time");
284: unset($oLanguage);
285:
286: if ($sFormatDate == "") {
287: $sFormatDate = "%d.%m.%Y";
288: }
289: if ($sFormatTime == "") {
290: $sFormatTime = "%H:%M";
291: }
292:
293:
294: $sFrom = $this->get("newsfrom");
295: $sFromName = $this->get("newsfromname");
296: $sSubject = $this->get("subject");
297: $sMessageText = $this->get("message_text");
298: $sMessageHTML = $this->get("message_html");
299: $dNewsDate = strtotime($this->get("newsdate"));
300: $sEncoding = $this->get("encoding");
301: $bIsHTML = false;
302: if ($this->get("type") == "html" && $sMessageHTML != "") {
303: $bIsHTML = true;
304: }
305:
306: $bDispatch = false;
307: if ($this->get("dispatch") == 1) {
308: $bDispatch = true;
309: }
310:
311:
312:
313: $sMessageText = str_replace("MAIL_DATE", strftime($sFormatDate, $dNewsDate), $sMessageText);
314: $sMessageText = str_replace("MAIL_TIME", strftime($sFormatTime, $dNewsDate), $sMessageText);
315: $sMessageText = str_replace("MAIL_NUMBER", $this->get("rcpcount"), $sMessageText);
316:
317:
318: if ($bIsHTML) {
319: $sMessageHTML = str_replace("MAIL_DATE", strftime($sFormatDate, $dNewsDate), $sMessageHTML);
320: $sMessageHTML = str_replace("MAIL_TIME", strftime($sFormatTime, $dNewsDate), $sMessageHTML);
321: $sMessageHTML = str_replace("MAIL_NUMBER", $this->get("rcpcount"), $sMessageHTML);
322: }
323:
324:
325: $bPluginEnabled = false;
326: if (getSystemProperty("newsletter", "newsletter-recipients-plugin") == "true") {
327: $bPluginEnabled = true;
328: $aPlugins = array();
329:
330: if (is_array($cfg['plugins']['recipients'])) {
331: foreach ($cfg['plugins']['recipients'] as $sPlugin) {
332: plugin_include("recipients", $sPlugin . "/" . $sPlugin . ".php");
333: if (function_exists("recipients_" . $sPlugin . "_wantedVariables")) {
334: $aPlugins[$sPlugin] = call_user_func("recipients_" . $sPlugin . "_wantedVariables");
335: }
336: }
337: }
338: }
339:
340:
341: if (!is_object($oLogs)) {
342: $oLogs = new NewsletterLogCollection();
343: } else {
344: $oLogs->resetQuery();
345: }
346: $oLogs->setWhere("idnewsjob", $this->get($this->getPrimaryKeyName()));
347: $oLogs->setWhere("status", "pending");
348:
349: if ($bDispatch) {
350: $oLogs->setLimit(0, $this->get("dispatch_count"));
351: }
352:
353: $oLogs->query();
354: while (($oLog = $oLogs->next()) !== false) {
355: $iCount++;
356: $oLog->set("status", "sending");
357: $oLog->store();
358:
359: $sRcpMsgText = $sMessageText;
360: $sRcpMsgHTML = $sMessageHTML;
361:
362: $sKey = $oLog->get("rcphash");
363: $sEMail = $oLog->get("rcpemail");
364:
365:
366: if (false === isValidMail($sEMail)) {
367: $oLog->set("status", "error (invalid email)");
368: $oLog->store();
369: continue;
370: }
371:
372: $bSendHTML = false;
373: if ($oLog->get("rcpnewstype") == 1) {
374: $bSendHTML = true;
375: }
376:
377: if (strlen($sKey) == 30) {
378:
379: $sRcpMsgText = str_replace("{KEY}", $sKey, $sRcpMsgText);
380: $sRcpMsgText = str_replace("MAIL_MAIL", $sEMail, $sRcpMsgText);
381: $sRcpMsgText = str_replace("MAIL_NAME", $oLog->get("rcpname"), $sRcpMsgText);
382:
383:
384: if ($bIsHTML && $bSendHTML) {
385: $sRcpMsgHTML = str_replace("{KEY}", $sKey, $sRcpMsgHTML);
386: $sRcpMsgHTML = str_replace("MAIL_MAIL", $sEMail, $sRcpMsgHTML);
387: $sRcpMsgHTML = str_replace("MAIL_NAME", $oLog->get("rcpname"), $sRcpMsgHTML);
388: }
389:
390: if ($bPluginEnabled) {
391:
392:
393: $recipient = new NewsletterRecipient();
394: $recipient->loadByPrimaryKey($oLog->get("idnewsrcp"));
395:
396: foreach ($aPlugins as $sPlugin => $aPluginVar) {
397: foreach ($aPluginVar as $sPluginVar) {
398:
399: $sRcpMsgText = str_replace("MAIL_" . strtoupper($sPluginVar), call_user_func("recipients_" . $sPlugin . "_getvalue", $sPluginVar), $sRcpMsgText);
400:
401:
402: if ($bIsHTML && $bSendHTML) {
403: $sRcpMsgHTML = str_replace("MAIL_" . strtoupper($sPluginVar), call_user_func("recipients_" . $sPlugin . "_getvalue", $sPluginVar), $sRcpMsgHTML);
404: }
405: }
406: }
407: unset($recipient);
408: }
409:
410: $mailer = new cMailer();
411: $mailer->setCharset($sEncoding);
412:
413: $to = $sEMail;
414: if ($bIsHTML && $bSendHTML) {
415: $body = $sRcpMsgHTML;
416: } else {
417: $body = $sRcpMsgText . "\n\n";
418: }
419: $contentType = 'text/plain';
420: if ($bIsHTML && $bSendHTML) {
421: $contentType = 'text/html';
422: }
423:
424:
425: try {
426:
427: $message = Swift_Message::newInstance($sSubject, $body, $contentType, $sEncoding);
428: $message->setFrom($sFrom, $sFromName);
429: $message->setTo($to);
430:
431:
432: $result = $mailer->send($message);
433: } catch (Exception $e) {
434: $result = false;
435: }
436:
437: if ($result) {
438: $oLog->set("status", "successful");
439: $oLog->set("sent", date("Y-m-d H:i:s"), false);
440: } else {
441: $oLog->set("status", "error (sending)");
442: }
443: } else {
444: $oLog->set("status", "error (key)");
445: }
446: $oLog->store();
447: }
448:
449: $this->set("sendcount", $this->get("sendcount") + $iCount);
450:
451: if ($iCount == 0 || !$bDispatch) {
452:
453: $this->set("status", 9);
454: $this->set("finished", date("Y-m-d H:i:s"), false);
455: } else if ($bDispatch) {
456:
457: $oLogs->resetQuery();
458: $oLogs->setWhere("idnewsjob", $this->get($this->getPrimaryKeyName()));
459: $oLogs->setWhere("status", "pending");
460: $oLogs->setLimit(0, $this->get("dispatch_count"));
461: $oLogs->query();
462:
463: If ($oLogs->next()) {
464:
465: $this->set("status", 1);
466: $this->set("started", "0000-00-00 00:00:00", false);
467: } else {
468:
469: $this->set("status", 9);
470: $this->set("finished", date("Y-m-d H:i:s"), false);
471: }
472: } else {
473:
474: $this->set("status", 1);
475: $this->set("started", "0000-00-00 00:00:00", false);
476: }
477: $this->store();
478: }
479:
480: return $iCount;
481: }
482:
483: 484: 485:
486: public function store() {
487: if ($this->get("rcpcount") == 0) {
488:
489: $this->set("status", 9);
490: if ($this->get("started") == "0000-00-00 00:00:00") {
491: $this->set("started", date("Y-m-d H:i:s"), false);
492: }
493: $this->set("finished", date("Y-m-d H:i:s"), false);
494: }
495:
496: return parent::store();
497: }
498:
499: }
500: