⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ajaxacapplication.class.php

📁 老子上传了已经
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?php
    /**
     * Copyright 2005-06 Zervaas Enterprises (www.zervaas.com.au)
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     *
     * @package AjaxAC
     */

    define('AJAXAC_VERSION', '1.0-alpha');

    /**
     * The different levels of debugging
     */
    define('AJAXAC_DEBUG_NONE', 0);
    define('AJAXAC_DEBUG_CORE_ERROR', 1 << 0);

    define('AJAXAC_DEBUG_ERROR_ALL', AJAXAC_DEBUG_CORE_ERROR);
    define('AJAXAC_DEBUG_ALL', -1);

    /**
     * The base class for all AjaxAC applications. Contains a wide range of functionality
     * for handling requests, subrequest, attaching actions/events/widgets/etc.. as well
     * as generating subrequest URL's and piecing together application JavaScript code.
     * This class should never been instantiated directly, as it won't do anything. Instead,
     * specific applications should extend this class, creating widgets + their events, as
     * well as actions + handlers for sub-requests
     *
     * @author  Quentin Zervaas
     * @access  Public
     */
    class AjaxACApplication
    {
        /**
         * The default character set to use for returned subrequest
         * data. Used only if not specified in the application config
         */
        var $_defaultCharset = 'UTF-8';


        /**
         * AjaxACApplication
         *
         * Constructor. Sets up the base actions, as well as installing
         * the application configuration
         *
         * @access  public
         * @param   array   $config     The application configuration, containing key/name pairs
         */
        function AjaxACApplication($config = array())
        {
            if (!is_array($config))
                $config = array($config);

            $this->_config = $config;

            // try and set the charset value if it hasn't been specified in $config
            $this->setConfigValue('charset', $this->_defaultCharset, false);

            // parameter to set the script name. If this is empty then the current
            // script name will be auto-detected
            $this->setConfigValue('url.script_name', '', false);

            // parameter to store sub-request action in. If empty it is specified like
            // index.php/someaction, other it is like index.php?action=someaction
            $this->setConfigValue('url.action_parameter', '', false);

            // parameter to determine whether or not to send the content-length header
            // which may break multi-byte encodings
            // @todo Find a way to send correct content length with multi-byte encodings
            $this->setConfigValue('js.send_content_length', true, false);


            // various debugging parameters
            $this->setConfigValue('debug.level', AJAXAC_DEBUG_NONE, false);
            $this->setConfigValue('debug.log_file', 'ajaxac.log', false);
        }


        /**
         * handleAction
         *
         * Handle a subrequest. This determines if the request action exists, and if
         * so, then performs if, using the passed on params and request data
         *
         * @access  package
         * @param   string  $action         The action name. The callback will be action_$action
         * @param   array   $params         A 0-indexed array containing each request path element after the action.
         *                                  For example, /path/to/ajaxac/actionname/param1/param2
         * @param   array   $requestData    The '$_GET' data from the request
         */
        function handleAction($action = null, $params = array(), $requestData = array())
        {
            $this->_params = $params;
            $this->_requestData = $requestData;
            $callback = 'action_' . $action;
            if (method_exists($this, $callback)) {
                $this->$callback();
            }
        }


        /**
         * getApplicationUrl
         *
         * Generates the application URL, for the specified action. This is used
         * to generate the request file for XMLHttp sub-requests
         *
         * @access  package
         * @todo    Add extra options for more path/request params
         * @param   string  $action     The action to generate the URL for. Must be a valid action
         * @return  string              The generated application URL
         */
        function getApplicationUrl($action = '')
        {
            if (!isset($this->_web_path)) {
                $this->_web_path = $this->getConfigValue('url.script_name');
                if (strlen($this->_web_path) == 0)
                    $this->_web_path = $_SERVER['SCRIPT_NAME'];
            }

            $ret = $this->_web_path;

            if (strlen($action) > 0 && $this->actionExists($action)) {
                $actionParameter = $this->getConfigValue('url.action_parameter');
                if (strlen($actionParameter) == 0)
                    $ret = $this->_web_path . '/' . $action;
                else
                    $ret = $this->_web_path . '?' . $actionParameter . '=' . $action;
            }

            return $ret;
        }


        /**
         * handleRequest
         *
         * Fetches parameters + options from the current request (be it the main
         * request or a sub-request), and hands it to the application for handling
         *
         * @todo    Allow for extra parameters when action store in GET variable
         * @access  public
         */
        function handleRequest()
        {
            $actionParameter = $this->getConfigValue('url.action_parameter');
            if (strlen($actionParameter) == 0) {
                $path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
                $params = array_filter(explode('/', $path));

                $action = count($params) > 0 ? array_shift($params) : '';
            }
            else {
                $action = isset($_GET[$actionParameter]) ? $_GET[$actionParameter] : '';
                $params = array();
            }

            $this->handleAction($action, $params, $_GET);
        }


        /**
         * registerActions
         *
         * @access protected
         *
         * Register one or more actions, to be handled using a callback
         * named something like action_actionName(). Takes an arbitrary
         * number of parameters.
         */
        function registerActions()
        {
            foreach (func_get_args() as $action) {
                $action = trim($action);
                if (strlen($action) > 0)
                    $this->_actions[] = $action;
            }
        }


        /**
         * actionExists
         *
         * Returns whether or not an action exists
         *
         * @access  protected
         * @return  bool        True if the action exists, false if not
         */
        function actionExists($action)
        {
            return in_array($action, $this->_actions);
        }


        /**
         * getParam
         *
         * Retrieve a parameter from the user request. You can choose to return
         * some default value if the parameter is null or not set
         *
         * @access  protected
         * @param   string  $key        The name of the param to return
         * @param   mixed   $default    A default value to return if the key doesn't exist or is null
         * @return  mixed               The fetched or default value
         */
        function getParam($key, $default = null)
        {
            if (!array_key_exists($key, $this->_params) || is_null($this->_params[$key]))
                return $default;
            return $this->_params[$key];
        }


        /**
         * getRequestValue
         *
         * Retrieve a parameter from the user request. You can choose to return
         * some default value if the parameter is null or not set
         *
         * @access  protected
         * @param   string  $key        The name of the request value to return
         * @param   mixed   $default    A default value to return if the key doesn't exist or is null
         * @return  mixed               The fetched or default value
         */
        function getRequestValue($key, $default = null)
        {
            if (!array_key_exists($key, $this->_requestData) || is_null($this->_requestData[$key]))
                return $default;
            return $this->_requestData[$key];
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -