<?php
require_once('libraries/phpmailer/PHPMailerAutoload.php');
require_once('config.mail.php');
if (!class_exists('crm_users')) require_once("common_cadline_libraries/crm_users.class.php");

$email          = trim($_GET['email']);
$topicId        = 54;
$maxLeiratkozas = 10;

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) { // $email is a valid email address
    //egy ip cimrol 24 oran belul $maxLeiratkozas lehetseges
    crm_database::getInstance()->query("SELECT * FROM `" . JMLADATBAZIS . "`.`unsubscribe` WHERE `ip`='" . $_SERVER['REMOTE_ADDR'] . "' AND datum>='" . date('Y-m-d H:i:s', time() - 86400) . "';");
    $leiratkozott = crm_database::getInstance()->fetchAssoc();

    if (count($leiratkozott) >= $maxLeiratkozas) {
        echo 'too many unsubscribe.';
        exit();
    }

    crm_database::getInstance()->query("SELECT DISTINCT(nUserID) FROM " . CRMADATBAZIS . ".u_emails WHERE `newsletter`=1 AND email='" . $email . "';");
    $res = crm_database::getInstance()->fetchAssoc();

    if (count($res)) {
        $ids = array();

        foreach ($res as $sor => $user) {
            $nUserID    = $user['nUserID'];
            $ids[]      = $nUserID;

            // u_history-ba beírás
            crm_users::AddUserHistory(array(
                'nUserID'   => $nUserID,
                'nTopicID'  => $topicId,
                'strInfo'   => $email
            ));

            // user adatok kikeresese
            crm_database::getInstance()->query("SELECT * FROM " . CRMADATBAZIS . ".users WHERE nUserID=" . $nUserID . " LIMIT 1;");
            $u = crm_database::getInstance()->fetchAssoc();
            $u = $u[0];

            // userstatus felülírása -5-re, ha 99 vagy <= 8
            if ((int)$u['nUserStatus'] == 99 || (int)$u['nUserStatus'] <= 8) crm_users::SetUserStatus($nUserID, -5);

            // unsubscribe tabla insert
            crm_database::getInstance()->insert("`" . JMLADATBAZIS . "`.`unsubscribe`", array(
                'name' => $u['strName'],
                'email' => $email,
                'deleted' => 1,
                'datum' => date('Y-m-d H:i:s', time()),
                'ip' => $_SERVER['REMOTE_ADDR']
            ));
        }

        // e_mail newsletter=>0
        crm_database::getInstance()->update("`" . CRMADATBAZIS . "`.`u_emails`", array('newsletter' => 0), array('email' => $email));

        $msg = "Helló Adminisztrátor,<br />Az alábbi email címmel leiratkozott egy felhasználó: " . $email . "<br />";

        if (count($ids)) {
            foreach ($ids as $id) $msg .= "<a href='http://crm.cadline.hu/hu/users/show/" . $id . "'>http://crm.cadline.hu/hu/users/show/" . $id . "</a><br />";
        }

        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->SMTPAuth     = true;
        $mail->CharSet      = 'UTF-8';
        $mail->Host         = $mailconfig['Host'];
        $mail->Username     = $mailconfig['Username'];
        $mail->Password     = $mailconfig['Password'];
        $mail->SMTPSecure   = $mailconfig['SMTPSecure'];
        $mail->Port         = $mailconfig['Port'];
        $mail->isHTML(TRUE);
        $mail->setFrom('info@cadline.hu');
        $mail->addAddress('webdeveloper@cadline.hu');
        $mail->addAddress('marketing@cadline.hu');
        $mail->Subject = 'Hírlevél leiratkozás';
        $mail->msgHTML($msg);
        $mail->AltBody = strip_tags($msg);
        $mail->send();
        unset($mail);

        print "<script>window.location.href = '/index.php?option=com_content&view=article&id=580';</script>";
    } else {
        echo ($email . " email not found.<br />");
        exit();
    }
} else {
    echo ($email . " is not a valid email address.<br />");
    exit();
}
