1: <?php
2: /**
3: * ManiaLib - Lightweight PHP framework for Manialinks
4: *
5: * @see http://code.google.com/p/manialib/
6: * @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
7: * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
8: * @version $Revision: 590 $:
9: * @author $Author: maximeraoust $:
10: * @date $Date: 2011-12-27 19:40:38 +0100 (mar., 27 déc. 2011) $:
11: */
12:
13: namespace ManiaLib\Utils;
14:
15: abstract class URI
16: {
17:
18: /**
19: * Returns the Current URL.
20: *
21: * @return string The current URI
22: * @see http://code.google.com/p/oauth2-php
23: * @author Originally written by Naitik Shah <naitik@facebook.com>.
24: * @author Update to draft v10 by Edison Wong <hswong3i@pantarei-design.com>
25: */
26: static function getCurrent()
27: {
28: $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://'
29: : 'http://';
30: $current_uri = $protocol.$_SERVER['HTTP_HOST'].self::getRequestURI();
31: $parts = parse_url($current_uri);
32:
33: $query = '';
34: if(!empty($parts['query']))
35: {
36: $params = array();
37: parse_str($parts['query'], $params);
38: $params = array_filter($params);
39: if(!empty($params))
40: {
41: $query = '?'.http_build_query($params, '', '&');
42: }
43: }
44:
45: // Use port if non default.
46: $port = isset($parts['port']) &&
47: (($protocol === 'http://' && $parts['port'] !== 80) || ($protocol === 'https://' && $parts['port'] !== 443))
48: ? ':'.$parts['port'] : '';
49:
50: // Rebuild.
51: return $protocol.$parts['host'].$port.$parts['path'].$query;
52: }
53:
54: /**
55: * Since $_SERVER['REQUEST_URI'] is only available on Apache, we
56: * generate an equivalent using other environment variables.
57: *
58: * @see http://code.google.com/p/oauth2-php
59: * @author Originally written by Naitik Shah <naitik@facebook.com>.
60: * @author Update to draft v10 by Edison Wong <hswong3i@pantarei-design.com>
61: */
62: protected static function getRequestURI()
63: {
64: if(isset($_SERVER['REQUEST_URI']))
65: {
66: $uri = $_SERVER['REQUEST_URI'];
67: }
68: else
69: {
70: if(isset($_SERVER['argv']))
71: {
72: $uri = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['argv'][0];
73: }
74: elseif(isset($_SERVER['QUERY_STRING']))
75: {
76: $uri = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
77: }
78: else
79: {
80: $uri = $_SERVER['SCRIPT_NAME'];
81: }
82: }
83: // Prevent multiple slashes to avoid cross site requests
84: $uri = '/'.ltrim($uri, '/');
85:
86: return $uri;
87: }
88:
89: }
90:
91: ?>