Dotclear

source: inc/admin/lib.dc.adminurl.php @ 2873:87714a6289b7

Revision 2873:87714a6289b7, 5.1 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Not necessary to rawurlencode() the current folder as adminurl->get (or ->redirect) will, fixes #2029

Line 
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 -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14/**
15@ingroup DC_CORE
16@nosubgrouping
17@brief URL Handler for admin urls
18
19*/
20class 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='&amp;',$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          // Dirty hack to get back %s instead of %25s in URLs used with (s)printf(), as http_build_query urlencode() its result.
90          return $parametric ? str_replace('%25s','%s',$u) : $u;
91     }
92
93     /**
94      * retrieves a URL given its name, and optional parameters
95      *
96      * @param  string $name      URL Name
97      * @param  array  $params    query string parameters, given as an associative array
98      * @param  boolean $urlencode set to true if url may not be encoded
99      * @param  string $suffix suffix to be added to the QS parameters
100      * @return string            the forged url
101      */
102     public function redirect($name,$params=array(),$suffix="")
103     {
104          if (!isset($this->urls[$name])) {
105               throw new exception ('Unknown URL handler for '.$name);
106          }
107          http::redirect($this->get($name,$params,'&').$suffix);
108     }
109
110     /**
111      * retrieves a php page given its name, and optional parameters
112      * acts like get, but without the query string, should be used within forms actions
113      *
114      * @param  string $name      URL Name
115      * @return string            the forged url
116      */
117     public function getBase($name)
118     {
119          if (!isset($this->urls[$name])) {
120               throw new exception ('Unknown URL handler for '.$name);
121          }
122          return $this->urls[$name]['url'];
123     }
124
125     /**
126      * forges form hidden fields to pass to a generated <form>. Should be used in combination with
127      * form action retrieved from getBase()
128      *
129      * @param  string $name      URL Name
130      * @param  array  $params    query string parameters, given as an associative array
131      * @return string            the forged form data
132      */
133     public function getHiddenFormFields($name,$params=array())
134     {
135          if (!isset($this->urls[$name])) {
136               throw new exception ('Unknown URL handler for '.$name);
137          }
138          $url = $this->urls[$name];
139          $p = array_merge($url['qs'],$params);
140          $str = '';
141          foreach ((array)$p as $k => $v) {
142               $str .= form::hidden(array($k),$v);
143          }
144          return $str;
145     }
146
147     /**
148      * retrieves a URL (decoded — useful for echoing) given its name, and optional parameters
149      *
150      * @deprecated      should be used carefully, parameters are no more escaped
151      *
152      * @param  string $name      URL Name
153      * @param  array  $params    query string parameters, given as an associative array
154      * @param  string $separator separator to use between QS parameters
155      * @return string            the forged decoded url
156      */
157     public function decode($name,$params=array(),$separator='&')
158     {
159          return urldecode($this->get($name,$params,false,$separator));
160     }
161
162     /**
163      * Returns $urls property content.
164      *
165      * @return  ArrayObject
166      */
167     public function dumpUrls()
168     {
169          return $this->urls;
170     }
171}
Note: See TracBrowser for help on using the repository browser.

Sites map