New onsite patient portal, take 4.
[openemr.git] / portal / patient / fwk / libs / verysimple / Phreeze / SimpleRouter.php
blob4f66cb4d72dd3260a358e30f3f2768456298895c
1 <?php
2 /** @package verysimple::Phreeze */
3 require_once ("verysimple/HTTP/RequestUtil.php");
4 require_once ("verysimple/Phreeze/IRouter.php");
6 /**
7 * SimpleRouter is a URL router that parses URLs in the following format:
8 * http://server/index.php?ROUTE
9 * This router can be used in a situation where URL re-writing is not
10 * available or wanted on the host server
12 * The route must be the first param in the querysting and does not
13 * have a key. Additional querystring parameters can be appended
14 * and will have no effect on the route.
16 * Note that this router does not currently support different routes
17 * based on params passed through nor is wildcard matching supported
19 * @package verysimple::HTTP
20 * @author VerySimple Inc.
21 * @copyright 1997-2007 VerySimple, Inc. http://www.verysimple.com
22 * @license http://www.gnu.org/licenses/lgpl.html LGPL
23 * @version 1.0
25 class SimpleRouter implements IRouter {
26 public static $ROUTE_NOT_FOUND = "Default.Error404";
27 private $routes = array ();
28 private $defaultAction = 'Default.Home';
29 private $appRootUrl = '';
31 /**
33 * @param string $appRootUrl
34 * @param string $defaultAction
35 * @param array $mapping
36 * routeMap
38 public function __construct($appRootUrl = '', $defaultAction = '', $mapping = array()) {
39 if ($defaultAction)
40 $this->defaultAction = $defaultAction;
41 $this->routes = $mapping;
44 /**
45 * Given a controller, method and params, returns a url that points
46 * to the correct location
48 * @param string $controller
49 * @param string $method
50 * @param array $params
51 * in the format param1=val1&param2=val2
52 * @return string URL
54 public function GetUrl($controller, $method, $params = '', $requestMethod = '') {
55 throw new Exception ( 'Not yet implemented' );
58 /**
59 * Returns the controller and method for the given URI
61 * @param
62 * string the url, if not provided will be obtained using the current URL
63 * @return array($controller,$method)
65 public function GetRoute($uri = "") {
66 $match = '';
68 $qs = $uri ? $uri : (array_key_exists ( 'QUERY_STRING', $_SERVER ) ? $_SERVER ['QUERY_STRING'] : '');
69 $parsed = explode ( '&', $qs, 2 );
70 $action = $parsed [0];
72 if (strpos ( $action, '=' ) > - 1 || ! $action) {
73 // the querystring is empty or the first param is a named param, which we ignore
74 $match = $this->defaultAction;
75 } else {
76 // otherwise we have a route. see if we have a match in the routemap, otherwise return the 'not found' route
77 $method = RequestUtil::GetMethod ();
78 $route = $method . ':' . $action;
79 $match = array_key_exists ( $route, $this->routes ) ? $this->routes [$route] ['route'] : self::$ROUTE_NOT_FOUND;
82 return explode ( '.', $match, 2 );
85 /**
86 * In the case of a rewrite url, the url itself contains the parameter
87 * for example http://server/param1/param2/param3.
88 * These params
89 * are parsed and the param with the given index is returned
91 * @return string (or $default if not provided)
92 * @param
93 * string default value to return if parameter is empty
95 public function GetUrlParam($key, $default = '') {
96 return array_key_exists ( $key, $_REQUEST ) ? $_REQUEST [$key] : $default;
99 /**
100 * In the case of a rewrite url, the url itself contains the parameter
101 * for example http://server/param1/param2/param3.
102 * These params
103 * are parsed and returned as an array
105 * @return array
107 public function GetUrlParams() {
108 return $_REQUEST;
112 * Returns the RESTful part of the url
113 * For example, localhost/users/5 will return users/5
115 * @return string
117 public function GetUri() {