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