1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | /** |
---|
15 | @ingroup DC_CORE |
---|
16 | @nosubgrouping |
---|
17 | @brief URL Handler for admin urls |
---|
18 | |
---|
19 | */ |
---|
20 | class dcAdminURL |
---|
21 | { |
---|
22 | /** @var dcCore dcCore instance */ |
---|
23 | protected $core; |
---|
24 | protected $urls; |
---|
25 | |
---|
26 | /** |
---|
27 | Inits dcAdminURL object |
---|
28 | |
---|
29 | @param core <b>dcCore</b> Dotclear core reference |
---|
30 | */ |
---|
31 | public function __construct($core) |
---|
32 | { |
---|
33 | $this->core = $core; |
---|
34 | $this->urls = new ArrayObject(); |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * Registers a new url |
---|
39 | * @param string $name the url name |
---|
40 | * @param string $url url value |
---|
41 | * @param array $params query string params (optional) |
---|
42 | */ |
---|
43 | public function register($name,$url,$params=array()) |
---|
44 | { |
---|
45 | $this->urls[$name] = array('url' => $url, 'qs' => $params); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Registers a new url as a copy of an existing one |
---|
50 | * @param string $name url name |
---|
51 | * @param streing $orig url to copy information from |
---|
52 | * @param array $params extra parameters to add |
---|
53 | * @param string $newurl new url if different from the original |
---|
54 | */ |
---|
55 | public function registercopy($name,$orig,$params=array(),$newurl='') |
---|
56 | { |
---|
57 | if (!isset($this->urls[$orig])) { |
---|
58 | throw new exception ('Unknown URL handler for '.$orig); |
---|
59 | } |
---|
60 | $url = $this->urls[$orig]; |
---|
61 | $url['qs'] = array_merge($url['qs'],$params); |
---|
62 | if ($newurl != '') { |
---|
63 | $url['url'] = $newurl; |
---|
64 | } |
---|
65 | $this->urls[$name] = $url; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * retrieves a URL given its name, and optional parameters |
---|
70 | * |
---|
71 | * @param string $name URL Name |
---|
72 | * @param array $params query string parameters, given as an associative array |
---|
73 | * @param boolean $urlencode set to true if url may not be encoded |
---|
74 | * @param string $separator separator to use between QS parameters |
---|
75 | * @param boolean $parametric set to true if url will be used as (s)printf() format. |
---|
76 | * @return string the forged url |
---|
77 | */ |
---|
78 | public function get($name,$params=array(),$separator='&',$parametric=false) |
---|
79 | { |
---|
80 | if (!isset($this->urls[$name])) { |
---|
81 | throw new exception ('Unknown URL handler for '.$name); |
---|
82 | } |
---|
83 | $url = $this->urls[$name]; |
---|
84 | $p = array_merge($url['qs'],$params); |
---|
85 | $u = $url['url']; |
---|
86 | if (!empty($p)) { |
---|
87 | $u .= '?'.http_build_query($p,'',$separator); |
---|
88 | } |
---|
89 | if ($parametric) { |
---|
90 | // Dirty hack to get back %[n$]s instead of %25[{0..9}%24]s in URLs used with (s)printf(), as http_build_query urlencode() its result. |
---|
91 | $u = preg_replace('/\%25(([0-9])+?\%24)*?s/','%$2s',$u); |
---|
92 | } |
---|
93 | return $u; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * retrieves a URL given its name, and optional parameters |
---|
98 | * |
---|
99 | * @param string $name URL Name |
---|
100 | * @param array $params query string parameters, given as an associative array |
---|
101 | * @param boolean $urlencode set to true if url may not be encoded |
---|
102 | * @param string $suffix suffix to be added to the QS parameters |
---|
103 | * @return string the forged url |
---|
104 | */ |
---|
105 | public function redirect($name,$params=array(),$suffix="") |
---|
106 | { |
---|
107 | if (!isset($this->urls[$name])) { |
---|
108 | throw new exception ('Unknown URL handler for '.$name); |
---|
109 | } |
---|
110 | http::redirect($this->get($name,$params,'&').$suffix); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * retrieves a php page given its name, and optional parameters |
---|
115 | * acts like get, but without the query string, should be used within forms actions |
---|
116 | * |
---|
117 | * @param string $name URL Name |
---|
118 | * @return string the forged url |
---|
119 | */ |
---|
120 | public function getBase($name) |
---|
121 | { |
---|
122 | if (!isset($this->urls[$name])) { |
---|
123 | throw new exception ('Unknown URL handler for '.$name); |
---|
124 | } |
---|
125 | return $this->urls[$name]['url']; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * forges form hidden fields to pass to a generated <form>. Should be used in combination with |
---|
130 | * form action retrieved from getBase() |
---|
131 | * |
---|
132 | * @param string $name URL Name |
---|
133 | * @param array $params query string parameters, given as an associative array |
---|
134 | * @return string the forged form data |
---|
135 | */ |
---|
136 | public function getHiddenFormFields($name,$params=array()) |
---|
137 | { |
---|
138 | if (!isset($this->urls[$name])) { |
---|
139 | throw new exception ('Unknown URL handler for '.$name); |
---|
140 | } |
---|
141 | $url = $this->urls[$name]; |
---|
142 | $p = array_merge($url['qs'],$params); |
---|
143 | $str = ''; |
---|
144 | foreach ((array)$p as $k => $v) { |
---|
145 | $str .= form::hidden(array($k),$v); |
---|
146 | } |
---|
147 | return $str; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * retrieves a URL (decoded — useful for echoing) given its name, and optional parameters |
---|
152 | * |
---|
153 | * @deprecated should be used carefully, parameters are no more escaped |
---|
154 | * |
---|
155 | * @param string $name URL Name |
---|
156 | * @param array $params query string parameters, given as an associative array |
---|
157 | * @param string $separator separator to use between QS parameters |
---|
158 | * @return string the forged decoded url |
---|
159 | */ |
---|
160 | public function decode($name,$params=array(),$separator='&') |
---|
161 | { |
---|
162 | return urldecode($this->get($name,$params,false,$separator)); |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * Returns $urls property content. |
---|
167 | * |
---|
168 | * @return ArrayObject |
---|
169 | */ |
---|
170 | public function dumpUrls() |
---|
171 | { |
---|
172 | return $this->urls; |
---|
173 | } |
---|
174 | } |
---|