<?php

include_once __DIR__ . '/../../../Amfphp/util/HttpUtil.php';
include_once __DIR__ . '/debug.php';

use loyalsoft\RenderTime;

/**
 * Description of MainMenu
 * 后台主界面
 * @author gwang (mail@wanggangzero.cn)
 */
class MainMenu extends core {

//put your code here

    public static function index() {
        if (self::_checkGrant()) {
            self::view('mainmenu.tpl');
        } else {
            self::login();
        }
    }

    public static function login() {
        $isAccessGranted = self::_checkGrant(); // 访问授权控制

        $name = Amfphp_BackOffice_AccessManager::SESSION_FIELD_ROLES;           // "91bfa85e5204b5806d536a098caacd31";
        $pwd = Amfphp_BackOffice_AccessManager::AMFPHP_ADMIN_ROLE;              // "a08dd655a04c8c4cd541c92c6d5d1350";

        $errorMessage = '';
        $redirectToHome = false;
        try {
            $paras = loyalsoft\query_paras();
            if ($isAccessGranted) {
                $redirectToHome = true;
            } else if (isset($paras['username'])) {
//user is logging in.

                $username = $paras['username'];
                $password = $paras['password'];

                if (md5($username) == $name && md5($password) == $pwd) {
                    if (session_id() == '') {
                        session_start();
                    }
                    if (!isset($_SESSION[$name])) {
                        $_SESSION[$name] = array();
                    }

                    $_SESSION[$name][$pwd] = true; # 设置授权

                    $redirectToHome = true;
                } else {
                    throw new Exception('Invalid username/password');
                }
            }
            if ($redirectToHome) {
                echo "<script>         window.location = './index.php';        </script>";
                return;
            }
        } catch (Exception $e) {
            $errorMessage = $e->getMessage();
        }

        self::view('login.tpl', compact('redirectToHome', 'errorMessage'));
    }

    public static function debug() {
        if (self::_checkGrant()) {
            self::view(__FUNCTION__ . ".tpl");
        } else {
            self::login();
        }
    }

    public static function monitor() {
        if (self::_checkGrant()) {
            self::view(__FUNCTION__ . ".tpl");
        } else {
            self::login();
        }
    }

    public static function call() {
        if (self::_checkGrant()) {
            $params = loyalsoft\query_paras();
            $className = $params['className'];
            $mname = $params['method'];
            if ($className && $mname) {
                include_once __DIR__ . '/../../../Amfphp/Services/' . str_replace("loyalsoft\\", "", $className) . '.php'; # 直接向客户端输出返回值
                $rt = new RenderTime();
                ob_start();
                $result = call_user_func_array(array($className, $mname), array_values($_POST));
                $content = ob_end_flush();
                $rt->end();
                if (strlen($content) > 0) {
                    echo("输出: " . $content);
                }
                echo "<hr/><hr/>";
                echo("返回值: " . json_encode($result));
                echo "<hr/><hr/>";
                echo("耗时:" . $rt->getRenderTime());
            }
        } else {
            self::login();
        }
    }

    /**
     * 查验授权
     * @return boolean
     */
    private static function _checkGrant() {  // 访问授权控制
        $accessManager = new Amfphp_BackOffice_AccessManager();
        return $accessManager->isAccessGranted();
    }

}
