Exemple #1
0
function wifidog_register_perform()
{
    session_start();
    $page = CurrentPageName();
    $tpl = new templates();
    $autocreate_confirmed = 0;
    if ($GLOBALS["HOTSPOT_DEBUG"]) {
        wifidog_logs("Starting wifidog_register_perform()");
    }
    $wifidog_build_uri = wifidog_build_uri();
    $uriext = $wifidog_build_uri[0];
    $HiddenFields = $wifidog_build_uri[1];
    $sock = new wifidog_settings($_SESSION["WIFIDOG_RULES"]);
    $ENABLED_SMTP = intval($sock->GET_INFO("ENABLED_SMTP"));
    $wifidog_templates = new wifidog_templates($_SESSION["WIFIDOG_RULES"]);
    $ENABLED_REDIRECT_LOGIN = intval($sock->GET_INFO("ENABLED_REDIRECT_LOGIN"));
    $email = trim(strtolower($_POST["email"]));
    $q = new mysql_hotspot();
    $password2 = trim($_POST["password2"]);
    $password = trim($_POST["password"]);
    $passphrase = trim(strtolower($_POST["passphrase"]));
    if ($GLOBALS["HOTSPOT_DEBUG"]) {
        wifidog_logs("{$email}: Ask a passphrase: {$wifidog_templates->REGISTER_GENERIC_PASSWORD}");
    }
    if ($wifidog_templates->REGISTER_GENERIC_PASSWORD == 1) {
        if ($passphrase != trim(strtolower($wifidog_templates->REGISTER_GENERIC_PASSTXT))) {
            return wifidog_register($wifidog_templates->char($wifidog_templates->REGISTER_GENERIC_PASSERR));
        }
    }
    if ($password2 != $password) {
        return wifidog_register("{password_mismatch}");
    }
    if ($ENABLED_SMTP == 1) {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            return wifidog_register("{error_email_invalid}");
        }
        $tr = explode("@", $email);
        $domain = $tr[1];
        if (!checkdnsrr($domain, 'MX')) {
            return wifidog_register("&laquo;{$domain}&raquo;<br>{error_domain_email_invalid}");
        }
    }
    $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT uid,autocreate FROM hotspot_members WHERE `uid`='{$email}'"));
    if ($ENABLED_REDIRECT_LOGIN == 1) {
        if (intval($ligne["autocreate"]) == 1) {
            if ($ligne["uid"] != null) {
                $q->QUERY_SQL("DELETE FROM hotspot_members WHERE `uid`='{$email}'");
                $ligne["uid"] = null;
            }
        }
    }
    if ($ligne["uid"] != null) {
        if ($ENABLED_SMTP == 1) {
            $link = "<br><strong><a href=\"{$page}?wifidog-recover=yes&{$uriext}\">{lost_password}</a></strong>";
        }
        return wifidog_register($tpl->_ENGINE_parse_body("{this_account_already_exists}<br>{$link}"));
    }
    wifidog_logs("Ruleid: {$_SESSION["WIFIDOG_RULES"]}");
    $MAC = $_REQUEST["mac"];
    $REGISTER_MAX_TIME = intval($sock->GET_INFO("REGISTER_MAX_TIME"));
    $ArticaSplashHotSpotEndTime = intval($sock->GET_INFO("ArticaSplashHotSpotEndTime"));
    if ($REGISTER_MAX_TIME == 0) {
        $REGISTER_MAX_TIME = 5;
    }
    wifidog_logs("Ruleid: REGISTER_MAX_TIME:{$REGISTER_MAX_TIME}");
    wifidog_logs("Ruleid: ArticaSplashHotSpotEndTime:{$ArticaSplashHotSpotEndTime}");
    $password = md5($password);
    $creationtime = time();
    $autocreate_maxttl = $ArticaSplashHotSpotEndTime;
    $sessionkey = md5($password . $creationtime . $email);
    $gateway_addr = $_REQUEST["gw_address"];
    $gw_port = $_REQUEST["gw_port"];
    $gw_id = $_REQUEST["gw_id"];
    $ARP = $_REQUEST["mac"];
    $url = $_REQUEST["url"];
    $token = $_REQUEST["token"];
    if ($token == null) {
        if ($GLOBALS["HOTSPOT_DEBUG"]) {
            wifidog_logs("no token retrive", __FUNCTION__, __LINE__);
        }
        $token = generateToken($ARP);
        if ($token == null) {
            $token = md5(uniqid(rand(), 1));
            if ($GLOBALS["HOTSPOT_DEBUG"]) {
                wifidog_logs("Generate a temporary token {$token}", __FUNCTION__, __LINE__);
            }
        }
    }
    if ($ENABLED_SMTP == 0) {
        $autocreate_confirmed = 1;
    }
    if ($GLOBALS["HOTSPOT_DEBUG"]) {
        wifidog_logs("Create new member {$email},{$password},TTL:{$ArticaSplashHotSpotEndTime}", __FUNCTION__, __LINE__);
    }
    $sql = "INSERT IGNORE INTO hotspot_members \n\t(uid,username,token,ruleid,ttl,sessiontime,password,enabled,creationtime,autocreate,autocreate_confirmed,autocreate_maxttl,sessionkey,MAC) VALUES\n\t('{$email}','{$email}','{$token}','{$_SESSION["WIFIDOG_RULES"]}','{$ArticaSplashHotSpotEndTime}','','{$password}',1,'{$creationtime}',1,'{$autocreate_confirmed}',{$autocreate_maxttl},'{$sessionkey}','{$MAC}')";
    $q->QUERY_SQL($sql);
    wifidog_logs("[{$ARP}]:[{$GLOBALS["CONNECTED_IP"]}]: {$email}: Create New member with a token {$token}", __FUNCTION__, __LINE__);
    if (!$q->ok) {
        events(0, "MySQL error: {$q->mysql_error} (Line: " . __LINE__ . ")");
        wifidog_logs("{$q->mysql_error}");
        wifidog_logs("{$sql}");
        return wifidog_register($q->mysql_error);
    }
    if ($ENABLED_SMTP == 0) {
        $array["SERVER_NAME"] = $_SERVER["SERVER_NAME"];
        $array["redirecturi"] = $url;
        $array["LOGIN"] = $email;
        $array["redirecturi"] = $_REQUEST["url"];
        $array["REMOTE_ADDR"] = $_REQUEST["ip"];
        $array["token"] = $token;
        $array["HOST"] = $_REQUEST["ip"];
        $array["ruleid"] = $_SESSION["WIFIDOG_RULES"];
        UnLock($array);
        wifidog_logs("wifidog_auth/" . __FUNCTION__ . ":: SESSION(HOTSPOT_REDIRECT_URL) = {$url}");
        $_SESSION["HOTSPOT_AUTO_REGISTER"] = true;
        $_SESSION["HOTSPOT_REDIRECT_URL"] = $url;
        $redirecturi = "http://{$gateway_addr}:{$gw_port}/wifidog/auth?token={$token}";
        wifidog_logs("wifidog_auth/" . __FUNCTION__ . ":: Redirect Token: {$token} to {$redirecturi}");
        header("Location: {$redirecturi}");
        return;
    }
    $proto = "http";
    $myHostname = $_SERVER["HTTP_HOST"];
    $page = CurrentPageName();
    if (isset($_SERVER["HTTPS"])) {
        $proto = "https";
    }
    $URL_REDIRECT = "{$proto}://{$myHostname}/{$page}?wifidog-confirm={$sessionkey}";
    $smtp_sender = $sock->GET_INFO("smtp_sender");
    include_once dirname(__FILE__) . "/ressources/externals/mime/mime.inc";
    $message = new Mail_mime("\r\n");
    $text = "<p style=font-size:18px>{$wifidog_templates->REGISTER_MESSAGE}</p>\n\t<p>\t\n\t<hr>\n\t\t<center>\n\t\t\t<a href=\"{$URL_REDIRECT}\" style='font-size:22px;text-decoration:underline'>{$URL_REDIRECT}</a>\n\t\t</center>\n\t</p>";
    $message->setFrom($smtp_sender);
    $message->addTo($email);
    $message->setSubject($wifidog_templates->REGISTER_SUBJECT);
    $message->setTXTBody(strip_tags($text));
    $message->setHTMLBody($text);
    $finalbody = $message->getMessage();
    $webauth_msmtp = new webauth_msmtp($smtp_sender, $finalbody, $email);
    if (!$webauth_msmtp->Send()) {
        events(0, "SMTP Error[method 1]: {$webauth_msmtp->logs}", $webauth_msmtp->logs);
        $content[] = "Method 1:\n{$webauth_msmtp->logs}\n";
        $smtp = new smtp();
        if ($sock->GET_INFO("smtp_auth_user") != null) {
            $params["auth"] = true;
            $params["user"] = $sock->GET_INFO("smtp_auth_user");
            $params["pass"] = $sock->GET_INFO("smtp_auth_passwd");
        }
        $params["host"] = $sock->GET_INFO("smtp_server_name");
        $params["port"] = $sock->GET_INFO("smtp_server_port");
        if (!$smtp->connect($params)) {
            $content[] = "Method2 Error:{$smtp->error_number} {$smtp->error_text}";
            events(0, "SMTP Error[method 2 - Connect]: {$params["host"]} {$smtp->error_number} {$smtp->error_text}", @implode("\n", $content));
            return wifidog_register("{$email}: {error_while_sending_message} {error} {$smtp->error_number} {$smtp->error_text}");
        }
        if (!$smtp->send(array("from" => $smtp_sender, "recipients" => $email, "body" => $finalbody, "headers" => null))) {
            events(0, "SMTP Error[method 2 - Send]: {$smtp->error_number} {$smtp->error_text}", null);
            $smtp->quit();
            return wifidog_register("{error_while_sending_message} {error} {$smtp->error_number} {$smtp->error_text}");
        }
        if ($GLOBALS["HOTSPOT_DEBUG"]) {
            wifidog_logs("From: {$smtp_sender} to {$email} {$wifidog_templates->REGISTER_SUBJECT} success");
        }
        $smtp->quit();
    }
    if ($GLOBALS["HOTSPOT_DEBUG"]) {
        wifidog_logs("wifidog_auth/" . __FUNCTION__ . ":: Token: {$token} -> UnLock(..");
    }
    $array["LOGIN"] = $email;
    $array["ARP"] = $_REQUEST["mac"];
    $array["token"] = $token;
    $array["redirecturi"] = $_REQUEST["url"];
    $array["REMOTE_ADDR"] = $_REQUEST["ip"];
    $array["REGISTER"] = true;
    if (!UnLock($array)) {
        if ($GLOBALS["HOTSPOT_DEBUG"]) {
            wifidog_logs("wifidog_auth/" . __FUNCTION__ . "::failed_to_create_session");
        }
        return wifidog_register("{error} {failed_to_create_session}");
    }
    wifidog_logs("wifidog_auth/" . __FUNCTION__ . ":: SESSION(HOTSPOT_REDIRECT_URL) = {$url}");
    $_SESSION["HOTSPOT_AUTO_REGISTER"] = true;
    $_SESSION["HOTSPOT_REDIRECT_URL"] = $url;
    $redirecturi = "http://{$gateway_addr}:{$gw_port}/wifidog/auth?token={$token}";
    wifidog_logs("wifidog_auth/" . __FUNCTION__ . ":: Redirect Token: {$token} to {$redirecturi}");
    header("Location: {$redirecturi}");
}
Exemple #2
0
try {
    if (isset($_SESSION['access_token']) && $client->getAccessToken()) {
        // Make API Calls
        if (isset($_POST['send'])) {
            $to = $_POST['to'];
            $bcc = $_POST['bcc'];
            $cc = $_POST['cc'];
            $body = $_POST['message'];
            $subject = $_POST['subject'];
            $mime->addTo($to);
            $mime->addBcc($bcc);
            $mime->addCc($cc);
            $mime->setTXTBody($body);
            $mime->setHTMLBody($body);
            $mime->setSubject($subject);
            $message_body = $mime->getMessage();
            $encoded_message = base64url_encode($message_body);
            // Gmail Message Body
            $message = new Google_Service_Gmail_Message();
            $message->setRaw($encoded_message);
            // Send the Email
            $email = $service->users_messages->send('me', $message);
            if ($email->getId()) {
                $notice = '<div class="alert alert-success">Email Sent successfully!</div>';
            } else {
                $notice = '<div class="alert alert-danger">Oops...something went wrong, try again later</div>';
            }
        } else {
            if (isset($_POST['draft'])) {
                $to = $_POST['to'];
                $bcc = $_POST['bcc'];
 public function as_text()
 {
     $script = '';
     $variables = '';
     $exts = array();
     // rules
     $activeRules = 0;
     foreach ($this->content as $rule) {
         $tests = array();
         $i = 0;
         if ($rule['disabled'] == 1) {
             $script .= '# rule:[' . $rule['name'] . "]" . RCUBE_SIEVE_NEWLINE;
             $script .= '# disabledRule:[' . $this->_safe_serial(serialize($rule)) . "]" . RCUBE_SIEVE_NEWLINE;
         } else {
             // header
             $script .= '# rule:[' . $rule['name'] . "]" . RCUBE_SIEVE_NEWLINE;
             // constraints expressions
             foreach ($rule['tests'] as $test) {
                 $tests[$i] = '';
                 switch ($test['type']) {
                     case 'size':
                         $tests[$i] .= $test['not'] ? 'not ' : '';
                         $tests[$i] .= 'size :' . ($test['operator'] == 'under' ? 'under ' : 'over ') . $test['target'];
                         break;
                     case 'virustest':
                     case 'spamtest':
                         array_push($exts, $test['type']);
                         array_push($exts, 'relational');
                         array_push($exts, 'comparator-i;ascii-numeric');
                         $tests[$i] .= $test['not'] ? 'not ' : '';
                         $tests[$i] .= $test['type'] . ' :value ' . ($test['operator'] == 'eq' ? '"eq" ' : ($test['operator'] == 'le' ? '"le" ' : '"ge" ')) . ':comparator "i;ascii-numeric" "' . $test['target'] . '"';
                         break;
                     case 'true':
                         $tests[$i] .= $test['not'] ? 'not true' : 'true';
                         break;
                     case 'exists':
                         $tests[$i] .= $test['not'] ? 'not ' : '';
                         if (is_array($test['header'])) {
                             $tests[$i] .= 'exists ["' . implode('", "', $this->_escape_string($test['header'])) . '"]';
                         } else {
                             $tests[$i] .= 'exists "' . $this->_escape_string($test['header']) . '"';
                         }
                         break;
                     case 'envelope':
                         array_push($exts, 'envelope');
                     case 'header':
                     case 'address':
                         if ($test['operator'] == 'regex') {
                             array_push($exts, 'regex');
                         } elseif (substr($test['operator'], 0, 5) == 'count' || substr($test['operator'], 0, 5) == 'value') {
                             array_push($exts, 'relational');
                         } elseif ($test['operator'] == 'user' || $test['operator'] == 'detail' || $test['operator'] == 'domain') {
                             array_push($exts, 'subaddress');
                         }
                         $tests[$i] .= $test['not'] ? 'not ' : '';
                         $tests[$i] .= $test['type'] . ' :' . $test['operator'];
                         if ($test['comparator'] != '') {
                             if ($test['comparator'] != 'i;ascii-casemap' && $test['comparator'] != 'i;octet') {
                                 array_push($exts, 'comparator-' . $test['comparator']);
                             }
                             $tests[$i] .= ' :comparator "' . $test['comparator'] . '"';
                         }
                         if (is_array($test['header'])) {
                             $tests[$i] .= ' ["' . implode('", "', $this->_escape_string($test['header'])) . '"]';
                         } else {
                             $tests[$i] .= ' "' . $this->_escape_string($test['header']) . '"';
                         }
                         if (is_array($test['target'])) {
                             $tests[$i] .= ' ["' . implode('", "', $this->_escape_string($test['target'])) . '"]';
                         } else {
                             $tests[$i] .= ' "' . $this->_escape_string($test['target']) . '"';
                         }
                         break;
                     case 'body':
                         array_push($exts, 'body');
                         if ($test['operator'] == 'regex') {
                             array_push($exts, 'regex');
                         } elseif (substr($test['operator'], 0, 5) == 'count' || substr($test['operator'], 0, 5) == 'value') {
                             array_push($exts, 'relational');
                         }
                         $tests[$i] .= $test['not'] ? 'not ' : '';
                         $tests[$i] .= $test['type'];
                         if ($test['bodypart'] != '') {
                             $tests[$i] .= ' :' . $test['bodypart'];
                         }
                         if ($test['contentpart'] != '') {
                             $tests[$i] .= ' "' . $test['contentpart'] . '"';
                         }
                         $tests[$i] .= ' :' . $test['operator'];
                         if ($test['comparator'] != '') {
                             if ($test['comparator'] != 'i;ascii-casemap' && $test['comparator'] != 'i;octet') {
                                 array_push($exts, 'comparator-' . $test['comparator']);
                             }
                             $tests[$i] .= ' :comparator "' . $test['comparator'] . '"';
                         }
                         if (is_array($test['target'])) {
                             $tests[$i] .= ' ["' . implode('", "', $this->_escape_string($test['target'])) . '"]';
                         } else {
                             $tests[$i] .= ' "' . $this->_escape_string($test['target']) . '"';
                         }
                         break;
                     case 'date':
                         array_push($exts, 'date');
                         if ($test['operator'] == 'regex') {
                             array_push($exts, 'regex');
                         } elseif (substr($test['operator'], 0, 5) == 'count' || substr($test['operator'], 0, 5) == 'value') {
                             array_push($exts, 'relational');
                         }
                         $tests[$i] .= $test['not'] ? 'not ' : '';
                         $tests[$i] .= $test['header'];
                         $timezone = rcmail::get_instance()->config->get('timezone', 'auto');
                         if ($timezone != 'auto') {
                             $server_time = gmdate('U');
                             $user_time = format_date(time(), 'U');
                             if ($server_time == $user_time) {
                                 $zone = '+0000';
                             } else {
                                 if ($user_time > $server_time) {
                                     $user_time -= $server_time;
                                     // hours
                                     $zone = '+' . str_pad(intval(intval($user_time) / 3600), 2, "0", STR_PAD_LEFT);
                                     // minutes
                                     $zone .= str_pad(intval($user_time / 60 % 60), 2, "0", STR_PAD_LEFT);
                                 } else {
                                     $server_time -= $user_time;
                                     // hours
                                     $zone = '-' . str_pad(intval(intval($server_time) / 3600), 2, "0", STR_PAD_LEFT);
                                     // minutes
                                     $zone .= str_pad(intval($server_time / 60 % 60), 2, "0", STR_PAD_LEFT);
                                 }
                             }
                             $tests[$i] .= ' :zone ' . '"' . $zone . '"';
                         }
                         $tests[$i] .= ' :' . $test['operator'];
                         if ($test['comparator'] != '') {
                             if ($test['comparator'] != 'i;ascii-casemap' && $test['comparator'] != 'i;octet') {
                                 array_push($exts, 'comparator-' . $test['comparator']);
                             }
                             $tests[$i] .= ' :comparator "' . $test['comparator'] . '"';
                         }
                         $tests[$i] .= ' "' . $this->_escape_string($test['datepart']) . '"';
                         $tests[$i] .= ' "' . $this->_escape_string($test['target']) . '"';
                         break;
                 }
                 $i++;
             }
             $script .= ($activeRules > 0 && $this->elsif ? 'els' : '') . ($rule['join'] ? 'if allof (' : 'if anyof (');
             $activeRules++;
             if (sizeof($tests) > 1) {
                 $script .= implode("," . RCUBE_SIEVE_NEWLINE . RCUBE_SIEVE_INDENT, $tests);
             } elseif (sizeof($tests)) {
                 $script .= $tests[0];
             } else {
                 $script .= 'true';
             }
             $script .= ")" . RCUBE_SIEVE_NEWLINE . "{" . RCUBE_SIEVE_NEWLINE;
             // action(s)
             $actions = '';
             foreach ($rule['actions'] as $action) {
                 switch ($action['type']) {
                     case 'fileinto':
                     case 'fileinto_copy':
                         array_push($exts, 'fileinto');
                         $args = '';
                         if ($action['type'] == 'fileinto_copy') {
                             array_push($exts, 'copy');
                             $args .= ' :copy';
                         }
                         if ($action['create']) {
                             array_push($exts, 'mailbox');
                             $args .= ' :create';
                         }
                         // variables support in fileinto by David Warden
                         if (preg_match('/\\$\\{\\d+\\}/', $action['target'])) {
                             array_push($exts, 'variables');
                         }
                         $actions .= RCUBE_SIEVE_INDENT . "fileinto" . $args . " \"" . $this->_escape_string($action['target']) . "\";" . RCUBE_SIEVE_NEWLINE;
                         break;
                     case 'redirect':
                     case 'redirect_copy':
                         $args = '';
                         if ($action['type'] == 'redirect_copy') {
                             array_push($exts, 'copy');
                             $args .= ' :copy';
                         }
                         $tokens = preg_split('/[,;\\s]/', $action['target']);
                         foreach ($tokens as $email) {
                             $actions .= RCUBE_SIEVE_INDENT . "redirect" . $args . " \"" . $this->_escape_string($email) . "\";" . RCUBE_SIEVE_NEWLINE;
                         }
                         break;
                     case 'reject':
                     case 'ereject':
                         array_push($exts, $action['type']);
                         if (strpos($action['target'], "\n") !== false) {
                             $actions .= RCUBE_SIEVE_INDENT . $action['type'] . " text:" . RCUBE_SIEVE_NEWLINE . $action['target'] . RCUBE_SIEVE_NEWLINE . "." . RCUBE_SIEVE_NEWLINE . ";" . RCUBE_SIEVE_NEWLINE;
                         } else {
                             $actions .= RCUBE_SIEVE_INDENT . $action['type'] . " \"" . $this->_escape_string($action['target']) . "\";" . RCUBE_SIEVE_NEWLINE;
                         }
                         break;
                     case 'vacation':
                         array_push($exts, 'vacation');
                         $action['subject'] = $this->_escape_string($action['subject']);
                         //							// encoding subject header with mb_encode provides better results with asian characters
                         //							if (function_exists("mb_encode_mimeheader"))
                         //							{
                         //								mb_internal_encoding($action['charset']);
                         //								$action['subject'] = mb_encode_mimeheader($action['subject'], $action['charset'], 'Q');
                         //								mb_internal_encoding(RCMAIL_CHARSET);
                         //							}
                         // detect original recipient
                         if ($action['from'] == 'auto' && strpos($variables, 'set "from"') === false) {
                             array_push($exts, 'variables');
                             $variables .= 'set "from" "";' . RCUBE_SIEVE_NEWLINE;
                             $variables .= 'if header :matches "to" "*" {' . RCUBE_SIEVE_NEWLINE;
                             $variables .= RCUBE_SIEVE_INDENT . 'set "from" "${1}";' . RCUBE_SIEVE_NEWLINE;
                             $variables .= '}' . RCUBE_SIEVE_NEWLINE;
                             $action['from'] = "\${from}";
                         }
                         // append original subject
                         if ($action['origsubject'] == '1' && strpos($variables, 'set "subject"') === false) {
                             array_push($exts, 'variables');
                             $variables .= 'set "subject" "";' . RCUBE_SIEVE_NEWLINE;
                             $variables .= 'if header :matches "subject" "*" {' . RCUBE_SIEVE_NEWLINE;
                             $variables .= RCUBE_SIEVE_INDENT . 'set "subject" "${1}";' . RCUBE_SIEVE_NEWLINE;
                             $variables .= '}' . RCUBE_SIEVE_NEWLINE;
                             $action['subject'] = trim($action['subject']);
                             if (substr($action['subject'], -1, 1) != ":") {
                                 $action['subject'] .= ":";
                             }
                             $action['subject'] .= " \${subject}";
                         }
                         $actions .= RCUBE_SIEVE_INDENT . "vacation" . RCUBE_SIEVE_NEWLINE;
                         $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":days " . $action['days'] . RCUBE_SIEVE_NEWLINE;
                         if (!empty($action['addresses'])) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":addresses [\"" . str_replace(",", "\",\"", $this->_escape_string($action['addresses'])) . "\"]" . RCUBE_SIEVE_NEWLINE;
                         }
                         if (!empty($action['subject'])) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":subject \"" . $action['subject'] . "\"" . RCUBE_SIEVE_NEWLINE;
                         }
                         if (!empty($action['handle'])) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":handle \"" . $this->_escape_string($action['handle']) . "\"" . RCUBE_SIEVE_NEWLINE;
                         }
                         if (!empty($action['from'])) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":from \"" . $this->_escape_string($action['from']) . "\"" . RCUBE_SIEVE_NEWLINE;
                         }
                         if ($action['htmlmsg']) {
                             $MAIL_MIME = new Mail_mime("\r\n");
                             $action['msg'] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">' . "\r\n<html><body>\r\n" . $action['msg'] . "\r\n</body></html>\r\n";
                             $MAIL_MIME->setHTMLBody($action['msg']);
                             // add a plain text version of the e-mail as an alternative part.
                             $h2t = new html2text($action['msg'], false, true, 0);
                             $plainTextPart = $h2t->get_text();
                             if (!$plainTextPart) {
                                 // empty message body breaks attachment handling in drafts
                                 $plainTextPart = "\r\n";
                             } else {
                                 // make sure all line endings are CRLF (#1486712)
                                 $plainTextPart = preg_replace('/\\r?\\n/', "\r\n", $plainTextPart);
                             }
                             $MAIL_MIME->setTXTBody($plainTextPart);
                             $MAIL_MIME->setParam('html_charset', $action['charset']);
                             $MAIL_MIME->setParam('text_charset', $action['charset']);
                             $action['msg'] = $MAIL_MIME->getMessage();
                         }
                         // escape lines which start is a .
                         $action['msg'] = preg_replace('/(^|\\r?\\n)\\./', "\$1..", $action['msg']);
                         if ($action['htmlmsg']) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":mime text:" . RCUBE_SIEVE_NEWLINE . $action['msg'] . RCUBE_SIEVE_NEWLINE . "." . RCUBE_SIEVE_NEWLINE . ";" . RCUBE_SIEVE_NEWLINE;
                         } elseif ($action['charset'] != "UTF-8") {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":mime text:" . RCUBE_SIEVE_NEWLINE . "Content-Type: text/plain; charset=" . $action['charset'] . RCUBE_SIEVE_NEWLINE . RCUBE_SIEVE_NEWLINE . $action['msg'] . RCUBE_SIEVE_NEWLINE . "." . RCUBE_SIEVE_NEWLINE . ";" . RCUBE_SIEVE_NEWLINE;
                         } elseif (strpos($action['msg'], "\n") !== false) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . "text:" . RCUBE_SIEVE_NEWLINE . $action['msg'] . RCUBE_SIEVE_NEWLINE . "." . RCUBE_SIEVE_NEWLINE . ";" . RCUBE_SIEVE_NEWLINE;
                         } else {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . "\"" . $this->_escape_string($action['msg']) . "\";" . RCUBE_SIEVE_NEWLINE;
                         }
                         break;
                     case 'imapflags':
                     case 'imap4flags':
                         array_push($exts, $action['type']);
                         if (strpos($actions, "setflag") !== false) {
                             $actions .= RCUBE_SIEVE_INDENT . "addflag \"" . $this->_escape_string($action['target']) . "\";" . RCUBE_SIEVE_NEWLINE;
                         } else {
                             $actions .= RCUBE_SIEVE_INDENT . "setflag \"" . $this->_escape_string($action['target']) . "\";" . RCUBE_SIEVE_NEWLINE;
                         }
                         break;
                     case 'notify':
                         array_push($exts, 'notify');
                         $actions .= RCUBE_SIEVE_INDENT . "notify" . RCUBE_SIEVE_NEWLINE;
                         $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":method \"" . $this->_escape_string($action['method']) . "\"" . RCUBE_SIEVE_NEWLINE;
                         if (!empty($action['options'])) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":options [\"" . str_replace(",", "\",\"", $this->_escape_string($action['options'])) . "\"]" . RCUBE_SIEVE_NEWLINE;
                         }
                         if (!empty($action['from'])) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":from \"" . $this->_escape_string($action['from']) . "\"" . RCUBE_SIEVE_NEWLINE;
                         }
                         if (!empty($action['importance'])) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":importance \"" . $this->_escape_string($action['importance']) . "\"" . RCUBE_SIEVE_NEWLINE;
                         }
                         $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":message \"" . $this->_escape_string($action['msg']) . "\";" . RCUBE_SIEVE_NEWLINE;
                         break;
                     case 'enotify':
                         array_push($exts, 'enotify');
                         $actions .= RCUBE_SIEVE_INDENT . "notify" . RCUBE_SIEVE_NEWLINE;
                         if (!empty($action['options'])) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":options [\"" . str_replace(",", "\",\"", $this->_escape_string($action['options'])) . "\"]" . RCUBE_SIEVE_NEWLINE;
                         }
                         if (!empty($action['from'])) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":from \"" . $this->_escape_string($action['from']) . "\"" . RCUBE_SIEVE_NEWLINE;
                         }
                         if (!empty($action['importance'])) {
                             $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":importance \"" . $this->_escape_string($action['importance']) . "\"" . RCUBE_SIEVE_NEWLINE;
                         }
                         $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . ":message \"" . $this->_escape_string($action['msg']) . "\"" . RCUBE_SIEVE_NEWLINE;
                         $actions .= RCUBE_SIEVE_INDENT . RCUBE_SIEVE_INDENT . "\"" . $this->_escape_string($action['method']) . "\";" . RCUBE_SIEVE_NEWLINE;
                         break;
                     case 'keep':
                     case 'discard':
                     case 'stop':
                         $actions .= RCUBE_SIEVE_INDENT . $action['type'] . ";" . RCUBE_SIEVE_NEWLINE;
                         break;
                 }
             }
             $script .= $actions . "}" . RCUBE_SIEVE_NEWLINE;
         }
     }
     if ($variables) {
         $variables .= RCUBE_SIEVE_NEWLINE;
     }
     // requires
     $exts = array_unique($exts);
     if (sizeof($exts)) {
         $script = 'require ["' . implode('","', $exts) . "\"];" . RCUBE_SIEVE_NEWLINE . RCUBE_SIEVE_NEWLINE . $variables . $script;
     }
     // author
     if ($script && RCUBE_SIEVE_HEADER) {
         $script = RCUBE_SIEVE_HEADER . RCUBE_SIEVE_NEWLINE . $script;
     }
     return $script;
 }
function tests_smtp()
{
    //ini_set('display_errors', 1);ini_set('error_reporting', E_ALL);ini_set('error_prepend_string',null);ini_set('error_append_string',null);
    include_once dirname(__FILE__) . "/ressources/externals/mime/mime.inc";
    header("content-type: application/x-javascript");
    $sock = new sockets();
    $sock = new wifidog_settings($_GET["ruleid"]);
    $wifidog_templates = new wifidog_templates($_GET["ruleid"]);
    $ArticaSplashHotSpotEndTime = $sock->GET_INFO("ArticaSplashHotSpotEndTime");
    $proto = "http";
    $myHostname = $_SERVER["HTTP_HOST"];
    $page = CurrentPageName();
    if (isset($_SERVER["HTTPS"])) {
        $proto = "https";
    }
    $URL_REDIRECT = "{$proto}://{$myHostname}/{$page}?wifidog-confirm=NONE";
    $tpl = new templates();
    $smtp_sender = $sock->GET_INFO("smtp_sender");
    if ($GLOBALS["VERBOSE"]) {
        echo "new Mail_mime....<br>\n";
    }
    include_once dirname(__FILE__) . "/ressources/externals/mime/mime.inc";
    $message = new Mail_mime("\r\n");
    $text = "<p style=font-size:18px>{$wifidog_templates->REGISTER_MESSAGE}</p><p>\n\t<hr><center><a href=\"{$URL_REDIRECT}\" style='font-size:22px;text-decoration:underline'>{$URL_REDIRECT}</a></center></p>";
    $message->setFrom($smtp_sender);
    $message->addTo($smtp_sender);
    $message->setSubject($wifidog_templates->REGISTER_SUBJECT);
    $message->setTXTBody(strip_tags($text));
    // for plain-text
    $message->setHTMLBody($text);
    $finalbody = $message->getMessage();
    if ($GLOBALS["VERBOSE"]) {
        echo $finalbody . "<hr>\n";
    }
    $webauth_msmtp = new webauth_msmtp($smtp_sender, $finalbody, $smtp_sender, $_GET["ruleid"]);
    if ($webauth_msmtp->Send()) {
        echo "alert('Rule: {$_GET["ruleid"]} {$smtp_sender} " . $tpl->javascript_parse_text("{$wifidog_templates->REGISTER_SUBJECT}\nTo {$smtp_sender}: {success}") . "');";
        return;
    } else {
        echo "alert('Rule: {$_GET["ruleid"]} {$smtp_sender} Method 1 " . $tpl->javascript_parse_text($webauth_msmtp->logs) . "');";
    }
    $smtp = new smtp();
    if ($sock->GET_INFO("smtp_auth_user") != null) {
        $params["auth"] = true;
        $params["user"] = $sock->GET_INFO("smtp_auth_user");
        $params["pass"] = $sock->GET_INFO("smtp_auth_passwd");
    }
    $params["host"] = $sock->GET_INFO("smtp_server_name");
    $params["port"] = $sock->GET_INFO("smtp_server_port");
    if (!$smtp->connect($params)) {
        echo "alert('" . $tpl->javascript_parse_text("{error_while_sending_message} {error} {$smtp->error_number} {$smtp->error_text}") . "');";
        return;
    }
    if (!$smtp->send(array("from" => $smtp_sender, "recipients" => $smtp_sender, "body" => $finalbody, "headers" => null))) {
        $smtp->quit();
        echo "alert('" . $tpl->javascript_parse_text("{error_while_sending_message} {error} {$smtp->error_number} {$smtp->error_text}") . "');";
        return;
    }
    echo "alert('" . $tpl->javascript_parse_text("{$wifidog_templates->REGISTER_SUBJECT}\nTo {$smtp_sender}: {success}") . "');";
    $smtp->quit();
}
 /**
  * Remove attachments from a message
  */
 function remove_attachments()
 {
     $rcmail = rcmail::get_instance();
     $imap = $rcmail->storage;
     $MESSAGE = new rcube_message(get_input_value('_uid', RCUBE_INPUT_GET));
     $headers = $this->_parse_headers($imap->get_raw_headers($MESSAGE->uid));
     // set message charset as default
     if (!empty($MESSAGE->headers->charset)) {
         $imap->set_charset($MESSAGE->headers->charset);
     }
     // Remove old MIME headers
     unset($headers['MIME-Version']);
     unset($headers['Content-Type']);
     $MAIL_MIME = new Mail_mime($rcmail->config->header_delimiter());
     $MAIL_MIME->headers($headers);
     if ($MESSAGE->has_html_part()) {
         $body = $MESSAGE->first_html_part();
         $MAIL_MIME->setHTMLBody($body);
     }
     $body = $MESSAGE->first_text_part();
     $MAIL_MIME->setTXTBody($body, false, true);
     foreach ($MESSAGE->attachments as $attachment) {
         if ($attachment->mime_id != get_input_value('_part', RCUBE_INPUT_GET) && get_input_value('_part', RCUBE_INPUT_GET) != '-1') {
             $MAIL_MIME->addAttachment($MESSAGE->get_part_content($attachment->mime_id), $attachment->mimetype, $attachment->filename, false, $attachment->encoding, $attachment->disposition, '', $attachment->charset);
         }
     }
     foreach ($MESSAGE->mime_parts as $attachment) {
         if (!empty($attachment->content_id)) {
             // covert CID to Mail_MIME format
             $attachment->content_id = str_replace('<', '', $attachment->content_id);
             $attachment->content_id = str_replace('>', '', $attachment->content_id);
             if (empty($attachment->filename)) {
                 $attachment->filename = $attachment->content_id;
             }
             $MESSAGE_body = $MAIL_MIME->getHTMLBody();
             $dispurl = 'cid:' . $attachment->content_id;
             $MESSAGE_body = str_replace($dispurl, $attachment->filename, $MESSAGE_body);
             $MAIL_MIME->setHTMLBody($MESSAGE_body);
             $MAIL_MIME->addHTMLImage($MESSAGE->get_part_content($attachment->mime_id), $attachment->mimetype, $attachment->filename, false);
         }
     }
     // encoding settings for mail composing
     $MAIL_MIME->setParam('head_encoding', $MESSAGE->headers->encoding);
     $MAIL_MIME->setParam('head_charset', $MESSAGE->headers->charset);
     foreach ($MESSAGE->mime_parts as $mime_id => $part) {
         $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
         if ($mimetype == 'text/html') {
             $MAIL_MIME->setParam('text_encoding', $part->encoding);
             $MAIL_MIME->setParam('html_charset', $part->charset);
         } else {
             if ($mimetype == 'text/plain') {
                 $MAIL_MIME->setParam('html_encoding', $part->encoding);
                 $MAIL_MIME->setParam('text_charset', $part->charset);
             }
         }
     }
     $saved = $imap->save_message($_SESSION['mbox'], $MAIL_MIME->getMessage());
     //write_log("debug","saved=".$saved);
     if ($saved) {
         $imap->delete_message($MESSAGE->uid);
         // Assume the one we just added has the highest UID
         //dsoares $uids = $imap->conn->fetchUIDs($imap->mod_mailbox($_SESSION['mbox']));
         //dsoares $uid = end($uids);
         $uid = $saved;
         //dsoares
         // set flags
         foreach ($MESSAGE->headers->flags as $flag) {
             $imap->set_flag($uid, strtoupper($flag), $_SESSION['mbox']);
         }
         $this->api->output->command('display_message', $this->gettext('attachmentremoved'), 'confirmation');
         $this->api->output->command('removeattachments_reload', $uid);
     } else {
         $this->api->output->command('display_message', $this->gettext('removefailed'), 'error');
     }
     $this->api->output->send();
 }