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 = parent::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->primaryKey), $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->primaryKey));
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->primaryKey));
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: $bSendHTML = false;
365: if ($oLog->get("rcpnewstype") == 1) {
366: $bSendHTML = true;
367: }
368:
369: if (strlen($sKey) == 30) {
370:
371: $sRcpMsgText = str_replace("{KEY}", $sKey, $sRcpMsgText);
372: $sRcpMsgText = str_replace("MAIL_MAIL", $sEMail, $sRcpMsgText);
373: $sRcpMsgText = str_replace("MAIL_NAME", $oLog->get("rcpname"), $sRcpMsgText);
374:
375:
376: if ($bIsHTML && $bSendHTML) {
377: $sRcpMsgHTML = str_replace("{KEY}", $sKey, $sRcpMsgHTML);
378: $sRcpMsgHTML = str_replace("MAIL_MAIL", $sEMail, $sRcpMsgHTML);
379: $sRcpMsgHTML = str_replace("MAIL_NAME", $oLog->get("rcpname"), $sRcpMsgHTML);
380: }
381:
382: if ($bPluginEnabled) {
383:
384:
385: $recipient = new NewsletterRecipient();
386: $recipient->loadByPrimaryKey($oLog->get("idnewsrcp"));
387:
388: foreach ($aPlugins as $sPlugin => $aPluginVar) {
389: foreach ($aPluginVar as $sPluginVar) {
390:
391: $sRcpMsgText = str_replace("MAIL_" . strtoupper($sPluginVar), call_user_func("recipients_" . $sPlugin . "_getvalue", $sPluginVar), $sRcpMsgText);
392:
393:
394: if ($bIsHTML && $bSendHTML) {
395: $sRcpMsgHTML = str_replace("MAIL_" . strtoupper($sPluginVar), call_user_func("recipients_" . $sPlugin . "_getvalue", $sPluginVar), $sRcpMsgHTML);
396: }
397: }
398: }
399: unset($recipient);
400: }
401:
402: $mailer = new cMailer();
403: $mailer->setCharset($sEncoding);
404:
405: $to = $sEMail;
406: if ($bIsHTML && $bSendHTML) {
407: $body = $sRcpMsgHTML;
408: } else {
409: $body = $sRcpMsgText . "\n\n";
410: }
411: $contentType = 'text/plain';
412: if ($bIsHTML && $bSendHTML) {
413: $contentType = 'text/html';
414: }
415:
416: $message = Swift_Message::newInstance($sSubject, $body, $contentType, $sEncoding);
417: $message->setFrom($sFrom, $sFromName);
418: $message->setTo($to);
419: $result = $mailer->send($message);
420:
421: if ($result) {
422: $oLog->set("status", "successful");
423: $oLog->set("sent", date("Y-m-d H:i:s"), false);
424: } else {
425: $oLog->set("status", "error (sending)");
426: }
427: } else {
428: $oLog->set("status", "error (key)");
429: }
430: $oLog->store();
431: }
432:
433: $this->set("sendcount", $this->get("sendcount") + $iCount);
434:
435: if ($iCount == 0 || !$bDispatch) {
436:
437: $this->set("status", 9);
438: $this->set("finished", date("Y-m-d H:i:s"), false);
439: } else if ($bDispatch) {
440:
441: $oLogs->resetQuery();
442: $oLogs->setWhere("idnewsjob", $this->get($this->primaryKey));
443: $oLogs->setWhere("status", "pending");
444: $oLogs->setLimit(0, $this->get("dispatch_count"));
445: $oLogs->query();
446:
447: If ($oLogs->next()) {
448:
449: $this->set("status", 1);
450: $this->set("started", "0000-00-00 00:00:00", false);
451: } else {
452:
453: $this->set("status", 9);
454: $this->set("finished", date("Y-m-d H:i:s"), false);
455: }
456: } else {
457:
458: $this->set("status", 1);
459: $this->set("started", "0000-00-00 00:00:00", false);
460: }
461: $this->store();
462: }
463:
464: return $iCount;
465: }
466:
467: 468: 469:
470: public function store() {
471: if ($this->get("rcpcount") == 0) {
472:
473: $this->set("status", 9);
474: if ($this->get("started") == "0000-00-00 00:00:00") {
475: $this->set("started", date("Y-m-d H:i:s"), false);
476: }
477: $this->set("finished", date("Y-m-d H:i:s"), false);
478: }
479:
480: return parent::store();
481: }
482:
483: }
484: