コード例 #1
0
ファイル: Controller.php プロジェクト: vectrex/vxphp
 /**
  * check whether a an XMLHttpRequest was submitted
  * this will look for a key 'xmlHttpRequest' in both GET and POST and
  * set the Controller::isXhr flag  and
  * decode the parameters accordingly into their ParameterBages
  * in addition the presence of ifuRequest in GET is checked for handling IFRAME uploads
  *
  * this method is geared to fully support the vxJS.widget.xhrForm()
  */
 private function prepareForXhr()
 {
     // do we have a GET XHR?
     if ($this->request->getMethod() === 'GET' && $this->request->query->get('xmlHttpRequest')) {
         $this->xhrBag = $this->request->query;
         foreach (json_decode($this->xhrBag->get('xmlHttpRequest'), TRUE) as $key => $value) {
             $this->xhrBag->set($key, $value);
         }
     } else {
         if ($this->request->getMethod() === 'POST' && $this->request->request->get('xmlHttpRequest')) {
             $this->xhrBag = $this->request->request;
             foreach (json_decode($this->xhrBag->get('xmlHttpRequest'), TRUE) as $key => $value) {
                 $this->xhrBag->set($key, $value);
             }
         } else {
             if ($this->request->query->get('ifuRequest')) {
                 // POST already contains all the parameters
                 $this->request->request->set('httpRequest', 'ifuSubmit');
             } else {
                 $this->isXhr = FALSE;
                 return;
             }
         }
     }
     $this->isXhr = TRUE;
     // handle request for apc upload poll, this will not be left to individual controller
     if ($this->xhrBag && $this->xhrBag->get('httpRequest') === 'apcPoll') {
         $id = $this->xhrBag->get('id');
         if ($this->config->server['apc_on'] && $id) {
             $apcData = apc_fetch('upload_' . $id);
         }
         if (isset($apcData['done']) && $apcData['done'] == 1) {
             apc_clear_cache('user');
         }
         JsonResponse::create($apcData)->send();
         exit;
     }
 }
コード例 #2
0
ファイル: HtmlForm.php プロジェクト: vectrex/vxphp
 /**
  * initialize parameter bag
  * parameterbag is either supplied, or depending on request method retrieved from request object
  *
  * @param ParameterBag $bag
  * @return HtmlForm
  *
  */
 public function bindRequestParameters(ParameterBag $bag = NULL)
 {
     if ($bag) {
         $this->requestValues = $bag;
     } else {
         if ($this->request->getMethod() == 'GET') {
             $this->requestValues = $this->request->query;
         } else {
             $this->requestValues = $this->request->request;
         }
     }
     // set form element values
     foreach ($this->elements as $name => $element) {
         if (is_array($element)) {
             $this->setElementArrayRequestValue($name);
         } else {
             $this->setElementRequestValue($element);
         }
     }
     return $this;
 }
コード例 #3
0
ファイル: AnchorHref.php プロジェクト: vectrex/vxphp
 /**
  * callback to turn href shortcuts into site conform valid URLs
  *
  * $/foo/bar?baz=1 becomes /foo/bar?baz=1 or index.php/foo/bar?baz=1
  *
  * @param array $matches
  * @return string
  */
 private function filterHref($matches)
 {
     static $script;
     static $niceUri;
     if (empty($script)) {
         $script = trim(Request::createFromGlobals()->getScriptName(), '/');
     }
     if (empty($niceUri)) {
         $niceUri = Application::getInstance()->getConfig()->site->use_nice_uris == 1;
     }
     $matches[4] = html_entity_decode($matches[4]);
     $uriParts = array();
     if ($niceUri) {
         if ($script !== 'index.php') {
             $uriParts[] = basename($script, '.php');
         }
     } else {
         $uriParts[] = $script;
     }
     if ($matches[3] !== '') {
         $uriParts[] = $matches[3];
     }
     $uri = implode('/', $uriParts) . $matches[4];
     return "<a{$matches[1]} href={$matches[2]}/{$uri}{$matches[2]}{$matches[5]}>";
 }
コード例 #4
0
ファイル: MenuGenerator.php プロジェクト: vectrex/vxphp
 /**
  * render menu markup
  *
  * @return string
  */
 public function render()
 {
     // check authentication
     if (!$this->authenticateMenu($this->menu)) {
         return '';
     }
     // if menu has not been prepared yet, do it now (caching avoids re-parsing for submenus)
     if (!in_array($this->menu, self::$primedMenus, TRUE)) {
         // clear selected menu entries (which remain in the session)
         $this->clearSelectedMenuEntries($this->menu);
         // walk entire menu and add dynamic entries where necessary
         $this->completeMenu($this->menu);
         // prepare path segments to identify active menu entries
         $this->pathSegments = explode('/', trim($this->request->getPathInfo(), '/'));
         // skip script name
         if ($this->useNiceUris && basename($this->request->getScriptName()) != 'index.php') {
             array_shift($this->pathSegments);
         }
         // skip locale if one found
         if (count($this->pathSegments) && Application::getInstance()->hasLocale($this->pathSegments[0])) {
             array_shift($this->pathSegments);
         }
         // walk tree until an active entry is reached
         $this->walkMenuTree($this->menu, $this->pathSegments[0] === '' ? explode('/', $this->route->getPath()) : $this->pathSegments);
         // cache menu for multiple renderings
         self::$primedMenus[] = $this->menu;
     }
     $htmlId = $this->id . 'menu';
     // drill down to required submenu (if only submenu needs to be rendered)
     $m = $this->menu;
     if ($this->level !== FALSE) {
         $htmlId .= '_level_' . $this->level;
         if ($this->level > 0) {
             while ($this->level-- > 0) {
                 $e = $this->menu->getSelectedEntry();
                 if (!$e || !$e->getSubMenu()) {
                     break;
                 }
                 $m = $e->getSubMenu();
             }
             if ($this->level >= 0) {
                 return '';
             }
         }
     }
     // output
     // instantiate renderer class, defaults to SimpleListRenderer
     if (!empty($this->decorator)) {
         $rendererName = $this->decorator;
     } else {
         $rendererName = 'SimpleList';
     }
     $className = __NAMESPACE__ . '\\Menu\\Renderer\\' . $rendererName . 'Renderer';
     $renderer = new $className($m);
     $renderer->setParameters($this->renderArgs);
     // enable or disable display of submenus
     $m->setShowSubmenus($this->level === FALSE);
     // enable or disable always active menu
     $m->setForceActive(self::$forceActiveMenu);
     // if no container tag was specified, use a DIV element
     if (!isset($this->renderArgs['containerTag'])) {
         $this->renderArgs['containerTag'] = 'div';
     }
     // omit wrapper, if a falsy container tag was specified
     if ($this->renderArgs['containerTag']) {
         return sprintf('<%1$s%2$s>%3$s</%1$s>', $this->renderArgs['containerTag'], isset($this->renderArgs['omitId']) && $this->renderArgs['omitId'] ? '' : ' id="' . $htmlId . '"', $renderer->render());
     }
     return $renderer->render();
 }
コード例 #5
0
ファイル: Route.php プロジェクト: vectrex/vxphp
 /**
  * redirect using configured redirect information
  * if route has no redirect set, redirect will lead to "start page"
  *
  * @param array $queryParams
  * @param number $statusCode
  */
 public function redirect($queryParams = [], $statusCode = 302)
 {
     $request = Request::createFromGlobals();
     $application = Application::getInstance();
     $urlSegments = [$request->getSchemeAndHttpHost()];
     if ($application->hasNiceUris()) {
         if (($scriptName = basename($request->getScriptName(), '.php')) !== 'index') {
             $urlSegments[] = $scriptName;
         }
     } else {
         $urlSegments[] = trim($request->getScriptName(), '/');
     }
     if (count($queryParams)) {
         $query = '?' . http_build_query($queryParams);
     } else {
         $query = '';
     }
     return new RedirectResponse(implode('/', $urlSegments) . '/' . $this->redirect . $query);
 }
コード例 #6
0
ファイル: Router.php プロジェクト: vectrex/vxphp
 /**
  *
  * @param string $scriptName (e.g. index.php, admin.php)
  * @param array $pathSegments
  *
  * @return \vxPHP\Routing\Route
  */
 private static function getRouteFromConfig($scriptName, array $pathSegments = NULL)
 {
     $routes = Application::getInstance()->getConfig()->routes;
     // if no page given try to get the first from list
     if (is_null($pathSegments) && isset($routes[$scriptName])) {
         return array_shift($routes[$scriptName]);
     }
     $pathToCheck = implode('/', $pathSegments);
     $requestMethod = Request::createFromGlobals()->getMethod();
     $foundRoute = NULL;
     $default = NULL;
     // iterate over routes and try to find the "best" match
     foreach ($routes[$scriptName] as $route) {
         // keep default route as fallback, when no match is found
         if ($route->getRouteId() === 'default') {
             $default = $route;
         }
         // pick route only when request method requirement is met
         if (preg_match('~(?:/|^)' . $route->getMatchExpression() . '(?:/|$)~', $pathToCheck) && $route->allowsRequestMethod($requestMethod)) {
             // if no route was found yet, pick this first match
             if (!isset($foundRoute)) {
                 $foundRoute = $route;
             } else {
                 // if a route has been found previously, choose the more "precise" and/or later one
                 // choose the route with more satisfied placeholders
                 // @todo could be optimized
                 if (count(self::getSatisfiedPlaceholders($route, $pathToCheck)) >= count(self::getSatisfiedPlaceholders($foundRoute, $pathToCheck))) {
                     $foundRoute = $route;
                 }
             }
         }
     }
     // return "normal" route, if found
     if (isset($foundRoute)) {
         return $foundRoute;
     }
     // return default route as fallback (if available)
     return $default;
 }