| 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 string $separator separator to use between QS parameters |
|---|
| 74 | * @return string the forged url |
|---|
| 75 | */ |
|---|
| 76 | public function get($name,$params=array(),$separator='&') |
|---|
| 77 | { |
|---|
| 78 | if (!isset($this->urls[$name])) { |
|---|
| 79 | throw new exception ('Unknown URL handler for '.$name); |
|---|
| 80 | } |
|---|
| 81 | $url = $this->urls[$name]; |
|---|
| 82 | $p = array_merge($url['qs'],$params); |
|---|
| 83 | $u = $url['url']; |
|---|
| 84 | if (!empty($p)) { |
|---|
| 85 | $u .= '?'.http_build_query($p,'',$separator); |
|---|
| 86 | } |
|---|
| 87 | return $u; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * retrieves a URL (decoded — useful for echoing) given its name, and optional parameters |
|---|
| 92 | * |
|---|
| 93 | * @param string $name URL Name |
|---|
| 94 | * @param array $params query string parameters, given as an associative array |
|---|
| 95 | * @param string $separator separator to use between QS parameters |
|---|
| 96 | * @return string the forged decoded url |
|---|
| 97 | */ |
|---|
| 98 | public function decode($name,$params=array(),$separator='&') |
|---|
| 99 | { |
|---|
| 100 | return urldecode($this->get($name,$params,$separator)); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Returns $urls property content. |
|---|
| 105 | * |
|---|
| 106 | * @return ArrayObject |
|---|
| 107 | */ |
|---|
| 108 | public function dumpUrls() |
|---|
| 109 | { |
|---|
| 110 | return $this->urls; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|