SiteController.php 4.52 KB
<?php

class SiteController extends Controller
{
    /**
     * Declares class-based actions.
     */
    public function actions()
    {
        return array(
                        // captcha action renders the CAPTCHA image displayed on the contact page
                        'captcha' => array(
                                              'class' => 'CCaptchaAction',
                                              'backColor' => 0xFFFFFF,
                                          ),
                        // page action renders "static" pages stored under 'protected/views/site/pages'
                        // They can be accessed via: index.php?r=site/page&view=FileName
                        'page' => array('class' => 'CViewAction'),
                   );
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex()
    {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        if (!Yii::app()->user->isGuest)
        {
            $this->render('index', array('model' => $this->getSystemInfo()));
        }
        else
        {
            $this->actionLogin();
        }
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
        if ( $error = Yii::app()->errorHandler->error)
        {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    /**
     * Displays the contact page
     */
    public function actionContact()
    {
        $model = new ContactForm;
        if ( isset($_POST['ContactForm']) )
        {
            $model->attributes = $_POST['ContactForm'];
            if ( $model->validate() )
            {
                $headers = "From: {$model->email}\r\nReply-To: {$model->email}";
                mail(Yii::app()->params['adminEmail'], $model->subject, $model->body, $headers);
                Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
                $this->refresh();
            }
        }
        $this->render('contact', array('model' => $model));
    }

    /**
     * Displays the login page
     */
    public function actionLogin()
    {
        $model = new LoginForm;

        // if it is ajax validation request
        if ( (isset($_POST['ajax'])) && ($_POST['ajax'] === 'login-form') )
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        // collect user input data
        if ( isset($_POST['LoginForm']) )
        {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            
            if ( ($model->validate()) && ($model->login()) )
            {
                $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        // display the login form
        $this->renderPartial('login',array('model' => $model));
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }
    
    /**
     * 获取系统信息
     */
    protected function getSystemInfo()
    {
        $info = array(
                         '操作系统' => PHP_OS,
                         '运行环境' => $_SERVER["SERVER_SOFTWARE"],
                         'PHP运行方式' => php_sapi_name(),
                         '上传附件限制' => ini_get('upload_max_filesize'),
                         '执行时间限制' => ini_get('max_execution_time').'秒',
                         '服务器时间' => date("Y年n月j日 H:i:s"),
                         '北京时间' => gmdate("Y年n月j日 H:i:s", time() + 8 * 3600),
                         '服务器域名/IP' => $_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]',
                         '剩余空间' => round((@disk_free_space(".")/(1024*1024*1024)), 2).'G',
                         'register_globals' => (get_cfg_var("register_globals") == "1" ) ? "ON" : "OFF",
                         'magic_quotes_gpc' => (1 === get_magic_quotes_gpc()) ? "YES" : "NO",
                         'magic_quotes_runtime' => (1 === get_magic_quotes_runtime()) ? "YES": "NO",
                     );
        return $info;
    }
}
?>