<?php
/**
 * This file is part of
 * pragmaMx - Web Content Management System.
 * Copyright by pragmaMx Developer Team - http://www.pragmamx.org
 *
 * pragmaMx is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * $Revision: 1.15.2.9 $
 * $Author: tora60 $
 * $Date: 2008/02/02 10:40:53 $
 *
 * this file based on:
 * PHP-NUKE-Addon Newsletter 1.40
 * Copyright (c) by Andreas Guenther, 26.08.2001
 * webmaster@bariba.de / http://www.bariba.de
 * php-nuke Web Portal System - http://phpnuke.org/
 * Thatware - http://thatware.org/
 */

defined('mxMainFileLoaded') or die('access denied');

if (!defined('PMX_VERSION')) {
    mxErrorScreen('pragmaMx >= 0.1.10 is required');
}

$module_name = basename(dirname(__FILE__));
mxGetLangfile($module_name);
include("modules/$module_name/mycgi.php");
$index = 1;
$limit = 20;
$pagetitle = _new_title;

$nlop = (empty($nlop)) ? "" : $nlop;
$synctables = (empty($synctables)) ? "0" : $synctables;

/**
 * Prints the startscreen of the Newsletter
 */
function Newsletter()
{
    global $prefix, $module_name, $user_prefix;
    require("includes/captcha/settings.php");
    $userinfo = mxGetUserData();
    $userinfo['email'] = (empty($userinfo['email'])) ? _new_youraddress : $userinfo['email'];

    include_once("header.php");
    OpenTable();
    cgiCenter();
    cgiFontClass("title");
    echo _new_title;
    cgiFontClass();
    cgiCenter(1);
    cgiBR(2);
    echo _new_welcome;
    cgiBR(2);
    cgiFormMod("index", "action");
    echo _new_email;
    cgiBR();
    cgiText("new_email", $userinfo['email'], 50, 100);
    cgiBR(2);
    echo _new_choose;
    cgiBR();
    cgiSelect("new_sub");
    cgiOption("sub", _new_subscribe);
    cgiOption("unsub", _new_unsubscribe);
    cgiSelect();
    cgiBR(2);
    echo _new_choosetype;
    cgiBR();
    cgiSelect("new_type");
    cgiOption("0", _new_typetext);
    cgiOption("1", _new_typehtml);
    cgiSelect();
    cgiBR(2);
    if (($newsletteron) && ((!MX_IS_USER) || ((MX_IS_USER) && ($captchauseron))) && (!MX_IS_ADMIN)) {
        cgiCaptcha();
        cgiBR(2);
    }
    cgiCenter();
    if (($newsletteron) && ((!MX_IS_USER) || ((MX_IS_USER) && ($captchauseron))) && (!MX_IS_ADMIN)) {
        cgiCaptchaReload(_CAPTCHARELOAD);
    }
    cgiSubmit(_new_sendit);
    cgiCenter(1);
    cgiFormE();
	// Aenderung bei _Gerry_ 
	cgiCenter();
	cgiBR(2);
	cgiLinkM("index", "viewall", _new_viewold);
	cgiCenter(1);
	// Aenderung bei _Gerry_ Ende
    CloseTable();
    include_once("footer.php");
}

/**
 * Sends a confirmation mail, Unsubscribes
 */
function Action($new_email, $new_sub, $new_type)
{
    global $prefix, $adminmail, $sitename, $module_name, $slogan;
    require("includes/captcha/settings.php");
    include_once("header.php");
    OpenTable();
    if (!mxCheckEmail($new_email)) {
        cgiCenter();
        echo _new_noemail . "\n";
        cgiBR();
        cgiLinkM("index", "def", _new_clickhere);
        cgiCenter(1);
        CloseTable();
        include_once("footer.php");
        return;
    }
    if (($newsletteron) && ((!MX_IS_USER) || ((MX_IS_USER) && ($captchauseron))) && (!MX_IS_ADMIN)) {
        require_once("includes/captcha/__captcha/PhraseFactory.php");
        if ((!isset($_POST['captcha'])) || (empty($_POST['captcha']))) {
            $_POST['captcha'] = "abc!!def";
        }
        $givenCaptcha = $_POST['captcha'];
        $storedCaptcha = PhraseFactory::get('', $passphraselenght, $givenCaptcha, $storagetype);
        if ($givenCaptcha !== $storedCaptcha) {
            cgiCenter();
            echo "<center><b>" . _CAPTCHAWRONG . "</b><br /><br />" . _GOBACK . "</center>";
            cgiCenter(1);
            CloseTable();
            include_once("footer.php");
            return;
        }
    }
    if ($new_sub == 'sub') { // User wants to subscribe the letter
        // Check, if subscribed already
        $query = "SELECT id, email, status, html, checkkey FROM ${prefix}_newsletter WHERE email = '$new_email'";
        $result = sql_query($query);
        list($n_id, $n_email, $n_status, $n_html, $n_checkkey) = sql_fetch_row($result);
        if ($n_id <> '') {
            cgiCenter();
            echo _new_already . "\n";
            cgiBR();
            cgiLinkM("index", "def", _new_clickhere);
            cgiCenter(1);
            CloseTable();
            include_once("footer.php");
            return;
        }
        srand ((double)microtime() * 1000000);
        $mycode = rand();
        $time = time();
        $userip = MX_REMOTE_ADDR;
        $userhost = gethostbyaddr($userip);
        $query = "INSERT INTO ${prefix}_newsletter SET email = '$new_email', status = '1', checkkey='$mycode', html='$new_type', regdate='$time', regip='$userip', reghost='$userhost'";
        if (!sql_query($query)) {
            echo sql_errno() . ": " . sql_error() . "<br />";
            CloseTable();
            include_once("footer.php");
            return;
        }
        $buildlink = MX_BASE_URL . "modules.php?name=" . $module_name . "&func=confirm&new_email=" . $new_email . "&new_check=" . $mycode;
        // mxMail($new_email, _new_subject, _new_conftext . "\n\n$buildlink", $GLOBALS['adminmail'], "text", "Newsletter-Sub");
        mxMail($new_email, _new_subject, _new_conftext . "\n" . $slogan . "\n\n" . $buildlink . "\n", $GLOBALS['adminmail'], "text", "Newsletter-Sub", "", 120);
        cgiCenter();
        echo _new_senok;
        cgiBR();
        cgiLinkM("index", "def", _new_clickhere);
        cgiCenter(1);
        CloseTable();
        include_once("footer.php");
        return;
    }
    if ($new_sub == 'unsub') { // User wants to unsubscribe the letter
        $query = "DELETE FROM ${prefix}_newsletter WHERE email='$new_email'";
        if (!sql_query($query)) {
            echo sql_errno() . ": " . sql_error() . "<br />";
            CloseTable();
            include_once("footer.php");
            return;
        }
        cgiCenter();
        echo _new_unsbuok;
        cgiBR();
        cgiLinkM("index", "def", _new_clickhere);
        cgiCenter(1);
        CloseTable();
        include_once("footer.php");
        return;
    }
    CloseTable();
    include_once("footer.php");
}
// ### Double-Opt-In Check
function DoubleOptIn($nlop)
{
    global $prefix, $user_prefix, $adminmail, $module_name, $sitename, $slogan;
    include_once("header.php");
    OpenTable();
    if (!MX_IS_ADMIN) {
        echo _new_accessdenied . "\n";
        CloseTable();
        include_once("footer.php");
        return;
    }
    if ($nlop == 'intro') {
        echo "<b>" . _new_doi . "</b>\n";
        cgiBR(2);
        echo _new_doiintro . "\n";
        cgiBR(3);
        echo _new_doisendit . "\n";
        cgiBR(2);
        echo "<form method=\"post\" action=\"modules.php?name=" . $module_name . "&amp;func=doiconfirm\">\n";
        echo "<input type=\"checkbox\" name=\"nlop\" value=\"doicheck\" />&nbsp;&nbsp;" . _new_doisenditcheck . "\n";
        cgiBR(2);
        echo "<input type=\"submit\" value=\"" . _new_sendit . "\" />&nbsp;&nbsp;\n";
        echo "<input type=\"button\" onClick=\"history.go(-1)\" value=\"" . _new_doicancel . "\" /></form>\n";
    } elseif ($nlop == "doicheck") {
        $send = 0;
        $nlquery = sql_query("SELECT id, email FROM ${prefix}_newsletter WHERE status='2'");
        @ini_set("max_execution_time", "300");
        mxBulkMail(1);
        while (list($id, $email) = sql_fetch_row($nlquery)) {
            $mycode = time();
            $buildlink = MX_BASE_URL . "modules.php?name=" . $module_name . "&func=confirm&new_email=" . $email . "&new_check=" . $mycode;
            $message = _new_doimail1 . "\n\n\t" . MX_BASE_URL . "\n\t" . $sitename . "\n\t" . $slogan . "\n\n" . _new_doimail2 . "\n" . $buildlink . "\n\n\n" . _new_doimail3 . "\n\n" . $sitename . " (" . MX_BASE_URL . ")\n";
            if (mxMail($email, _new_doimailsub . $sitename, $message , $GLOBALS['adminmail'], "text", "Double-Opt-In", "", 120)) {
                // deactivate the newsletter-option in the users table
                sql_query("UPDATE ${user_prefix}_users SET newsletter='' WHERE email='$email' OR femail='$email'");
                sql_query("UPDATE " . $prefix . "_newsletter SET status='3', checkkey='$mycode' WHERE id='$id'");
                $send++;
            }
        }
        mxBulkMail(0);
        @ini_restore("max_execution_time");
        cgiCenter();
        echo "<b>" . $send . "</b> " . _new_ok . "\n";
        cgiBR(2);
        cgiLinkM("index", "admin", _new_clickhere);
        cgiCenter(1);
    } elseif ($nlop == "doilist") {
        echo "<b>" . _new_doilist . "</b>\n";
        cgiBR(2);
        echo _new_doilisttext . "\n";
        cgiBR(2);
        echo "<form method=\"post\" action=\"modules.php?name=" . $module_name . "&amp;func=doiconfirm\">\n";
        echo "<input type=\"hidden\" name=\"nlop\" value=\"doisendlist\" />\n";
        echo "<input type=\"submit\" value=\"" . _new_sendit . "\" />&nbsp;&nbsp;\n";
        echo "<input type=\"button\" onClick=\"history.go(-1)\" value=\"" . _new_doicancel . "\" /></form>\n";
    } elseif ($nlop == "doisendlist") {
        $result = sql_query("SELECT id FROM " . $prefix . "_newsletter WHERE status='2'");
        $count = sql_num_rows($result);
        $doilist = "-----------------------------------------------------------------\n" . "List of active newsletter subscribers from the virtual host:\n\n" . "Server-Name:\t" . $_SERVER['SERVER_NAME'] . "\n" . "Server-Admin:\t" . $_SERVER['SERVER_ADMIN'] . "\n" . "Server-IP:\t" . $_SERVER['SERVER_ADDR'] . "\n\n" . "running pragmaMx Content-Management-System:\n\n" . "Site-Name:\t" . $sitename . "\n" . "Site-Admin:\t" . $adminmail . "\n" . "Site-URL:\t" . MX_BASE_URL . "\n" . "Site-Slogan:\t" . $slogan . "\n\n" . "Number of active newsletter subscribers: " . $count . "\n\n" . "Subscribing to this newsletter is done in a double-opt-in\n" . "process. All informations transmited by each user during the\n" . "registration process over the activation-link are listed below.\n" . "-----------------------------------------------------------------\n";
        $buildquery = sql_query("SELECT id, email, regdate, regip, reghost FROM " . $prefix . "_newsletter WHERE status='2' ORDER BY email");
        while (list($id, $email, $regdate, $regip, $reghost) = sql_fetch_row($buildquery)) {
            $doilist .= "\n" . $email . "\n\tUser-ID: " . $id . "\n\tReg-Date (GMT): " . gmdate("d.m.Y, H:m:s", $regdate) . "\n\tReg-IP: " . $regip . "\n\tReg-Host: " . $reghost . "\n";
        }
        if (mxMail($adminmail, "Double-Opt-In List for: " . $sitename . "", $doilist , $GLOBALS['adminmail'], "text", "Double-Opt-In List", "", 120)) {
            cgiCenter();
            echo _new_doilistsendok;
            cgiBR(2);
            cgiLinkM("index", "admin", _new_clickhere);
            cgiCenter(1);
        }
    } else {
        cgiCenter();
        echo _new_doierror;
        cgiBR(2);
        cgiLinkM("index", "doiconfirm&amp;nlop=intro", _new_clickhere);
        cgiCenter(1);
    }
    CloseTable();
    include_once("footer.php");
}
// ############
/**
 * Called by the confirmation link sets status=2 which means get the letter
 */
function Confirm($new_email, $new_check)
{
    global $prefix, $module_name;
    include_once("header.php");
    OpenTable();
    $query = "SELECT * FROM ${prefix}_newsletter WHERE (email='$new_email' AND checkkey = '$new_check')";
    $result = sql_query($query);
    if (sql_num_rows($result) != 1) {
        cgiCenter();
        echo _new_subnot . "\n";
        cgiBR();
        cgiLinkM("index", "def", _new_clickhere);
        cgiCenter(1);
        CloseTable();
        include_once("footer.php");
        return;
    }
    $time = time();
    $userip = MX_REMOTE_ADDR;
    $userhost = gethostbyaddr($userip);
    $query = "UPDATE ${prefix}_newsletter SET status='2', checkkey = '0', regdate='$time', regip='$userip', reghost='$userhost' WHERE email='$new_email'";
    if (!sql_query($query)) {
        echo sql_errno() . ": " . sql_error() . "<br />";
        CloseTable();
        include_once("footer.php");
        return;
    }
    cgiCenter();
    echo _new_subok . "\n";
    cgiBR();
    cgiLinkM("index", "def", _new_clickhere);
    cgiCenter(1);
    CloseTable();
    include_once("footer.php");
}

/**
 * This is for you... Writing the Newsletter
 */
function Admin()
{
    global $admin, $module_name, $prefix, $user_prefix, $adminmail;
    include_once("header.php");
    $send = '';
    $sub = '';
    $text = '';
    $htmltext = '';
    $maxstories = 25;
    // Double-Opt-In Check
    $olderthan = time() - 604800;
    $doicheck = sql_query("SELECT id, email FROM ${prefix}_newsletter WHERE status='3' AND checkkey<'$olderthan'");
    while (list($doideath, $doideathemail) = sql_fetch_row($doicheck)) {
        sql_query("UPDATE ${user_prefix}_users SET newsletter='0' WHERE email='$doideathemail' OR femail='$doideathemail'");
        sql_query("DELETE FROM ${prefix}_newsletter WHERE id='$doideath'");
        sql_query("DELETE FROM ${prefix}_newsletter_sendto WHERE userid='$doideath'");
    }
    OpenTable();
    if (!MX_IS_ADMIN) {
        echo _new_accessdenied . "\n";
        CloseTable();
        include_once("footer.php");
        return;
    }
    if (strlen($send) == 0) {
        $send = $adminmail;
    }
    if (strlen($sub) == 0) {
        $sub = _new_defaultsubject;
    }
    cgiCenter();
    cgiFontClass("title");
    echo _new_admintitle . "\n";
    cgiFontClass();
    cgiCenter(1);
    cgiBR(2);
    echo _new_countabo . "\n";
    $query = "SELECT * FROM ${prefix}_newsletter WHERE status='2'";
    $result = sql_query($query);
    $count = sql_num_rows($result);

    $query1 = "SELECT id FROM ${prefix}_newsletter_send";
    $result1 = sql_query($query1);
    $countold = sql_num_rows($result1);

    echo ": $count " . _new_abo . "\n";
    cgiBR();
    cgiLinkM('index', 'viewsub', _new_viewlink, "menge=" . $count . "&amp;counter=0");
    cgiBR();
    cgiLinkM("index", "viewall", "" . _new_viewold . " (" . $countold . " " . _new_amount . ")", "menge=" . $countold . "&amp;counter=0");
    cgiBR(2);
    cgiLinkM('index', 'doiconfirm&amp;nlop=intro', _new_doi);
    cgiBR();
    cgiLinkM('index', 'doiconfirm&amp;nlop=doilist', _new_doilist);
    cgiBR(2);
    cgiFormMod("index", "adminsend");
    echo _new_newslettersubject . "\n";
    cgiBR();
    cgiText("sub", stripslashes($sub), 50, 100);
    cgiBR(2);
    echo _new_newtext . "\n";
    cgiBR();
    cgiTextArea("text", 20, 60, stripslashes($text));
    cgiBR(2);
    echo _new_newtexthtml . "\n";
    cgiBR(2);
    cgiTextArea("htmltext", 20, 60, stripslashes($htmltext));
    cgiBR(2);
    echo _new_alternatesender . "\n";
    cgiBR();
    cgiText("absender", "", 50, 100);
    cgiBR(2);
    CloseTable();
    // Add Story
    OpenTable();
    echo _new_attachstory . "\n";
    cgiBR();
    cgiSelect("sid", "MULTIPLE", 1);
    $query = "SELECT sid, title FROM ${prefix}_stories WHERE `time` <= now() ORDER BY `time` DESC LIMIT $maxstories";
    $result = sql_query($query);
    while (list ($sid, $title) = sql_fetch_row($result)) {
        cgiOption($sid, $title, 1);
    }
    cgiSelect();
    cgiBR(2);
    echo _new_attachtype . "\n";
    cgiBR();
    cgiSelect("sidtype");
    cgiOption("0", _new_none, _new_none);
    cgiOption("1", _new_linkonly);
    cgiOption("2", _new_homeandlink);
    cgiOption("3", _new_fullstory);
    cgiSelect();
    cgiBR(2);
    echo "<input type=\"checkbox\" name=\"separator_sid\" value=\"1\" checked=\"checked\" />\n";
    echo _new_addseperator . "\n";
    cgiBR(2);
    CloseTable();
    // Add Banner
    OpenTable();
    echo _new_addbanner . "\n";
    cgiBR();
    $query = "SELECT bid, imageurl FROM ${prefix}_banner";
    $result = sql_query($query);
    while (list ($bid, $imageurl) = sql_fetch_row($result)) {
        if (strlen($imageurl) > 0) {
            echo "<input type=\"checkbox\" name=\"bid[]\" value=\"" . $bid . "\" /> <img src=\"" . $imageurl . "\" width=\"200\" height=\"40\" align=\"middle\" alt=\"\" /><br /><br />\n";
        }
    }
    cgiBR(2);
    echo "<input type=\"checkbox\" name=\"separator_bid\" value=\"1\" checked=\"checked\" />\n";
    echo _new_addseperator . "\n";
    cgiBR(2);
    cgiCenter();
    cgiSubmit(_new_sendit);
    cgiCenter(1);
    cgiBR(2);
    cgiFormE();
    CloseTable();
    include_once("footer.php");
}

function BR2NL($value)
{
    $returnvalue = str_replace("<br>", "\n", $value);
    $value = str_replace("<br />", "\n", $returnvalue);
    return ($value);
}

/**
 * This actualy sends the mails
 */
function Adminsend($sub, $text, $htmltext, $absender, $bid, $sid, $sidtype, $separator_sid, $separator_bid, $hashtml, $hastext)
{
    global $admin, $module_name, $prefix, $user_prefix, $adminmail, $sitename;
    $sidtype = intval($sidtype);
    $separator_bid = intval($separator_bid);
    $separator_sid = intval($separator_sid);
    if ($hashtml == 'no') {
        $htmltext = mxNL2BR($text);
    }
    if ($hastext == 'no') {
        $text = BR2NL($htmltext);
    }
    include_once("header.php");
    $count = 0;
    OpenTable();
    if (!MX_IS_ADMIN) {
        echo _new_accessdenied . "\n";
        CloseTable();
        include_once("footer.php");
        return;
    }
    if ($separator_sid == '1') {
        $text = $text . "\n" . _new_textseperator . "\n";
        $htmltext = $htmltext . "<br />" . _new_htmlseperator . "<br />";
    }
    foreach ($sid as $storyid) {
        $storyid = intval($storyid);
        if ($storyid != 0) {
            $query = "SELECT sid, title, hometext, bodytext FROM ${prefix}_stories WHERE sid='$storyid'";
            $result = sql_query($query);
            list($sid, $title, $hometext, $bodytext) = sql_fetch_row($result);
            if (strlen($title) == 0) {
                print "sidtype";
                $sidtype = '0';
            }
            switch ($sidtype) {
                case '0' : // Send nothing
                    break;

                case '1' : // Send link only
                    $text = $text . "\n" . $title . "\n\n\n" . _new_linktext . "\n" . MX_BASE_URL . "modules.php?name=News&file=article&sid=" . $sid . "&mode=thread&order=0&thold=0";
                    $htmltext = $htmltext . "<br /><br /><a href=\"" . MX_BASE_URL . "modules.php?name=News&amp;file=article&amp;sid=" . $sid . "&amp;mode=thread&amp;order=0&amp;thold=0\">" . $title . "</a><br />";
                    break;

                case '2' : // Send hometext and link
                    $text = $text . "\n\n\n" . $title . "\n" . $hometext . "\n\n" . _new_linktext . "\n" . MX_BASE_URL . "modules.php?name=News&file=article&sid=" . $sid . "&amp;mode=thread&amp;order=0&amp;thold=0";
                    $htmltext = $htmltext . '<br /><br />' . $title . '<br /><br />' . $hometext . "<br /><br /><a href=\"" . MX_BASE_URL . "modules.php?name=News&amp;file=article&amp;sid=" . $sid . "&amp;mode=thread&order=0&amp;thold=0\">" . _new_linktext . "</a><br />";
                    break;

                case '3' : // Send full Story
                    $text = $text . "\n\n\n" . $title . "\n" . $hometext . "\n" . $bodytext . "\n\n" . _new_linktext . "\n" . MX_BASE_URL . "modules.php?name=News&file=article&sid=" . $sid . "&mode=thread&order=0&thold=0";
                    $htmltext = $htmltext . '<br /><br />' . $title . '<br /><br />' . $hometext . "<br />" . $bodytext . "<br /><br /><a href=\"" . MX_BASE_URL . "modules.php?name=News&amp;file=article&amp;sid=" . $sid . "&amp;mode=thread&amp;order=0&amp;thold=0\">" . _new_linktext . "</a><br />";
                    break;
            }
            if ($separator_sid == '1') {
                $text = $text . "\n" . _new_textseperator . "\n";
                $htmltext = $htmltext . "\n" . _new_htmlseperator . "<br />";
            }
        }
    }
    foreach ($bid as $bannerid) {
        $bannerid = intval($bannerid);
        if ($bannerid != 0) {
            $query = "SELECT bid, imageurl, clickurl FROM ${prefix}_banner WHERE bid='$bannerid'";
            $result = sql_query($query);
            list($bid, $imageurl, $clickurl) = sql_fetch_row($result);
            $text = $text . "\n\n" . _new_bannertext . "\n" . $clickurl;
            $htmltext = $htmltext . "<br /><br /><a href=\"" . $clickurl . "\"><img src=\"" . $imageurl . "\" alt=\"\" /></a><br />";
            if ($separator_bid == '1') {
                $text = $text . "\n" . _new_textseperator . "\n";
                $htmltext = $htmltext . "<br />" . _new_htmlseperator . "<br />";
            }
        }
    }

    if (empty($absender) || !mxCheckEmail($absender)) {
        $absender = $GLOBALS['adminmail'];
    }
    cgiCenter();
    cgiFontClass("title");
    echo _new_admintitle . "\n";
    cgiFontClass();
    cgiCenter(1);
    $qry = "INSERT INTO ${prefix}_newsletter_send SET subject='" . mxAddSlashesForSQL($sub) . "', text='" . mxAddSlashesForSQL($text) . "', html='" . mxAddSlashesForSQL($htmltext) . "', send=NOW()";
    sql_query($qry);
    $newsletterid = sql_insert_id();
    $qry = "SELECT ${prefix}_newsletter.email,
			${prefix}_newsletter.html,
			${prefix}_newsletter.id,
			${prefix}_newsletter.status
			FROM ${prefix}_newsletter
			WHERE (((${prefix}_newsletter.status)=2));";
    $i = 0;
    $result = sql_query($qry);
    while ($newsletter[$i] = sql_fetch_array($result)) {
        $i++;
    }
    $result_set = "";
    if (empty($sub)) {
        $sub = "Newsletter-$newsletterid";
    }
    // only send if subscriber found
    if ($newsletter) {
        @ini_set("max_execution_time", "300");
        mxBulkMail(1);
        foreach($newsletter as $newsletter_to) {
            if (empty($newsletter_to['email'])) {
                break;
            }
            $buildlink = '' . MX_BASE_URL . 'modules.php?name=' . $module_name . '&func=action&new_email=' . $newsletter_to['email'] . '&new_sub=unsub&new_type=';
            $sub = mxStripSlashes(strip_tags($sub));
            // Sende Mail
            if ($newsletter_to['html'] != 1) {
                $sendtext = $text . "\n" . _new_textseperator . _new_infotext . "\n\t" . $GLOBALS['sitename'] . " (" . MX_BASE_URL . ")\n\n" . _new_infounsub . $buildlink . "" ;
                $msg = mxStripSlashes($sendtext);
                if (mxMail($newsletter_to['email'], $sub, $msg, $absender, "text", "Newsletter-$newsletterid", "", 120)) $count++;
            } else {
                $buildlink = str_replace('&', '&amp;', $buildlink);
                $htmlsendtext = mxNL2BR($htmltext) . '<br /><br />' . _new_textseperator . "<br />" . _new_infotext . "\n<br /><br />\t" . $GLOBALS['sitename'] . " (<a href=\"" . MX_BASE_URL . "\">" . MX_BASE_URL . "</a>)<br /><br />" . _new_infounsub . "<br /><a href=\"$buildlink\">$buildlink</a><br />";
                $msg = mxStripSlashes($htmlsendtext);
                if (mxMail($newsletter_to['email'], $sub, $msg, $absender, "html", "Newsletter-$newsletterid", "", 120)) $count++;
            }
            $result_set[] = $newsletter_to['id']; // $newsletter_to['id'];
        }
        mxBulkMail(0);
        @ini_restore("max_execution_time");
    }
    // schreibe Kontrolltabelle
    // only write if mail was send
    if ($result_set)
        foreach($result_set as $hast_send) {
        if (empty($hast_send)) {
            break;
        }

        $qry = "INSERT INTO ${prefix}_newsletter_sendto SET userid='" . $hast_send . "', newsletterid='$newsletterid', date=NOW()";
        sql_query($qry);
    }
    cgiCenter();
    cgiBR(2);
    echo "<b>" . $count . "</b> " . _new_ok . "\n";
    cgiBR();
    cgiLinkM("index", "admin", _new_clickhere);
    cgiCenter(1);
    CloseTable();
    include_once("footer.php");
}

/**
 * Prints a list of subscribers
 */
function ViewSubscribers($menge, $counter)
{
    global $admin, $module_name, $prefix, $limit;
    $menge = intval($menge);
    $counter = intval($counter);
    include_once("header.php");
    OpenTable();
    if (!MX_IS_ADMIN) {
        echo _new_accessdenied . "\n";
        CloseTable();
        include_once("footer.php");
        return;
    }
    echo "<center><table border=\"2\" width=\"90%\" rules=\"all\"><tr>\n";
    echo "<colgroup><col width=\"40%\"><col width=\"10%\">\n";
    echo "<col width=\"20%\"><col width=\"20%\">\n";
    echo "<col width=\"20%\"></colgroup><col width=\"20%\"></colgroup>\n";
    echo _new_subtablehead . "\n";
    echo "</tr>";
    $query = "SELECT t1.email, t1.status, t1.id, t1.html, t1.regdate, t1.regip, t2.userid, count(*) FROM ${prefix}_newsletter AS t1 LEFT JOIN ${prefix}_newsletter_sendto AS t2 ON t1.id = t2.userid where t1.status='2' GROUP BY t1.id ORDER BY t1.email limit $counter, $limit";
    $result = sql_query($query);
    $iii = 0;
    while (list($umail, $ustatus, $userid, $utype, $regdate, $regip, $userid, $anzahl) = sql_fetch_row($result)) {
        echo "<tr>";
        cgiTD($umail);
        if ($utype == '1') {
            cgiTD(_new_typehtml);
        } else {
            cgiTD(_new_typetext);
        }
        echo "<td><center>\n";
        cgiLinkM("index", "viewsend", $anzahl, "userid=$userid&amp;menge=$anzahl&amp;counter=0");
        echo "</td></center>\n";
        cgiTD(date("d.m.Y, H:i", $regdate));
        cgiTD($regip);
        echo "</tr>\n";
        $iii++;
    }
    echo "</table>\n";
    cgiBR(2);
    cgiCenter();
    cgiLinkM("index", "admin", _new_adminmenu);
    cgiBR(2);
    prevnext("&amp;func=viewsub&amp;menge=" . $menge . "", "?name=Newsletter", $counter, $iii, $menge, $limit);
    cgiCenter(1);
    CloseTable();
    include_once("footer.php");
}

/**
 * Show the subject of the send newsletter of the given $userid
 */
function ViewSendLetters($userid, $menge, $counter)
{
    global $module_name, $prefix, $limit;
    $userid = intval($userid);
    $menge = intval($menge);
    $counter = intval($counter);
    include_once("header.php");
    OpenTable();
    if (!MX_IS_ADMIN) {
        echo _new_accessdenied . "\n";
        CloseTable();
        include_once("footer.php");
        return;
    }
    cgiFontClass("title");
    echo _new_sendletters . "\n";
    cgiFontClass();
    cgiBR(2);
    echo "<center><table border=\"2\" width=\"80%\" rules=\"all\"><tr>\n";
    echo "<colgroup><col width=\"60%\"><col width=\"40%\"></colgroup>\n";
    echo _new_sendlettershead . "\n";
    $query = "SELECT t1.subject, t2.date, t1.id, t2.id, t2.userid, t2.newsletterid from ${prefix}_newsletter_send AS t1," . " ${prefix}_newsletter_sendto AS t2 where (t1.id = t2.newsletterid) and (t2.userid ='$userid') limit $counter,$limit";
    $result = sql_query($query);
    $hhh = 0;
    while (list($subject, $date, $newsid) = sql_fetch_row($result)) {
        echo "<tr>\n";
        echo "<td><center>\n";
        cgiLinkM("index", "viewletter", mxPrepareToDisplay(mxStripSlashes($subject)), "newsid=$newsid");
        echo "</center></td>\n";
        echo "<td><center>" . $date . "\n";
        echo "</center></td>";
        echo "</tr>";
        $hhh++;
    }
    echo "</table>";
    cgiBR(2);
    prevnext("&amp;func=viewsend&amp;userid=102&amp;menge=" . $menge . "", "?name=Newsletter", $counter, $hhh, $menge, $limit);
    cgiCenter();
    cgiLinkM("index", "admin", _new_adminmenu);
    cgiCenter(1);
    CloseTable();
    include_once("footer.php");
}

/**
 * Shows the text of the Newsletter
 */
function ViewLetter($newsid)
{
    global $admin, $module_name, $prefix;
    include_once("header.php");
    OpenTable();
	// Aenderung bei _Gerry_
    /* if (!MX_IS_ADMIN) {
        echo _new_accessdenied . "\n";
        CloseTable();
        include_once("footer.php");
        return;
    }
	*/
	// Aenderung bei _Gerry_ Ende
    cgiFontClass("title");
    echo _new_viewletter . "\n";
    cgiFontClass();
    cgiBR(2);
    $query = "SELECT subject, text, html, send FROM ${prefix}_newsletter_send WHERE id='$newsid'";
    $result = sql_query($query);
    if (list($subject, $text, $html, $senddate) = sql_fetch_row($result)) {
        echo $senddate;
        cgiBR(2);
        echo "<b>" . _new_newslettersubject . "&nbsp;" . mxStripSlashes($subject) . "</b>\n";
        cgiBR(3);
        echo mxNL2BR(mxStripSlashes($text));
        cgiBR(2);
        echo mxStripSlashes($html);
    } else {
        echo _new_notexttoprint . "\n";
    }
    cgiBR(2);
    cgiCenter();
	// Aenderung bei _Gerry_
     if (MX_IS_ADMIN) {
    cgiLinkM("index", "admin", _new_adminmenu);
	}
	// Aenderung bei _Gerry_
    cgiCenter(1);
    CloseTable();
    include_once("footer.php");
}
/**
 * Prints a survey of all send letters by now
 */

function ViewPastLetters($menge, $counter)
{
    global $module_name, $prefix, $limit;
    $menge = intval($menge);
    $counter = intval($counter);
    include_once("header.php");
    OpenTable();
	// Aenderung bei _Gerry_
   /* if (!MX_IS_ADMIN) {
        echo _new_accessdenied . "\n";
        CloseTable();
        include_once("footer.php");
        return;
    }*/
	// Aenderung bei _Gerry_ Ende
    cgiFontClass("title");
    echo _new_viewletter . "\n";
    cgiFontClass();
    cgiBR(2);
    echo "<center><table border=\"2\" width=\"80%\" rules=\"all\"><tr>\n";
    echo "<colgroup><col width=\"60%\"><col width=\"40%\"></colgroup>\n";
    echo _new_sendlettershead . "\n";
    $query = "SELECT subject, send, id FROM ${prefix}_newsletter_send limit " . $counter . ", " . $limit;
    $result = sql_query($query);
    $fff = 0;
    while (list($subject, $date, $id) = sql_fetch_row($result)) {
        echo "<tr>\n";
        echo "<td><center>\n";
        cgiLinkM("index", "viewletter", mxPrepareToDisplay(mxStripSlashes($subject)), "newsid=$id");
        echo "</center></td>\n";
        echo "<td><center>" . $date . "\n";
        echo "</center></td>\n";
        echo "</tr>\n";
        $fff++;
    }
    echo "</table>\n";
    cgiBR(2);
    prevnext("&amp;func=viewall&amp;menge=" . $menge . "", "?name=Newsletter", $counter, $fff, $menge, $limit);
    cgiCenter();
	// Aenderung bei _Gerry_
	if (MX_IS_ADMIN) {
   			cgiLinkM("index", "admin", _new_adminmenu);
    }
	// Aenderung bei _Gerry_ Ende
    cgiCenter(1);
    CloseTable();
    include_once("footer.php");
}

/**
 * Calls the different functions of the Addon
 * -------------------------------------------------------------------------------
 * Ruft die einzelnen Funktionen fuer das Addon auf
 */
global $sub, $text, $htmltext, $send, $sid, $sidtype;
if (empty($func)) $func = "";
switch ($func) {
    case 'admin':
        Admin();
        break;
    case 'adminaddstory':
        AdminAddStory($sid, $sidtype, $text, $htmltext, $sub, $send, $seperator);
        break;
    case 'adminaddbanner':
        AdminAddBanner($bid, $text, $htmltext, $sub, $send, $seperator);
        break;
    case 'adminsend':
        $bid = (empty($_POST['bid'])) ? 0 : $_POST['bid'];
        $sid = (empty($_POST['sid'])) ? 0 : $_POST['sid'];
        $separator_sid = (empty($_POST['separator_sid'])) ? 0 : $_POST['separator_sid'];
        $separator_bid = (empty($_POST['separator_bid'])) ? 0 : $_POST['separator_bid'];
        $sidtype = (empty($_POST['sidtype'])) ? 0 : $_POST['sidtype'];
        if (empty($htmltext)) {
            $hashtml = 'no';
        } else {
            $hashtml = 'yes';
        }
        if (empty($text)) {
            $hastext = 'no';
        } else {
            $hastext = 'yes';
        }
        Adminsend($sub, $text, $htmltext, $absender, $bid, $sid, $sidtype, $separator_sid, $separator_bid, $hashtml, $hastext);
        break;
    case 'confirm' :
        Confirm($new_email, $new_check);
        break;
    case 'doiconfirm' :
        DoubleOptIn($nlop);
        break;
    case 'action' :
        Action($new_email, $new_sub, $new_type);
        break;
    case 'viewsub' :
        ViewSubscribers($menge, $counter);
        break;
    case 'viewsend':
        ViewSendLetters($userid, $menge, $counter);
        break;
    case 'viewletter':
        ViewLetter($newsid);
        break;
    case 'viewall':
        ViewPastLetters($menge, $counter);
        break;
    default:
        Newsletter();
        break;
}

?>