source: branches/1.2/workflow/inc/class.utils.url.php @ 1349

Revision 1349, 8.9 KB checked in by niltonneto, 15 years ago (diff)

Ticket #561 - Inclusão do módulo Workflow faltante nessa versão.

  • Property svn:executable set to *
Line 
1<?php
2require_once('class.utils.php');
3/**
4 * Contains useful methods for URL building and handling
5 * @author Carlos Eduardo Nogueira Gonçalves
6 * @author Marcos Pont
7 * @version 1.0
8 * @link http://workflow.celepar.parana/doc-workflow/classes/urlutils Complete reference
9 * @package Workflow
10 * @license http://www.gnu.org/copyleft/gpl.html GPL
11 */
12class UrlUtils extends Utils
13{
14        /**
15         * @var string $protocol
16         * @access public
17         */
18        var $protocol;
19       
20        /**
21         * @var string $auth
22         * @access public
23         */
24        var $auth;
25                       
26        /**
27         * @var string $user
28         * @access public
29         */
30        var $user;
31       
32        /**
33         * @var string $pass
34         * @access public
35         */             
36        var $pass;             
37       
38        /**
39         * @var string $host
40         * @access public
41         */
42        var $host;             
43       
44        /**
45         * @var string $port
46         * @access public
47         */
48        var $port;
49                       
50        /**
51         * @var string $path
52         * @access public
53         */
54        var $path;
55       
56        /**
57         * @var string $file
58         * @access public
59         */             
60        var $file;
61       
62        /**
63         * @var string $parameters
64         * @access public
65         */             
66        var $parameters;
67       
68        /**
69         * @var string $fragment
70         * @access public
71         */
72        var $fragment; 
73
74
75        /**
76     * Constructor
77         * @access public
78         * @return object
79         */
80        function UrlUtils($url='') {           
81                if ($url != '') {
82                        $this->set($url);                       
83                }
84        }
85        /**
86         * Set the current url
87         * @param string $url
88         * @access public
89         */
90        function set($url) {
91                $this->_parse($url);
92        }
93       
94        /**
95         *
96         * @access public
97         */
98        function setFromCurrent() {
99                $this->set($this->uri());
100        }
101       
102        /**
103         * Get the current protocol
104         * @return string current protocol
105         * @access public
106         */
107        function getProtocol() {
108                return (isset($this->protocol) && !empty($this->protocol) ? $this->protocol : NULL);
109        }
110       
111        /**
112         * Get the current protocol scheme
113         * @return string
114         * @access public
115         */
116        function getScheme() {
117                $protocol = $this->getProtocol();
118                if (!$this->isNull($protocol))
119                        return strtolower($protocol) . '://';
120                else
121                        return NULL;
122        }
123       
124        /**
125         * get current auth
126         * @return string
127         * @access public
128         */
129        function getAuth() {
130                return (isset($this->auth) && !empty($this->auth) ? $this->auth : NULL);
131        }
132        /***
133         * Return the user if is set
134         * @access public
135         * @return string
136         */
137        function getUser() {
138                return (isset($this->user) && !empty($this->user) ? $this->user : NULL);
139        }
140       
141        /**
142         * Get the password
143         * @access public
144         * @return string
145         */
146        function getPass() {
147                return (isset($this->pass) && !empty($this->pass) ? $this->pass : NULL);
148        }
149       
150        /**
151         * Get the host
152         * @access public
153         * @return string
154         */
155        function getHost() {
156                if (!isset($this->host) || empty($this->host)) {                       
157                        return NULL;
158                }                       
159                if (ereg("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}", $this->host)) {
160                        return gethostbyaddr($this->host);
161                } else {
162                        return strtolower($this->host);
163                }
164        }
165       
166        /**
167         * Get port
168         * @access public
169         * @return string
170         */
171        function getPort() {
172                return (isset($this->port) && !empty($this->port) ? $this->port : NULL);
173        }
174       
175        /**
176         * Get path
177         * @access public
178         * @return string
179         */
180        function getPath() {
181                return (isset($this->path) && !empty($this->path) ? $this->path : NULL);
182        }
183        /**
184         * Get file
185         * @access public
186         * @return sting
187         */
188        function getFile() {
189                return (isset($this->file) && !empty($this->file) ? $this->file : NULL);
190        }
191        /**
192         * Get query string
193         * @return string query string
194         * @access public
195         */
196        function getQueryString($prefix=FALSE) {
197                return (isset($this->parameters) && !empty($this->parameters) ? ($prefix ? '?' . $this->parameters : $this->parameters) : NULL);
198        }
199       
200        /**
201         * Get query string array
202         * @access public
203         * @return array query string array
204         */
205        function getQueryStringArray() {
206                $queryString = $this->getQueryString();
207                if (!$this->isNull($queryString)) {
208                        parse_str($queryString, $result);
209                        return $result;
210                }
211                return NULL;
212        }
213       
214       
215        /**
216         * Add a parameter
217         * @param $name parameter name
218         * @param $value parameter value
219         * @access public
220         * @return void
221         */
222        function addParameter($name, $value) {
223                $queryString = $this->getQueryString();
224                if (!$this->isNull($queryString)) {
225                        $result = '';
226                        parse_str($queryString, $params);                       
227                        $params[$name] = $value;
228                        foreach ($params as $name => $value)
229                                $result .= ($result == '' ? "$name=$value" : "&$name=$value");
230                        $this->parameters = $result;
231                } else {
232                        $this->parameters = "$name=$value";
233                }
234        }
235       
236        /**
237         *
238         * Remove parameter
239         * @param string $name name of parameter
240         * @return void
241         */
242        function removeParameter($name) {
243                $query = $this->getQueryStringArray();
244                if (!$this->isNull($query)) {
245                        unset($query[$name]);
246                        $tmp = array();
247                        foreach ($query as $k => $v)
248                                $tmp[] = "$k=$v";
249                        $this->parameters = implode("&", $tmp);
250                }
251        }
252       
253       
254        /**
255         * Get fragment
256         * @access public
257         * @return string fragment
258         */
259        function getFragment() {
260                return (isset($this->fragment) && !empty($this->fragment) ? $this->fragment : NULL);
261        }
262
263        /**
264         * Get url
265         * @access public
266         * @return string returns the full url 
267         */
268        function getUrl() {
269                return sprintf("%s%s%s%s%s%s%s",
270                        (isset($this->protocol) && !empty($this->protocol) ? "{$this->protocol}://" : ''),
271                        (isset($this->auth) && !empty($this->auth) ? "{$this->user}:{$this->pass}@" : ''),
272                        strtolower($this->host),
273                        (isset($this->port) && !empty($this->port) ? ":{$this->port}" : ''),
274                        $this->path,
275                        (isset($this->parameters) && !empty($this->parameters) ? "?{$this->parameters}" : ''),
276                        (isset($this->fragment) && !empty($this->fragment) ? "#{$this->fragment}" : '')
277                );
278        }
279       
280        /**
281         * Get anchor
282         * @access public 
283         * @param $caption
284         *
285         */
286        function getAnchor($caption, $statusBarText='', $cssClass='') {
287                return parent::anchor($this->getUrl(), $caption, $statusBarText, $cssClass);
288        }
289       
290       
291        /**
292         * Reset the configuration
293         * @access public
294         * @return void
295         */
296        function reset() {
297                unset($this->protocol);
298                unset($this->auth);
299                unset($this->user);
300                unset($this->pass);
301                unset($this->host);
302                unset($this->port);
303                unset($this->path);
304                unset($this->file);
305                unset($this->parameters);
306                unset($this->fragment);
307        }
308       
309        /**
310         * Encode the current url
311         *
312         * @param string $url
313         * @param string $varName name of variable
314         * @return string encoded url
315         * @access public
316         */
317        function encode($url=NULL, $varName='p2gvar') {
318                // utiliza como padrao a URL da classe
319                if ($this->isNull($url))
320                        $url = $this->getUrl();
321                // busca a string de parametros
322                if (ereg("([^?#]+\??)?([^#]+)?(.*)", $url, $matches)) {                 
323                        if (!$this->isFalse($matches[2])) {
324                                // codifica os parametros
325                                $paramString = base64_encode(urlencode($matches[2]));
326                                $returnUrl = $this->parseString($matches[1]) . $varName . '=' . $paramString . $this->parseString($matches[3]);
327                        } else {
328                                $returnUrl = $url;
329                        }
330                }
331                return $returnUrl;
332        }
333       
334       
335        /**
336         * Decode the current url
337         *
338         * @param string $url url string
339         * @param bool $resultAsArray result is array
340         * @return array parameters array
341         * @access public
342         */
343        function decode($url=NULL, $returnAsArray=FALSE) {
344                // utiliza como padrï¿œo a URL da classe
345                if ($this->isNull($url))
346                        $url = $this->getUrl();
347                // busca os parï¿œmetros codificados           
348                ereg("([^?#]+\??)?([^#]+)?(.*)", $url, $matches);
349                if (!$this->isFalse($matches[2])) {
350                        parse_str($matches[2], $vars);
351                        if (list(, $value) = each($vars)) {
352                                // decodifica o conjunto de parï¿œmetros
353                                $paramString = urldecode(base64_decode($value));
354                                if ($returnAsArray) {
355                                        parse_str($paramString, $varsArray);
356                                        return $varsArray;
357                                } else {
358                                        return $this->parseString($matches[1]) . $paramString . $this->parseString($matches[3]);
359                                }
360                        }                       
361                }       
362                return FALSE;
363        }
364       
365        /**
366         * Parse the currente url
367         * @param string $url
368         * @return void
369         * @access public
370         */
371        function _parse($url) {
372        if (preg_match('!^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?!', $url, $matches)) {
373                        if (isset($matches[1]))
374                                $this->protocol = $matches[2];
375                        if (isset($matches[3]) && isset($matches[4])) {
376                                $atPos = strpos($matches[4], '@');
377                                if (!$this->isFalse($atPos)) {
378                                        $this->auth = $this->left($matches[4], $atPos);
379                                        $dotPos = strpos($this->auth, ':');
380                                        if (!$this->isFalse($dotPos)) {
381                                                $auth = explode(':', $this->auth);
382                                                $this->user = $auth[0];
383                                                $this->pass = $auth[1];
384                                        } else {
385                                                $this->user = $this->auth;
386                                        }
387                                        $matches[4] = substr($matches[4], $atPos+1);
388                                }
389                                $portPos = strrpos($matches[4], ':');
390                                if (!$this->isFalse($portPos)) {
391                                        $this->port = $this->parseIntegerPositive(substr($matches[4], $portPos+1));
392                                        if (!$this->port) {
393                                                $this->port = NULL;
394                                        }
395                                }
396                                $this->host = $portPos ? $this->left($matches[4], $portPos) : $matches[4];
397                        }
398                        if (isset($matches[5])) {
399                                $this->path = $matches[5];
400                                $slashPos = strrpos(substr($this->path, 1), '/');
401                                if (!$this->isFalse($slashPos)) {
402                                        $this->file = substr($this->path, $slashPos + 2);
403                                }
404                        }
405                        $this->path = $matches[5] ? $matches[5] : '';
406            if (isset($matches[6]) && $matches[6] != '')
407                                $this->parameters = $matches[7];
408            if (isset($matches[8]) && $matches[8] != '')
409                                $this->fragment = $matches[9];
410        }
411        }
412}
413?>
Note: See TracBrowser for help on using the repository browser.