Esempio n. 1
0
 public function route(Zend_Controller_Request_Abstract $dispatcher)
 {
     try {
         $getopt = new Zend_Console_Getopt(array('verbose|v' => 'Print verbose output', 'file|f=s' => 'File to upload'));
         $getopt->parse;
         $arguments = $getopt->getRemainingArgs();
     } catch (Zend_Console_Getopt_Exception $e) {
         echo $e->getUsageMessage();
         exit;
     }
     if ($arguments) {
         $command = array_shift($arguments);
         $action = array_shift($arguments);
         if (!preg_match('~\\W~', $command)) {
             $dispatcher->setControllerName($command);
             $dispatcher->setActionName($action);
             $dispatcher->setParams($arguments);
             if (isset($getopt->v)) {
                 $dispatcher->setParam('verbose', true);
             }
             if (isset($getopt->f)) {
                 $dispatcher->setParam('file', $getopt->f);
             }
             return $dispatcher;
         }
         echo "Invalid command.\n", exit;
     }
     echo "No command given.\n", exit;
 }
Esempio n. 2
0
    /**
     * Retrieve rendered contents of a controller action
     *
     * If the action results in a forward or redirect, returns empty string.
     *
     * @param  string $action
     * @param  string $controller
     * @param  string $module Defaults to default module
     * @param  array $params
     * @return string
     */
    public function action($action, $controller, $module = null, array $params = array())
    {
        $this->resetObjects();
        if (null === $module) {
            $module = $this->defaultModule;
        }

        // clone the view object to prevent over-writing of view variables
        $viewRendererObj = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        Zend_Controller_Action_HelperBroker::addHelper(clone $viewRendererObj);

        $this->request->setParams($params)
                      ->setModuleName($module)
                      ->setControllerName($controller)
                      ->setActionName($action)
                      ->setDispatched(true);

        $this->dispatcher->dispatch($this->request, $this->response);

        // reset the viewRenderer object to it's original state
        Zend_Controller_Action_HelperBroker::addHelper($viewRendererObj);


        if (!$this->request->isDispatched()
            || $this->response->isRedirect())
        {
            // forwards and redirects render nothing
            return '';
        }

        $return = $this->response->getBody();
        $this->resetObjects();
        return $return;
    }
Esempio n. 3
0
 /**
  *
  * @param Zend_Controller_Request_Abstract $dispatcher
  * @return Zend_Controller_Request_Abstract 
  */
 public function route(Zend_Controller_Request_Abstract $dispatcher)
 {
     $getopt = new Zend_Console_Getopt(array());
     $arguments = $getopt->getRemainingArgs();
     $controller = null;
     $action = null;
     $params = array();
     if ($arguments) {
         foreach ($arguments as $index => $command) {
             if (preg_match('/([a-z0-9]+)=([a-z0-9]+)/i', trim($command), $match)) {
                 switch ($match[1]) {
                     case 'controller':
                         $controller = $match[2];
                         break;
                     case 'action':
                         $action = $match[2];
                         break;
                     default:
                         $params[$match[1]] = $match[2];
                 }
             }
         }
         $action = empty($action) ? 'index' : $action;
         $controller = empty($controller) ? 'index' : $controller;
         $dispatcher->setControllerName($controller);
         $dispatcher->setActionName($action);
         $dispatcher->setParams($params);
         return $dispatcher;
     }
     echo "Invalid command.\n";
     echo "No command given.\n", exit;
 }
Esempio n. 4
0
 public function route(Zend_Controller_Request_Abstract $dispatcher)
 {
     $getopt = new Zend_Console_Getopt(array());
     $arguments = $getopt->getRemainingArgs();
     $controller = "";
     $action = "";
     $params = array();
     if ($arguments) {
         foreach ($arguments as $index => $command) {
             $details = explode("=", $command);
             if ($details[0] == "controller") {
                 $controller = $details[1];
             } else {
                 if ($details[0] == "action") {
                     $action = $details[1];
                 } else {
                     $params[$details[0]] = $details[1];
                 }
             }
         }
         if ($action == "" || $controller == "") {
             die("\n\t\t\t\t\t\tMissing Controller and Action Arguments\n\t\t\t\t\t\t==\n\t\t\t\t\t\tYou should have:\n\t\t\t\t\t\tphp script.php controller=[controllername] action=[action] token=[token]\n\t\t\t\t\t\t");
         }
         $dispatcher->setModuleName('cronjob');
         $dispatcher->setControllerName($controller);
         $dispatcher->setActionName($action);
         $dispatcher->setParams($params);
         return $dispatcher;
     }
     echo "Invalid command.\n", exit;
     echo "No command given.\n", exit;
 }
Esempio n. 5
0
 public function preDispatch(Zend_Controller_Request_Abstract $request)
 {
     if ($this->_request->isDelete()) {
         $deleteParams = array();
         parse_str($this->_request->getRawBody(), $deleteParams);
         $request->setParams($deleteParams);
     }
 }
Esempio n. 6
0
 /**
  * Before dispatching, digest PUT request body and set params
  *
  * @param Zend_Controller_Request_Abstract $request
  */
 public function preDispatch(Zend_Controller_Request_Abstract $request)
 {
     if (!$request instanceof Zend_Controller_Request_Http) {
         return;
     }
     if ($this->_request->isPut()) {
         $putParams = array();
         parse_str($this->_request->getRawBody(), $putParams);
         $request->setParams($putParams);
     }
 }
 public function route(Zend_Controller_Request_Abstract $dispatcher)
 {
     $getopt = new Zend_Console_Getopt(array());
     $getopt->addRules(array('controller|c=s' => 'Controller Name', 'action|a=s' => 'Controller Action', 'from|f=s' => 'From yyyy-mm-dd', 'to|t=s' => 'To yyyy-mm-dd'));
     $arguments = $getopt->getRemainingArgs();
     if ($getopt->getOption('controller') && $getopt->getOption('action')) {
         $dispatcher->setControllerName($getopt->getOption('controller'));
         $dispatcher->setActionName($getopt->getOption('action'));
         $dispatcher->setParams(array('from' => $getopt->getOption('from'), 'to' => $getopt->getOption('to')));
         return $dispatcher;
     }
 }
Esempio n. 8
0
 protected function _forwardLogin(Zend_Controller_Request_Abstract $request)
 {
     $request->setModuleName('kwf_controller_action_user');
     $request->setControllerName('login');
     $request->setDispatched(false);
     if (substr($request->getActionName(), 0, 4) == 'json') {
         $request->setActionName('json-login');
     } else {
         $params = array('location' => $request->getBaseUrl() . '/' . ltrim($request->getPathInfo(), '/'));
         $request->setParams($params);
         $request->setActionName('index');
     }
 }
Esempio n. 9
0
 public function routeStartup(Zend_Controller_Request_Abstract $request)
 {
     $this->_baseUrl = HTTP_HOST . $request->getBaseUrl();
     if ($this->_baseUrl) {
         $params = $request->getParams();
         if (sizeof($params)) {
             foreach ($params as $name => &$param) {
                 $param = str_ireplace($this->_baseUrl, '', $param);
                 if (array_key_exists($name, $_POST)) {
                     $_POST[$name] = $param;
                 }
             }
         }
         $request->setParams($params);
         Zend_Controller_Front::getInstance()->unregisterPlugin($this)->registerPlugin($this, 99999);
         // перерегистрируем плагин чтобы dispatchLoopShutdown запустился последним
     }
 }
Esempio n. 10
0
 /**
  * @inheritdoc
  * @throws Api_Exception_BadMethodCall
  */
 public function route(Zend_Controller_Request_Abstract $dispatcher)
 {
     $arguments = (new Zend_Console_Getopt([]))->getRemainingArgs();
     if ($arguments) {
         $command = array_shift($arguments);
         $action = array_shift($arguments);
         if (!$action) {
             $action = 'index';
         }
         if (!preg_match('~\\W~', $command)) {
             $dispatcher->setControllerName($command);
             $dispatcher->setActionName($action);
             $dispatcher->setModuleName('api');
             $dispatcher->setParams($arguments);
             return $dispatcher;
         }
     }
     throw new Api_Exception_BadMethodCall();
 }
Esempio n. 11
0
File: Sitemap.php Progetto: cwcw/cms
 /**
  * 
  */
 public function routeShutdown(Zend_Controller_Request_Abstract $request)
 {
     if ($request->has('mca')) {
         $mca = $request->getParam('mca');
         list($module, $controller, $action) = explode('/', $mca);
         $request->setModuleName($module)->setControllerName($controller)->setActionName($action);
         $keys = $request->getParam('key');
         $values = $request->getParam('value');
         $request->setParamSources();
         $params = array_combine($keys, $values);
         if (get_magic_quotes_gpc()) {
             foreach ($params as $key => &$value) {
                 $decode = Zend_Json::decode(stripslashes($value), Zend_Json::TYPE_ARRAY);
                 if (!empty($decode)) {
                     $value = $decode;
                 }
             }
         }
         $request->setParams($params);
     }
 }
Esempio n. 12
0
 /**
  * Refreshes the parameter list for the specified request.
  *
  * @param Zend_Controller_Request_Abstract $request
  * @param Array $params
  */
 protected function _applyParams(Zend_Controller_Request_Abstract $request, array $data)
 {
     $rData = empty($data['data']) ? array() : $data['data'];
     if (!Conjoon_Util_Array::isAssociative($rData)) {
         $request->setParam('data', $rData);
     } else {
         $request->setParams($rData);
     }
     unset($data['data']);
     $request->setParam($this->_config['extParameter'], $data);
 }
 /**
  * PHP only parses the body into $_POST if its a POST request
  * this parses the reqest body in accordance with RFC2616 spec regardless of the HTTP method
  */
 private function handleRequestBody(Zend_Controller_Request_Abstract $request)
 {
     $header = strtolower($request->getHeader('Content-Type'));
     // cleanup the charset part
     $header = current(explode(';', $header));
     // detect request body content type
     foreach ($this->requestTypes as $contentType) {
         if ($header == $contentType) {
             break;
         }
     }
     // extract the raw body
     $rawBody = $request->getRawBody();
     // treat these two separately because of the way PHP treats POST
     if (in_array($contentType, array('multipart/form-data', 'application/x-www-form-urlencoded'))) {
         // PHP takes care of everything for us in this case lets just modify the $_FILES array
         if ($request->isPost() && $contentType == 'multipart/form-data') {
             // if there are files, lets modify the array to match what we've done below
             foreach ($_FILES as &$file) {
                 $data = file_get_contents($file['tmp_name']);
                 $file['content'] = base64_encode($data);
             }
             // reset the array pointer
             unset($file);
         } else {
             switch ($contentType) {
                 case 'application/x-www-form-urlencoded':
                     parse_str($rawBody, $_POST);
                     break;
                     // this is wher the magic happens
                     // creates the $_FILES array for none POST requests
                 // this is wher the magic happens
                 // creates the $_FILES array for none POST requests
                 case 'multipart/form-data':
                     // extract the boundary
                     parse_str(end(explode(';', $request->getHeader('Content-Type'))));
                     if (isset($boundary)) {
                         // get rid of the boundary at the edges
                         if (preg_match(sprintf('/--%s(.+)--%s--/s', $boundary, $boundary), $rawBody, $regs)) {
                             // split into chuncks
                             $chunks = explode('--' . $boundary, trim($regs[1]));
                             foreach ($chunks as $chunk) {
                                 // parse each chunk
                                 if (preg_match('/Content-Disposition: form-data; name="(?P<name>.+?)"(?:; filename="(?P<filename>.+?)")?(?P<headers>(?:\\r|\\n)+?.+?(?:\\r|\\n)+?)?(?P<data>.+)/si', $chunk, $regs)) {
                                     // dedect a file upload
                                     if (!empty($regs['filename'])) {
                                         // put aside for further analysis
                                         $data = $regs['data'];
                                         $headers = $this->parseHeaders($regs['headers']);
                                         // set our params variable
                                         $_FILES[$regs['name']] = array('name' => $regs['filename'], 'type' => $headers['Content-Type'], 'size' => mb_strlen($data), 'content' => base64_encode($data));
                                         // otherwise its a regular key=value combination
                                     } else {
                                         $_POST[$regs['name']] = trim($regs['data']);
                                     }
                                 }
                             }
                         }
                     }
                     break;
             }
         }
         $request->setParams($_POST + $_FILES);
     } elseif (!empty($rawBody)) {
         // seems like we are dealing with an encoded request
         try {
             switch ($contentType) {
                 case 'text/javascript':
                 case 'application/json':
                 case 'application/javascript':
                     $_POST = (array) Zend_Json::decode($rawBody, Zend_Json::TYPE_OBJECT);
                     break;
                 case 'text/xml':
                 case 'application/xml':
                     $json = @Zend_Json::fromXml($rawBody);
                     $_POST = (array) Zend_Json::decode($json, Zend_Json::TYPE_OBJECT)->request;
                     break;
                 case 'text/php':
                 case 'application/x-httpd-php':
                 case 'application/x-httpd-php-source':
                     $_POST = (array) unserialize($rawBody);
                     break;
                 default:
                     $_POST = (array) $rawBody;
                     break;
             }
             $request->setParams($_POST);
         } catch (Exception $e) {
             $request->dispatchError(REST_Response::BAD_REQUEST, 'Invalid Payload Format');
             return;
         }
     }
 }
Esempio n. 14
0
 /**
  * Processes a request and sets its controller and action.  If
  * no route was possible, an exception is thrown.
  *
  * @param  \Zend_Controller_Request_Abstract
  * @throws \Zend_Controller_Router_Exception
  * @return \Zend_Controller_Request_Abstract|boolean
  */
 public function route(\Zend_Controller_Request_Abstract $request)
 {
     $options = array('help|h' => 'Show this help', 'org|o=i' => 'The user organization number', 'pwd|p=s' => 'User password', 'user|u=s' => 'The user name');
     $getopt = new \Zend_Console_Getopt($options);
     try {
         $getopt->parse();
     } catch (\Zend_Console_Getopt_Exception $e) {
         echo $this->_expandMessage($e);
         exit;
     }
     if ($getopt->getOption('h')) {
         // $getopt->s
         echo $this->_expandMessage($getopt);
         exit;
     }
     if ($request instanceof \MUtil_Controller_Request_Cli) {
         $request->setUserLogin($getopt->getOption('u'), $getopt->getOption('o'), $getopt->getOption('p'));
     }
     $arguments = $getopt->getRemainingArgs();
     if ($arguments) {
         $controller = array_shift($arguments);
         $action = array_shift($arguments);
         if (!$action) {
             $action = 'index';
         }
         if (preg_match('/^\\w+(-\\w+)*$/', $controller) && preg_match('/^\\w+(-\\w+)*$/', $action)) {
             $request->setControllerName($controller);
             $request->setActionName($action);
             $params[$request->getControllerKey()] = $controller;
             $params[$request->getActionKey()] = $action;
             foreach ($arguments as $arg) {
                 if (\MUtil_String::contains($arg, '=')) {
                     list($name, $value) = explode('=', $arg, 2);
                 } else {
                     $name = $arg;
                     $value = '';
                 }
                 $params[$name] = $value;
             }
             $request->setParams($params);
             return $request;
         }
         echo "Invalid command: {$controller}/{$action}.\n", exit;
     }
     echo "No command given.\n\n";
     echo $this->_expandMessage($getopt), exit;
 }
 /**
  * Route a request
  *
  * Routes requests of the format /controller/action by default (action may 
  * be omitted). Additional parameters may be specified as key/value pairs
  * separated by the directory separator: 
  * /controller/action/key/value/key/value. 
  *
  * To specify a module to use (basically, subdirectory) when routing the 
  * request, set the 'useModules' parameter via the front controller or 
  * {@link setParam()}: $router->setParam('useModules', true)
  * 
  * @param Zend_Controller_Request_Abstract $request 
  * @return void
  */
 public function route(Zend_Controller_Request_Abstract $request)
 {
     if (!$request instanceof Zend_Controller_Request_Http) {
         throw Zend::exception('Zend_Controller_Router_Exception', 'Zend_Controller_Router requires a Zend_Controller_Request_Http-based request object');
     }
     $pathInfo = $request->getPathInfo();
     $pathSegs = explode('/', trim($pathInfo, '/'));
     /**
      * Retrieve module if useModules is set in object
      */
     $useModules = $this->getParam('useModules');
     if (!empty($useModules)) {
         if (isset($pathSegs[0]) && !empty($pathSegs[0])) {
             $module = array_shift($pathSegs);
         }
     }
     /**
      * Get controller and action from request
      * Attempt to get from path_info; controller is first item, action 
      * second
      */
     if (isset($pathSegs[0]) && !empty($pathSegs[0])) {
         $controller = array_shift($pathSegs);
     }
     if (isset($pathSegs[0]) && !empty($pathSegs[0])) {
         $action = array_shift($pathSegs);
     }
     /**
      * Any optional parameters after the action are stored in
      * an array of key/value pairs:
      *
      * http://www.zend.com/controller-name/action-name/param-1/3/param-2/7
      *
      * $params = array(2) {
      *              ["param-1"]=> string(1) "3"
      *              ["param-2"]=> string(1) "7"
      * }
      */
     $params = array();
     $segs = count($pathSegs);
     if (0 < $segs) {
         for ($i = 0; $i < $segs; $i = $i + 2) {
             $key = urldecode($pathSegs[$i]);
             $value = isset($pathSegs[$i + 1]) ? urldecode($pathSegs[$i + 1]) : null;
             $params[$key] = $value;
         }
     }
     $request->setParams($params);
     /**
      * Set module, controller and action, now that params are set
      */
     if (isset($module)) {
         $request->setParam('module', urldecode($module));
     }
     if (isset($controller)) {
         $request->setControllerName(urldecode($controller));
     }
     if (isset($action)) {
         $request->setActionName(urldecode($action));
     }
     return $request;
 }