// Create a link to add a recipient
if ($perm->have_perm_area_action("recipients", "recipients_create")) {
    $oLink = new cHTMLLink();
    $oLink->setMultiLink("recipients", "", "recipients", "recipients_create");
    $oLink->setContent('<img style="margin-right: 4px;" src="' . $cfg["path"]["images"] . 'folder_new.gif" align="middle">' . i18n("Create recipient", $plugin_name) . '</a>');
    $sContent .= $oLink->render() . '<br />' . "\n";
}
// Create a link to import recipients
if ($perm->have_perm_area_action("recipients", "recipients_create")) {
    $oLink = new cHTMLLink();
    $oLink->setMultiLink("recipients", "", "recipients_import", "recipients_import");
    $oLink->setContent('<img style="margin-right: 4px;" src="' . $cfg["path"]["images"] . 'importieren.gif" align="middle">' . i18n("Import recipients", $plugin_name) . '</a>');
    $sContent .= $oLink->render() . '<br />' . "\n";
}
// Create a link to purge subscribed but not confirmed recipients
$iTimeframe = (int) $oClient->getProperty("newsletter", "purgetimeframe");
if ($iTimeframe <= 0) {
    $iTimeframe = 30;
}
if ($perm->have_perm_area_action("recipients", "recipients_delete")) {
    $oLink = new cHTMLLink();
    $oLink->setLink('javascript:showPurgeMsg("' . i18n("Purge recipients", $plugin_name) . '", "' . sprintf(i18n("Do you really want to remove recipients, that have not been confirmed since %s days and over?", $plugin_name), '"+purgetimeframe+"') . '")');
    $oLink->setContent('<img style="margin-right: 4px;" src="' . $cfg["path"]["images"] . 'delete.gif" align="middle">' . i18n("Purge recipients", $plugin_name) . '</a>');
    $sContent .= $oLink->render();
}
$sContent .= '</div>' . "\n";
$oTpl->set('s', 'VALUE_PURGETIMEFRAME', $iTimeframe);
// To template
$sLink = "actionrec";
$oListActionsRow = new cFoldingRow("f0d7bf80-e73e-11d9-8cd6-0800200c9a66", i18n("Actions", $plugin_name), $sLink, false);
$oListActionsRow->setContentData($sContent);
if (is_array($cfg['plugins']['recipients'])) {
    foreach ($cfg['plugins']['recipients'] as $plugin) {
        plugin_include("recipients", $plugin . "/" . $plugin . ".php");
    }
}
// Note, that the object name has to be $recipient for plugins
if ($action == "recipients_create" && $perm->have_perm_area_action($area, $action)) {
    $recipient = $oRecipients->create("*****@*****.**", " " . i18n("-- new recipient --", $plugin_name));
    $oPage->setReload();
} elseif ($action == "recipients_delete" && $perm->have_perm_area_action($area, $action)) {
    $oRecipients->delete($idrecipient);
    $recipient = new Recipient();
    $oPage->setReload();
} elseif ($action == "recipients_purge" && $perm->have_perm_area_action($area, "recipients_delete")) {
    $oClient = new cApiClient($client);
    $timeframe = $oClient->getProperty("newsletter", "purgetimeframe");
    if (!$timeframe) {
        $timeframe = 30;
    }
    $purgedrecipients = $oRecipients->purge($timeframe);
    /* backslashdollar: There is a problem translating \$ - it is either not recognized or translated correctly (using poEdit) */
    if ($purgedrecipients > 0) {
        $sNotis = $notification->messageBox("info", sprintf(str_replace("backslashdollar", "\$", i18n("%1backslashdollard recipients, which hasn't been confirmed since more than %2backslashdollard days has been removed.", $plugin_name)), $purgedrecipients, $timeframe), 0);
    } else {
        $sNotis = $notification->messageBox("info", sprintf(str_replace("backslashdollar", "\$", i18n("There are no recipients, which hasn't been confirmed since more than %2backslashdollard days has been removed.", $plugin_name)), 0, $timeframe), 0);
    }
    $recipient = new Recipient();
    $oPage->setReload();
} else {
    $recipient = new Recipient($idrecipient);
}
 /**
  * If newsletter is HTML newsletter and necessary data available
  * returns final HTML message
  * @return string HTML message
  */
 public function getHTMLMessage()
 {
     global $lang, $client, $cfgClient, $contenido;
     if ($this->get("type") == "html" && $this->get("idart") > 0 && $this->htmlArticleExists()) {
         // Article ID
         $iIDArt = $this->get("idart");
         // Category ID
         $oClientLang = new cApiClientLanguage(false, $client, $lang);
         $iIDCat = $oClientLang->getProperty("newsletter", "html_newsletter_idcat");
         unset($oClientLang);
         // Get http username and password, if frontend is protected
         $oClient = new cApiClient($client);
         $sHTTPUserName = $oClient->getProperty("newsletter", "html_username");
         $sHTTPPassword = $oClient->getProperty("newsletter", "html_password");
         unset($oClient);
         // Get HTML
         if ($iIDArt > 0 && $iIDCat > 0) {
             // Check, if newsletter is online and set temporarely online, otherwise
             $bSetOffline = false;
             $oArticles = new cApiArticleLanguageCollection();
             $oArticles->setWhere("idlang", $this->get("idlang"));
             $oArticles->setWhere("idart", $this->get("idart"));
             $oArticles->query();
             if ($oArticle = $oArticles->next()) {
                 if ($oArticle->get("online") == 0) {
                     $bSetOffline = true;
                     $oArticle->set("online", 1);
                     $oArticle->store();
                 }
                 unset($oArticle);
             }
             unset($oArticles);
             $sFile = "front_content.php?client={$client}&lang={$lang}&idcat={$iIDCat}&idart={$iIDArt}&noex=1&send=1";
             $aURL = parse_url($cfgClient[$client]['path']['htmlpath']);
             // TODO: Other schemes than http should be tested before use!
             if ($aURL["scheme"] == "https") {
                 $iPort = 443;
                 $sTarget = "ssl://" . $aURL["host"];
             } else {
                 $iPort = 80;
                 $sTarget = $aURL["host"];
             }
             if ($aURL["port"]) {
                 $iPort = $aURL["port"];
             }
             $iErrorNo = 0;
             $sErrorMsg = "";
             if ($iHandler = fsockopen($sTarget, $iPort, $iErrorNo, $sErrorMsg, 30)) {
                 // If you use HTTP 1.1 you may get chunked data... you could solve
                 // this easily by using HTTP 1.0, but then you get a problem with
                 // virtual servers, as HTTP 1.0 doesn't use the host information...
                 fputs($iHandler, "GET " . $aURL["path"] . $sFile . " HTTP/1.1\r\n");
                 fputs($iHandler, "Host: " . $aURL["host"] . "\r\n");
                 // Maybe the website has been protected using .htaccess, then login
                 if ($sHTTPUserName != "" && $sHTTPPassword != "") {
                     fputs($iHandler, "Authorization: Basic " . base64_encode("{$sHTTPUserName}:{$sHTTPPassword}") . "\r\n");
                 }
                 fputs($iHandler, "Referer: http://" . $aURL["host"] . "\r\n");
                 fputs($iHandler, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n");
                 fputs($iHandler, "Connection: close\r\n\r\n");
                 // Get the HTTP header and body separately
                 $sHTML = "";
                 $sHeader = "";
                 $bBody = false;
                 while (!feof($iHandler)) {
                     // $sLine = fgets($iHandler, 4096);
                     $sLine = fgets($iHandler, 1024);
                     if ($bBody) {
                         $sHTML .= $sLine;
                     } else {
                         if ($sLine == "\r\n") {
                             $bBody = true;
                         } else {
                             $sHeader .= $sLine;
                         }
                     }
                 }
                 fclose($iHandler);
                 $sHTML = $this->_deChunkHTTPBody($sHeader, $sHTML);
                 // Remove base tag
                 $sHTML = preg_replace('/<base href=(.*?)>/is', '', $sHTML, 1);
                 // Make URLs absolute
                 $sHTML = preg_replace_callback('/(url)\\((\'|\\")?(http:\\/\\/|https:\\/\\/|ftp:\\/\\/)?([A-Za-z0-9#\\.?\\-=_&\\/]*)[\'|\\"]?\\)/', array($this, "_callbackReplaceUrl"), $sHTML);
                 $sHTML = preg_replace_callback('/\\b(src|href|ftp)[ ]*=[ ]*"(http:\\/\\/|https:\\/\\/|ftp:\\/\\/)?([A-Za-z0-9#\\.?\\-=_&\\/]*)"/', array($this, "_callbackReplaceUrl"), $sHTML);
                 // Now replace anchor tags to the newsletter article itself just by the anchor
                 $sHTML = preg_replace("/(href|src)\\=(\"|\\')" . str_replace('/', '\\/', $cfgClient[$client]['path']['htmlpath']) . "front_content.php?idart=" . $iIDArt . "(.*)#(.*)(\"|\\')/", "\$1=" . "\$2" . "#" . "\$4" . "\$5", $sHTML);
                 // Now correct mailto tags
                 $sHTML = str_replace($cfgClient[$client]['path']['htmlpath'] . 'mailto:', 'mailto:', $sHTML);
                 $sReturn = $sHTML;
             } else {
                 if ($contenido) {
                     // Use i18n only in backend
                     $sErrorText = i18n("There was a problem getting the newsletter article using http. Error: %s (%s)", $plugin_name);
                 } else {
                     $sErrorText = "There was a problem getting the newsletter article using http. Error: %s (%s)";
                 }
                 $this->_sError = sprintf($sErrorText, $sErrorMsg, $iErrorNo);
                 $sReturn = false;
             }
             // Set previously offline article back to offline
             if ($bSetOffline) {
                 $oArticles = new cApiArticleLanguageCollection();
                 $oArticles->setWhere("idlang", $this->get("idlang"));
                 $oArticles->setWhere("idart", $this->get("idart"));
                 $oArticles->query();
                 if ($oArticle = $oArticles->next()) {
                     $oArticle->set("online", 0);
                     $oArticle->store();
                 }
                 unset($oArticle);
                 unset($oArticles);
             }
             return $sReturn;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }