public function runJob()
 {
     global $cfg, $cfgClient, $client, $recipient;
     $iCount = 0;
     if ($this->get("status") == 2) {
         // Job is currently running, check start time and restart if
         // started 5 minutes ago
         $dStart = strtotime($this->get("started"));
         $dNow = time();
         if ($dNow - $dStart > 5 * 60) {
             $this->set("status", 1);
             $this->set("started", "0000-00-00 00:00:00", false);
             $oLogs = new cNewsletterLogCollection();
             $oLogs->setWhere("idnewsjob", $this->get($this->primaryKey));
             $oLogs->setWhere("status", "sending");
             $oLogs->query();
             while ($oLog = $oLogs->next()) {
                 $oLog->set("status", "error (sending)");
                 $oLog->store();
             }
         }
     }
     if ($this->get("status") == 1) {
         // Job waiting for sending
         $this->set("status", 2);
         $this->set("started", date("Y-m-d H:i:s"), false);
         $this->store();
         // Initialization
         $aMessages = array();
         $oLanguage = new cApiLanguage($this->get("idlang"));
         $sFormatDate = $oLanguage->getProperty("dateformat", "date");
         $sFormatTime = $oLanguage->getProperty("dateformat", "time");
         unset($oLanguage);
         if ($sFormatDate == "") {
             $sFormatDate = 'Y-m-d';
         }
         if ($sFormatTime == "") {
             $sFormatTime = 'h:i a';
         }
         // Get newsletter data
         $sFrom = $this->get("newsfrom");
         $sFromName = $this->get("newsfromname");
         $sSubject = $this->get("subject");
         $sMessageText = $this->get("message_text");
         $sMessageHTML = $this->get("message_html");
         $dNewsDate = strtotime($this->get("newsdate"));
         $sEncoding = $this->get("encoding");
         $bIsHTML = false;
         if ($this->get("type") == "html" && $sMessageHTML != "") {
             $bIsHTML = true;
         }
         $bDispatch = false;
         if ($this->get("dispatch") == 1) {
             $bDispatch = true;
         }
         // Single replacements
         // Replace message tags (text message)
         $sMessageText = str_replace("MAIL_DATE", date($sFormatDate, $dNewsDate), $sMessageText);
         $sMessageText = str_replace("MAIL_TIME", date($sFormatTime, $dNewsDate), $sMessageText);
         $sMessageText = str_replace("MAIL_NUMBER", $this->get("rcpcount"), $sMessageText);
         // Replace message tags (html message)
         if ($bIsHTML) {
             $sMessageHTML = str_replace("MAIL_DATE", date($sFormatDate, $dNewsDate), $sMessageHTML);
             $sMessageHTML = str_replace("MAIL_TIME", date($sFormatTime, $dNewsDate), $sMessageHTML);
             $sMessageHTML = str_replace("MAIL_NUMBER", $this->get("rcpcount"), $sMessageHTML);
             # Link to online article -->
             if (!is_object($db)) {
                 $db = new DB_Contenido();
             }
             $sql = 'SELECT idart
                     FROM ' . $cfg['tab']['news'] . '
                     WHERE (idnews=' . $this->get('idnews') . ')';
             $db->query($sql);
             $db->next_record();
             $news_idart = $db->f('idart');
             $link = Contenido_Url::getInstance()->build(array('idart' => $news_idart, 'client' => $this->get('idclient'), 'lang' => $this->get("idlang"), 'nl' => $this->get('idnewsjob'), 'rcp' => '{RCP}'), true);
             $p1 = strpos($sMessageHTML, '<body');
             if ($p1 !== false) {
                 $p1 = strpos($sMessageHTML, '>', $p1) + 1;
             } else {
                 $p1 = 0;
             }
             $sOnlineText = getEffectiveSetting('newsletter-online-text', $this->get("idlang"), 'If the newsletter is not shown properly, please click here to view the online version.');
             $sMessageHTML = substr($sMessageHTML, 0, $p1) . '<div style="text-align: center; background-color: #FFF;"><a href="' . $link . '" style="font-weight: bold;">' . $sOnlineText . '</a></div>' . substr($sMessageHTML, $p1);
             # <-- Link to online article
             // Remove base tag
             $sMessageHTML = preg_replace('/<base href=(.*?)>/is', '', $sMessageHTML, 1);
             // Fix source path
             // TODO: Test any URL specification that may exist under the sun...
             $sMainURL = Contenido_Url::getInstance()->build(array('idcat' => getEffectiveSetting('navigation', 'idcat-home', 1), 'client' => $this->get('idclient'), 'lang' => $this->get("idlang")), true);
             $sSelfURL = Contenido_Url::getInstance()->build(array('idart' => $this->get("idart"), 'client' => $this->get('idclient'), 'lang' => $this->get("idlang")), true);
             $sMessageHTML = preg_replace("/(href|src)\\=(\"|\\')([^(http|#)])(\\/)?/", "\$1=" . "\$2" . $sMainURL . "\$3", $sMessageHTML);
             $sMessageHTML = preg_replace('/url\\([\\"\'](.*)[\\"\']\\)/', 'url(\'' . $sMainURL . '$1\')', $sMessageHTML);
             $sMessageHTML = str_replace('/cms//', '/', $sMessageHTML);
             // Now replace anchor tags to the newsletter article itself just by the anchor
             $sMessageHTML = preg_replace("/(href|src)\\=(\"|\\')" . str_replace('/', '\\/', $sSelfURL) . "(.*)#(.*)(\"|\\')/", "\$1=" . "\$2" . "#" . "\$4" . "\$5", $sMessageHTML);
             // Now correct mailto tags
             $sMessageHTML = str_replace($sMainURL . 'mailto:', 'mailto:', $sMessageHTML);
             # Remove the <noscript> info from the newsletter message
             $sMessageHTML = str_replace(array('This website is powered by drugCMS, the Content Management System with addictive potential.', 'For more info and download visit <a href="http://www.drugcms.org">www.drugcms.org</a>.', 'drugCMS is made in Germany.'), '', $sMessageHTML);
         }
         // Enabling plugin interface
         $bPluginEnabled = false;
         if (getSystemProperty("newsletter", "newsletter-recipients-plugin") == "true") {
             $bPluginEnabled = true;
             $aPlugins = array();
             if (is_array($cfg['plugins']['recipients'])) {
                 foreach ($cfg['plugins']['recipients'] as $sPlugin) {
                     plugin_include("recipients", $sPlugin . "/" . $sPlugin . ".php");
                     if (function_exists("recipients_" . $sPlugin . "_wantedVariables")) {
                         $aPlugins[$sPlugin] = call_user_func("recipients_" . $sPlugin . "_wantedVariables");
                     }
                 }
             }
         }
         // Get recipients (from log table)
         if (!is_object($oLogs)) {
             $oLogs = new cNewsletterLogCollection();
         } else {
             $oLogs->resetQuery();
         }
         $oLogs->setWhere("idnewsjob", $this->get($this->primaryKey));
         $oLogs->setWhere("status", "pending");
         if ($bDispatch) {
             $oLogs->setLimit(0, $this->get("dispatch_count"));
         }
         $oLogs->query();
         while ($oLog = $oLogs->next()) {
             $iCount++;
             $oLog->set("status", "sending");
             $oLog->store();
             $sRcpMsgText = $sMessageText;
             $sRcpMsgHTML = $sMessageHTML;
             $sKey = $oLog->get("rcphash");
             $sEMail = $oLog->get("rcpemail");
             $bSendHTML = false;
             if ($oLog->get("rcpnewstype") == 1) {
                 $bSendHTML = true;
                 // Recipient accepts html newsletter
             }
             if (strlen($sKey) == 30) {
                 // Prevents sending without having a key
                 $sRcpMsgText = str_replace("{KEY}", $sKey, $sRcpMsgText);
                 $sRcpMsgText = str_replace("MAIL_MAIL", $sEMail, $sRcpMsgText);
                 $sRcpMsgText = str_replace("MAIL_NAME", $oLog->get("rcpname"), $sRcpMsgText);
                 // Replace message tags (html message)
                 if ($bIsHTML && $bSendHTML) {
                     $sRcpMsgHTML = str_replace("{KEY}", $sKey, $sRcpMsgHTML);
                     $sRcpMsgHTML = str_replace("MAIL_MAIL", $sEMail, $sRcpMsgHTML);
                     $sRcpMsgHTML = str_replace("MAIL_NAME", $oLog->get("rcpname"), $sRcpMsgHTML);
                     $sRcpMsgHTML = str_replace(urlencode('{RCP}'), $sKey, $sRcpMsgHTML);
                 }
                 if ($bPluginEnabled) {
                     // Don't change name of $recipient variable as it is used in plugins!
                     $recipient = new Recipient();
                     $recipient->loadByPrimaryKey($oLog->get("idnewsrcp"));
                     foreach ($aPlugins as $sPlugin => $aPluginVar) {
                         foreach ($aPluginVar as $sPluginVar) {
                             // Replace tags in text message
                             $sRcpMsgText = str_replace("MAIL_" . strtoupper($sPluginVar), call_user_func("recipients_" . $sPlugin . "_getvalue", $sPluginVar), $sRcpMsgText);
                             // Replace tags in html message
                             if ($bIsHTML && $bSendHTML) {
                                 $sRcpMsgHTML = str_replace("MAIL_" . strtoupper($sPluginVar), call_user_func("recipients_" . $sPlugin . "_getvalue", $sPluginVar), $sRcpMsgHTML);
                             }
                         }
                     }
                     unset($recipient);
                 }
                 $oMail = new PHPMailer();
                 $oMail->CharSet = $sEncoding;
                 $oMail->IsHTML($bIsHTML && $bSendHTML);
                 $oMail->From = $sFrom;
                 $oMail->FromName = $sFromName;
                 $oMail->AddAddress($sEMail);
                 # Mailer Configuration -->
                 $sMailer = strtolower(getEffectiveSetting('newsletter', 'mailer'));
                 $sHost = getEffectiveSetting('newsletter', 'host');
                 $iPort = intval(getEffectiveSetting('newsletter', 'port'));
                 $sUsername = getEffectiveSetting('newsletter', 'username');
                 $sPassword = getEffectiveSetting('newsletter', 'password');
                 if (strlen($sMailer) == 0) {
                     $sMailer = strtolower(getEffectiveSetting('email', 'mailer'));
                     $sHost = getEffectiveSetting('email', 'host');
                     $iPort = intval(getEffectiveSetting('email', 'port'));
                     $sUsername = getEffectiveSetting('email', 'username');
                     $sPassword = getEffectiveSetting('email', 'password');
                 }
                 if (strlen($sMailer) == 0) {
                     setSystemProperty('newsletter', 'mailer', 'mail');
                     $sMailer = 'mail';
                 }
                 if (strlen($sHost) == 0) {
                     setSystemProperty('newsletter', 'host', '');
                 }
                 if ($iPort == 0) {
                     setSystemProperty('newsletter', 'port', '25');
                     $iPort = 25;
                 }
                 if (strlen($sUsername) == 0) {
                     setSystemProperty('newsletter', 'username', '');
                 }
                 if (strlen($sPassword) == 0) {
                     setSystemProperty('newsletter', 'password', '');
                 }
                 $oMail->Mailer = $sMailer;
                 if ($sMailer == 'smtp') {
                     $oMail->SMTPAuth = true;
                     $oMail->Host = $sHost;
                     $oMail->Port = $iPort;
                     $oMail->Username = $sUsername;
                     $oMail->Password = $sPassword;
                 }
                 # <-- Mailer Configuration
                 $oMail->Subject = $sSubject;
                 if ($bIsHTML && $bSendHTML) {
                     $oMail->Body = $sRcpMsgHTML;
                     $oMail->AltBody = $sRcpMsgText . "\n\n";
                 } else {
                     $oMail->Body = $sRcpMsgText . "\n\n";
                 }
                 if ($oMail->Send()) {
                     $oLog->set("status", "successful");
                     $oLog->set("sent", date("Y-m-d H:i:s"), false);
                 } else {
                     $oLog->set("status", "error (sending)");
                 }
             } else {
                 $oLog->set("status", "error (key)");
             }
             $oLog->store();
         }
         $this->set("sendcount", $this->get("sendcount") + $iCount);
         if ($iCount == 0 || !$bDispatch) {
             // No recipients remaining, job finished
             $this->set("status", 9);
             $this->set("finished", date("Y-m-d H:i:s"), false);
         } else {
             if ($bDispatch) {
                 // Check, if there are recipients remaining - stops job faster
                 $oLogs->resetQuery();
                 $oLogs->setWhere("idnewsjob", $this->get($this->primaryKey));
                 $oLogs->setWhere("status", "pending");
                 $oLogs->setLimit(0, $this->get("dispatch_count"));
                 $oLogs->query();
                 if ($oLogs->next()) {
                     // Remaining recipients found, set job back to pending
                     $this->set("status", 1);
                     $this->set("started", "0000-00-00 00:00:00", false);
                 } else {
                     // No remaining recipients, job finished
                     $this->set("status", 9);
                     $this->set("finished", date("Y-m-d H:i:s"), false);
                 }
             } else {
                 // Set job back to pending
                 $this->set("status", 1);
                 $this->set("started", "0000-00-00 00:00:00", false);
             }
         }
         $this->store();
     }
     return $iCount;
 }
     if ($iRecipients > 0 && $iRecipients > $_REQUEST["elemperpage"]) {
         $sBrowseLinks = "";
         for ($i = 1; $i <= ceil($iRecipients / $_REQUEST["elemperpage"]); $i++) {
             //$iNext = (($i - 1) * $_REQUEST["elemperpage"]) + 1;
             if ($sBrowseLinks !== "") {
                 $sBrowseLinks .= "&nbsp;";
             }
             if ($iNextPage == $i) {
                 $sBrowseLinks .= $i . "\n";
                 // I'm on the current page, no link
             } else {
                 $sBrowseLinks .= '<a href="' . $sess->url("main.php?area={$area}&action={$action}&frame={$frame}&idnewsjob=" . $_REQUEST["idnewsjob"] . "&nextpage={$i}&sortmode=" . $_REQUEST["sortmode"] . "&sortby=" . $_REQUEST["sortby"]) . '">' . $i . '</a>' . "\n";
             }
         }
     }
     $oLogs->setLimit($_REQUEST["elemperpage"] * ($iNextPage - 1), $_REQUEST["elemperpage"]);
 }
 if (!array_key_exists($_REQUEST["sortby"], $aCols)) {
     $_REQUEST["sortby"] = 0;
     // Sort by rcpname by default
 }
 $oLogs->setOrder($aCols[$_REQUEST["sortby"]] . " " . $_REQUEST["sortmode"]);
 $oLogs->query();
 $oImgDelete = new cHTMLImage("images/delete.gif");
 $oImgDelete->setAlt(i18n("Delete item", $plugin_name));
 $sImgDelete = $oImgDelete->render();
 unset($oImgDelete);
 $iCount = 0;
 $aNewsType[] = array();
 // Performance
 $aNewsType[0] = i18n("Text only", $plugin_name);