<?php

$microtime = microtime(1);
$server = '5.63.153.146';
$session_lifetime = 1800;

// ----------------------------------- ÐÑÐ»Ð°ÐŽÐºÐ° -------------------------------- //
error_reporting(32767);
ini_set('display_errors', 0);
// ----------------------------------- ÐÑÐ»Ð°ÐŽÐºÐ° -------------------------------- //

// ------------------------- Ð£ÑÑÐ°ÐœÐŸÐ²ÐºÐž ÑÐ°Ð±ÐŸÑÑ Ñ ÑÐµÑÑÐžÑÐŒÐž ---------------------- //
ini_set('arg_separator.output', '&');
ini_set('session.use_cookies', 1);
ini_set('session.use_trans_sid', 1);
ini_set('session.cookie_domain', $_SERVER['HTTP_HOST']);
ini_set('session.cookie_httponly', 1);
session_name('SID');
// ------------------------- Ð£ÑÑÐ°ÐœÐŸÐ²ÐºÐž ÑÐ°Ð±ÐŸÑÑ Ñ ÑÐµÑÑÐžÑÐŒÐž ---------------------- //

// ------------- Ð€ÑÐœÐºÑÐžÑ Ð·Ð°Ð¿ÑÐŸÑÐ° Ðº API, Ð¿ÑÐŸÐ²ÐµÑÐºÐ° ÐžÑÑÐµÑÐµÐœÐžÑ ÑÐµÑÑÐžÐž ------------- //
function api_query($query)
{
    $content = file_get_contents($query);
    if (strpos($content, 'Authorization required') or $content == '') {
        session_unset();
        session_destroy();
        header('Location: ?error=logon');
        ob_end_flush();
        exit();
    } else {
        return $content;
    }
}
// ------------- Ð€ÑÐœÐºÑÐžÑ Ð·Ð°Ð¿ÑÐŸÑÐ° Ðº API, Ð¿ÑÐŸÐ²ÐµÑÐºÐ° ÐžÑÑÐµÑÐµÐœÐžÑ ÑÐµÑÑÐžÐž ------------- //

// ------------------- ÐÑÑÐµÑÐžÐ·Ð°ÑÐžÑ Ð²ÑÐ²ÐŸÐŽÐ° Ðž ÑÐ¶Ð°ÑÐžÐµ ÐŽÐŸÐºÑÐŒÐµÐœÑÐ° ------------------ //
if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
    $accept_encoding = $_SERVER['HTTP_ACCEPT_ENCODING'];
} elseif (isset($_SERVER['HTTP_TE'])) {
    $accept_encoding = $_SERVER['HTTP_TE'];
} else {
    $accept_encoding = '';
}

if (strpos($accept_encoding, 'gzip')) {
    header('Content-Encoding: gzip');
    function gzencode5($data)
    {
        return gzencode($data, 5);
    }
    ob_start('gzencode5');
} elseif (strpos($accept_encoding, 'deflate')) {
    header('Content-Encoding: deflate');
    function gzdeflate5($output)
    {
        return gzdeflate($output, 5);
    }
    ob_start('gzdeflate5');
} else {
    ob_start();
}
// ------------------- ÐÑÑÐµÑÐžÐ·Ð°ÑÐžÑ Ð²ÑÐ²ÐŸÐŽÐ° Ðž ÑÐ¶Ð°ÑÐžÐµ ÐŽÐŸÐºÑÐŒÐµÐœÑÐ° ------------------ //

session_start();
$func = isset($_GET['func']) ? $_GET['func'] : '';

// ---------------------- ÐÑÐŸÐ²ÐµÑÐºÐ° ÐŽÐµÐ¹ÑÑÐ²ÐžÑÐµÐ»ÑÐœÐŸÑÑÐž ÑÐµÑÑÐžÐž -------------------- //
/*if ($func <> '' and $func <> 'exit') {
    if (empty($_SESSION['auth']) or empty($_SESSION['lifetime']) or empty($_SESSION['username']) or empty($_SESSION['password'])) {
        session_unset();
        session_destroy();
        header('Location: ?error=logon');
        ob_end_flush();
        exit();
    } elseif ($_SESSION['auth'] == '' or $_SESSION['lifetime'] == '' or $_SESSION['username'] == '' or $_SESSION['password'] == '') {
        session_unset();
        session_destroy();
        header('Location: ?error=logon');
        ob_end_flush();
        exit();
    } elseif ($_SESSION['lifetime'] < $_SERVER['REQUEST_TIME'] - $session_lifetime) {
        session_unset();
        session_destroy();
        header('Location: ?error=expire');
        ob_end_flush();
        exit();
    } else {
        $_SESSION['lifetime'] = $_SERVER['REQUEST_TIME'];
    }
}*/
// ---------------------- ÐÑÐŸÐ²ÐµÑÐºÐ° ÐŽÐµÐ¹ÑÑÐ²ÐžÑÐµÐ»ÑÐœÐŸÑÑÐž ÑÐµÑÑÐžÐž -------------------- //

// --------------------------- ÐÑÐºÐ»ÑÑÐµÐœÐžÐµ ÐºÑÑÐžÑÐŸÐ²Ð°ÐœÐžÑ ------------------------- //
#header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0');
#header('Pragma: no-cache');
#header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
#header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
// --------------------------- ÐÑÐºÐ»ÑÑÐµÐœÐžÐµ ÐºÑÑÐžÑÐŸÐ²Ð°ÐœÐžÑ ------------------------- //

// -------------------- ÐÑÐ¿ÑÐ°Ð²ÐºÐ° Ð·Ð°Ð³ÐŸÐ»ÐŸÐ²ÐºÐ° Ñ ÑÐžÐ¿ÐŸÐŒ ÐŽÐŸÐºÑÐŒÐµÐœÑÐ° ------------------ //
if (isset($_SERVER['HTTP_ACCEPT'])) {
    if (strpos($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml')) {
        header('Content-Type: application/xhtml+xml; charset=UTF-8');
    } else {
        header('Content-Type: text/html; charset=UTF-8');
    }
} else {
    header('Content-Type: text/html; charset=UTF-8');
}
// -------------------- ÐÑÐ¿ÑÐ°Ð²ÐºÐ° Ð·Ð°Ð³ÐŸÐ»ÐŸÐ²ÐºÐ° Ñ ÑÐžÐ¿ÐŸÐŒ ÐŽÐŸÐºÑÐŒÐµÐœÑÐ° ------------------ //


echo "\n\n";

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n";
echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru">' . "\n";
echo '<head>' . "\n";
echo '<title>W-Hosting.Tk - ISP Mobile</title>' . "\n";
echo '<meta http-equiv="Content-Type" content="application/vnd.wap.xhtml+xml; charset=UTF-8" />' . "\n";
echo '<meta name="viewport"
content= "width=device-width, initial-
scale=1.0" />' . "\n";
echo '<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />' . "\n";
echo '<link rel="stylesheet" type="text/css" href="style.css" />' . "\n";
echo '<link rel="stylesheet" type="text/css" href="bootstrap.css" />' . "\n";
echo '<script src="jquery.min.js"></script> ' . "\n";
echo '</head>' . "\n";
echo '<body>' . "\n";
echo '<div class="head"><a href="/">' . "\n";
echo '</a></div>' . "\n";

if ($func == '') {
    echo '<div style="text-align:center">' . "\n";
    if (empty($_POST['submit']) and (empty($_GET['username']) or empty($_GET['password']))) {
        if (isset($_GET['error'])) {
            if ($_GET['error'] == 'logon') {
                echo 'Ошибка!!!' . "\n";
                echo '<br />' . "\n";
            } elseif ($_GET['error'] == 'authfail') {
                echo 'Ошибка!!!' . "\n";
                echo '<br />' . "\n";
            } elseif ($_GET['error'] == 'expire') {
                echo 'Ошибка!!!' . "\n";
                echo '<br />' . "\n";
            }
        }
        echo '<form action="index.php" method="post">' . "\n";
        echo 'Логин:<br />' . "\n";
        echo '<input name="username" /><br />' . "\n";
        echo 'Пароль:<br />' . "\n";
        echo '<input name="password" type="password" /><br />' . "\n";
        echo '<input name="submit" type="submit" value="Войти" />' . "\n";
        echo '</form>' . "\n";

    } else {
        $content = api_query('https://' . $server . '/manager/ispmgr?func=auth&out=xml&username=' . urlencode($_REQUEST['username']) . '&password=' . urlencode($_REQUEST['password']));
        $parse_xml = simplexml_load_string($content);
        if (isset($parse_xml->auth)) {
            $_SESSION['lifetime'] = $_SERVER['REQUEST_TIME'];
            $_SESSION['auth'] = (string)$parse_xml->auth;
            $_SESSION['username'] = $_REQUEST['username'];
            $_SESSION['password'] = $_REQUEST['password'];
            $content = api_query('https://' . $server . '/manager/ispmgr?func=usrparam&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
            $parse_xml_usrparam = simplexml_load_string($content);
            $_SESSION['rows'] = (int)$parse_xml_usrparam->rows;

            if (isset($_GET['username']) and isset($_GET['password'])) {
                header('Location: ?func=menu&' . SID);
                ob_end_flush();
                exit;
            }

            echo 'Успешная авторизация!<br />' . "\n";
            echo '<a href="?func=menu">Перейти в меню</a>' . "\n";
        } else {
            session_unset();
            session_destroy();
            header('Location: ?error=authfail');
            ob_end_flush();
            exit();
        }
    }
    echo '</div>' . "\n";
} elseif (file_exists('./func/' . $func . '.php')) {
    require('./func/' . $func . '.php');
include 'menu.php';
} else {
    session_unset();
    session_destroy();
    header('Location: ?error=logon');
    ob_end_flush();
    exit();
}

echo '' . "\n";
echo '<div style="text-align:center" class="post">' . "\n";
echo '<font color="#FFFFFF">© <a href="http://vk.com/piminov_remont"><font color="#FFFFFF">Piminov Nikita</font></a></font>';

if (date('Y') <> '2010') {
    //echo '-' . date('Y');
}

echo "\n";
echo '</div>' . "\n";
echo '</body>' . "\n";
echo '</html>' . "\n";

echo "\n";
ob_end_flush();

?>