| 1 |
<?php |
| 2 |
|
| 3 |
class Minify_Env |
| 4 |
{ |
| 5 |
|
| 6 |
/** |
| 7 |
* @return string |
| 8 |
*/ |
| 9 |
public function getDocRoot() |
| 10 |
{ |
| 11 |
return $this->server['DOCUMENT_ROOT']; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* @return string |
| 16 |
*/ |
| 17 |
public function getRequestUri() |
| 18 |
{ |
| 19 |
return $this->server['REQUEST_URI']; |
| 20 |
} |
| 21 |
|
| 22 |
public function __construct($options = array()) |
| 23 |
{ |
| 24 |
$options = array_merge(array( |
| 25 |
'server' => $_SERVER, |
| 26 |
'get' => $_GET, |
| 27 |
'post' => $_POST, |
| 28 |
'cookie' => $_COOKIE, |
| 29 |
), $options); |
| 30 |
|
| 31 |
$this->server = $options['server']; |
| 32 |
if (empty($this->server['DOCUMENT_ROOT'])) { |
| 33 |
$this->server['DOCUMENT_ROOT'] = $this->computeDocRoot($options['server']); |
| 34 |
} else { |
| 35 |
$this->server['DOCUMENT_ROOT'] = rtrim($this->server['DOCUMENT_ROOT'], '/\\'); |
| 36 |
} |
| 37 |
|
| 38 |
$this->server['DOCUMENT_ROOT'] = $this->normalizePath($this->server['DOCUMENT_ROOT']); |
| 39 |
$this->get = $options['get']; |
| 40 |
$this->post = $options['post']; |
| 41 |
$this->cookie = $options['cookie']; |
| 42 |
} |
| 43 |
|
| 44 |
public function server($key = null) |
| 45 |
{ |
| 46 |
if (null === $key) { |
| 47 |
return $this->server; |
| 48 |
} |
| 49 |
|
| 50 |
return isset($this->server[$key]) ? $this->server[$key] : null; |
| 51 |
} |
| 52 |
|
| 53 |
public function cookie($key = null, $default = null) |
| 54 |
{ |
| 55 |
if (null === $key) { |
| 56 |
return $this->cookie; |
| 57 |
} |
| 58 |
|
| 59 |
return isset($this->cookie[$key]) ? $this->cookie[$key] : $default; |
| 60 |
} |
| 61 |
|
| 62 |
public function get($key = null, $default = null) |
| 63 |
{ |
| 64 |
if (null === $key) { |
| 65 |
return $this->get; |
| 66 |
} |
| 67 |
|
| 68 |
return isset($this->get[$key]) ? $this->get[$key] : $default; |
| 69 |
} |
| 70 |
|
| 71 |
public function post($key = null, $default = null) |
| 72 |
{ |
| 73 |
if (null === $key) { |
| 74 |
return $this->post; |
| 75 |
} |
| 76 |
|
| 77 |
return isset($this->post[$key]) ? $this->post[$key] : $default; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* turn windows-style slashes into unix-style, |
| 82 |
* remove trailing slash |
| 83 |
* and lowercase drive letter |
| 84 |
* |
| 85 |
* @param string $path absolute path |
| 86 |
* |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
public function normalizePath($path) |
| 90 |
{ |
| 91 |
$realpath = realpath($path); |
| 92 |
if ($realpath) { |
| 93 |
$path = $realpath; |
| 94 |
} |
| 95 |
|
| 96 |
$path = str_replace('\\', '/', $path); |
| 97 |
$path = rtrim($path, '/'); |
| 98 |
if (substr($path, 1, 1) === ':') { |
| 99 |
$path = lcfirst($path); |
| 100 |
} |
| 101 |
|
| 102 |
return $path; |
| 103 |
} |
| 104 |
|
| 105 |
protected $server; |
| 106 |
protected $get; |
| 107 |
protected $post; |
| 108 |
protected $cookie; |
| 109 |
|
| 110 |
/** |
| 111 |
* Compute $_SERVER['DOCUMENT_ROOT'] for IIS using SCRIPT_FILENAME and SCRIPT_NAME. |
| 112 |
* |
| 113 |
* @param array $server |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
protected function computeDocRoot(array $server) |
| 117 |
{ |
| 118 |
if (isset($server['SERVER_SOFTWARE']) && 0 !== strpos($server['SERVER_SOFTWARE'], 'Microsoft-IIS/')) { |
| 119 |
throw new InvalidArgumentException('DOCUMENT_ROOT is not provided and could not be computed'); |
| 120 |
} |
| 121 |
|
| 122 |
$substrLength = strlen($server['SCRIPT_FILENAME']) - strlen($server['SCRIPT_NAME']); |
| 123 |
$docRoot = substr($server['SCRIPT_FILENAME'], 0, $substrLength); |
| 124 |
|
| 125 |
return rtrim($docRoot, '\\'); |
| 126 |
} |
| 127 |
} |
| 128 |
|