Beispiel #1
0
 public function testRandomString()
 {
     $re = '|^[A-Za-z][A-Za-z0-9]*$|';
     $this->assertTrue(preg_match($re, $s = FWU::randomString()));
     $this->assertEqual(strlen($s), 40);
     $this->assertTrue(preg_match($re, $s = FWU::randomString(1)));
     $this->assertEqual(strlen($s), 1);
 }
Beispiel #2
0
 private static function _parse($content)
 {
     // create DOMDocument instance
     $dom = new DOMDocument();
     if (!$dom->loadXML('<root>' . $content . '</root>')) {
         throw new Exception('menu plugin: PHP DOM couldn\'t parse menu XML.');
     }
     $dom->normalizeDocument();
     // process <a> elements
     $dom_links = $dom->getElementsByTagName('a');
     $links = array();
     foreach ($dom_links as $dom_link) {
         // get `l' attribute
         if ($dom_link->hasAttribute('l')) {
             $link_id = trim($dom_link->getAttribute('l'));
             if (!$link_id) {
                 throw new Exception('menu plugin: link id cannot be empty.');
             }
             $dom_link->removeAttribute('l');
         } else {
             $link_id = null;
         }
         // add placeholders
         $placeholders = array();
         $placeholders['class'] = '--' . FWU::randomString() . '--';
         if ($dom_link->hasAttribute('class')) {
             $dom_link->setAttribute('class', trim($dom_link->getAttribute('class') . ' ' . $placeholders['class']));
         } else {
             $dom_link->setAttribute('class', $placeholders['class']);
         }
         if (!$dom_link->hasAttribute('id')) {
             $placeholders['id'] = '--' . FWU::randomString() . '--';
             $dom_link->setAttribute('id', $placeholders['id']);
         } else {
             $placeholders['id'] = null;
         }
         // add to $links array
         $links[] = array('id' => $link_id, 'placeholders' => $placeholders);
     }
     // done
     return array('links' => $links, 'html' => preg_replace('|^\\s*<\\?xml version="1.0"\\?>\\s*<root>(.*)</root>\\s*$|s', '$1', $dom->saveXML()));
 }
Beispiel #3
0
 /**
  * Dispatch request
  *
  * @param string|array $url
  * @return void
  */
 public function dispatch($url)
 {
     assert('is_string($url) || is_array($url)');
     // check recursion
     static $dispatch_count = 0;
     if ($dispatch_count >= 50) {
         throw new FWException('FWRequest->dispatch: Too much recursion. (loop?)');
     }
     ++$dispatch_count;
     // get router
     $router = $this->getRouter();
     if (!$router) {
         throw new FWException('Please FWRouter->setRouter() first.');
     }
     try {
         // route url
         if (is_string($url)) {
             $route = $router->route($url);
             if (!$route) {
                 $this->forward404();
             }
             if (!isset($route['controller']) || !isset($route['action'])) {
                 $this->forward404();
             }
         } else {
             $route = $url;
         }
         // validate controller and action names
         $controller = FWU::underscoresToCamelcase($route['controller']);
         $action = FWU::underscoresToCamelcase($route['action']);
         $controller_re = '|^[A-Za-z][A-Za-z0-9]*$|';
         $action_re = '|^[A-Za-z0-9]+$|';
         if (!preg_match($controller_re, $controller) || !preg_match($action_re, $action)) {
             $this->forward404();
         }
         // controller exists?
         $controller_class = $controller . 'Controller';
         $controller_file = APP_DIR . '/controllers/' . $controller_class . '.class.php';
         if (!is_file($controller_file)) {
             $this->forward404();
         }
         require_once $controller_file;
         if (!class_exists($controller_class)) {
             $this->forward404();
         }
         // action exists?
         $action_method = 'execute' . $action;
         if (!method_exists($controller_class, $action_method)) {
             $this->forward404();
         }
         // set parameters
         $this->flushParameters();
         $this->addParameters($route);
         $this->addParameters($_GET);
         $this->addParameters($_POST);
         // call action
         $obj = new $controller_class();
         $obj->setRequest($this);
         $this->forward404Unless($obj->dispatch($action));
     } catch (FWForwardException $e) {
         $this->dispatch($e->parameters);
     }
 }