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