]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/TUrlManager.php
Add Baculum
[bacula/bacula] / gui / baculum / framework / Web / TUrlManager.php
1 <?php
2 /**
3  * TUrlManager class file
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link http://www.pradosoft.com/
7  * @copyright Copyright &copy; 2005-2013 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @version $Id $
10  * @package System.Web
11  */
12
13 /**
14  * TUrlManager class
15  *
16  * TUrlManager is the base class for managing URLs that can be
17  * recognized by PRADO applications. It provides the default implementation
18  * for parsing and constructing URLs.
19  *
20  * Derived classes may override {@link constructUrl} and {@link parseUrl}
21  * to provide customized URL schemes.
22  *
23  * By default, {@link THttpRequest} uses TUrlManager as its URL manager.
24  * If you want to use your customized URL manager, load your manager class
25  * as an application module and set {@link THttpRequest::setUrlManager THttpRequest.UrlManager}
26  * with the ID of your URL manager module.
27  *
28  * @author Qiang Xue <qiang.xue@gmail.com>
29  * @version $Id $
30  * @package System.Web
31  * @since 3.0.6
32  */
33 class TUrlManager extends TModule
34 {
35         /**
36          * Constructs a URL that can be recognized by PRADO.
37          *
38          * This method provides the actual implementation used by {@link THttpRequest::constructUrl}.
39          * Override this method if you want to provide your own way of URL formatting.
40          * If you do so, you may also need to override {@link parseUrl} so that the URL can be properly parsed.
41          *
42          * The URL is constructed as the following format:
43          * /entryscript.php?serviceID=serviceParameter&get1=value1&...
44          * If {@link THttpRequest::setUrlFormat THttpRequest.UrlFormat} is 'Path',
45          * the following format is used instead:
46          * /entryscript.php/serviceID/serviceParameter/get1,value1/get2,value2...
47          * If {@link THttpRequest::setUrlFormat THttpRequest.UrlFormat} is 'HiddenPath',
48          * then entryscript.php will be hidden and the following format is used instead:
49          * /serviceID/serviceParameter/get1,value1/get2,value2...
50          * In order to use the 'HiddenPath' format you need proper url rewrite configuration;
51          * here's an example for Apache's .htaccess:
52          * <cdde>
53          * Options +FollowSymLinks  
54          * RewriteEngine On
55          * RewriteCond %{REQUEST_FILENAME} !-d
56          * RewriteCond %{REQUEST_FILENAME} !-f
57          * RewriteRule ^(.*)$ index.php/$1 [L]
58          * </code>
59          * @param string service ID
60          * @param string service parameter
61          * @param array GET parameters, null if not provided
62          * @param boolean whether to encode the ampersand in URL
63          * @param boolean whether to encode the GET parameters (their names and values)
64          * @return string URL
65          * @see parseUrl
66          */
67         public function constructUrl($serviceID,$serviceParam,$getItems,$encodeAmpersand,$encodeGetItems)
68         {
69                 $url=$serviceID.'='.urlencode($serviceParam);
70                 $amp=$encodeAmpersand?'&amp;':'&';
71                 $request=$this->getRequest();
72                 if(is_array($getItems) || $getItems instanceof Traversable)
73                 {
74                         if($encodeGetItems)
75                         {
76                                 foreach($getItems as $name=>$value)
77                                 {
78                                         if(is_array($value))
79                                         {
80                                                 $name=urlencode($name.'[]');
81                                                 foreach($value as $v)
82                                                         $url.=$amp.$name.'='.urlencode($v);
83                                         }
84                                         else
85                                                 $url.=$amp.urlencode($name).'='.urlencode($value);
86                                 }
87                         }
88                         else
89                         {
90                                 foreach($getItems as $name=>$value)
91                                 {
92                                         if(is_array($value))
93                                         {
94                                                 foreach($value as $v)
95                                                         $url.=$amp.$name.'[]='.$v;
96                                         }
97                                         else
98                                                 $url.=$amp.$name.'='.$value;
99                                 }
100                         }
101                 }
102
103                 switch($request->getUrlFormat())
104                 {
105                         case THttpRequestUrlFormat::Path:
106                                 return $request->getApplicationUrl().'/'.strtr($url,array($amp=>'/','?'=>'/','='=>$request->getUrlParamSeparator()));
107                         case THttpRequestUrlFormat::HiddenPath:
108                                 return rtrim(dirname($request->getApplicationUrl()), '/').'/'.strtr($url,array($amp=>'/','?'=>'/','='=>$request->getUrlParamSeparator()));
109                         default:
110                                 return $request->getApplicationUrl().'?'.$url;
111                 }
112         }
113
114         /**
115          * Parses the request URL and returns an array of input parameters.
116          * This method is automatically invoked by {@link THttpRequest} when
117          * handling a user request.
118          *
119          * In general, this method should parse the path info part of the requesting URL
120          * and generate an array of name-value pairs according to some scheme.
121          * The current implementation deals with both 'Get' and 'Path' URL formats.
122          *
123          * You may override this method to support customized URL format.
124          * @return array list of input parameters, indexed by parameter names
125          * @see constructUrl
126          */
127         public function parseUrl()
128         {
129                 $request=$this->getRequest();
130                 $pathInfo=trim($request->getPathInfo(),'/');
131                 if(($request->getUrlFormat()===THttpRequestUrlFormat::Path ||
132                         $request->getUrlFormat()===THttpRequestUrlFormat::HiddenPath) &&
133                         $pathInfo!=='')
134                 {
135                         $separator=$request->getUrlParamSeparator();
136                         $paths=explode('/',$pathInfo);
137                         $getVariables=array();
138                         foreach($paths as $path)
139                         {
140                                 if(($path=trim($path))!=='')
141                                 {
142                                         if(($pos=strpos($path,$separator))!==false)
143                                         {
144                                                 $name=substr($path,0,$pos);
145                                                 $value=substr($path,$pos+1);
146                                                 if(($pos=strpos($name,'[]'))!==false)
147                                                         $getVariables[substr($name,0,$pos)][]=$value;
148                                                 else
149                                                         $getVariables[$name]=$value;
150                                         }
151                                         else
152                                                 $getVariables[$path]='';
153                                 }
154                         }
155                         return $getVariables;
156                 }
157                 else
158                         return array();
159         }
160 }
161