Subversion Repositories Applications.papyrus

Rev

Rev 1688 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1688 Rev 1706
Line 30... Line 30...
30
// |                                                                       |
30
// |                                                                       |
31
// +-----------------------------------------------------------------------+
31
// +-----------------------------------------------------------------------+
32
// | Author: Richard Heyes <richard at php net>                            |
32
// | Author: Richard Heyes <richard at php net>                            |
33
// +-----------------------------------------------------------------------+
33
// +-----------------------------------------------------------------------+
34
//
34
//
35
// $Id: URL.php,v 1.2 2006-03-13 21:00:48 ddelon Exp $
35
// $Id: URL.php,v 1.2.6.1 2007-11-19 14:06:14 alexandre_tb Exp $
36
//
36
//
37
// Net_URL Class
37
// Net_URL Class
Line -... Line 38...
-
 
38
 
38
 
39
 
39
class Net_URL
40
class Net_URL
-
 
41
{
40
{
42
    var $options = array('encode_query_keys' => false);
41
    /**
43
    /**
42
    * Full url
44
    * Full url
43
    * @var string
45
    * @var string
44
    */
46
    */
Line 119... Line 121...
119
    *                            multiple querystrings with the same name
121
    *                            multiple querystrings with the same name
120
    *                            exist
122
    *                            exist
121
    */
123
    */
122
    function __construct($url = null, $useBrackets = true)
124
    function __construct($url = null, $useBrackets = true)
123
    {
125
    {
-
 
126
        $this->url = $url;
-
 
127
        $this->useBrackets = $useBrackets;
-
 
128
 
-
 
129
        $this->initialize();
-
 
130
    }
-
 
131
 
-
 
132
    function initialize()
-
 
133
    {
124
        $HTTP_SERVER_VARS  = !empty($_SERVER) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
134
        $HTTP_SERVER_VARS  = !empty($_SERVER) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
Line 125... Line -...
125
 
-
 
126
        $this->useBrackets = $useBrackets;
-
 
127
        $this->url         = $url;
135
 
128
        $this->user        = '';
136
        $this->user        = '';
129
        $this->pass        = '';
137
        $this->pass        = '';
130
        $this->host        = '';
138
        $this->host        = '';
131
        $this->port        = 80;
139
        $this->port        = 80;
132
        $this->path        = '';
140
        $this->path        = '';
133
        $this->querystring = array();
141
        $this->querystring = array();
Line 134... Line 142...
134
        $this->anchor      = '';
142
        $this->anchor      = '';
135
 
143
 
136
        // Only use defaults if not an absolute URL given
-
 
137
        if (!preg_match('/^[a-z0-9]+:\/\//i', $url)) {
144
        // Only use defaults if not an absolute URL given
138
 
-
 
Line 139... Line 145...
139
            $this->protocol    =  'http';
145
        if (!preg_match('/^[a-z0-9]+:\/\//i', $this->url)) {
140
            
146
            $this->protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http');
141
 
147
 
-
 
148
            /**
142
            /**
149
            * Figure out host/port
-
 
150
            */
143
            * Figure out host/port
151
            if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) && 
144
            */
152
                preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches)) 
145
            if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) AND preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches)) {
153
            {
146
                $host = $matches[1];
154
                $host = $matches[1];
147
                if (!empty($matches[3])) {
155
                if (!empty($matches[3])) {
Line 159... Line 167...
159
            $this->querystring = isset($HTTP_SERVER_VARS['QUERY_STRING']) ? $this->_parseRawQuerystring($HTTP_SERVER_VARS['QUERY_STRING']) : null;
167
            $this->querystring = isset($HTTP_SERVER_VARS['QUERY_STRING']) ? $this->_parseRawQuerystring($HTTP_SERVER_VARS['QUERY_STRING']) : null;
160
            $this->anchor      = '';
168
            $this->anchor      = '';
161
        }
169
        }
Line 162... Line 170...
162
 
170
 
163
        // Parse the url and store the various parts
171
        // Parse the url and store the various parts
164
        if (!empty($url)) {
172
        if (!empty($this->url)) {
Line 165... Line 173...
165
            $urlinfo = parse_url($url);
173
            $urlinfo = parse_url($this->url);
166
 
174
 
Line 167... Line 175...
167
            // Default querystring
175
            // Default querystring
Line 199... Line 207...
199
                        break;
207
                        break;
200
                }
208
                }
201
            }
209
            }
202
        }
210
        }
203
    }
211
    }
204
 
-
 
205
    /**
212
    /**
206
    * Returns full url
213
    * Returns full url
207
    *
214
    *
208
    * @return string Full url
215
    * @return string Full url
209
    * @access public
216
    * @access public
Line 222... Line 229...
222
 
229
 
223
        return $this->url;
230
        return $this->url;
Line 224... Line 231...
224
    }
231
    }
225
 
232
 
-
 
233
    /**
-
 
234
    * Adds or updates a querystring item (URL parameter).
-
 
235
    * Automatically encodes parameters with rawurlencode() if $preencoded
-
 
236
    *  is false.
226
    /**
237
    * You can pass an array to $value, it gets mapped via [] in the URL if
227
    * Adds a querystring item
238
    * $this->useBrackets is activated.
228
    *
239
    *
229
    * @param  string $name       Name of item
240
    * @param  string $name       Name of item
230
    * @param  string $value      Value of item
241
    * @param  string $value      Value of item
231
    * @param  bool   $preencoded Whether value is urlencoded or not, default = not
242
    * @param  bool   $preencoded Whether value is urlencoded or not, default = not
232
    * @access public
243
    * @access public
233
    */
244
    */
-
 
245
    function addQueryString($name, $value, $preencoded = false)
-
 
246
    {
-
 
247
        if ($this->getOption('encode_query_keys')) {
-
 
248
            $name = rawurlencode($name);
234
    function addQueryString($name, $value, $preencoded = false)
249
        }
235
    {
250
 
236
        if ($preencoded) {
251
        if ($preencoded) {
237
            $this->querystring[$name] = $value;
252
            $this->querystring[$name] = $value;
238
        } else {
253
        } else {
Line 246... Line 261...
246
    * @param  string $name Name of item
261
    * @param  string $name Name of item
247
    * @access public
262
    * @access public
248
    */
263
    */
249
    function removeQueryString($name)
264
    function removeQueryString($name)
250
    {
265
    {
-
 
266
        if ($this->getOption('encode_query_keys')) {
-
 
267
            $name = rawurlencode($name);
-
 
268
        }
-
 
269
 
251
        if (isset($this->querystring[$name])) {
270
        if (isset($this->querystring[$name])) {
252
            unset($this->querystring[$name]);
271
            unset($this->querystring[$name]);
253
        }
272
        }
254
    }
273
    }
Line 272... Line 291...
272
    */
291
    */
273
    function getQueryString()
292
    function getQueryString()
274
    {
293
    {
275
        if (!empty($this->querystring)) {
294
        if (!empty($this->querystring)) {
276
            foreach ($this->querystring as $name => $value) {
295
            foreach ($this->querystring as $name => $value) {
-
 
296
                // Encode var name
-
 
297
                $name = rawurlencode($name);
-
 
298
 
277
                if (is_array($value)) {
299
                if (is_array($value)) {
278
                    foreach ($value as $k => $v) {
300
                    foreach ($value as $k => $v) {
279
                        $querystring[] = $this->useBrackets ? sprintf('%s[%s]=%s', $name, $k, $v) : ($name . '=' . $v);
301
                        $querystring[] = $this->useBrackets ? sprintf('%s[%s]=%s', $name, $k, $v) : ($name . '=' . $v);
280
                    }
302
                    }
281
                } elseif (!is_null($value)) {
303
                } elseif (!is_null($value)) {
Line 310... Line 332...
310
                $key   = substr($part, 0, strpos($part, '='));
332
                $key   = substr($part, 0, strpos($part, '='));
311
            } else {
333
            } else {
312
                $value = null;
334
                $value = null;
313
                $key   = $part;
335
                $key   = $part;
314
            }
336
            }
-
 
337
 
315
            if (substr($key, -2) == '[]') {
338
            if (!$this->getOption('encode_query_keys')) {
316
                $key = substr($key, 0, -2);
339
                $key = rawurldecode($key);
-
 
340
            }
-
 
341
 
-
 
342
            if (preg_match('#^(.*)\[([0-9a-z_-]*)\]#i', $key, $matches)) {
-
 
343
                $key = $matches[1];
-
 
344
                $idx = $matches[2];
-
 
345
 
-
 
346
                // Ensure is an array
317
                if (@!is_array($return[$key])) {
347
                if (empty($return[$key]) || !is_array($return[$key])) {
318
                    $return[$key]   = array();
348
                    $return[$key] = array();
-
 
349
                }
-
 
350
 
-
 
351
                // Add data
-
 
352
                if ($idx === '') {
319
                    $return[$key][] = $value;
353
                    $return[$key][] = $value;
320
                } else {
354
                } else {
321
                    $return[$key][] = $value;
355
                    $return[$key][$idx] = $value;
322
                }
356
                }
323
            } elseif (!$this->useBrackets AND !empty($return[$key])) {
357
            } elseif (!$this->useBrackets AND !empty($return[$key])) {
324
                $return[$key]   = (array)$return[$key];
358
                $return[$key]   = (array)$return[$key];
325
                $return[$key][] = $value;
359
                $return[$key][] = $value;
326
            } else {
360
            } else {
Line 339... Line 373...
339
    * /foo/bar/../../boo.php => /boo.php
373
    * /foo/bar/../../boo.php => /boo.php
340
    * /foo/bar/.././/boo.php => /foo/boo.php
374
    * /foo/bar/.././/boo.php => /foo/boo.php
341
    *
375
    *
342
    * This method can also be called statically.
376
    * This method can also be called statically.
343
    *
377
    *
344
    * @param  string $url URL path to resolve
378
    * @param  string $path URL path to resolve
345
    * @return string      The result
379
    * @return string      The result
346
    */
380
    */
347
    function resolvePath($path)
381
    function resolvePath($path)
348
    {
382
    {
349
        $path = explode('/', str_replace('//', '/', $path));
383
        $path = explode('/', str_replace('//', '/', $path));
Line 402... Line 436...
402
    * @param integer $port     Optional port (standard port is used by default)
436
    * @param integer $port     Optional port (standard port is used by default)
403
    */
437
    */
404
    function setProtocol($protocol, $port = null)
438
    function setProtocol($protocol, $port = null)
405
    {
439
    {
406
        $this->protocol = $protocol;
440
        $this->protocol = $protocol;
407
        $this->port = is_null($port) ? $this->getStandardPort() : $port;
441
        $this->port     = is_null($port) ? $this->getStandardPort($protocol) : $port;
-
 
442
    }
-
 
443
 
-
 
444
    /**
-
 
445
     * Set an option
-
 
446
     *
-
 
447
     * This function set an option
-
 
448
     * to be used thorough the script.
-
 
449
     *
-
 
450
     * @access public
-
 
451
     * @param  string $optionName  The optionname to set
-
 
452
     * @param  string $value       The value of this option.
-
 
453
     */
-
 
454
    function setOption($optionName, $value)
-
 
455
    {
-
 
456
        if (!array_key_exists($optionName, $this->options)) {
-
 
457
            return false;
-
 
458
        }
-
 
459
 
-
 
460
        $this->options[$optionName] = $value;
-
 
461
        $this->initialize();
-
 
462
    }
-
 
463
 
-
 
464
    /**
-
 
465
     * Get an option
-
 
466
     *
-
 
467
     * This function gets an option
-
 
468
     * from the $this->options array
-
 
469
     * and return it's value.
-
 
470
     *
-
 
471
     * @access public
-
 
472
     * @param  string $opionName  The name of the option to retrieve
-
 
473
     * @see    $this->options
-
 
474
     */
-
 
475
    function getOption($optionName)
-
 
476
    {
-
 
477
        if (!isset($this->options[$optionName])) {
-
 
478
            return false;
-
 
479
        }
-
 
480
 
-
 
481
        return $this->options[$optionName];
408
    }
482
    }
Line 409... Line 483...
409
 
483
 
410
}
484
}