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