@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | $headers = $this->request->getHeaders(); |
| 217 | 217 | $reqHost = $this->request->getUrl()->getHost(); |
| 218 | 218 | if (!($reqPort = $this->request->getUrl()->getPort())) { |
| 219 | - $reqPort = $secure? 443: 80; |
|
| 219 | + $reqPort = $secure ? 443 : 80; |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | if ($host = $this->request->getConfig('proxy_host')) { |
@@ -253,7 +253,7 @@ discard block |
||
| 253 | 253 | empty($headers['connection'])) || |
| 254 | 254 | (!empty($headers['connection']) && |
| 255 | 255 | 'Keep-Alive' == $headers['connection']); |
| 256 | - $host = ((!$secure || $proxy)? 'tcp://': 'ssl://') . $host; |
|
| 256 | + $host = ((!$secure || $proxy) ? 'tcp://' : 'ssl://') . $host; |
|
| 257 | 257 | |
| 258 | 258 | $options = array(); |
| 259 | 259 | if ($secure || $tunnel) { |
@@ -274,8 +274,8 @@ discard block |
||
| 274 | 274 | // Changing SSL context options after connection is established does *not* |
| 275 | 275 | // work, we need a new connection if options change |
| 276 | 276 | $remote = $host . ':' . $port; |
| 277 | - $socketKey = $remote . (($secure && $proxy)? "->{$reqHost}:{$reqPort}": '') . |
|
| 278 | - (empty($options)? '': ':' . serialize($options)); |
|
| 277 | + $socketKey = $remote . (($secure && $proxy) ? "->{$reqHost}:{$reqPort}" : '') . |
|
| 278 | + (empty($options) ? '' : ':' . serialize($options)); |
|
| 279 | 279 | unset($this->socket); |
| 280 | 280 | |
| 281 | 281 | // We use persistent connections and have a connected socket? |
@@ -283,14 +283,14 @@ discard block |
||
| 283 | 283 | if ($keepAlive && !empty(self::$sockets[$socketKey]) && |
| 284 | 284 | !feof(self::$sockets[$socketKey]) |
| 285 | 285 | ) { |
| 286 | - $this->socket =& self::$sockets[$socketKey]; |
|
| 286 | + $this->socket = & self::$sockets[$socketKey]; |
|
| 287 | 287 | |
| 288 | 288 | } elseif ($secure && $proxy && !$tunnel) { |
| 289 | 289 | $this->establishTunnel(); |
| 290 | 290 | $this->request->setLastEvent( |
| 291 | 291 | 'connect', "ssl://{$reqHost}:{$reqPort} via {$host}:{$port}" |
| 292 | 292 | ); |
| 293 | - self::$sockets[$socketKey] =& $this->socket; |
|
| 293 | + self::$sockets[$socketKey] = & $this->socket; |
|
| 294 | 294 | |
| 295 | 295 | } else { |
| 296 | 296 | // Set SSL context options if doing HTTPS request or creating a tunnel |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | ); |
| 314 | 314 | } |
| 315 | 315 | $this->request->setLastEvent('connect', $remote); |
| 316 | - self::$sockets[$socketKey] =& $this->socket; |
|
| 316 | + self::$sockets[$socketKey] = & $this->socket; |
|
| 317 | 317 | } |
| 318 | 318 | return $keepAlive; |
| 319 | 319 | } |
@@ -515,7 +515,7 @@ discard block |
||
| 515 | 515 | // probably credentials are invalid |
| 516 | 516 | $ret = false; |
| 517 | 517 | } |
| 518 | - self::$challenges[$prefix] =& $challenge; |
|
| 518 | + self::$challenges[$prefix] = & $challenge; |
|
| 519 | 519 | } |
| 520 | 520 | return $ret; |
| 521 | 521 | } |
@@ -696,12 +696,10 @@ discard block |
||
| 696 | 696 | 'nonce="' . $challenge['nonce'] . '", ' . |
| 697 | 697 | 'uri="' . $url . '", ' . |
| 698 | 698 | 'response="' . $digest . '"' . |
| 699 | - (!empty($challenge['opaque'])? |
|
| 700 | - ', opaque="' . $challenge['opaque'] . '"': |
|
| 701 | - '') . |
|
| 702 | - (!empty($challenge['qop'])? |
|
| 703 | - ', qop="auth", nc=' . $nc . ', cnonce="' . $challenge['cnonce'] . '"': |
|
| 704 | - ''); |
|
| 699 | + (!empty($challenge['opaque']) ? |
|
| 700 | + ', opaque="' . $challenge['opaque'] . '"' : '') . |
|
| 701 | + (!empty($challenge['qop']) ? |
|
| 702 | + ', qop="auth", nc=' . $nc . ', cnonce="' . $challenge['cnonce'] . '"' : ''); |
|
| 705 | 703 | } |
| 706 | 704 | |
| 707 | 705 | /** |
@@ -725,17 +723,16 @@ discard block |
||
| 725 | 723 | |
| 726 | 724 | case HTTP_Request2::AUTH_DIGEST: |
| 727 | 725 | unset($this->serverChallenge); |
| 728 | - $fullUrl = ('/' == $requestUrl[0])? |
|
| 726 | + $fullUrl = ('/' == $requestUrl[0]) ? |
|
| 729 | 727 | $this->request->getUrl()->getScheme() . '://' . |
| 730 | - $requestHost . $requestUrl: |
|
| 731 | - $requestUrl; |
|
| 728 | + $requestHost . $requestUrl : $requestUrl; |
|
| 732 | 729 | foreach (array_keys(self::$challenges) as $key) { |
| 733 | 730 | if ($key == substr($fullUrl, 0, strlen($key))) { |
| 734 | 731 | $headers['authorization'] = $this->createDigestResponse( |
| 735 | 732 | $auth['user'], $auth['password'], |
| 736 | 733 | $requestUrl, self::$challenges[$key] |
| 737 | 734 | ); |
| 738 | - $this->serverChallenge =& self::$challenges[$key]; |
|
| 735 | + $this->serverChallenge = & self::$challenges[$key]; |
|
| 739 | 736 | break; |
| 740 | 737 | } |
| 741 | 738 | } |
@@ -781,7 +778,7 @@ discard block |
||
| 781 | 778 | $user, $password, |
| 782 | 779 | $requestUrl, self::$challenges[$proxyUrl] |
| 783 | 780 | ); |
| 784 | - $this->proxyChallenge =& self::$challenges[$proxyUrl]; |
|
| 781 | + $this->proxyChallenge = & self::$challenges[$proxyUrl]; |
|
| 785 | 782 | } |
| 786 | 783 | break; |
| 787 | 784 | |
@@ -807,9 +804,9 @@ discard block |
||
| 807 | 804 | $connect = HTTP_Request2::METHOD_CONNECT == $this->request->getMethod(); |
| 808 | 805 | $host = $url->getHost(); |
| 809 | 806 | |
| 810 | - $defaultPort = 0 == strcasecmp($url->getScheme(), 'https')? 443: 80; |
|
| 807 | + $defaultPort = 0 == strcasecmp($url->getScheme(), 'https') ? 443 : 80; |
|
| 811 | 808 | if (($port = $url->getPort()) && $port != $defaultPort || $connect) { |
| 812 | - $host .= ':' . (empty($port)? $defaultPort: $port); |
|
| 809 | + $host .= ':' . (empty($port) ? $defaultPort : $port); |
|
| 813 | 810 | } |
| 814 | 811 | // Do not overwrite explicitly set 'Host' header, see bug #16146 |
| 815 | 812 | if (!isset($headers['host'])) { |
@@ -829,7 +826,7 @@ discard block |
||
| 829 | 826 | } |
| 830 | 827 | $path = $url->getPath(); |
| 831 | 828 | $query = $url->getQuery(); |
| 832 | - $requestUrl .= (empty($path)? '/': $path) . (empty($query)? '': '?' . $query); |
|
| 829 | + $requestUrl .= (empty($path) ? '/' : $path) . (empty($query) ? '' : '?' . $query); |
|
| 833 | 830 | } |
| 834 | 831 | |
| 835 | 832 | if ('1.1' == $this->request->getConfig('protocol_version') && |
@@ -920,7 +917,7 @@ discard block |
||
| 920 | 917 | // 3. ... If a message is received with both a |
| 921 | 918 | // Transfer-Encoding header field and a Content-Length header field, |
| 922 | 919 | // the latter MUST be ignored. |
| 923 | - $toRead = ($chunked || null === $length)? null: $length; |
|
| 920 | + $toRead = ($chunked || null === $length) ? null : $length; |
|
| 924 | 921 | $this->chunkLength = 0; |
| 925 | 922 | |
| 926 | 923 | while (!feof($this->socket) && (is_null($toRead) || 0 < $toRead)) { |
@@ -62,19 +62,19 @@ discard block |
||
| 62 | 62 | /** |
| 63 | 63 | * Regular expression for 'token' rule from RFC 2616 |
| 64 | 64 | */ |
| 65 | - const REGEXP_TOKEN = '[^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+'; |
|
| 65 | + const REGEXP_TOKEN = '[^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+'; |
|
| 66 | 66 | |
| 67 | 67 | /** |
| 68 | 68 | * Regular expression for 'quoted-string' rule from RFC 2616 |
| 69 | 69 | */ |
| 70 | - const REGEXP_QUOTED_STRING = '"(?:\\\\.|[^\\\\"])*"'; |
|
| 70 | + const REGEXP_QUOTED_STRING = '"(?:\\\\.|[^\\\\"])*"'; |
|
| 71 | 71 | |
| 72 | 72 | /** |
| 73 | 73 | * Connected sockets, needed for Keep-Alive support |
| 74 | 74 | * @var array |
| 75 | 75 | * @see connect() |
| 76 | 76 | */ |
| 77 | - protected static $sockets = array(); |
|
| 77 | + protected static $sockets = array(); |
|
| 78 | 78 | |
| 79 | 79 | /** |
| 80 | 80 | * Data for digest authentication scheme |
@@ -88,39 +88,39 @@ discard block |
||
| 88 | 88 | * |
| 89 | 89 | * @var array |
| 90 | 90 | */ |
| 91 | - protected static $challenges = array(); |
|
| 91 | + protected static $challenges = array(); |
|
| 92 | 92 | |
| 93 | 93 | /** |
| 94 | 94 | * Connected socket |
| 95 | 95 | * @var resource |
| 96 | 96 | * @see connect() |
| 97 | 97 | */ |
| 98 | - protected $socket; |
|
| 98 | + protected $socket; |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * Challenge used for server digest authentication |
| 102 | 102 | * @var array |
| 103 | 103 | */ |
| 104 | - protected $serverChallenge; |
|
| 104 | + protected $serverChallenge; |
|
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * Challenge used for proxy digest authentication |
| 108 | 108 | * @var array |
| 109 | 109 | */ |
| 110 | - protected $proxyChallenge; |
|
| 110 | + protected $proxyChallenge; |
|
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * Sum of start time and global timeout, exception will be thrown if request continues past this time |
| 114 | 114 | * @var integer |
| 115 | 115 | */ |
| 116 | - protected $deadline = null; |
|
| 116 | + protected $deadline = null; |
|
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | 119 | * Remaining length of the current chunk, when reading chunked response |
| 120 | 120 | * @var integer |
| 121 | 121 | * @see readChunked() |
| 122 | 122 | */ |
| 123 | - protected $chunkLength = 0; |
|
| 123 | + protected $chunkLength = 0; |
|
| 124 | 124 | |
| 125 | 125 | /** |
| 126 | 126 | * Remaining amount of redirections to follow |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | * @var integer |
| 132 | 132 | */ |
| 133 | - protected $redirectCountdown = null; |
|
| 133 | + protected $redirectCountdown = null; |
|
| 134 | 134 | |
| 135 | 135 | /** |
| 136 | 136 | * Sends request to the remote server and returns its response |
@@ -139,69 +139,69 @@ discard block |
||
| 139 | 139 | * @return HTTP_Request2_Response |
| 140 | 140 | * @throws HTTP_Request2_Exception |
| 141 | 141 | */ |
| 142 | - public function sendRequest(HTTP_Request2 $request) |
|
| 143 | - { |
|
| 144 | - $this->request = $request; |
|
| 145 | - |
|
| 146 | - // Use global request timeout if given, see feature requests #5735, #8964 |
|
| 147 | - if ($timeout = $request->getConfig('timeout')) { |
|
| 148 | - $this->deadline = time() + $timeout; |
|
| 149 | - } else { |
|
| 150 | - $this->deadline = null; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - try { |
|
| 154 | - $keepAlive = $this->connect(); |
|
| 155 | - $headers = $this->prepareHeaders(); |
|
| 156 | - if (false === @fwrite($this->socket, $headers, strlen($headers))) { |
|
| 157 | - throw new HTTP_Request2_Exception('Error writing request'); |
|
| 158 | - } |
|
| 159 | - // provide request headers to the observer, see request #7633 |
|
| 160 | - $this->request->setLastEvent('sentHeaders', $headers); |
|
| 161 | - $this->writeBody(); |
|
| 162 | - |
|
| 163 | - if ($this->deadline && time() > $this->deadline) { |
|
| 164 | - throw new HTTP_Request2_Exception( |
|
| 165 | - 'Request timed out after ' . |
|
| 166 | - $request->getConfig('timeout') . ' second(s)' |
|
| 167 | - ); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $response = $this->readResponse(); |
|
| 171 | - |
|
| 172 | - if (!$this->canKeepAlive($keepAlive, $response)) { |
|
| 173 | - $this->disconnect(); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - if ($this->shouldUseProxyDigestAuth($response)) { |
|
| 177 | - return $this->sendRequest($request); |
|
| 178 | - } |
|
| 179 | - if ($this->shouldUseServerDigestAuth($response)) { |
|
| 180 | - return $this->sendRequest($request); |
|
| 181 | - } |
|
| 182 | - if ($authInfo = $response->getHeader('authentication-info')) { |
|
| 183 | - $this->updateChallenge($this->serverChallenge, $authInfo); |
|
| 184 | - } |
|
| 185 | - if ($proxyInfo = $response->getHeader('proxy-authentication-info')) { |
|
| 186 | - $this->updateChallenge($this->proxyChallenge, $proxyInfo); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - } catch (Exception $e) { |
|
| 190 | - $this->disconnect(); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - unset($this->request, $this->requestBody); |
|
| 194 | - |
|
| 195 | - if (!empty($e)) { |
|
| 196 | - throw $e; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - if (!$request->getConfig('follow_redirects') || !$response->isRedirect()) { |
|
| 200 | - return $response; |
|
| 201 | - } else { |
|
| 202 | - return $this->handleRedirect($request, $response); |
|
| 203 | - } |
|
| 204 | - } |
|
| 142 | + public function sendRequest(HTTP_Request2 $request) |
|
| 143 | + { |
|
| 144 | + $this->request = $request; |
|
| 145 | + |
|
| 146 | + // Use global request timeout if given, see feature requests #5735, #8964 |
|
| 147 | + if ($timeout = $request->getConfig('timeout')) { |
|
| 148 | + $this->deadline = time() + $timeout; |
|
| 149 | + } else { |
|
| 150 | + $this->deadline = null; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + try { |
|
| 154 | + $keepAlive = $this->connect(); |
|
| 155 | + $headers = $this->prepareHeaders(); |
|
| 156 | + if (false === @fwrite($this->socket, $headers, strlen($headers))) { |
|
| 157 | + throw new HTTP_Request2_Exception('Error writing request'); |
|
| 158 | + } |
|
| 159 | + // provide request headers to the observer, see request #7633 |
|
| 160 | + $this->request->setLastEvent('sentHeaders', $headers); |
|
| 161 | + $this->writeBody(); |
|
| 162 | + |
|
| 163 | + if ($this->deadline && time() > $this->deadline) { |
|
| 164 | + throw new HTTP_Request2_Exception( |
|
| 165 | + 'Request timed out after ' . |
|
| 166 | + $request->getConfig('timeout') . ' second(s)' |
|
| 167 | + ); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $response = $this->readResponse(); |
|
| 171 | + |
|
| 172 | + if (!$this->canKeepAlive($keepAlive, $response)) { |
|
| 173 | + $this->disconnect(); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + if ($this->shouldUseProxyDigestAuth($response)) { |
|
| 177 | + return $this->sendRequest($request); |
|
| 178 | + } |
|
| 179 | + if ($this->shouldUseServerDigestAuth($response)) { |
|
| 180 | + return $this->sendRequest($request); |
|
| 181 | + } |
|
| 182 | + if ($authInfo = $response->getHeader('authentication-info')) { |
|
| 183 | + $this->updateChallenge($this->serverChallenge, $authInfo); |
|
| 184 | + } |
|
| 185 | + if ($proxyInfo = $response->getHeader('proxy-authentication-info')) { |
|
| 186 | + $this->updateChallenge($this->proxyChallenge, $proxyInfo); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + } catch (Exception $e) { |
|
| 190 | + $this->disconnect(); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + unset($this->request, $this->requestBody); |
|
| 194 | + |
|
| 195 | + if (!empty($e)) { |
|
| 196 | + throw $e; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + if (!$request->getConfig('follow_redirects') || !$response->isRedirect()) { |
|
| 200 | + return $response; |
|
| 201 | + } else { |
|
| 202 | + return $this->handleRedirect($request, $response); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | 206 | /** |
| 207 | 207 | * Connects to the remote server |
@@ -209,114 +209,114 @@ discard block |
||
| 209 | 209 | * @return bool whether the connection can be persistent |
| 210 | 210 | * @throws HTTP_Request2_Exception |
| 211 | 211 | */ |
| 212 | - protected function connect() |
|
| 213 | - { |
|
| 214 | - $secure = 0 == strcasecmp($this->request->getUrl()->getScheme(), 'https'); |
|
| 215 | - $tunnel = HTTP_Request2::METHOD_CONNECT == $this->request->getMethod(); |
|
| 216 | - $headers = $this->request->getHeaders(); |
|
| 217 | - $reqHost = $this->request->getUrl()->getHost(); |
|
| 218 | - if (!($reqPort = $this->request->getUrl()->getPort())) { |
|
| 219 | - $reqPort = $secure? 443: 80; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - if ($host = $this->request->getConfig('proxy_host')) { |
|
| 223 | - if (!($port = $this->request->getConfig('proxy_port'))) { |
|
| 224 | - throw new HTTP_Request2_Exception('Proxy port not provided'); |
|
| 225 | - } |
|
| 226 | - $proxy = true; |
|
| 227 | - } else { |
|
| 228 | - $host = $reqHost; |
|
| 229 | - $port = $reqPort; |
|
| 230 | - $proxy = false; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - if ($tunnel && !$proxy) { |
|
| 234 | - throw new HTTP_Request2_Exception( |
|
| 235 | - "Trying to perform CONNECT request without proxy" |
|
| 236 | - ); |
|
| 237 | - } |
|
| 238 | - if ($secure && !in_array('ssl', stream_get_transports())) { |
|
| 239 | - throw new HTTP_Request2_Exception( |
|
| 240 | - 'Need OpenSSL support for https:// requests' |
|
| 241 | - ); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive |
|
| 245 | - // connection token to a proxy server... |
|
| 246 | - if ($proxy && !$secure && |
|
| 247 | - !empty($headers['connection']) && 'Keep-Alive' == $headers['connection'] |
|
| 248 | - ) { |
|
| 249 | - $this->request->setHeader('connection'); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $keepAlive = ('1.1' == $this->request->getConfig('protocol_version') && |
|
| 253 | - empty($headers['connection'])) || |
|
| 254 | - (!empty($headers['connection']) && |
|
| 255 | - 'Keep-Alive' == $headers['connection']); |
|
| 256 | - $host = ((!$secure || $proxy)? 'tcp://': 'ssl://') . $host; |
|
| 257 | - |
|
| 258 | - $options = array(); |
|
| 259 | - if ($secure || $tunnel) { |
|
| 260 | - foreach ($this->request->getConfig() as $name => $value) { |
|
| 261 | - if ('ssl_' == substr($name, 0, 4) && null !== $value) { |
|
| 262 | - if ('ssl_verify_host' == $name) { |
|
| 263 | - if ($value) { |
|
| 264 | - $options['CN_match'] = $reqHost; |
|
| 265 | - } |
|
| 266 | - } else { |
|
| 267 | - $options[substr($name, 4)] = $value; |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - ksort($options); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - // Changing SSL context options after connection is established does *not* |
|
| 275 | - // work, we need a new connection if options change |
|
| 276 | - $remote = $host . ':' . $port; |
|
| 277 | - $socketKey = $remote . (($secure && $proxy)? "->{$reqHost}:{$reqPort}": '') . |
|
| 278 | - (empty($options)? '': ':' . serialize($options)); |
|
| 279 | - unset($this->socket); |
|
| 280 | - |
|
| 281 | - // We use persistent connections and have a connected socket? |
|
| 282 | - // Ensure that the socket is still connected, see bug #16149 |
|
| 283 | - if ($keepAlive && !empty(self::$sockets[$socketKey]) && |
|
| 284 | - !feof(self::$sockets[$socketKey]) |
|
| 285 | - ) { |
|
| 286 | - $this->socket =& self::$sockets[$socketKey]; |
|
| 287 | - |
|
| 288 | - } elseif ($secure && $proxy && !$tunnel) { |
|
| 289 | - $this->establishTunnel(); |
|
| 290 | - $this->request->setLastEvent( |
|
| 291 | - 'connect', "ssl://{$reqHost}:{$reqPort} via {$host}:{$port}" |
|
| 292 | - ); |
|
| 293 | - self::$sockets[$socketKey] =& $this->socket; |
|
| 294 | - |
|
| 295 | - } else { |
|
| 296 | - // Set SSL context options if doing HTTPS request or creating a tunnel |
|
| 297 | - $context = stream_context_create(); |
|
| 298 | - foreach ($options as $name => $value) { |
|
| 299 | - if (!stream_context_set_option($context, 'ssl', $name, $value)) { |
|
| 300 | - throw new HTTP_Request2_Exception( |
|
| 301 | - "Error setting SSL context option '{$name}'" |
|
| 302 | - ); |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - $this->socket = @stream_socket_client( |
|
| 306 | - $remote, $errno, $errstr, |
|
| 307 | - $this->request->getConfig('connect_timeout'), |
|
| 308 | - STREAM_CLIENT_CONNECT, $context |
|
| 309 | - ); |
|
| 310 | - if (!$this->socket) { |
|
| 311 | - throw new HTTP_Request2_Exception( |
|
| 312 | - "Unable to connect to {$remote}. Error #{$errno}: {$errstr}" |
|
| 313 | - ); |
|
| 314 | - } |
|
| 315 | - $this->request->setLastEvent('connect', $remote); |
|
| 316 | - self::$sockets[$socketKey] =& $this->socket; |
|
| 317 | - } |
|
| 318 | - return $keepAlive; |
|
| 319 | - } |
|
| 212 | + protected function connect() |
|
| 213 | + { |
|
| 214 | + $secure = 0 == strcasecmp($this->request->getUrl()->getScheme(), 'https'); |
|
| 215 | + $tunnel = HTTP_Request2::METHOD_CONNECT == $this->request->getMethod(); |
|
| 216 | + $headers = $this->request->getHeaders(); |
|
| 217 | + $reqHost = $this->request->getUrl()->getHost(); |
|
| 218 | + if (!($reqPort = $this->request->getUrl()->getPort())) { |
|
| 219 | + $reqPort = $secure? 443: 80; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + if ($host = $this->request->getConfig('proxy_host')) { |
|
| 223 | + if (!($port = $this->request->getConfig('proxy_port'))) { |
|
| 224 | + throw new HTTP_Request2_Exception('Proxy port not provided'); |
|
| 225 | + } |
|
| 226 | + $proxy = true; |
|
| 227 | + } else { |
|
| 228 | + $host = $reqHost; |
|
| 229 | + $port = $reqPort; |
|
| 230 | + $proxy = false; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + if ($tunnel && !$proxy) { |
|
| 234 | + throw new HTTP_Request2_Exception( |
|
| 235 | + "Trying to perform CONNECT request without proxy" |
|
| 236 | + ); |
|
| 237 | + } |
|
| 238 | + if ($secure && !in_array('ssl', stream_get_transports())) { |
|
| 239 | + throw new HTTP_Request2_Exception( |
|
| 240 | + 'Need OpenSSL support for https:// requests' |
|
| 241 | + ); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive |
|
| 245 | + // connection token to a proxy server... |
|
| 246 | + if ($proxy && !$secure && |
|
| 247 | + !empty($headers['connection']) && 'Keep-Alive' == $headers['connection'] |
|
| 248 | + ) { |
|
| 249 | + $this->request->setHeader('connection'); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $keepAlive = ('1.1' == $this->request->getConfig('protocol_version') && |
|
| 253 | + empty($headers['connection'])) || |
|
| 254 | + (!empty($headers['connection']) && |
|
| 255 | + 'Keep-Alive' == $headers['connection']); |
|
| 256 | + $host = ((!$secure || $proxy)? 'tcp://': 'ssl://') . $host; |
|
| 257 | + |
|
| 258 | + $options = array(); |
|
| 259 | + if ($secure || $tunnel) { |
|
| 260 | + foreach ($this->request->getConfig() as $name => $value) { |
|
| 261 | + if ('ssl_' == substr($name, 0, 4) && null !== $value) { |
|
| 262 | + if ('ssl_verify_host' == $name) { |
|
| 263 | + if ($value) { |
|
| 264 | + $options['CN_match'] = $reqHost; |
|
| 265 | + } |
|
| 266 | + } else { |
|
| 267 | + $options[substr($name, 4)] = $value; |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + ksort($options); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + // Changing SSL context options after connection is established does *not* |
|
| 275 | + // work, we need a new connection if options change |
|
| 276 | + $remote = $host . ':' . $port; |
|
| 277 | + $socketKey = $remote . (($secure && $proxy)? "->{$reqHost}:{$reqPort}": '') . |
|
| 278 | + (empty($options)? '': ':' . serialize($options)); |
|
| 279 | + unset($this->socket); |
|
| 280 | + |
|
| 281 | + // We use persistent connections and have a connected socket? |
|
| 282 | + // Ensure that the socket is still connected, see bug #16149 |
|
| 283 | + if ($keepAlive && !empty(self::$sockets[$socketKey]) && |
|
| 284 | + !feof(self::$sockets[$socketKey]) |
|
| 285 | + ) { |
|
| 286 | + $this->socket =& self::$sockets[$socketKey]; |
|
| 287 | + |
|
| 288 | + } elseif ($secure && $proxy && !$tunnel) { |
|
| 289 | + $this->establishTunnel(); |
|
| 290 | + $this->request->setLastEvent( |
|
| 291 | + 'connect', "ssl://{$reqHost}:{$reqPort} via {$host}:{$port}" |
|
| 292 | + ); |
|
| 293 | + self::$sockets[$socketKey] =& $this->socket; |
|
| 294 | + |
|
| 295 | + } else { |
|
| 296 | + // Set SSL context options if doing HTTPS request or creating a tunnel |
|
| 297 | + $context = stream_context_create(); |
|
| 298 | + foreach ($options as $name => $value) { |
|
| 299 | + if (!stream_context_set_option($context, 'ssl', $name, $value)) { |
|
| 300 | + throw new HTTP_Request2_Exception( |
|
| 301 | + "Error setting SSL context option '{$name}'" |
|
| 302 | + ); |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + $this->socket = @stream_socket_client( |
|
| 306 | + $remote, $errno, $errstr, |
|
| 307 | + $this->request->getConfig('connect_timeout'), |
|
| 308 | + STREAM_CLIENT_CONNECT, $context |
|
| 309 | + ); |
|
| 310 | + if (!$this->socket) { |
|
| 311 | + throw new HTTP_Request2_Exception( |
|
| 312 | + "Unable to connect to {$remote}. Error #{$errno}: {$errstr}" |
|
| 313 | + ); |
|
| 314 | + } |
|
| 315 | + $this->request->setLastEvent('connect', $remote); |
|
| 316 | + self::$sockets[$socketKey] =& $this->socket; |
|
| 317 | + } |
|
| 318 | + return $keepAlive; |
|
| 319 | + } |
|
| 320 | 320 | |
| 321 | 321 | /** |
| 322 | 322 | * Establishes a tunnel to a secure remote server via HTTP CONNECT request |
@@ -328,40 +328,40 @@ discard block |
||
| 328 | 328 | * @link http://tools.ietf.org/html/rfc2817#section-5.2 |
| 329 | 329 | * @throws HTTP_Request2_Exception |
| 330 | 330 | */ |
| 331 | - protected function establishTunnel() |
|
| 332 | - { |
|
| 333 | - $donor = new self; |
|
| 334 | - $connect = new HTTP_Request2( |
|
| 335 | - $this->request->getUrl(), HTTP_Request2::METHOD_CONNECT, |
|
| 336 | - array_merge($this->request->getConfig(), |
|
| 337 | - array('adapter' => $donor)) |
|
| 338 | - ); |
|
| 339 | - $response = $connect->send(); |
|
| 340 | - // Need any successful (2XX) response |
|
| 341 | - if (200 > $response->getStatus() || 300 <= $response->getStatus()) { |
|
| 342 | - throw new HTTP_Request2_Exception( |
|
| 343 | - 'Failed to connect via HTTPS proxy. Proxy response: ' . |
|
| 344 | - $response->getStatus() . ' ' . $response->getReasonPhrase() |
|
| 345 | - ); |
|
| 346 | - } |
|
| 347 | - $this->socket = $donor->socket; |
|
| 348 | - |
|
| 349 | - $modes = array( |
|
| 350 | - STREAM_CRYPTO_METHOD_TLS_CLIENT, |
|
| 351 | - STREAM_CRYPTO_METHOD_SSLv3_CLIENT, |
|
| 352 | - STREAM_CRYPTO_METHOD_SSLv23_CLIENT, |
|
| 353 | - STREAM_CRYPTO_METHOD_SSLv2_CLIENT |
|
| 354 | - ); |
|
| 355 | - |
|
| 356 | - foreach ($modes as $mode) { |
|
| 357 | - if (stream_socket_enable_crypto($this->socket, true, $mode)) { |
|
| 358 | - return; |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - throw new HTTP_Request2_Exception( |
|
| 362 | - 'Failed to enable secure connection when connecting through proxy' |
|
| 363 | - ); |
|
| 364 | - } |
|
| 331 | + protected function establishTunnel() |
|
| 332 | + { |
|
| 333 | + $donor = new self; |
|
| 334 | + $connect = new HTTP_Request2( |
|
| 335 | + $this->request->getUrl(), HTTP_Request2::METHOD_CONNECT, |
|
| 336 | + array_merge($this->request->getConfig(), |
|
| 337 | + array('adapter' => $donor)) |
|
| 338 | + ); |
|
| 339 | + $response = $connect->send(); |
|
| 340 | + // Need any successful (2XX) response |
|
| 341 | + if (200 > $response->getStatus() || 300 <= $response->getStatus()) { |
|
| 342 | + throw new HTTP_Request2_Exception( |
|
| 343 | + 'Failed to connect via HTTPS proxy. Proxy response: ' . |
|
| 344 | + $response->getStatus() . ' ' . $response->getReasonPhrase() |
|
| 345 | + ); |
|
| 346 | + } |
|
| 347 | + $this->socket = $donor->socket; |
|
| 348 | + |
|
| 349 | + $modes = array( |
|
| 350 | + STREAM_CRYPTO_METHOD_TLS_CLIENT, |
|
| 351 | + STREAM_CRYPTO_METHOD_SSLv3_CLIENT, |
|
| 352 | + STREAM_CRYPTO_METHOD_SSLv23_CLIENT, |
|
| 353 | + STREAM_CRYPTO_METHOD_SSLv2_CLIENT |
|
| 354 | + ); |
|
| 355 | + |
|
| 356 | + foreach ($modes as $mode) { |
|
| 357 | + if (stream_socket_enable_crypto($this->socket, true, $mode)) { |
|
| 358 | + return; |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + throw new HTTP_Request2_Exception( |
|
| 362 | + 'Failed to enable secure connection when connecting through proxy' |
|
| 363 | + ); |
|
| 364 | + } |
|
| 365 | 365 | |
| 366 | 366 | /** |
| 367 | 367 | * Checks whether current connection may be reused or should be closed |
@@ -371,34 +371,34 @@ discard block |
||
| 371 | 371 | * @param HTTP_Request2_Response response object to check |
| 372 | 372 | * @return boolean |
| 373 | 373 | */ |
| 374 | - protected function canKeepAlive($requestKeepAlive, HTTP_Request2_Response $response) |
|
| 375 | - { |
|
| 376 | - // Do not close socket on successful CONNECT request |
|
| 377 | - if (HTTP_Request2::METHOD_CONNECT == $this->request->getMethod() && |
|
| 378 | - 200 <= $response->getStatus() && 300 > $response->getStatus() |
|
| 379 | - ) { |
|
| 380 | - return true; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - $lengthKnown = 'chunked' == strtolower($response->getHeader('transfer-encoding')) || |
|
| 384 | - null !== $response->getHeader('content-length'); |
|
| 385 | - $persistent = 'keep-alive' == strtolower($response->getHeader('connection')) || |
|
| 386 | - (null === $response->getHeader('connection') && |
|
| 387 | - '1.1' == $response->getVersion()); |
|
| 388 | - return $requestKeepAlive && $lengthKnown && $persistent; |
|
| 389 | - } |
|
| 374 | + protected function canKeepAlive($requestKeepAlive, HTTP_Request2_Response $response) |
|
| 375 | + { |
|
| 376 | + // Do not close socket on successful CONNECT request |
|
| 377 | + if (HTTP_Request2::METHOD_CONNECT == $this->request->getMethod() && |
|
| 378 | + 200 <= $response->getStatus() && 300 > $response->getStatus() |
|
| 379 | + ) { |
|
| 380 | + return true; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + $lengthKnown = 'chunked' == strtolower($response->getHeader('transfer-encoding')) || |
|
| 384 | + null !== $response->getHeader('content-length'); |
|
| 385 | + $persistent = 'keep-alive' == strtolower($response->getHeader('connection')) || |
|
| 386 | + (null === $response->getHeader('connection') && |
|
| 387 | + '1.1' == $response->getVersion()); |
|
| 388 | + return $requestKeepAlive && $lengthKnown && $persistent; |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | 391 | /** |
| 392 | 392 | * Disconnects from the remote server |
| 393 | 393 | */ |
| 394 | - protected function disconnect() |
|
| 395 | - { |
|
| 396 | - if (is_resource($this->socket)) { |
|
| 397 | - fclose($this->socket); |
|
| 398 | - $this->socket = null; |
|
| 399 | - $this->request->setLastEvent('disconnect'); |
|
| 400 | - } |
|
| 401 | - } |
|
| 394 | + protected function disconnect() |
|
| 395 | + { |
|
| 396 | + if (is_resource($this->socket)) { |
|
| 397 | + fclose($this->socket); |
|
| 398 | + $this->socket = null; |
|
| 399 | + $this->request->setLastEvent('disconnect'); |
|
| 400 | + } |
|
| 401 | + } |
|
| 402 | 402 | |
| 403 | 403 | /** |
| 404 | 404 | * Handles HTTP redirection |
@@ -412,49 +412,49 @@ discard block |
||
| 412 | 412 | * @return HTTP_Request2_Response Response from a new location |
| 413 | 413 | * @throws HTTP_Request2_Exception |
| 414 | 414 | */ |
| 415 | - protected function handleRedirect(HTTP_Request2 $request, |
|
| 416 | - HTTP_Request2_Response $response) |
|
| 417 | - { |
|
| 418 | - if (is_null($this->redirectCountdown)) { |
|
| 419 | - $this->redirectCountdown = $request->getConfig('max_redirects'); |
|
| 420 | - } |
|
| 421 | - if (0 == $this->redirectCountdown) { |
|
| 422 | - // Copying cURL behaviour |
|
| 423 | - throw new HTTP_Request2_Exception( |
|
| 424 | - 'Maximum (' . $request->getConfig('max_redirects') . ') redirects followed' |
|
| 425 | - ); |
|
| 426 | - } |
|
| 427 | - $redirectUrl = new Net_URL2( |
|
| 428 | - $response->getHeader('location'), |
|
| 429 | - array(Net_URL2::OPTION_USE_BRACKETS => $request->getConfig('use_brackets')) |
|
| 430 | - ); |
|
| 431 | - // refuse non-HTTP redirect |
|
| 432 | - if ($redirectUrl->isAbsolute() |
|
| 433 | - && !in_array($redirectUrl->getScheme(), array('http', 'https')) |
|
| 434 | - ) { |
|
| 435 | - throw new HTTP_Request2_Exception( |
|
| 436 | - 'Refusing to redirect to a non-HTTP URL ' . $redirectUrl->__toString() |
|
| 437 | - ); |
|
| 438 | - } |
|
| 439 | - // Theoretically URL should be absolute (see http://tools.ietf.org/html/rfc2616#section-14.30), |
|
| 440 | - // but in practice it is often not |
|
| 441 | - if (!$redirectUrl->isAbsolute()) { |
|
| 442 | - $redirectUrl = $request->getUrl()->resolve($redirectUrl); |
|
| 443 | - } |
|
| 444 | - $redirect = clone $request; |
|
| 445 | - $redirect->setUrl($redirectUrl); |
|
| 446 | - if (303 == $response->getStatus() || (!$request->getConfig('strict_redirects') |
|
| 447 | - && in_array($response->getStatus(), array(301, 302))) |
|
| 448 | - ) { |
|
| 449 | - $redirect->setMethod(HTTP_Request2::METHOD_GET); |
|
| 450 | - $redirect->setBody(''); |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - if (0 < $this->redirectCountdown) { |
|
| 454 | - $this->redirectCountdown--; |
|
| 455 | - } |
|
| 456 | - return $this->sendRequest($redirect); |
|
| 457 | - } |
|
| 415 | + protected function handleRedirect(HTTP_Request2 $request, |
|
| 416 | + HTTP_Request2_Response $response) |
|
| 417 | + { |
|
| 418 | + if (is_null($this->redirectCountdown)) { |
|
| 419 | + $this->redirectCountdown = $request->getConfig('max_redirects'); |
|
| 420 | + } |
|
| 421 | + if (0 == $this->redirectCountdown) { |
|
| 422 | + // Copying cURL behaviour |
|
| 423 | + throw new HTTP_Request2_Exception( |
|
| 424 | + 'Maximum (' . $request->getConfig('max_redirects') . ') redirects followed' |
|
| 425 | + ); |
|
| 426 | + } |
|
| 427 | + $redirectUrl = new Net_URL2( |
|
| 428 | + $response->getHeader('location'), |
|
| 429 | + array(Net_URL2::OPTION_USE_BRACKETS => $request->getConfig('use_brackets')) |
|
| 430 | + ); |
|
| 431 | + // refuse non-HTTP redirect |
|
| 432 | + if ($redirectUrl->isAbsolute() |
|
| 433 | + && !in_array($redirectUrl->getScheme(), array('http', 'https')) |
|
| 434 | + ) { |
|
| 435 | + throw new HTTP_Request2_Exception( |
|
| 436 | + 'Refusing to redirect to a non-HTTP URL ' . $redirectUrl->__toString() |
|
| 437 | + ); |
|
| 438 | + } |
|
| 439 | + // Theoretically URL should be absolute (see http://tools.ietf.org/html/rfc2616#section-14.30), |
|
| 440 | + // but in practice it is often not |
|
| 441 | + if (!$redirectUrl->isAbsolute()) { |
|
| 442 | + $redirectUrl = $request->getUrl()->resolve($redirectUrl); |
|
| 443 | + } |
|
| 444 | + $redirect = clone $request; |
|
| 445 | + $redirect->setUrl($redirectUrl); |
|
| 446 | + if (303 == $response->getStatus() || (!$request->getConfig('strict_redirects') |
|
| 447 | + && in_array($response->getStatus(), array(301, 302))) |
|
| 448 | + ) { |
|
| 449 | + $redirect->setMethod(HTTP_Request2::METHOD_GET); |
|
| 450 | + $redirect->setBody(''); |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + if (0 < $this->redirectCountdown) { |
|
| 454 | + $this->redirectCountdown--; |
|
| 455 | + } |
|
| 456 | + return $this->sendRequest($redirect); |
|
| 457 | + } |
|
| 458 | 458 | |
| 459 | 459 | /** |
| 460 | 460 | * Checks whether another request should be performed with server digest auth |
@@ -473,52 +473,52 @@ discard block |
||
| 473 | 473 | * @return boolean whether another request should be performed |
| 474 | 474 | * @throws HTTP_Request2_Exception in case of unsupported challenge parameters |
| 475 | 475 | */ |
| 476 | - protected function shouldUseServerDigestAuth(HTTP_Request2_Response $response) |
|
| 477 | - { |
|
| 478 | - // no sense repeating a request if we don't have credentials |
|
| 479 | - if (401 != $response->getStatus() || !$this->request->getAuth()) { |
|
| 480 | - return false; |
|
| 481 | - } |
|
| 482 | - if (!$challenge = $this->parseDigestChallenge($response->getHeader('www-authenticate'))) { |
|
| 483 | - return false; |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - $url = $this->request->getUrl(); |
|
| 487 | - $scheme = $url->getScheme(); |
|
| 488 | - $host = $scheme . '://' . $url->getHost(); |
|
| 489 | - if ($port = $url->getPort()) { |
|
| 490 | - if ((0 == strcasecmp($scheme, 'http') && 80 != $port) || |
|
| 491 | - (0 == strcasecmp($scheme, 'https') && 443 != $port) |
|
| 492 | - ) { |
|
| 493 | - $host .= ':' . $port; |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - if (!empty($challenge['domain'])) { |
|
| 498 | - $prefixes = array(); |
|
| 499 | - foreach (preg_split('/\\s+/', $challenge['domain']) as $prefix) { |
|
| 500 | - // don't bother with different servers |
|
| 501 | - if ('/' == substr($prefix, 0, 1)) { |
|
| 502 | - $prefixes[] = $host . $prefix; |
|
| 503 | - } |
|
| 504 | - } |
|
| 505 | - } |
|
| 506 | - if (empty($prefixes)) { |
|
| 507 | - $prefixes = array($host . '/'); |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - $ret = true; |
|
| 511 | - foreach ($prefixes as $prefix) { |
|
| 512 | - if (!empty(self::$challenges[$prefix]) && |
|
| 513 | - (empty($challenge['stale']) || strcasecmp('true', $challenge['stale'])) |
|
| 514 | - ) { |
|
| 515 | - // probably credentials are invalid |
|
| 516 | - $ret = false; |
|
| 517 | - } |
|
| 518 | - self::$challenges[$prefix] =& $challenge; |
|
| 519 | - } |
|
| 520 | - return $ret; |
|
| 521 | - } |
|
| 476 | + protected function shouldUseServerDigestAuth(HTTP_Request2_Response $response) |
|
| 477 | + { |
|
| 478 | + // no sense repeating a request if we don't have credentials |
|
| 479 | + if (401 != $response->getStatus() || !$this->request->getAuth()) { |
|
| 480 | + return false; |
|
| 481 | + } |
|
| 482 | + if (!$challenge = $this->parseDigestChallenge($response->getHeader('www-authenticate'))) { |
|
| 483 | + return false; |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + $url = $this->request->getUrl(); |
|
| 487 | + $scheme = $url->getScheme(); |
|
| 488 | + $host = $scheme . '://' . $url->getHost(); |
|
| 489 | + if ($port = $url->getPort()) { |
|
| 490 | + if ((0 == strcasecmp($scheme, 'http') && 80 != $port) || |
|
| 491 | + (0 == strcasecmp($scheme, 'https') && 443 != $port) |
|
| 492 | + ) { |
|
| 493 | + $host .= ':' . $port; |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + if (!empty($challenge['domain'])) { |
|
| 498 | + $prefixes = array(); |
|
| 499 | + foreach (preg_split('/\\s+/', $challenge['domain']) as $prefix) { |
|
| 500 | + // don't bother with different servers |
|
| 501 | + if ('/' == substr($prefix, 0, 1)) { |
|
| 502 | + $prefixes[] = $host . $prefix; |
|
| 503 | + } |
|
| 504 | + } |
|
| 505 | + } |
|
| 506 | + if (empty($prefixes)) { |
|
| 507 | + $prefixes = array($host . '/'); |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + $ret = true; |
|
| 511 | + foreach ($prefixes as $prefix) { |
|
| 512 | + if (!empty(self::$challenges[$prefix]) && |
|
| 513 | + (empty($challenge['stale']) || strcasecmp('true', $challenge['stale'])) |
|
| 514 | + ) { |
|
| 515 | + // probably credentials are invalid |
|
| 516 | + $ret = false; |
|
| 517 | + } |
|
| 518 | + self::$challenges[$prefix] =& $challenge; |
|
| 519 | + } |
|
| 520 | + return $ret; |
|
| 521 | + } |
|
| 522 | 522 | |
| 523 | 523 | /** |
| 524 | 524 | * Checks whether another request should be performed with proxy digest auth |
@@ -537,28 +537,28 @@ discard block |
||
| 537 | 537 | * @return boolean whether another request should be performed |
| 538 | 538 | * @throws HTTP_Request2_Exception in case of unsupported challenge parameters |
| 539 | 539 | */ |
| 540 | - protected function shouldUseProxyDigestAuth(HTTP_Request2_Response $response) |
|
| 541 | - { |
|
| 542 | - if (407 != $response->getStatus() || !$this->request->getConfig('proxy_user')) { |
|
| 543 | - return false; |
|
| 544 | - } |
|
| 545 | - if (!($challenge = $this->parseDigestChallenge($response->getHeader('proxy-authenticate')))) { |
|
| 546 | - return false; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - $key = 'proxy://' . $this->request->getConfig('proxy_host') . |
|
| 550 | - ':' . $this->request->getConfig('proxy_port'); |
|
| 551 | - |
|
| 552 | - if (!empty(self::$challenges[$key]) && |
|
| 553 | - (empty($challenge['stale']) || strcasecmp('true', $challenge['stale'])) |
|
| 554 | - ) { |
|
| 555 | - $ret = false; |
|
| 556 | - } else { |
|
| 557 | - $ret = true; |
|
| 558 | - } |
|
| 559 | - self::$challenges[$key] = $challenge; |
|
| 560 | - return $ret; |
|
| 561 | - } |
|
| 540 | + protected function shouldUseProxyDigestAuth(HTTP_Request2_Response $response) |
|
| 541 | + { |
|
| 542 | + if (407 != $response->getStatus() || !$this->request->getConfig('proxy_user')) { |
|
| 543 | + return false; |
|
| 544 | + } |
|
| 545 | + if (!($challenge = $this->parseDigestChallenge($response->getHeader('proxy-authenticate')))) { |
|
| 546 | + return false; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + $key = 'proxy://' . $this->request->getConfig('proxy_host') . |
|
| 550 | + ':' . $this->request->getConfig('proxy_port'); |
|
| 551 | + |
|
| 552 | + if (!empty(self::$challenges[$key]) && |
|
| 553 | + (empty($challenge['stale']) || strcasecmp('true', $challenge['stale'])) |
|
| 554 | + ) { |
|
| 555 | + $ret = false; |
|
| 556 | + } else { |
|
| 557 | + $ret = true; |
|
| 558 | + } |
|
| 559 | + self::$challenges[$key] = $challenge; |
|
| 560 | + return $ret; |
|
| 561 | + } |
|
| 562 | 562 | |
| 563 | 563 | /** |
| 564 | 564 | * Extracts digest method challenge from (WWW|Proxy)-Authenticate header value |
@@ -588,48 +588,48 @@ discard block |
||
| 588 | 588 | * no challenge is present in header value |
| 589 | 589 | * @throws HTTP_Request2_Exception in case of unsupported challenge parameters |
| 590 | 590 | */ |
| 591 | - protected function parseDigestChallenge($headerValue) |
|
| 592 | - { |
|
| 593 | - $authParam = '(' . self::REGEXP_TOKEN . ')\\s*=\\s*(' . |
|
| 594 | - self::REGEXP_TOKEN . '|' . self::REGEXP_QUOTED_STRING . ')'; |
|
| 595 | - $challenge = "!(?<=^|\\s|,)Digest ({$authParam}\\s*(,\\s*|$))+!"; |
|
| 596 | - if (!preg_match($challenge, $headerValue, $matches)) { |
|
| 597 | - return false; |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - preg_match_all('!' . $authParam . '!', $matches[0], $params); |
|
| 601 | - $paramsAry = array(); |
|
| 602 | - $knownParams = array('realm', 'domain', 'nonce', 'opaque', 'stale', |
|
| 603 | - 'algorithm', 'qop'); |
|
| 604 | - for ($i = 0; $i < count($params[0]); $i++) { |
|
| 605 | - // section 3.2.1: Any unrecognized directive MUST be ignored. |
|
| 606 | - if (in_array($params[1][$i], $knownParams)) { |
|
| 607 | - if ('"' == substr($params[2][$i], 0, 1)) { |
|
| 608 | - $paramsAry[$params[1][$i]] = substr($params[2][$i], 1, -1); |
|
| 609 | - } else { |
|
| 610 | - $paramsAry[$params[1][$i]] = $params[2][$i]; |
|
| 611 | - } |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - // we only support qop=auth |
|
| 615 | - if (!empty($paramsAry['qop']) && |
|
| 616 | - !in_array('auth', array_map('trim', explode(',', $paramsAry['qop']))) |
|
| 617 | - ) { |
|
| 618 | - throw new HTTP_Request2_Exception( |
|
| 619 | - "Only 'auth' qop is currently supported in digest authentication, " . |
|
| 620 | - "server requested '{$paramsAry['qop']}'" |
|
| 621 | - ); |
|
| 622 | - } |
|
| 623 | - // we only support algorithm=MD5 |
|
| 624 | - if (!empty($paramsAry['algorithm']) && 'MD5' != $paramsAry['algorithm']) { |
|
| 625 | - throw new HTTP_Request2_Exception( |
|
| 626 | - "Only 'MD5' algorithm is currently supported in digest authentication, " . |
|
| 627 | - "server requested '{$paramsAry['algorithm']}'" |
|
| 628 | - ); |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - return $paramsAry; |
|
| 632 | - } |
|
| 591 | + protected function parseDigestChallenge($headerValue) |
|
| 592 | + { |
|
| 593 | + $authParam = '(' . self::REGEXP_TOKEN . ')\\s*=\\s*(' . |
|
| 594 | + self::REGEXP_TOKEN . '|' . self::REGEXP_QUOTED_STRING . ')'; |
|
| 595 | + $challenge = "!(?<=^|\\s|,)Digest ({$authParam}\\s*(,\\s*|$))+!"; |
|
| 596 | + if (!preg_match($challenge, $headerValue, $matches)) { |
|
| 597 | + return false; |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + preg_match_all('!' . $authParam . '!', $matches[0], $params); |
|
| 601 | + $paramsAry = array(); |
|
| 602 | + $knownParams = array('realm', 'domain', 'nonce', 'opaque', 'stale', |
|
| 603 | + 'algorithm', 'qop'); |
|
| 604 | + for ($i = 0; $i < count($params[0]); $i++) { |
|
| 605 | + // section 3.2.1: Any unrecognized directive MUST be ignored. |
|
| 606 | + if (in_array($params[1][$i], $knownParams)) { |
|
| 607 | + if ('"' == substr($params[2][$i], 0, 1)) { |
|
| 608 | + $paramsAry[$params[1][$i]] = substr($params[2][$i], 1, -1); |
|
| 609 | + } else { |
|
| 610 | + $paramsAry[$params[1][$i]] = $params[2][$i]; |
|
| 611 | + } |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + // we only support qop=auth |
|
| 615 | + if (!empty($paramsAry['qop']) && |
|
| 616 | + !in_array('auth', array_map('trim', explode(',', $paramsAry['qop']))) |
|
| 617 | + ) { |
|
| 618 | + throw new HTTP_Request2_Exception( |
|
| 619 | + "Only 'auth' qop is currently supported in digest authentication, " . |
|
| 620 | + "server requested '{$paramsAry['qop']}'" |
|
| 621 | + ); |
|
| 622 | + } |
|
| 623 | + // we only support algorithm=MD5 |
|
| 624 | + if (!empty($paramsAry['algorithm']) && 'MD5' != $paramsAry['algorithm']) { |
|
| 625 | + throw new HTTP_Request2_Exception( |
|
| 626 | + "Only 'MD5' algorithm is currently supported in digest authentication, " . |
|
| 627 | + "server requested '{$paramsAry['algorithm']}'" |
|
| 628 | + ); |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + return $paramsAry; |
|
| 632 | + } |
|
| 633 | 633 | |
| 634 | 634 | /** |
| 635 | 635 | * Parses [Proxy-]Authentication-Info header value and updates challenge |
@@ -638,26 +638,26 @@ discard block |
||
| 638 | 638 | * @param string value of [Proxy-]Authentication-Info header |
| 639 | 639 | * @todo validate server rspauth response |
| 640 | 640 | */ |
| 641 | - protected function updateChallenge(&$challenge, $headerValue) |
|
| 642 | - { |
|
| 643 | - $authParam = '!(' . self::REGEXP_TOKEN . ')\\s*=\\s*(' . |
|
| 644 | - self::REGEXP_TOKEN . '|' . self::REGEXP_QUOTED_STRING . ')!'; |
|
| 645 | - $paramsAry = array(); |
|
| 646 | - |
|
| 647 | - preg_match_all($authParam, $headerValue, $params); |
|
| 648 | - for ($i = 0; $i < count($params[0]); $i++) { |
|
| 649 | - if ('"' == substr($params[2][$i], 0, 1)) { |
|
| 650 | - $paramsAry[$params[1][$i]] = substr($params[2][$i], 1, -1); |
|
| 651 | - } else { |
|
| 652 | - $paramsAry[$params[1][$i]] = $params[2][$i]; |
|
| 653 | - } |
|
| 654 | - } |
|
| 655 | - // for now, just update the nonce value |
|
| 656 | - if (!empty($paramsAry['nextnonce'])) { |
|
| 657 | - $challenge['nonce'] = $paramsAry['nextnonce']; |
|
| 658 | - $challenge['nc'] = 1; |
|
| 659 | - } |
|
| 660 | - } |
|
| 641 | + protected function updateChallenge(&$challenge, $headerValue) |
|
| 642 | + { |
|
| 643 | + $authParam = '!(' . self::REGEXP_TOKEN . ')\\s*=\\s*(' . |
|
| 644 | + self::REGEXP_TOKEN . '|' . self::REGEXP_QUOTED_STRING . ')!'; |
|
| 645 | + $paramsAry = array(); |
|
| 646 | + |
|
| 647 | + preg_match_all($authParam, $headerValue, $params); |
|
| 648 | + for ($i = 0; $i < count($params[0]); $i++) { |
|
| 649 | + if ('"' == substr($params[2][$i], 0, 1)) { |
|
| 650 | + $paramsAry[$params[1][$i]] = substr($params[2][$i], 1, -1); |
|
| 651 | + } else { |
|
| 652 | + $paramsAry[$params[1][$i]] = $params[2][$i]; |
|
| 653 | + } |
|
| 654 | + } |
|
| 655 | + // for now, just update the nonce value |
|
| 656 | + if (!empty($paramsAry['nextnonce'])) { |
|
| 657 | + $challenge['nonce'] = $paramsAry['nextnonce']; |
|
| 658 | + $challenge['nc'] = 1; |
|
| 659 | + } |
|
| 660 | + } |
|
| 661 | 661 | |
| 662 | 662 | /** |
| 663 | 663 | * Creates a value for [Proxy-]Authorization header when using digest authentication |
@@ -669,40 +669,40 @@ discard block |
||
| 669 | 669 | * @return string value of [Proxy-]Authorization request header |
| 670 | 670 | * @link http://tools.ietf.org/html/rfc2617#section-3.2.2 |
| 671 | 671 | */ |
| 672 | - protected function createDigestResponse($user, $password, $url, &$challenge) |
|
| 673 | - { |
|
| 674 | - if (false !== ($q = strpos($url, '?')) && |
|
| 675 | - $this->request->getConfig('digest_compat_ie') |
|
| 676 | - ) { |
|
| 677 | - $url = substr($url, 0, $q); |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - $a1 = md5($user . ':' . $challenge['realm'] . ':' . $password); |
|
| 681 | - $a2 = md5($this->request->getMethod() . ':' . $url); |
|
| 682 | - |
|
| 683 | - if (empty($challenge['qop'])) { |
|
| 684 | - $digest = md5($a1 . ':' . $challenge['nonce'] . ':' . $a2); |
|
| 685 | - } else { |
|
| 686 | - $challenge['cnonce'] = 'Req2.' . rand(); |
|
| 687 | - if (empty($challenge['nc'])) { |
|
| 688 | - $challenge['nc'] = 1; |
|
| 689 | - } |
|
| 690 | - $nc = sprintf('%08x', $challenge['nc']++); |
|
| 691 | - $digest = md5($a1 . ':' . $challenge['nonce'] . ':' . $nc . ':' . |
|
| 692 | - $challenge['cnonce'] . ':auth:' . $a2); |
|
| 693 | - } |
|
| 694 | - return 'Digest username="' . str_replace(array('\\', '"'), array('\\\\', '\\"'), $user) . '", ' . |
|
| 695 | - 'realm="' . $challenge['realm'] . '", ' . |
|
| 696 | - 'nonce="' . $challenge['nonce'] . '", ' . |
|
| 697 | - 'uri="' . $url . '", ' . |
|
| 698 | - 'response="' . $digest . '"' . |
|
| 699 | - (!empty($challenge['opaque'])? |
|
| 700 | - ', opaque="' . $challenge['opaque'] . '"': |
|
| 701 | - '') . |
|
| 702 | - (!empty($challenge['qop'])? |
|
| 703 | - ', qop="auth", nc=' . $nc . ', cnonce="' . $challenge['cnonce'] . '"': |
|
| 704 | - ''); |
|
| 705 | - } |
|
| 672 | + protected function createDigestResponse($user, $password, $url, &$challenge) |
|
| 673 | + { |
|
| 674 | + if (false !== ($q = strpos($url, '?')) && |
|
| 675 | + $this->request->getConfig('digest_compat_ie') |
|
| 676 | + ) { |
|
| 677 | + $url = substr($url, 0, $q); |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + $a1 = md5($user . ':' . $challenge['realm'] . ':' . $password); |
|
| 681 | + $a2 = md5($this->request->getMethod() . ':' . $url); |
|
| 682 | + |
|
| 683 | + if (empty($challenge['qop'])) { |
|
| 684 | + $digest = md5($a1 . ':' . $challenge['nonce'] . ':' . $a2); |
|
| 685 | + } else { |
|
| 686 | + $challenge['cnonce'] = 'Req2.' . rand(); |
|
| 687 | + if (empty($challenge['nc'])) { |
|
| 688 | + $challenge['nc'] = 1; |
|
| 689 | + } |
|
| 690 | + $nc = sprintf('%08x', $challenge['nc']++); |
|
| 691 | + $digest = md5($a1 . ':' . $challenge['nonce'] . ':' . $nc . ':' . |
|
| 692 | + $challenge['cnonce'] . ':auth:' . $a2); |
|
| 693 | + } |
|
| 694 | + return 'Digest username="' . str_replace(array('\\', '"'), array('\\\\', '\\"'), $user) . '", ' . |
|
| 695 | + 'realm="' . $challenge['realm'] . '", ' . |
|
| 696 | + 'nonce="' . $challenge['nonce'] . '", ' . |
|
| 697 | + 'uri="' . $url . '", ' . |
|
| 698 | + 'response="' . $digest . '"' . |
|
| 699 | + (!empty($challenge['opaque'])? |
|
| 700 | + ', opaque="' . $challenge['opaque'] . '"': |
|
| 701 | + '') . |
|
| 702 | + (!empty($challenge['qop'])? |
|
| 703 | + ', qop="auth", nc=' . $nc . ', cnonce="' . $challenge['cnonce'] . '"': |
|
| 704 | + ''); |
|
| 705 | + } |
|
| 706 | 706 | |
| 707 | 707 | /** |
| 708 | 708 | * Adds 'Authorization' header (if needed) to request headers array |
@@ -712,41 +712,41 @@ discard block |
||
| 712 | 712 | * @param string request URL (needed for digest authentication) |
| 713 | 713 | * @throws HTTP_Request2_Exception |
| 714 | 714 | */ |
| 715 | - protected function addAuthorizationHeader(&$headers, $requestHost, $requestUrl) |
|
| 716 | - { |
|
| 717 | - if (!($auth = $this->request->getAuth())) { |
|
| 718 | - return; |
|
| 719 | - } |
|
| 720 | - switch ($auth['scheme']) { |
|
| 721 | - case HTTP_Request2::AUTH_BASIC: |
|
| 722 | - $headers['authorization'] = |
|
| 723 | - 'Basic ' . base64_encode($auth['user'] . ':' . $auth['password']); |
|
| 724 | - break; |
|
| 725 | - |
|
| 726 | - case HTTP_Request2::AUTH_DIGEST: |
|
| 727 | - unset($this->serverChallenge); |
|
| 728 | - $fullUrl = ('/' == $requestUrl[0])? |
|
| 729 | - $this->request->getUrl()->getScheme() . '://' . |
|
| 730 | - $requestHost . $requestUrl: |
|
| 731 | - $requestUrl; |
|
| 732 | - foreach (array_keys(self::$challenges) as $key) { |
|
| 733 | - if ($key == substr($fullUrl, 0, strlen($key))) { |
|
| 734 | - $headers['authorization'] = $this->createDigestResponse( |
|
| 735 | - $auth['user'], $auth['password'], |
|
| 736 | - $requestUrl, self::$challenges[$key] |
|
| 737 | - ); |
|
| 738 | - $this->serverChallenge =& self::$challenges[$key]; |
|
| 739 | - break; |
|
| 740 | - } |
|
| 741 | - } |
|
| 742 | - break; |
|
| 743 | - |
|
| 744 | - default: |
|
| 745 | - throw new HTTP_Request2_Exception( |
|
| 746 | - "Unknown HTTP authentication scheme '{$auth['scheme']}'" |
|
| 747 | - ); |
|
| 748 | - } |
|
| 749 | - } |
|
| 715 | + protected function addAuthorizationHeader(&$headers, $requestHost, $requestUrl) |
|
| 716 | + { |
|
| 717 | + if (!($auth = $this->request->getAuth())) { |
|
| 718 | + return; |
|
| 719 | + } |
|
| 720 | + switch ($auth['scheme']) { |
|
| 721 | + case HTTP_Request2::AUTH_BASIC: |
|
| 722 | + $headers['authorization'] = |
|
| 723 | + 'Basic ' . base64_encode($auth['user'] . ':' . $auth['password']); |
|
| 724 | + break; |
|
| 725 | + |
|
| 726 | + case HTTP_Request2::AUTH_DIGEST: |
|
| 727 | + unset($this->serverChallenge); |
|
| 728 | + $fullUrl = ('/' == $requestUrl[0])? |
|
| 729 | + $this->request->getUrl()->getScheme() . '://' . |
|
| 730 | + $requestHost . $requestUrl: |
|
| 731 | + $requestUrl; |
|
| 732 | + foreach (array_keys(self::$challenges) as $key) { |
|
| 733 | + if ($key == substr($fullUrl, 0, strlen($key))) { |
|
| 734 | + $headers['authorization'] = $this->createDigestResponse( |
|
| 735 | + $auth['user'], $auth['password'], |
|
| 736 | + $requestUrl, self::$challenges[$key] |
|
| 737 | + ); |
|
| 738 | + $this->serverChallenge =& self::$challenges[$key]; |
|
| 739 | + break; |
|
| 740 | + } |
|
| 741 | + } |
|
| 742 | + break; |
|
| 743 | + |
|
| 744 | + default: |
|
| 745 | + throw new HTTP_Request2_Exception( |
|
| 746 | + "Unknown HTTP authentication scheme '{$auth['scheme']}'" |
|
| 747 | + ); |
|
| 748 | + } |
|
| 749 | + } |
|
| 750 | 750 | |
| 751 | 751 | /** |
| 752 | 752 | * Adds 'Proxy-Authorization' header (if needed) to request headers array |
@@ -755,43 +755,43 @@ discard block |
||
| 755 | 755 | * @param string request URL (needed for digest authentication) |
| 756 | 756 | * @throws HTTP_Request2_Exception |
| 757 | 757 | */ |
| 758 | - protected function addProxyAuthorizationHeader(&$headers, $requestUrl) |
|
| 759 | - { |
|
| 760 | - if (!$this->request->getConfig('proxy_host') || |
|
| 761 | - !($user = $this->request->getConfig('proxy_user')) || |
|
| 762 | - (0 == strcasecmp('https', $this->request->getUrl()->getScheme()) && |
|
| 763 | - HTTP_Request2::METHOD_CONNECT != $this->request->getMethod()) |
|
| 764 | - ) { |
|
| 765 | - return; |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - $password = $this->request->getConfig('proxy_password'); |
|
| 769 | - switch ($this->request->getConfig('proxy_auth_scheme')) { |
|
| 770 | - case HTTP_Request2::AUTH_BASIC: |
|
| 771 | - $headers['proxy-authorization'] = |
|
| 772 | - 'Basic ' . base64_encode($user . ':' . $password); |
|
| 773 | - break; |
|
| 774 | - |
|
| 775 | - case HTTP_Request2::AUTH_DIGEST: |
|
| 776 | - unset($this->proxyChallenge); |
|
| 777 | - $proxyUrl = 'proxy://' . $this->request->getConfig('proxy_host') . |
|
| 778 | - ':' . $this->request->getConfig('proxy_port'); |
|
| 779 | - if (!empty(self::$challenges[$proxyUrl])) { |
|
| 780 | - $headers['proxy-authorization'] = $this->createDigestResponse( |
|
| 781 | - $user, $password, |
|
| 782 | - $requestUrl, self::$challenges[$proxyUrl] |
|
| 783 | - ); |
|
| 784 | - $this->proxyChallenge =& self::$challenges[$proxyUrl]; |
|
| 785 | - } |
|
| 786 | - break; |
|
| 787 | - |
|
| 788 | - default: |
|
| 789 | - throw new HTTP_Request2_Exception( |
|
| 790 | - "Unknown HTTP authentication scheme '" . |
|
| 791 | - $this->request->getConfig('proxy_auth_scheme') . "'" |
|
| 792 | - ); |
|
| 793 | - } |
|
| 794 | - } |
|
| 758 | + protected function addProxyAuthorizationHeader(&$headers, $requestUrl) |
|
| 759 | + { |
|
| 760 | + if (!$this->request->getConfig('proxy_host') || |
|
| 761 | + !($user = $this->request->getConfig('proxy_user')) || |
|
| 762 | + (0 == strcasecmp('https', $this->request->getUrl()->getScheme()) && |
|
| 763 | + HTTP_Request2::METHOD_CONNECT != $this->request->getMethod()) |
|
| 764 | + ) { |
|
| 765 | + return; |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + $password = $this->request->getConfig('proxy_password'); |
|
| 769 | + switch ($this->request->getConfig('proxy_auth_scheme')) { |
|
| 770 | + case HTTP_Request2::AUTH_BASIC: |
|
| 771 | + $headers['proxy-authorization'] = |
|
| 772 | + 'Basic ' . base64_encode($user . ':' . $password); |
|
| 773 | + break; |
|
| 774 | + |
|
| 775 | + case HTTP_Request2::AUTH_DIGEST: |
|
| 776 | + unset($this->proxyChallenge); |
|
| 777 | + $proxyUrl = 'proxy://' . $this->request->getConfig('proxy_host') . |
|
| 778 | + ':' . $this->request->getConfig('proxy_port'); |
|
| 779 | + if (!empty(self::$challenges[$proxyUrl])) { |
|
| 780 | + $headers['proxy-authorization'] = $this->createDigestResponse( |
|
| 781 | + $user, $password, |
|
| 782 | + $requestUrl, self::$challenges[$proxyUrl] |
|
| 783 | + ); |
|
| 784 | + $this->proxyChallenge =& self::$challenges[$proxyUrl]; |
|
| 785 | + } |
|
| 786 | + break; |
|
| 787 | + |
|
| 788 | + default: |
|
| 789 | + throw new HTTP_Request2_Exception( |
|
| 790 | + "Unknown HTTP authentication scheme '" . |
|
| 791 | + $this->request->getConfig('proxy_auth_scheme') . "'" |
|
| 792 | + ); |
|
| 793 | + } |
|
| 794 | + } |
|
| 795 | 795 | |
| 796 | 796 | |
| 797 | 797 | /** |
@@ -800,88 +800,88 @@ discard block |
||
| 800 | 800 | * @return string |
| 801 | 801 | * @throws HTTP_Request2_Exception |
| 802 | 802 | */ |
| 803 | - protected function prepareHeaders() |
|
| 804 | - { |
|
| 805 | - $headers = $this->request->getHeaders(); |
|
| 806 | - $url = $this->request->getUrl(); |
|
| 807 | - $connect = HTTP_Request2::METHOD_CONNECT == $this->request->getMethod(); |
|
| 808 | - $host = $url->getHost(); |
|
| 809 | - |
|
| 810 | - $defaultPort = 0 == strcasecmp($url->getScheme(), 'https')? 443: 80; |
|
| 811 | - if (($port = $url->getPort()) && $port != $defaultPort || $connect) { |
|
| 812 | - $host .= ':' . (empty($port)? $defaultPort: $port); |
|
| 813 | - } |
|
| 814 | - // Do not overwrite explicitly set 'Host' header, see bug #16146 |
|
| 815 | - if (!isset($headers['host'])) { |
|
| 816 | - $headers['host'] = $host; |
|
| 817 | - } |
|
| 818 | - |
|
| 819 | - if ($connect) { |
|
| 820 | - $requestUrl = $host; |
|
| 821 | - |
|
| 822 | - } else { |
|
| 823 | - if (!$this->request->getConfig('proxy_host') || |
|
| 824 | - 0 == strcasecmp($url->getScheme(), 'https') |
|
| 825 | - ) { |
|
| 826 | - $requestUrl = ''; |
|
| 827 | - } else { |
|
| 828 | - $requestUrl = $url->getScheme() . '://' . $host; |
|
| 829 | - } |
|
| 830 | - $path = $url->getPath(); |
|
| 831 | - $query = $url->getQuery(); |
|
| 832 | - $requestUrl .= (empty($path)? '/': $path) . (empty($query)? '': '?' . $query); |
|
| 833 | - } |
|
| 834 | - |
|
| 835 | - if ('1.1' == $this->request->getConfig('protocol_version') && |
|
| 836 | - extension_loaded('zlib') && !isset($headers['accept-encoding']) |
|
| 837 | - ) { |
|
| 838 | - $headers['accept-encoding'] = 'gzip, deflate'; |
|
| 839 | - } |
|
| 840 | - |
|
| 841 | - $this->addAuthorizationHeader($headers, $host, $requestUrl); |
|
| 842 | - $this->addProxyAuthorizationHeader($headers, $requestUrl); |
|
| 843 | - $this->calculateRequestLength($headers); |
|
| 844 | - |
|
| 845 | - $headersStr = $this->request->getMethod() . ' ' . $requestUrl . ' HTTP/' . |
|
| 846 | - $this->request->getConfig('protocol_version') . "\r\n"; |
|
| 847 | - foreach ($headers as $name => $value) { |
|
| 848 | - $canonicalName = implode('-', array_map('ucfirst', explode('-', $name))); |
|
| 849 | - $headersStr .= $canonicalName . ': ' . $value . "\r\n"; |
|
| 850 | - } |
|
| 851 | - return $headersStr . "\r\n"; |
|
| 852 | - } |
|
| 803 | + protected function prepareHeaders() |
|
| 804 | + { |
|
| 805 | + $headers = $this->request->getHeaders(); |
|
| 806 | + $url = $this->request->getUrl(); |
|
| 807 | + $connect = HTTP_Request2::METHOD_CONNECT == $this->request->getMethod(); |
|
| 808 | + $host = $url->getHost(); |
|
| 809 | + |
|
| 810 | + $defaultPort = 0 == strcasecmp($url->getScheme(), 'https')? 443: 80; |
|
| 811 | + if (($port = $url->getPort()) && $port != $defaultPort || $connect) { |
|
| 812 | + $host .= ':' . (empty($port)? $defaultPort: $port); |
|
| 813 | + } |
|
| 814 | + // Do not overwrite explicitly set 'Host' header, see bug #16146 |
|
| 815 | + if (!isset($headers['host'])) { |
|
| 816 | + $headers['host'] = $host; |
|
| 817 | + } |
|
| 818 | + |
|
| 819 | + if ($connect) { |
|
| 820 | + $requestUrl = $host; |
|
| 821 | + |
|
| 822 | + } else { |
|
| 823 | + if (!$this->request->getConfig('proxy_host') || |
|
| 824 | + 0 == strcasecmp($url->getScheme(), 'https') |
|
| 825 | + ) { |
|
| 826 | + $requestUrl = ''; |
|
| 827 | + } else { |
|
| 828 | + $requestUrl = $url->getScheme() . '://' . $host; |
|
| 829 | + } |
|
| 830 | + $path = $url->getPath(); |
|
| 831 | + $query = $url->getQuery(); |
|
| 832 | + $requestUrl .= (empty($path)? '/': $path) . (empty($query)? '': '?' . $query); |
|
| 833 | + } |
|
| 834 | + |
|
| 835 | + if ('1.1' == $this->request->getConfig('protocol_version') && |
|
| 836 | + extension_loaded('zlib') && !isset($headers['accept-encoding']) |
|
| 837 | + ) { |
|
| 838 | + $headers['accept-encoding'] = 'gzip, deflate'; |
|
| 839 | + } |
|
| 840 | + |
|
| 841 | + $this->addAuthorizationHeader($headers, $host, $requestUrl); |
|
| 842 | + $this->addProxyAuthorizationHeader($headers, $requestUrl); |
|
| 843 | + $this->calculateRequestLength($headers); |
|
| 844 | + |
|
| 845 | + $headersStr = $this->request->getMethod() . ' ' . $requestUrl . ' HTTP/' . |
|
| 846 | + $this->request->getConfig('protocol_version') . "\r\n"; |
|
| 847 | + foreach ($headers as $name => $value) { |
|
| 848 | + $canonicalName = implode('-', array_map('ucfirst', explode('-', $name))); |
|
| 849 | + $headersStr .= $canonicalName . ': ' . $value . "\r\n"; |
|
| 850 | + } |
|
| 851 | + return $headersStr . "\r\n"; |
|
| 852 | + } |
|
| 853 | 853 | |
| 854 | 854 | /** |
| 855 | 855 | * Sends the request body |
| 856 | 856 | * |
| 857 | 857 | * @throws HTTP_Request2_Exception |
| 858 | 858 | */ |
| 859 | - protected function writeBody() |
|
| 860 | - { |
|
| 861 | - if (in_array($this->request->getMethod(), self::$bodyDisallowed) || |
|
| 862 | - 0 == $this->contentLength |
|
| 863 | - ) { |
|
| 864 | - return; |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - $position = 0; |
|
| 868 | - $bufferSize = $this->request->getConfig('buffer_size'); |
|
| 869 | - while ($position < $this->contentLength) { |
|
| 870 | - if (is_string($this->requestBody)) { |
|
| 871 | - $str = substr($this->requestBody, $position, $bufferSize); |
|
| 872 | - } elseif (is_resource($this->requestBody)) { |
|
| 873 | - $str = fread($this->requestBody, $bufferSize); |
|
| 874 | - } else { |
|
| 875 | - $str = $this->requestBody->read($bufferSize); |
|
| 876 | - } |
|
| 877 | - if (false === @fwrite($this->socket, $str, strlen($str))) { |
|
| 878 | - throw new HTTP_Request2_Exception('Error writing request'); |
|
| 879 | - } |
|
| 880 | - // Provide the length of written string to the observer, request #7630 |
|
| 881 | - $this->request->setLastEvent('sentBodyPart', strlen($str)); |
|
| 882 | - $position += strlen($str); |
|
| 883 | - } |
|
| 884 | - } |
|
| 859 | + protected function writeBody() |
|
| 860 | + { |
|
| 861 | + if (in_array($this->request->getMethod(), self::$bodyDisallowed) || |
|
| 862 | + 0 == $this->contentLength |
|
| 863 | + ) { |
|
| 864 | + return; |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + $position = 0; |
|
| 868 | + $bufferSize = $this->request->getConfig('buffer_size'); |
|
| 869 | + while ($position < $this->contentLength) { |
|
| 870 | + if (is_string($this->requestBody)) { |
|
| 871 | + $str = substr($this->requestBody, $position, $bufferSize); |
|
| 872 | + } elseif (is_resource($this->requestBody)) { |
|
| 873 | + $str = fread($this->requestBody, $bufferSize); |
|
| 874 | + } else { |
|
| 875 | + $str = $this->requestBody->read($bufferSize); |
|
| 876 | + } |
|
| 877 | + if (false === @fwrite($this->socket, $str, strlen($str))) { |
|
| 878 | + throw new HTTP_Request2_Exception('Error writing request'); |
|
| 879 | + } |
|
| 880 | + // Provide the length of written string to the observer, request #7630 |
|
| 881 | + $this->request->setLastEvent('sentBodyPart', strlen($str)); |
|
| 882 | + $position += strlen($str); |
|
| 883 | + } |
|
| 884 | + } |
|
| 885 | 885 | |
| 886 | 886 | /** |
| 887 | 887 | * Reads the remote server's response |
@@ -889,70 +889,70 @@ discard block |
||
| 889 | 889 | * @return HTTP_Request2_Response |
| 890 | 890 | * @throws HTTP_Request2_Exception |
| 891 | 891 | */ |
| 892 | - protected function readResponse() |
|
| 893 | - { |
|
| 894 | - $bufferSize = $this->request->getConfig('buffer_size'); |
|
| 895 | - |
|
| 896 | - do { |
|
| 897 | - $response = new HTTP_Request2_Response($this->readLine($bufferSize), true); |
|
| 898 | - do { |
|
| 899 | - $headerLine = $this->readLine($bufferSize); |
|
| 900 | - $response->parseHeaderLine($headerLine); |
|
| 901 | - } while ('' != $headerLine); |
|
| 902 | - } while (in_array($response->getStatus(), array(100, 101))); |
|
| 903 | - |
|
| 904 | - $this->request->setLastEvent('receivedHeaders', $response); |
|
| 905 | - |
|
| 906 | - // No body possible in such responses |
|
| 907 | - if (HTTP_Request2::METHOD_HEAD == $this->request->getMethod() || |
|
| 908 | - (HTTP_Request2::METHOD_CONNECT == $this->request->getMethod() && |
|
| 909 | - 200 <= $response->getStatus() && 300 > $response->getStatus()) || |
|
| 910 | - in_array($response->getStatus(), array(204, 304)) |
|
| 911 | - ) { |
|
| 912 | - return $response; |
|
| 913 | - } |
|
| 914 | - |
|
| 915 | - $chunked = 'chunked' == $response->getHeader('transfer-encoding'); |
|
| 916 | - $length = $response->getHeader('content-length'); |
|
| 917 | - $hasBody = false; |
|
| 918 | - if ($chunked || null === $length || 0 < intval($length)) { |
|
| 919 | - // RFC 2616, section 4.4: |
|
| 920 | - // 3. ... If a message is received with both a |
|
| 921 | - // Transfer-Encoding header field and a Content-Length header field, |
|
| 922 | - // the latter MUST be ignored. |
|
| 923 | - $toRead = ($chunked || null === $length)? null: $length; |
|
| 924 | - $this->chunkLength = 0; |
|
| 925 | - |
|
| 926 | - while (!feof($this->socket) && (is_null($toRead) || 0 < $toRead)) { |
|
| 927 | - if ($chunked) { |
|
| 928 | - $data = $this->readChunked($bufferSize); |
|
| 929 | - } elseif (is_null($toRead)) { |
|
| 930 | - $data = $this->fread($bufferSize); |
|
| 931 | - } else { |
|
| 932 | - $data = $this->fread(min($toRead, $bufferSize)); |
|
| 933 | - $toRead -= strlen($data); |
|
| 934 | - } |
|
| 935 | - if ('' == $data && (!$this->chunkLength || feof($this->socket))) { |
|
| 936 | - break; |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - $hasBody = true; |
|
| 940 | - if ($this->request->getConfig('store_body')) { |
|
| 941 | - $response->appendBody($data); |
|
| 942 | - } |
|
| 943 | - if (!in_array($response->getHeader('content-encoding'), array('identity', null))) { |
|
| 944 | - $this->request->setLastEvent('receivedEncodedBodyPart', $data); |
|
| 945 | - } else { |
|
| 946 | - $this->request->setLastEvent('receivedBodyPart', $data); |
|
| 947 | - } |
|
| 948 | - } |
|
| 949 | - } |
|
| 950 | - |
|
| 951 | - if ($hasBody) { |
|
| 952 | - $this->request->setLastEvent('receivedBody', $response); |
|
| 953 | - } |
|
| 954 | - return $response; |
|
| 955 | - } |
|
| 892 | + protected function readResponse() |
|
| 893 | + { |
|
| 894 | + $bufferSize = $this->request->getConfig('buffer_size'); |
|
| 895 | + |
|
| 896 | + do { |
|
| 897 | + $response = new HTTP_Request2_Response($this->readLine($bufferSize), true); |
|
| 898 | + do { |
|
| 899 | + $headerLine = $this->readLine($bufferSize); |
|
| 900 | + $response->parseHeaderLine($headerLine); |
|
| 901 | + } while ('' != $headerLine); |
|
| 902 | + } while (in_array($response->getStatus(), array(100, 101))); |
|
| 903 | + |
|
| 904 | + $this->request->setLastEvent('receivedHeaders', $response); |
|
| 905 | + |
|
| 906 | + // No body possible in such responses |
|
| 907 | + if (HTTP_Request2::METHOD_HEAD == $this->request->getMethod() || |
|
| 908 | + (HTTP_Request2::METHOD_CONNECT == $this->request->getMethod() && |
|
| 909 | + 200 <= $response->getStatus() && 300 > $response->getStatus()) || |
|
| 910 | + in_array($response->getStatus(), array(204, 304)) |
|
| 911 | + ) { |
|
| 912 | + return $response; |
|
| 913 | + } |
|
| 914 | + |
|
| 915 | + $chunked = 'chunked' == $response->getHeader('transfer-encoding'); |
|
| 916 | + $length = $response->getHeader('content-length'); |
|
| 917 | + $hasBody = false; |
|
| 918 | + if ($chunked || null === $length || 0 < intval($length)) { |
|
| 919 | + // RFC 2616, section 4.4: |
|
| 920 | + // 3. ... If a message is received with both a |
|
| 921 | + // Transfer-Encoding header field and a Content-Length header field, |
|
| 922 | + // the latter MUST be ignored. |
|
| 923 | + $toRead = ($chunked || null === $length)? null: $length; |
|
| 924 | + $this->chunkLength = 0; |
|
| 925 | + |
|
| 926 | + while (!feof($this->socket) && (is_null($toRead) || 0 < $toRead)) { |
|
| 927 | + if ($chunked) { |
|
| 928 | + $data = $this->readChunked($bufferSize); |
|
| 929 | + } elseif (is_null($toRead)) { |
|
| 930 | + $data = $this->fread($bufferSize); |
|
| 931 | + } else { |
|
| 932 | + $data = $this->fread(min($toRead, $bufferSize)); |
|
| 933 | + $toRead -= strlen($data); |
|
| 934 | + } |
|
| 935 | + if ('' == $data && (!$this->chunkLength || feof($this->socket))) { |
|
| 936 | + break; |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + $hasBody = true; |
|
| 940 | + if ($this->request->getConfig('store_body')) { |
|
| 941 | + $response->appendBody($data); |
|
| 942 | + } |
|
| 943 | + if (!in_array($response->getHeader('content-encoding'), array('identity', null))) { |
|
| 944 | + $this->request->setLastEvent('receivedEncodedBodyPart', $data); |
|
| 945 | + } else { |
|
| 946 | + $this->request->setLastEvent('receivedBodyPart', $data); |
|
| 947 | + } |
|
| 948 | + } |
|
| 949 | + } |
|
| 950 | + |
|
| 951 | + if ($hasBody) { |
|
| 952 | + $this->request->setLastEvent('receivedBody', $response); |
|
| 953 | + } |
|
| 954 | + return $response; |
|
| 955 | + } |
|
| 956 | 956 | |
| 957 | 957 | /** |
| 958 | 958 | * Reads until either the end of the socket or a newline, whichever comes first |
@@ -964,27 +964,27 @@ discard block |
||
| 964 | 964 | * @return Available data up to the newline (not including newline) |
| 965 | 965 | * @throws HTTP_Request2_Exception In case of timeout |
| 966 | 966 | */ |
| 967 | - protected function readLine($bufferSize) |
|
| 968 | - { |
|
| 969 | - $line = ''; |
|
| 970 | - while (!feof($this->socket)) { |
|
| 971 | - if ($this->deadline) { |
|
| 972 | - stream_set_timeout($this->socket, max($this->deadline - time(), 1)); |
|
| 973 | - } |
|
| 974 | - $line .= @fgets($this->socket, $bufferSize); |
|
| 975 | - $info = stream_get_meta_data($this->socket); |
|
| 976 | - if ($info['timed_out'] || $this->deadline && time() > $this->deadline) { |
|
| 977 | - $reason = $this->deadline |
|
| 978 | - ? 'after ' . $this->request->getConfig('timeout') . ' second(s)' |
|
| 979 | - : 'due to default_socket_timeout php.ini setting'; |
|
| 980 | - throw new HTTP_Request2_Exception("Request timed out {$reason}"); |
|
| 981 | - } |
|
| 982 | - if (substr($line, -1) == "\n") { |
|
| 983 | - return rtrim($line, "\r\n"); |
|
| 984 | - } |
|
| 985 | - } |
|
| 986 | - return $line; |
|
| 987 | - } |
|
| 967 | + protected function readLine($bufferSize) |
|
| 968 | + { |
|
| 969 | + $line = ''; |
|
| 970 | + while (!feof($this->socket)) { |
|
| 971 | + if ($this->deadline) { |
|
| 972 | + stream_set_timeout($this->socket, max($this->deadline - time(), 1)); |
|
| 973 | + } |
|
| 974 | + $line .= @fgets($this->socket, $bufferSize); |
|
| 975 | + $info = stream_get_meta_data($this->socket); |
|
| 976 | + if ($info['timed_out'] || $this->deadline && time() > $this->deadline) { |
|
| 977 | + $reason = $this->deadline |
|
| 978 | + ? 'after ' . $this->request->getConfig('timeout') . ' second(s)' |
|
| 979 | + : 'due to default_socket_timeout php.ini setting'; |
|
| 980 | + throw new HTTP_Request2_Exception("Request timed out {$reason}"); |
|
| 981 | + } |
|
| 982 | + if (substr($line, -1) == "\n") { |
|
| 983 | + return rtrim($line, "\r\n"); |
|
| 984 | + } |
|
| 985 | + } |
|
| 986 | + return $line; |
|
| 987 | + } |
|
| 988 | 988 | |
| 989 | 989 | /** |
| 990 | 990 | * Wrapper around fread(), handles global request timeout |
@@ -993,21 +993,21 @@ discard block |
||
| 993 | 993 | * @return Data read from socket |
| 994 | 994 | * @throws HTTP_Request2_Exception In case of timeout |
| 995 | 995 | */ |
| 996 | - protected function fread($length) |
|
| 997 | - { |
|
| 998 | - if ($this->deadline) { |
|
| 999 | - stream_set_timeout($this->socket, max($this->deadline - time(), 1)); |
|
| 1000 | - } |
|
| 1001 | - $data = fread($this->socket, $length); |
|
| 1002 | - $info = stream_get_meta_data($this->socket); |
|
| 1003 | - if ($info['timed_out'] || $this->deadline && time() > $this->deadline) { |
|
| 1004 | - $reason = $this->deadline |
|
| 1005 | - ? 'after ' . $this->request->getConfig('timeout') . ' second(s)' |
|
| 1006 | - : 'due to default_socket_timeout php.ini setting'; |
|
| 1007 | - throw new HTTP_Request2_Exception("Request timed out {$reason}"); |
|
| 1008 | - } |
|
| 1009 | - return $data; |
|
| 1010 | - } |
|
| 996 | + protected function fread($length) |
|
| 997 | + { |
|
| 998 | + if ($this->deadline) { |
|
| 999 | + stream_set_timeout($this->socket, max($this->deadline - time(), 1)); |
|
| 1000 | + } |
|
| 1001 | + $data = fread($this->socket, $length); |
|
| 1002 | + $info = stream_get_meta_data($this->socket); |
|
| 1003 | + if ($info['timed_out'] || $this->deadline && time() > $this->deadline) { |
|
| 1004 | + $reason = $this->deadline |
|
| 1005 | + ? 'after ' . $this->request->getConfig('timeout') . ' second(s)' |
|
| 1006 | + : 'due to default_socket_timeout php.ini setting'; |
|
| 1007 | + throw new HTTP_Request2_Exception("Request timed out {$reason}"); |
|
| 1008 | + } |
|
| 1009 | + return $data; |
|
| 1010 | + } |
|
| 1011 | 1011 | |
| 1012 | 1012 | /** |
| 1013 | 1013 | * Reads a part of response body encoded with chunked Transfer-Encoding |
@@ -1016,31 +1016,31 @@ discard block |
||
| 1016 | 1016 | * @return string |
| 1017 | 1017 | * @throws HTTP_Request2_Exception |
| 1018 | 1018 | */ |
| 1019 | - protected function readChunked($bufferSize) |
|
| 1020 | - { |
|
| 1021 | - // at start of the next chunk? |
|
| 1022 | - if (0 == $this->chunkLength) { |
|
| 1023 | - $line = $this->readLine($bufferSize); |
|
| 1024 | - if (!preg_match('/^([0-9a-f]+)/i', $line, $matches)) { |
|
| 1025 | - throw new HTTP_Request2_Exception( |
|
| 1026 | - "Cannot decode chunked response, invalid chunk length '{$line}'" |
|
| 1027 | - ); |
|
| 1028 | - } else { |
|
| 1029 | - $this->chunkLength = hexdec($matches[1]); |
|
| 1030 | - // Chunk with zero length indicates the end |
|
| 1031 | - if (0 == $this->chunkLength) { |
|
| 1032 | - $this->readLine($bufferSize); |
|
| 1033 | - return ''; |
|
| 1034 | - } |
|
| 1035 | - } |
|
| 1036 | - } |
|
| 1037 | - $data = $this->fread(min($this->chunkLength, $bufferSize)); |
|
| 1038 | - $this->chunkLength -= strlen($data); |
|
| 1039 | - if (0 == $this->chunkLength) { |
|
| 1040 | - $this->readLine($bufferSize); // Trailing CRLF |
|
| 1041 | - } |
|
| 1042 | - return $data; |
|
| 1043 | - } |
|
| 1019 | + protected function readChunked($bufferSize) |
|
| 1020 | + { |
|
| 1021 | + // at start of the next chunk? |
|
| 1022 | + if (0 == $this->chunkLength) { |
|
| 1023 | + $line = $this->readLine($bufferSize); |
|
| 1024 | + if (!preg_match('/^([0-9a-f]+)/i', $line, $matches)) { |
|
| 1025 | + throw new HTTP_Request2_Exception( |
|
| 1026 | + "Cannot decode chunked response, invalid chunk length '{$line}'" |
|
| 1027 | + ); |
|
| 1028 | + } else { |
|
| 1029 | + $this->chunkLength = hexdec($matches[1]); |
|
| 1030 | + // Chunk with zero length indicates the end |
|
| 1031 | + if (0 == $this->chunkLength) { |
|
| 1032 | + $this->readLine($bufferSize); |
|
| 1033 | + return ''; |
|
| 1034 | + } |
|
| 1035 | + } |
|
| 1036 | + } |
|
| 1037 | + $data = $this->fread(min($this->chunkLength, $bufferSize)); |
|
| 1038 | + $this->chunkLength -= strlen($data); |
|
| 1039 | + if (0 == $this->chunkLength) { |
|
| 1040 | + $this->readLine($bufferSize); // Trailing CRLF |
|
| 1041 | + } |
|
| 1042 | + return $data; |
|
| 1043 | + } |
|
| 1044 | 1044 | } |
| 1045 | 1045 | |
| 1046 | 1046 | ?> |
| 1047 | 1047 | \ No newline at end of file |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | } else { |
| 113 | 113 | for ($i = 0; $i < count($f['fp']); $i++) { |
| 114 | 114 | $upload = array( |
| 115 | - 'name' => ($useBrackets? $fieldName . '[' . $i . ']': $fieldName) |
|
| 115 | + 'name' => ($useBrackets ? $fieldName . '[' . $i . ']' : $fieldName) |
|
| 116 | 116 | ); |
| 117 | 117 | foreach (array('fp', 'filename', 'size', 'type') as $key) { |
| 118 | 118 | $upload[$key] = $f[$key][$i]; |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | $ret .= substr($header, $this->_pos[1], $length); |
| 189 | 189 | $length -= min(strlen($header) - $this->_pos[1], $length); |
| 190 | 190 | } |
| 191 | - $filePos = max(0, $this->_pos[1] - strlen($header)); |
|
| 191 | + $filePos = max(0, $this->_pos[1] - strlen($header)); |
|
| 192 | 192 | if ($length > 0 && $filePos < $this->_uploads[$pos]['size']) { |
| 193 | 193 | $ret .= fread($this->_uploads[$pos]['fp'], $length); |
| 194 | 194 | $length -= min($length, $this->_uploads[$pos]['size'] - $filePos); |
@@ -1,45 +1,45 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Helper class for building multipart/form-data request body |
|
| 4 | - * |
|
| 5 | - * PHP version 5 |
|
| 6 | - * |
|
| 7 | - * LICENSE: |
|
| 8 | - * |
|
| 9 | - * Copyright (c) 2008, 2009, Alexey Borzov <[email protected]> |
|
| 10 | - * All rights reserved. |
|
| 11 | - * |
|
| 12 | - * Redistribution and use in source and binary forms, with or without |
|
| 13 | - * modification, are permitted provided that the following conditions |
|
| 14 | - * are met: |
|
| 15 | - * |
|
| 16 | - * * Redistributions of source code must retain the above copyright |
|
| 17 | - * notice, this list of conditions and the following disclaimer. |
|
| 18 | - * * Redistributions in binary form must reproduce the above copyright |
|
| 19 | - * notice, this list of conditions and the following disclaimer in the |
|
| 20 | - * documentation and/or other materials provided with the distribution. |
|
| 21 | - * * The names of the authors may not be used to endorse or promote products |
|
| 22 | - * derived from this software without specific prior written permission. |
|
| 23 | - * |
|
| 24 | - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
| 25 | - * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
| 26 | - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
| 27 | - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
| 28 | - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
| 29 | - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
| 30 | - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
| 31 | - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
| 32 | - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
| 33 | - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
| 34 | - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
| 35 | - * |
|
| 36 | - * @category HTTP |
|
| 37 | - * @package HTTP_Request2 |
|
| 38 | - * @author Alexey Borzov <[email protected]> |
|
| 39 | - * @license http://opensource.org/licenses/bsd-license.php New BSD License |
|
| 40 | - * @version SVN: $Id: MultipartBody.php 290192 2009-11-03 21:29:32Z avb $ |
|
| 41 | - * @link http://pear.php.net/package/HTTP_Request2 |
|
| 42 | - */ |
|
| 3 | + * Helper class for building multipart/form-data request body |
|
| 4 | + * |
|
| 5 | + * PHP version 5 |
|
| 6 | + * |
|
| 7 | + * LICENSE: |
|
| 8 | + * |
|
| 9 | + * Copyright (c) 2008, 2009, Alexey Borzov <[email protected]> |
|
| 10 | + * All rights reserved. |
|
| 11 | + * |
|
| 12 | + * Redistribution and use in source and binary forms, with or without |
|
| 13 | + * modification, are permitted provided that the following conditions |
|
| 14 | + * are met: |
|
| 15 | + * |
|
| 16 | + * * Redistributions of source code must retain the above copyright |
|
| 17 | + * notice, this list of conditions and the following disclaimer. |
|
| 18 | + * * Redistributions in binary form must reproduce the above copyright |
|
| 19 | + * notice, this list of conditions and the following disclaimer in the |
|
| 20 | + * documentation and/or other materials provided with the distribution. |
|
| 21 | + * * The names of the authors may not be used to endorse or promote products |
|
| 22 | + * derived from this software without specific prior written permission. |
|
| 23 | + * |
|
| 24 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
| 25 | + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
| 26 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
| 27 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
| 28 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
| 29 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
| 30 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
| 31 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
| 32 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
| 33 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
| 34 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
| 35 | + * |
|
| 36 | + * @category HTTP |
|
| 37 | + * @package HTTP_Request2 |
|
| 38 | + * @author Alexey Borzov <[email protected]> |
|
| 39 | + * @license http://opensource.org/licenses/bsd-license.php New BSD License |
|
| 40 | + * @version SVN: $Id: MultipartBody.php 290192 2009-11-03 21:29:32Z avb $ |
|
| 41 | + * @link http://pear.php.net/package/HTTP_Request2 |
|
| 42 | + */ |
|
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | 45 | * Class for building multipart/form-data request body |
@@ -59,31 +59,31 @@ discard block |
||
| 59 | 59 | * MIME boundary |
| 60 | 60 | * @var string |
| 61 | 61 | */ |
| 62 | - private $_boundary; |
|
| 62 | + private $_boundary; |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * Form parameters added via {@link HTTP_Request2::addPostParameter()} |
| 66 | 66 | * @var array |
| 67 | 67 | */ |
| 68 | - private $_params = array(); |
|
| 68 | + private $_params = array(); |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * File uploads added via {@link HTTP_Request2::addUpload()} |
| 72 | 72 | * @var array |
| 73 | 73 | */ |
| 74 | - private $_uploads = array(); |
|
| 74 | + private $_uploads = array(); |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * Header for parts with parameters |
| 78 | 78 | * @var string |
| 79 | 79 | */ |
| 80 | - private $_headerParam = "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n"; |
|
| 80 | + private $_headerParam = "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n"; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * Header for parts with uploads |
| 84 | 84 | * @var string |
| 85 | 85 | */ |
| 86 | - private $_headerUpload = "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n"; |
|
| 86 | + private $_headerUpload = "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n"; |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * Current position in parameter and upload arrays |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | * |
| 94 | 94 | * @var array |
| 95 | 95 | */ |
| 96 | - private $_pos = array(0, 0); |
|
| 96 | + private $_pos = array(0, 0); |
|
| 97 | 97 | |
| 98 | 98 | |
| 99 | 99 | /** |
@@ -103,59 +103,59 @@ discard block |
||
| 103 | 103 | * @param array file uploads set via {@link HTTP_Request2::addUpload()} |
| 104 | 104 | * @param bool whether to append brackets to array variable names |
| 105 | 105 | */ |
| 106 | - public function __construct(array $params, array $uploads, $useBrackets = true) |
|
| 107 | - { |
|
| 108 | - $this->_params = self::_flattenArray('', $params, $useBrackets); |
|
| 109 | - foreach ($uploads as $fieldName => $f) { |
|
| 110 | - if (!is_array($f['fp'])) { |
|
| 111 | - $this->_uploads[] = $f + array('name' => $fieldName); |
|
| 112 | - } else { |
|
| 113 | - for ($i = 0; $i < count($f['fp']); $i++) { |
|
| 114 | - $upload = array( |
|
| 115 | - 'name' => ($useBrackets? $fieldName . '[' . $i . ']': $fieldName) |
|
| 116 | - ); |
|
| 117 | - foreach (array('fp', 'filename', 'size', 'type') as $key) { |
|
| 118 | - $upload[$key] = $f[$key][$i]; |
|
| 119 | - } |
|
| 120 | - $this->_uploads[] = $upload; |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - } |
|
| 106 | + public function __construct(array $params, array $uploads, $useBrackets = true) |
|
| 107 | + { |
|
| 108 | + $this->_params = self::_flattenArray('', $params, $useBrackets); |
|
| 109 | + foreach ($uploads as $fieldName => $f) { |
|
| 110 | + if (!is_array($f['fp'])) { |
|
| 111 | + $this->_uploads[] = $f + array('name' => $fieldName); |
|
| 112 | + } else { |
|
| 113 | + for ($i = 0; $i < count($f['fp']); $i++) { |
|
| 114 | + $upload = array( |
|
| 115 | + 'name' => ($useBrackets? $fieldName . '[' . $i . ']': $fieldName) |
|
| 116 | + ); |
|
| 117 | + foreach (array('fp', 'filename', 'size', 'type') as $key) { |
|
| 118 | + $upload[$key] = $f[$key][$i]; |
|
| 119 | + } |
|
| 120 | + $this->_uploads[] = $upload; |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | 126 | /** |
| 127 | 127 | * Returns the length of the body to use in Content-Length header |
| 128 | 128 | * |
| 129 | 129 | * @return integer |
| 130 | 130 | */ |
| 131 | - public function getLength() |
|
| 132 | - { |
|
| 133 | - $boundaryLength = strlen($this->getBoundary()); |
|
| 134 | - $headerParamLength = strlen($this->_headerParam) - 4 + $boundaryLength; |
|
| 135 | - $headerUploadLength = strlen($this->_headerUpload) - 8 + $boundaryLength; |
|
| 136 | - $length = $boundaryLength + 6; |
|
| 137 | - foreach ($this->_params as $p) { |
|
| 138 | - $length += $headerParamLength + strlen($p[0]) + strlen($p[1]) + 2; |
|
| 139 | - } |
|
| 140 | - foreach ($this->_uploads as $u) { |
|
| 141 | - $length += $headerUploadLength + strlen($u['name']) + strlen($u['type']) + |
|
| 142 | - strlen($u['filename']) + $u['size'] + 2; |
|
| 143 | - } |
|
| 144 | - return $length; |
|
| 145 | - } |
|
| 131 | + public function getLength() |
|
| 132 | + { |
|
| 133 | + $boundaryLength = strlen($this->getBoundary()); |
|
| 134 | + $headerParamLength = strlen($this->_headerParam) - 4 + $boundaryLength; |
|
| 135 | + $headerUploadLength = strlen($this->_headerUpload) - 8 + $boundaryLength; |
|
| 136 | + $length = $boundaryLength + 6; |
|
| 137 | + foreach ($this->_params as $p) { |
|
| 138 | + $length += $headerParamLength + strlen($p[0]) + strlen($p[1]) + 2; |
|
| 139 | + } |
|
| 140 | + foreach ($this->_uploads as $u) { |
|
| 141 | + $length += $headerUploadLength + strlen($u['name']) + strlen($u['type']) + |
|
| 142 | + strlen($u['filename']) + $u['size'] + 2; |
|
| 143 | + } |
|
| 144 | + return $length; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | 147 | /** |
| 148 | 148 | * Returns the boundary to use in Content-Type header |
| 149 | 149 | * |
| 150 | 150 | * @return string |
| 151 | 151 | */ |
| 152 | - public function getBoundary() |
|
| 153 | - { |
|
| 154 | - if (empty($this->_boundary)) { |
|
| 155 | - $this->_boundary = '--' . md5('PEAR-HTTP_Request2-' . microtime()); |
|
| 156 | - } |
|
| 157 | - return $this->_boundary; |
|
| 158 | - } |
|
| 152 | + public function getBoundary() |
|
| 153 | + { |
|
| 154 | + if (empty($this->_boundary)) { |
|
| 155 | + $this->_boundary = '--' . md5('PEAR-HTTP_Request2-' . microtime()); |
|
| 156 | + } |
|
| 157 | + return $this->_boundary; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | 160 | /** |
| 161 | 161 | * Returns next chunk of request body |
@@ -163,69 +163,69 @@ discard block |
||
| 163 | 163 | * @param integer Amount of bytes to read |
| 164 | 164 | * @return string Up to $length bytes of data, empty string if at end |
| 165 | 165 | */ |
| 166 | - public function read($length) |
|
| 167 | - { |
|
| 168 | - $ret = ''; |
|
| 169 | - $boundary = $this->getBoundary(); |
|
| 170 | - $paramCount = count($this->_params); |
|
| 171 | - $uploadCount = count($this->_uploads); |
|
| 172 | - while ($length > 0 && $this->_pos[0] <= $paramCount + $uploadCount) { |
|
| 173 | - $oldLength = $length; |
|
| 174 | - if ($this->_pos[0] < $paramCount) { |
|
| 175 | - $param = sprintf($this->_headerParam, $boundary, |
|
| 176 | - $this->_params[$this->_pos[0]][0]) . |
|
| 177 | - $this->_params[$this->_pos[0]][1] . "\r\n"; |
|
| 178 | - $ret .= substr($param, $this->_pos[1], $length); |
|
| 179 | - $length -= min(strlen($param) - $this->_pos[1], $length); |
|
| 166 | + public function read($length) |
|
| 167 | + { |
|
| 168 | + $ret = ''; |
|
| 169 | + $boundary = $this->getBoundary(); |
|
| 170 | + $paramCount = count($this->_params); |
|
| 171 | + $uploadCount = count($this->_uploads); |
|
| 172 | + while ($length > 0 && $this->_pos[0] <= $paramCount + $uploadCount) { |
|
| 173 | + $oldLength = $length; |
|
| 174 | + if ($this->_pos[0] < $paramCount) { |
|
| 175 | + $param = sprintf($this->_headerParam, $boundary, |
|
| 176 | + $this->_params[$this->_pos[0]][0]) . |
|
| 177 | + $this->_params[$this->_pos[0]][1] . "\r\n"; |
|
| 178 | + $ret .= substr($param, $this->_pos[1], $length); |
|
| 179 | + $length -= min(strlen($param) - $this->_pos[1], $length); |
|
| 180 | 180 | |
| 181 | - } elseif ($this->_pos[0] < $paramCount + $uploadCount) { |
|
| 182 | - $pos = $this->_pos[0] - $paramCount; |
|
| 183 | - $header = sprintf($this->_headerUpload, $boundary, |
|
| 184 | - $this->_uploads[$pos]['name'], |
|
| 185 | - $this->_uploads[$pos]['filename'], |
|
| 186 | - $this->_uploads[$pos]['type']); |
|
| 187 | - if ($this->_pos[1] < strlen($header)) { |
|
| 188 | - $ret .= substr($header, $this->_pos[1], $length); |
|
| 189 | - $length -= min(strlen($header) - $this->_pos[1], $length); |
|
| 190 | - } |
|
| 191 | - $filePos = max(0, $this->_pos[1] - strlen($header)); |
|
| 192 | - if ($length > 0 && $filePos < $this->_uploads[$pos]['size']) { |
|
| 193 | - $ret .= fread($this->_uploads[$pos]['fp'], $length); |
|
| 194 | - $length -= min($length, $this->_uploads[$pos]['size'] - $filePos); |
|
| 195 | - } |
|
| 196 | - if ($length > 0) { |
|
| 197 | - $start = $this->_pos[1] + ($oldLength - $length) - |
|
| 198 | - strlen($header) - $this->_uploads[$pos]['size']; |
|
| 199 | - $ret .= substr("\r\n", $start, $length); |
|
| 200 | - $length -= min(2 - $start, $length); |
|
| 201 | - } |
|
| 181 | + } elseif ($this->_pos[0] < $paramCount + $uploadCount) { |
|
| 182 | + $pos = $this->_pos[0] - $paramCount; |
|
| 183 | + $header = sprintf($this->_headerUpload, $boundary, |
|
| 184 | + $this->_uploads[$pos]['name'], |
|
| 185 | + $this->_uploads[$pos]['filename'], |
|
| 186 | + $this->_uploads[$pos]['type']); |
|
| 187 | + if ($this->_pos[1] < strlen($header)) { |
|
| 188 | + $ret .= substr($header, $this->_pos[1], $length); |
|
| 189 | + $length -= min(strlen($header) - $this->_pos[1], $length); |
|
| 190 | + } |
|
| 191 | + $filePos = max(0, $this->_pos[1] - strlen($header)); |
|
| 192 | + if ($length > 0 && $filePos < $this->_uploads[$pos]['size']) { |
|
| 193 | + $ret .= fread($this->_uploads[$pos]['fp'], $length); |
|
| 194 | + $length -= min($length, $this->_uploads[$pos]['size'] - $filePos); |
|
| 195 | + } |
|
| 196 | + if ($length > 0) { |
|
| 197 | + $start = $this->_pos[1] + ($oldLength - $length) - |
|
| 198 | + strlen($header) - $this->_uploads[$pos]['size']; |
|
| 199 | + $ret .= substr("\r\n", $start, $length); |
|
| 200 | + $length -= min(2 - $start, $length); |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | - } else { |
|
| 204 | - $closing = '--' . $boundary . "--\r\n"; |
|
| 205 | - $ret .= substr($closing, $this->_pos[1], $length); |
|
| 206 | - $length -= min(strlen($closing) - $this->_pos[1], $length); |
|
| 207 | - } |
|
| 208 | - if ($length > 0) { |
|
| 209 | - $this->_pos = array($this->_pos[0] + 1, 0); |
|
| 210 | - } else { |
|
| 211 | - $this->_pos[1] += $oldLength; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - return $ret; |
|
| 215 | - } |
|
| 203 | + } else { |
|
| 204 | + $closing = '--' . $boundary . "--\r\n"; |
|
| 205 | + $ret .= substr($closing, $this->_pos[1], $length); |
|
| 206 | + $length -= min(strlen($closing) - $this->_pos[1], $length); |
|
| 207 | + } |
|
| 208 | + if ($length > 0) { |
|
| 209 | + $this->_pos = array($this->_pos[0] + 1, 0); |
|
| 210 | + } else { |
|
| 211 | + $this->_pos[1] += $oldLength; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + return $ret; |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | 217 | /** |
| 218 | 218 | * Sets the current position to the start of the body |
| 219 | 219 | * |
| 220 | 220 | * This allows reusing the same body in another request |
| 221 | 221 | */ |
| 222 | - public function rewind() |
|
| 223 | - { |
|
| 224 | - $this->_pos = array(0, 0); |
|
| 225 | - foreach ($this->_uploads as $u) { |
|
| 226 | - rewind($u['fp']); |
|
| 227 | - } |
|
| 228 | - } |
|
| 222 | + public function rewind() |
|
| 223 | + { |
|
| 224 | + $this->_pos = array(0, 0); |
|
| 225 | + foreach ($this->_uploads as $u) { |
|
| 226 | + rewind($u['fp']); |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | 230 | /** |
| 231 | 231 | * Returns the body as string |
@@ -235,11 +235,11 @@ discard block |
||
| 235 | 235 | * |
| 236 | 236 | * @return string |
| 237 | 237 | */ |
| 238 | - public function __toString() |
|
| 239 | - { |
|
| 240 | - $this->rewind(); |
|
| 241 | - return $this->read($this->getLength()); |
|
| 242 | - } |
|
| 238 | + public function __toString() |
|
| 239 | + { |
|
| 240 | + $this->rewind(); |
|
| 241 | + return $this->read($this->getLength()); |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | 244 | |
| 245 | 245 | /** |
@@ -251,24 +251,24 @@ discard block |
||
| 251 | 251 | * @param bool whether to append [] to array variables' names |
| 252 | 252 | * @return array array with the following items: array('item name', 'item value'); |
| 253 | 253 | */ |
| 254 | - private static function _flattenArray($name, $values, $useBrackets) |
|
| 255 | - { |
|
| 256 | - if (!is_array($values)) { |
|
| 257 | - return array(array($name, $values)); |
|
| 258 | - } else { |
|
| 259 | - $ret = array(); |
|
| 260 | - foreach ($values as $k => $v) { |
|
| 261 | - if (empty($name)) { |
|
| 262 | - $newName = $k; |
|
| 263 | - } elseif ($useBrackets) { |
|
| 264 | - $newName = $name . '[' . $k . ']'; |
|
| 265 | - } else { |
|
| 266 | - $newName = $name; |
|
| 267 | - } |
|
| 268 | - $ret = array_merge($ret, self::_flattenArray($newName, $v, $useBrackets)); |
|
| 269 | - } |
|
| 270 | - return $ret; |
|
| 271 | - } |
|
| 272 | - } |
|
| 254 | + private static function _flattenArray($name, $values, $useBrackets) |
|
| 255 | + { |
|
| 256 | + if (!is_array($values)) { |
|
| 257 | + return array(array($name, $values)); |
|
| 258 | + } else { |
|
| 259 | + $ret = array(); |
|
| 260 | + foreach ($values as $k => $v) { |
|
| 261 | + if (empty($name)) { |
|
| 262 | + $newName = $k; |
|
| 263 | + } elseif ($useBrackets) { |
|
| 264 | + $newName = $name . '[' . $k . ']'; |
|
| 265 | + } else { |
|
| 266 | + $newName = $name; |
|
| 267 | + } |
|
| 268 | + $ret = array_merge($ret, self::_flattenArray($newName, $v, $useBrackets)); |
|
| 269 | + } |
|
| 270 | + return $ret; |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | 273 | } |
| 274 | 274 | ?> |
@@ -1,46 +1,46 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * An observer useful for debugging / testing. |
|
| 4 | - * |
|
| 5 | - * PHP version 5 |
|
| 6 | - * |
|
| 7 | - * LICENSE: |
|
| 8 | - * |
|
| 9 | - * Copyright (c) 2008, 2009, Alexey Borzov <[email protected]> |
|
| 10 | - * All rights reserved. |
|
| 11 | - * |
|
| 12 | - * Redistribution and use in source and binary forms, with or without |
|
| 13 | - * modification, are permitted provided that the following conditions |
|
| 14 | - * are met: |
|
| 15 | - * |
|
| 16 | - * * Redistributions of source code must retain the above copyright |
|
| 17 | - * notice, this list of conditions and the following disclaimer. |
|
| 18 | - * * Redistributions in binary form must reproduce the above copyright |
|
| 19 | - * notice, this list of conditions and the following disclaimer in the |
|
| 20 | - * documentation and/or other materials provided with the distribution. |
|
| 21 | - * * The names of the authors may not be used to endorse or promote products |
|
| 22 | - * derived from this software without specific prior written permission. |
|
| 23 | - * |
|
| 24 | - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
| 25 | - * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
| 26 | - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
| 27 | - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
| 28 | - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
| 29 | - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
| 30 | - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
| 31 | - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
| 32 | - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
| 33 | - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
| 34 | - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
| 35 | - * |
|
| 36 | - * @category HTTP |
|
| 37 | - * @package HTTP_Request2 |
|
| 38 | - * @author David Jean Louis <[email protected]> |
|
| 39 | - * @author Alexey Borzov <[email protected]> |
|
| 40 | - * @license http://opensource.org/licenses/bsd-license.php New BSD License |
|
| 41 | - * @version SVN: $Id: Log.php 293416 2010-01-11 18:06:15Z avb $ |
|
| 42 | - * @link http://pear.php.net/package/HTTP_Request2 |
|
| 43 | - */ |
|
| 3 | + * An observer useful for debugging / testing. |
|
| 4 | + * |
|
| 5 | + * PHP version 5 |
|
| 6 | + * |
|
| 7 | + * LICENSE: |
|
| 8 | + * |
|
| 9 | + * Copyright (c) 2008, 2009, Alexey Borzov <[email protected]> |
|
| 10 | + * All rights reserved. |
|
| 11 | + * |
|
| 12 | + * Redistribution and use in source and binary forms, with or without |
|
| 13 | + * modification, are permitted provided that the following conditions |
|
| 14 | + * are met: |
|
| 15 | + * |
|
| 16 | + * * Redistributions of source code must retain the above copyright |
|
| 17 | + * notice, this list of conditions and the following disclaimer. |
|
| 18 | + * * Redistributions in binary form must reproduce the above copyright |
|
| 19 | + * notice, this list of conditions and the following disclaimer in the |
|
| 20 | + * documentation and/or other materials provided with the distribution. |
|
| 21 | + * * The names of the authors may not be used to endorse or promote products |
|
| 22 | + * derived from this software without specific prior written permission. |
|
| 23 | + * |
|
| 24 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
| 25 | + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
| 26 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
| 27 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
| 28 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
| 29 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
| 30 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
| 31 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
| 32 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
| 33 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
| 34 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
| 35 | + * |
|
| 36 | + * @category HTTP |
|
| 37 | + * @package HTTP_Request2 |
|
| 38 | + * @author David Jean Louis <[email protected]> |
|
| 39 | + * @author Alexey Borzov <[email protected]> |
|
| 40 | + * @license http://opensource.org/licenses/bsd-license.php New BSD License |
|
| 41 | + * @version SVN: $Id: Log.php 293416 2010-01-11 18:06:15Z avb $ |
|
| 42 | + * @link http://pear.php.net/package/HTTP_Request2 |
|
| 43 | + */ |
|
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | 46 | * Exception class for HTTP_Request2 package |
@@ -92,124 +92,124 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | class HTTP_Request2_Observer_Log implements SplObserver |
| 94 | 94 | { |
| 95 | - // properties {{{ |
|
| 95 | + // properties {{{ |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * The log target, it can be a a resource or a PEAR Log instance. |
|
| 99 | - * |
|
| 100 | - * @var resource|Log $target |
|
| 101 | - */ |
|
| 102 | - protected $target = null; |
|
| 97 | + /** |
|
| 98 | + * The log target, it can be a a resource or a PEAR Log instance. |
|
| 99 | + * |
|
| 100 | + * @var resource|Log $target |
|
| 101 | + */ |
|
| 102 | + protected $target = null; |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * The events to log. |
|
| 106 | - * |
|
| 107 | - * @var array $events |
|
| 108 | - */ |
|
| 109 | - public $events = array( |
|
| 110 | - 'connect', |
|
| 111 | - 'sentHeaders', |
|
| 112 | - 'sentBodyPart', |
|
| 113 | - 'receivedHeaders', |
|
| 114 | - 'receivedBody', |
|
| 115 | - 'disconnect', |
|
| 116 | - ); |
|
| 104 | + /** |
|
| 105 | + * The events to log. |
|
| 106 | + * |
|
| 107 | + * @var array $events |
|
| 108 | + */ |
|
| 109 | + public $events = array( |
|
| 110 | + 'connect', |
|
| 111 | + 'sentHeaders', |
|
| 112 | + 'sentBodyPart', |
|
| 113 | + 'receivedHeaders', |
|
| 114 | + 'receivedBody', |
|
| 115 | + 'disconnect', |
|
| 116 | + ); |
|
| 117 | 117 | |
| 118 | - // }}} |
|
| 119 | - // __construct() {{{ |
|
| 118 | + // }}} |
|
| 119 | + // __construct() {{{ |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * Constructor. |
|
| 123 | - * |
|
| 124 | - * @param mixed $target Can be a file path (default: php://output), a resource, |
|
| 125 | - * or an instance of the PEAR Log class. |
|
| 126 | - * @param array $events Array of events to listen to (default: all events) |
|
| 127 | - * |
|
| 128 | - * @return void |
|
| 129 | - */ |
|
| 130 | - public function __construct($target = 'php://output', array $events = array()) |
|
| 131 | - { |
|
| 132 | - if (!empty($events)) { |
|
| 133 | - $this->events = $events; |
|
| 134 | - } |
|
| 135 | - if (is_resource($target) || $target instanceof Log) { |
|
| 136 | - $this->target = $target; |
|
| 137 | - } elseif (false === ($this->target = @fopen($target, 'ab'))) { |
|
| 138 | - throw new HTTP_Request2_Exception("Unable to open '{$target}'"); |
|
| 139 | - } |
|
| 140 | - } |
|
| 121 | + /** |
|
| 122 | + * Constructor. |
|
| 123 | + * |
|
| 124 | + * @param mixed $target Can be a file path (default: php://output), a resource, |
|
| 125 | + * or an instance of the PEAR Log class. |
|
| 126 | + * @param array $events Array of events to listen to (default: all events) |
|
| 127 | + * |
|
| 128 | + * @return void |
|
| 129 | + */ |
|
| 130 | + public function __construct($target = 'php://output', array $events = array()) |
|
| 131 | + { |
|
| 132 | + if (!empty($events)) { |
|
| 133 | + $this->events = $events; |
|
| 134 | + } |
|
| 135 | + if (is_resource($target) || $target instanceof Log) { |
|
| 136 | + $this->target = $target; |
|
| 137 | + } elseif (false === ($this->target = @fopen($target, 'ab'))) { |
|
| 138 | + throw new HTTP_Request2_Exception("Unable to open '{$target}'"); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - // }}} |
|
| 143 | - // update() {{{ |
|
| 142 | + // }}} |
|
| 143 | + // update() {{{ |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Called when the request notifies us of an event. |
|
| 147 | - * |
|
| 148 | - * @param HTTP_Request2 $subject The HTTP_Request2 instance |
|
| 149 | - * |
|
| 150 | - * @return void |
|
| 151 | - */ |
|
| 152 | - public function update(SplSubject $subject) |
|
| 153 | - { |
|
| 154 | - $event = $subject->getLastEvent(); |
|
| 155 | - if (!in_array($event['name'], $this->events)) { |
|
| 156 | - return; |
|
| 157 | - } |
|
| 145 | + /** |
|
| 146 | + * Called when the request notifies us of an event. |
|
| 147 | + * |
|
| 148 | + * @param HTTP_Request2 $subject The HTTP_Request2 instance |
|
| 149 | + * |
|
| 150 | + * @return void |
|
| 151 | + */ |
|
| 152 | + public function update(SplSubject $subject) |
|
| 153 | + { |
|
| 154 | + $event = $subject->getLastEvent(); |
|
| 155 | + if (!in_array($event['name'], $this->events)) { |
|
| 156 | + return; |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - switch ($event['name']) { |
|
| 160 | - case 'connect': |
|
| 161 | - $this->log('* Connected to ' . $event['data']); |
|
| 162 | - break; |
|
| 163 | - case 'sentHeaders': |
|
| 164 | - $headers = explode("\r\n", $event['data']); |
|
| 165 | - array_pop($headers); |
|
| 166 | - foreach ($headers as $header) { |
|
| 167 | - $this->log('> ' . $header); |
|
| 168 | - } |
|
| 169 | - break; |
|
| 170 | - case 'sentBodyPart': |
|
| 171 | - $this->log('> ' . $event['data'] . ' byte(s) sent'); |
|
| 172 | - break; |
|
| 173 | - case 'receivedHeaders': |
|
| 174 | - $this->log(sprintf('< HTTP/%s %s %s', |
|
| 175 | - $event['data']->getVersion(), |
|
| 176 | - $event['data']->getStatus(), |
|
| 177 | - $event['data']->getReasonPhrase())); |
|
| 178 | - $headers = $event['data']->getHeader(); |
|
| 179 | - foreach ($headers as $key => $val) { |
|
| 180 | - $this->log('< ' . $key . ': ' . $val); |
|
| 181 | - } |
|
| 182 | - $this->log('< '); |
|
| 183 | - break; |
|
| 184 | - case 'receivedBody': |
|
| 185 | - $this->log($event['data']->getBody()); |
|
| 186 | - break; |
|
| 187 | - case 'disconnect': |
|
| 188 | - $this->log('* Disconnected'); |
|
| 189 | - break; |
|
| 190 | - } |
|
| 191 | - } |
|
| 159 | + switch ($event['name']) { |
|
| 160 | + case 'connect': |
|
| 161 | + $this->log('* Connected to ' . $event['data']); |
|
| 162 | + break; |
|
| 163 | + case 'sentHeaders': |
|
| 164 | + $headers = explode("\r\n", $event['data']); |
|
| 165 | + array_pop($headers); |
|
| 166 | + foreach ($headers as $header) { |
|
| 167 | + $this->log('> ' . $header); |
|
| 168 | + } |
|
| 169 | + break; |
|
| 170 | + case 'sentBodyPart': |
|
| 171 | + $this->log('> ' . $event['data'] . ' byte(s) sent'); |
|
| 172 | + break; |
|
| 173 | + case 'receivedHeaders': |
|
| 174 | + $this->log(sprintf('< HTTP/%s %s %s', |
|
| 175 | + $event['data']->getVersion(), |
|
| 176 | + $event['data']->getStatus(), |
|
| 177 | + $event['data']->getReasonPhrase())); |
|
| 178 | + $headers = $event['data']->getHeader(); |
|
| 179 | + foreach ($headers as $key => $val) { |
|
| 180 | + $this->log('< ' . $key . ': ' . $val); |
|
| 181 | + } |
|
| 182 | + $this->log('< '); |
|
| 183 | + break; |
|
| 184 | + case 'receivedBody': |
|
| 185 | + $this->log($event['data']->getBody()); |
|
| 186 | + break; |
|
| 187 | + case 'disconnect': |
|
| 188 | + $this->log('* Disconnected'); |
|
| 189 | + break; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - // }}} |
|
| 194 | - // log() {{{ |
|
| 193 | + // }}} |
|
| 194 | + // log() {{{ |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * Logs the given message to the configured target. |
|
| 198 | - * |
|
| 199 | - * @param string $message Message to display |
|
| 200 | - * |
|
| 201 | - * @return void |
|
| 202 | - */ |
|
| 203 | - protected function log($message) |
|
| 204 | - { |
|
| 205 | - if ($this->target instanceof Log) { |
|
| 206 | - $this->target->debug($message); |
|
| 207 | - } elseif (is_resource($this->target)) { |
|
| 208 | - fwrite($this->target, $message . "\r\n"); |
|
| 209 | - } |
|
| 210 | - } |
|
| 196 | + /** |
|
| 197 | + * Logs the given message to the configured target. |
|
| 198 | + * |
|
| 199 | + * @param string $message Message to display |
|
| 200 | + * |
|
| 201 | + * @return void |
|
| 202 | + */ |
|
| 203 | + protected function log($message) |
|
| 204 | + { |
|
| 205 | + if ($this->target instanceof Log) { |
|
| 206 | + $this->target->debug($message); |
|
| 207 | + } elseif (is_resource($this->target)) { |
|
| 208 | + fwrite($this->target, $message . "\r\n"); |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - // }}} |
|
| 212 | + // }}} |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | ?> |
| 216 | 216 | \ No newline at end of file |
@@ -157,36 +157,36 @@ |
||
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | switch ($event['name']) { |
| 160 | - case 'connect': |
|
| 161 | - $this->log('* Connected to ' . $event['data']); |
|
| 162 | - break; |
|
| 163 | - case 'sentHeaders': |
|
| 164 | - $headers = explode("\r\n", $event['data']); |
|
| 165 | - array_pop($headers); |
|
| 166 | - foreach ($headers as $header) { |
|
| 167 | - $this->log('> ' . $header); |
|
| 168 | - } |
|
| 169 | - break; |
|
| 170 | - case 'sentBodyPart': |
|
| 171 | - $this->log('> ' . $event['data'] . ' byte(s) sent'); |
|
| 172 | - break; |
|
| 173 | - case 'receivedHeaders': |
|
| 174 | - $this->log(sprintf('< HTTP/%s %s %s', |
|
| 175 | - $event['data']->getVersion(), |
|
| 176 | - $event['data']->getStatus(), |
|
| 177 | - $event['data']->getReasonPhrase())); |
|
| 178 | - $headers = $event['data']->getHeader(); |
|
| 179 | - foreach ($headers as $key => $val) { |
|
| 180 | - $this->log('< ' . $key . ': ' . $val); |
|
| 181 | - } |
|
| 182 | - $this->log('< '); |
|
| 183 | - break; |
|
| 184 | - case 'receivedBody': |
|
| 185 | - $this->log($event['data']->getBody()); |
|
| 186 | - break; |
|
| 187 | - case 'disconnect': |
|
| 188 | - $this->log('* Disconnected'); |
|
| 189 | - break; |
|
| 160 | + case 'connect': |
|
| 161 | + $this->log('* Connected to ' . $event['data']); |
|
| 162 | + break; |
|
| 163 | + case 'sentHeaders': |
|
| 164 | + $headers = explode("\r\n", $event['data']); |
|
| 165 | + array_pop($headers); |
|
| 166 | + foreach ($headers as $header) { |
|
| 167 | + $this->log('> ' . $header); |
|
| 168 | + } |
|
| 169 | + break; |
|
| 170 | + case 'sentBodyPart': |
|
| 171 | + $this->log('> ' . $event['data'] . ' byte(s) sent'); |
|
| 172 | + break; |
|
| 173 | + case 'receivedHeaders': |
|
| 174 | + $this->log(sprintf('< HTTP/%s %s %s', |
|
| 175 | + $event['data']->getVersion(), |
|
| 176 | + $event['data']->getStatus(), |
|
| 177 | + $event['data']->getReasonPhrase())); |
|
| 178 | + $headers = $event['data']->getHeader(); |
|
| 179 | + foreach ($headers as $key => $val) { |
|
| 180 | + $this->log('< ' . $key . ': ' . $val); |
|
| 181 | + } |
|
| 182 | + $this->log('< '); |
|
| 183 | + break; |
|
| 184 | + case 'receivedBody': |
|
| 185 | + $this->log($event['data']->getBody()); |
|
| 186 | + break; |
|
| 187 | + case 'disconnect': |
|
| 188 | + $this->log('* Disconnected'); |
|
| 189 | + break; |
|
| 190 | 190 | } |
| 191 | 191 | } |
| 192 | 192 | |
@@ -79,33 +79,33 @@ discard block |
||
| 79 | 79 | * HTTP protocol version (e.g. 1.0, 1.1) |
| 80 | 80 | * @var string |
| 81 | 81 | */ |
| 82 | - protected $version; |
|
| 82 | + protected $version; |
|
| 83 | 83 | |
| 84 | 84 | /** |
| 85 | 85 | * Status code |
| 86 | 86 | * @var integer |
| 87 | 87 | * @link http://tools.ietf.org/html/rfc2616#section-6.1.1 |
| 88 | 88 | */ |
| 89 | - protected $code; |
|
| 89 | + protected $code; |
|
| 90 | 90 | |
| 91 | 91 | /** |
| 92 | 92 | * Reason phrase |
| 93 | 93 | * @var string |
| 94 | 94 | * @link http://tools.ietf.org/html/rfc2616#section-6.1.1 |
| 95 | 95 | */ |
| 96 | - protected $reasonPhrase; |
|
| 96 | + protected $reasonPhrase; |
|
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | 99 | * Associative array of response headers |
| 100 | 100 | * @var array |
| 101 | 101 | */ |
| 102 | - protected $headers = array(); |
|
| 102 | + protected $headers = array(); |
|
| 103 | 103 | |
| 104 | 104 | /** |
| 105 | 105 | * Cookies set in the response |
| 106 | 106 | * @var array |
| 107 | 107 | */ |
| 108 | - protected $cookies = array(); |
|
| 108 | + protected $cookies = array(); |
|
| 109 | 109 | |
| 110 | 110 | /** |
| 111 | 111 | * Name of last header processed by parseHederLine() |
@@ -114,13 +114,13 @@ discard block |
||
| 114 | 114 | * |
| 115 | 115 | * @var string |
| 116 | 116 | */ |
| 117 | - protected $lastHeader = null; |
|
| 117 | + protected $lastHeader = null; |
|
| 118 | 118 | |
| 119 | 119 | /** |
| 120 | 120 | * Response body |
| 121 | 121 | * @var string |
| 122 | 122 | */ |
| 123 | - protected $body = ''; |
|
| 123 | + protected $body = ''; |
|
| 124 | 124 | |
| 125 | 125 | /** |
| 126 | 126 | * Whether the body is still encoded by Content-Encoding |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | * @var bool |
| 132 | 132 | */ |
| 133 | - protected $bodyEncoded; |
|
| 133 | + protected $bodyEncoded; |
|
| 134 | 134 | |
| 135 | 135 | /** |
| 136 | 136 | * Associative array of HTTP status code / reason phrase. |
@@ -138,64 +138,64 @@ discard block |
||
| 138 | 138 | * @var array |
| 139 | 139 | * @link http://tools.ietf.org/html/rfc2616#section-10 |
| 140 | 140 | */ |
| 141 | - protected static $phrases = array( |
|
| 142 | - |
|
| 143 | - // 1xx: Informational - Request received, continuing process |
|
| 144 | - 100 => 'Continue', |
|
| 145 | - 101 => 'Switching Protocols', |
|
| 146 | - |
|
| 147 | - // 2xx: Success - The action was successfully received, understood and |
|
| 148 | - // accepted |
|
| 149 | - 200 => 'OK', |
|
| 150 | - 201 => 'Created', |
|
| 151 | - 202 => 'Accepted', |
|
| 152 | - 203 => 'Non-Authoritative Information', |
|
| 153 | - 204 => 'No Content', |
|
| 154 | - 205 => 'Reset Content', |
|
| 155 | - 206 => 'Partial Content', |
|
| 156 | - |
|
| 157 | - // 3xx: Redirection - Further action must be taken in order to complete |
|
| 158 | - // the request |
|
| 159 | - 300 => 'Multiple Choices', |
|
| 160 | - 301 => 'Moved Permanently', |
|
| 161 | - 302 => 'Found', // 1.1 |
|
| 162 | - 303 => 'See Other', |
|
| 163 | - 304 => 'Not Modified', |
|
| 164 | - 305 => 'Use Proxy', |
|
| 165 | - 307 => 'Temporary Redirect', |
|
| 166 | - |
|
| 167 | - // 4xx: Client Error - The request contains bad syntax or cannot be |
|
| 168 | - // fulfilled |
|
| 169 | - 400 => 'Bad Request', |
|
| 170 | - 401 => 'Unauthorized', |
|
| 171 | - 402 => 'Payment Required', |
|
| 172 | - 403 => 'Forbidden', |
|
| 173 | - 404 => 'Not Found', |
|
| 174 | - 405 => 'Method Not Allowed', |
|
| 175 | - 406 => 'Not Acceptable', |
|
| 176 | - 407 => 'Proxy Authentication Required', |
|
| 177 | - 408 => 'Request Timeout', |
|
| 178 | - 409 => 'Conflict', |
|
| 179 | - 410 => 'Gone', |
|
| 180 | - 411 => 'Length Required', |
|
| 181 | - 412 => 'Precondition Failed', |
|
| 182 | - 413 => 'Request Entity Too Large', |
|
| 183 | - 414 => 'Request-URI Too Long', |
|
| 184 | - 415 => 'Unsupported Media Type', |
|
| 185 | - 416 => 'Requested Range Not Satisfiable', |
|
| 186 | - 417 => 'Expectation Failed', |
|
| 187 | - |
|
| 188 | - // 5xx: Server Error - The server failed to fulfill an apparently |
|
| 189 | - // valid request |
|
| 190 | - 500 => 'Internal Server Error', |
|
| 191 | - 501 => 'Not Implemented', |
|
| 192 | - 502 => 'Bad Gateway', |
|
| 193 | - 503 => 'Service Unavailable', |
|
| 194 | - 504 => 'Gateway Timeout', |
|
| 195 | - 505 => 'HTTP Version Not Supported', |
|
| 196 | - 509 => 'Bandwidth Limit Exceeded', |
|
| 197 | - |
|
| 198 | - ); |
|
| 141 | + protected static $phrases = array( |
|
| 142 | + |
|
| 143 | + // 1xx: Informational - Request received, continuing process |
|
| 144 | + 100 => 'Continue', |
|
| 145 | + 101 => 'Switching Protocols', |
|
| 146 | + |
|
| 147 | + // 2xx: Success - The action was successfully received, understood and |
|
| 148 | + // accepted |
|
| 149 | + 200 => 'OK', |
|
| 150 | + 201 => 'Created', |
|
| 151 | + 202 => 'Accepted', |
|
| 152 | + 203 => 'Non-Authoritative Information', |
|
| 153 | + 204 => 'No Content', |
|
| 154 | + 205 => 'Reset Content', |
|
| 155 | + 206 => 'Partial Content', |
|
| 156 | + |
|
| 157 | + // 3xx: Redirection - Further action must be taken in order to complete |
|
| 158 | + // the request |
|
| 159 | + 300 => 'Multiple Choices', |
|
| 160 | + 301 => 'Moved Permanently', |
|
| 161 | + 302 => 'Found', // 1.1 |
|
| 162 | + 303 => 'See Other', |
|
| 163 | + 304 => 'Not Modified', |
|
| 164 | + 305 => 'Use Proxy', |
|
| 165 | + 307 => 'Temporary Redirect', |
|
| 166 | + |
|
| 167 | + // 4xx: Client Error - The request contains bad syntax or cannot be |
|
| 168 | + // fulfilled |
|
| 169 | + 400 => 'Bad Request', |
|
| 170 | + 401 => 'Unauthorized', |
|
| 171 | + 402 => 'Payment Required', |
|
| 172 | + 403 => 'Forbidden', |
|
| 173 | + 404 => 'Not Found', |
|
| 174 | + 405 => 'Method Not Allowed', |
|
| 175 | + 406 => 'Not Acceptable', |
|
| 176 | + 407 => 'Proxy Authentication Required', |
|
| 177 | + 408 => 'Request Timeout', |
|
| 178 | + 409 => 'Conflict', |
|
| 179 | + 410 => 'Gone', |
|
| 180 | + 411 => 'Length Required', |
|
| 181 | + 412 => 'Precondition Failed', |
|
| 182 | + 413 => 'Request Entity Too Large', |
|
| 183 | + 414 => 'Request-URI Too Long', |
|
| 184 | + 415 => 'Unsupported Media Type', |
|
| 185 | + 416 => 'Requested Range Not Satisfiable', |
|
| 186 | + 417 => 'Expectation Failed', |
|
| 187 | + |
|
| 188 | + // 5xx: Server Error - The server failed to fulfill an apparently |
|
| 189 | + // valid request |
|
| 190 | + 500 => 'Internal Server Error', |
|
| 191 | + 501 => 'Not Implemented', |
|
| 192 | + 502 => 'Bad Gateway', |
|
| 193 | + 503 => 'Service Unavailable', |
|
| 194 | + 504 => 'Gateway Timeout', |
|
| 195 | + 505 => 'HTTP Version Not Supported', |
|
| 196 | + 509 => 'Bandwidth Limit Exceeded', |
|
| 197 | + |
|
| 198 | + ); |
|
| 199 | 199 | |
| 200 | 200 | /** |
| 201 | 201 | * Constructor, parses the response status line |
@@ -204,20 +204,20 @@ discard block |
||
| 204 | 204 | * @param bool Whether body is still encoded by Content-Encoding |
| 205 | 205 | * @throws HTTP_Request2_Exception if status line is invalid according to spec |
| 206 | 206 | */ |
| 207 | - public function __construct($statusLine, $bodyEncoded = true) |
|
| 208 | - { |
|
| 209 | - if (!preg_match('!^HTTP/(\d\.\d) (\d{3})(?: (.+))?!', $statusLine, $m)) { |
|
| 210 | - throw new HTTP_Request2_Exception("Malformed response: {$statusLine}"); |
|
| 211 | - } |
|
| 212 | - $this->version = $m[1]; |
|
| 213 | - $this->code = intval($m[2]); |
|
| 214 | - if (!empty($m[3])) { |
|
| 215 | - $this->reasonPhrase = trim($m[3]); |
|
| 216 | - } elseif (!empty(self::$phrases[$this->code])) { |
|
| 217 | - $this->reasonPhrase = self::$phrases[$this->code]; |
|
| 218 | - } |
|
| 219 | - $this->bodyEncoded = (bool)$bodyEncoded; |
|
| 220 | - } |
|
| 207 | + public function __construct($statusLine, $bodyEncoded = true) |
|
| 208 | + { |
|
| 209 | + if (!preg_match('!^HTTP/(\d\.\d) (\d{3})(?: (.+))?!', $statusLine, $m)) { |
|
| 210 | + throw new HTTP_Request2_Exception("Malformed response: {$statusLine}"); |
|
| 211 | + } |
|
| 212 | + $this->version = $m[1]; |
|
| 213 | + $this->code = intval($m[2]); |
|
| 214 | + if (!empty($m[3])) { |
|
| 215 | + $this->reasonPhrase = trim($m[3]); |
|
| 216 | + } elseif (!empty(self::$phrases[$this->code])) { |
|
| 217 | + $this->reasonPhrase = self::$phrases[$this->code]; |
|
| 218 | + } |
|
| 219 | + $this->bodyEncoded = (bool)$bodyEncoded; |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | 222 | /** |
| 223 | 223 | * Parses the line from HTTP response filling $headers array |
@@ -229,51 +229,51 @@ discard block |
||
| 229 | 229 | * |
| 230 | 230 | * @param string Line from HTTP response |
| 231 | 231 | */ |
| 232 | - public function parseHeaderLine($headerLine) |
|
| 233 | - { |
|
| 234 | - $headerLine = trim($headerLine, "\r\n"); |
|
| 235 | - |
|
| 236 | - // empty string signals the end of headers, process the received ones |
|
| 237 | - if ('' == $headerLine) { |
|
| 238 | - if (!empty($this->headers['set-cookie'])) { |
|
| 239 | - $cookies = is_array($this->headers['set-cookie'])? |
|
| 240 | - $this->headers['set-cookie']: |
|
| 241 | - array($this->headers['set-cookie']); |
|
| 242 | - foreach ($cookies as $cookieString) { |
|
| 243 | - $this->parseCookie($cookieString); |
|
| 244 | - } |
|
| 245 | - unset($this->headers['set-cookie']); |
|
| 246 | - } |
|
| 247 | - foreach (array_keys($this->headers) as $k) { |
|
| 248 | - if (is_array($this->headers[$k])) { |
|
| 249 | - $this->headers[$k] = implode(', ', $this->headers[$k]); |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - // string of the form header-name: header value |
|
| 254 | - } elseif (preg_match('!^([^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+):(.+)$!', $headerLine, $m)) { |
|
| 255 | - $name = strtolower($m[1]); |
|
| 256 | - $value = trim($m[2]); |
|
| 257 | - if (empty($this->headers[$name])) { |
|
| 258 | - $this->headers[$name] = $value; |
|
| 259 | - } else { |
|
| 260 | - if (!is_array($this->headers[$name])) { |
|
| 261 | - $this->headers[$name] = array($this->headers[$name]); |
|
| 262 | - } |
|
| 263 | - $this->headers[$name][] = $value; |
|
| 264 | - } |
|
| 265 | - $this->lastHeader = $name; |
|
| 266 | - |
|
| 267 | - // continuation of a previous header |
|
| 268 | - } elseif (preg_match('!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) { |
|
| 269 | - if (!is_array($this->headers[$this->lastHeader])) { |
|
| 270 | - $this->headers[$this->lastHeader] .= ' ' . trim($m[1]); |
|
| 271 | - } else { |
|
| 272 | - $key = count($this->headers[$this->lastHeader]) - 1; |
|
| 273 | - $this->headers[$this->lastHeader][$key] .= ' ' . trim($m[1]); |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - } |
|
| 232 | + public function parseHeaderLine($headerLine) |
|
| 233 | + { |
|
| 234 | + $headerLine = trim($headerLine, "\r\n"); |
|
| 235 | + |
|
| 236 | + // empty string signals the end of headers, process the received ones |
|
| 237 | + if ('' == $headerLine) { |
|
| 238 | + if (!empty($this->headers['set-cookie'])) { |
|
| 239 | + $cookies = is_array($this->headers['set-cookie'])? |
|
| 240 | + $this->headers['set-cookie']: |
|
| 241 | + array($this->headers['set-cookie']); |
|
| 242 | + foreach ($cookies as $cookieString) { |
|
| 243 | + $this->parseCookie($cookieString); |
|
| 244 | + } |
|
| 245 | + unset($this->headers['set-cookie']); |
|
| 246 | + } |
|
| 247 | + foreach (array_keys($this->headers) as $k) { |
|
| 248 | + if (is_array($this->headers[$k])) { |
|
| 249 | + $this->headers[$k] = implode(', ', $this->headers[$k]); |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + // string of the form header-name: header value |
|
| 254 | + } elseif (preg_match('!^([^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+):(.+)$!', $headerLine, $m)) { |
|
| 255 | + $name = strtolower($m[1]); |
|
| 256 | + $value = trim($m[2]); |
|
| 257 | + if (empty($this->headers[$name])) { |
|
| 258 | + $this->headers[$name] = $value; |
|
| 259 | + } else { |
|
| 260 | + if (!is_array($this->headers[$name])) { |
|
| 261 | + $this->headers[$name] = array($this->headers[$name]); |
|
| 262 | + } |
|
| 263 | + $this->headers[$name][] = $value; |
|
| 264 | + } |
|
| 265 | + $this->lastHeader = $name; |
|
| 266 | + |
|
| 267 | + // continuation of a previous header |
|
| 268 | + } elseif (preg_match('!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) { |
|
| 269 | + if (!is_array($this->headers[$this->lastHeader])) { |
|
| 270 | + $this->headers[$this->lastHeader] .= ' ' . trim($m[1]); |
|
| 271 | + } else { |
|
| 272 | + $key = count($this->headers[$this->lastHeader]) - 1; |
|
| 273 | + $this->headers[$this->lastHeader][$key] .= ' ' . trim($m[1]); |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | 277 | |
| 278 | 278 | /** |
| 279 | 279 | * Parses a Set-Cookie header to fill $cookies array |
@@ -281,86 +281,86 @@ discard block |
||
| 281 | 281 | * @param string value of Set-Cookie header |
| 282 | 282 | * @link http://web.archive.org/web/20080331104521/http://cgi.netscape.com/newsref/std/cookie_spec.html |
| 283 | 283 | */ |
| 284 | - protected function parseCookie($cookieString) |
|
| 285 | - { |
|
| 286 | - $cookie = array( |
|
| 287 | - 'expires' => null, |
|
| 288 | - 'domain' => null, |
|
| 289 | - 'path' => null, |
|
| 290 | - 'secure' => false |
|
| 291 | - ); |
|
| 292 | - |
|
| 293 | - // Only a name=value pair |
|
| 294 | - if (!strpos($cookieString, ';')) { |
|
| 295 | - $pos = strpos($cookieString, '='); |
|
| 296 | - $cookie['name'] = trim(substr($cookieString, 0, $pos)); |
|
| 297 | - $cookie['value'] = trim(substr($cookieString, $pos + 1)); |
|
| 298 | - |
|
| 299 | - // Some optional parameters are supplied |
|
| 300 | - } else { |
|
| 301 | - $elements = explode(';', $cookieString); |
|
| 302 | - $pos = strpos($elements[0], '='); |
|
| 303 | - $cookie['name'] = trim(substr($elements[0], 0, $pos)); |
|
| 304 | - $cookie['value'] = trim(substr($elements[0], $pos + 1)); |
|
| 305 | - |
|
| 306 | - for ($i = 1; $i < count($elements); $i++) { |
|
| 307 | - if (false === strpos($elements[$i], '=')) { |
|
| 308 | - $elName = trim($elements[$i]); |
|
| 309 | - $elValue = null; |
|
| 310 | - } else { |
|
| 311 | - list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i])); |
|
| 312 | - } |
|
| 313 | - $elName = strtolower($elName); |
|
| 314 | - if ('secure' == $elName) { |
|
| 315 | - $cookie['secure'] = true; |
|
| 316 | - } elseif ('expires' == $elName) { |
|
| 317 | - $cookie['expires'] = str_replace('"', '', $elValue); |
|
| 318 | - } elseif ('path' == $elName || 'domain' == $elName) { |
|
| 319 | - $cookie[$elName] = urldecode($elValue); |
|
| 320 | - } else { |
|
| 321 | - $cookie[$elName] = $elValue; |
|
| 322 | - } |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - $this->cookies[] = $cookie; |
|
| 326 | - } |
|
| 284 | + protected function parseCookie($cookieString) |
|
| 285 | + { |
|
| 286 | + $cookie = array( |
|
| 287 | + 'expires' => null, |
|
| 288 | + 'domain' => null, |
|
| 289 | + 'path' => null, |
|
| 290 | + 'secure' => false |
|
| 291 | + ); |
|
| 292 | + |
|
| 293 | + // Only a name=value pair |
|
| 294 | + if (!strpos($cookieString, ';')) { |
|
| 295 | + $pos = strpos($cookieString, '='); |
|
| 296 | + $cookie['name'] = trim(substr($cookieString, 0, $pos)); |
|
| 297 | + $cookie['value'] = trim(substr($cookieString, $pos + 1)); |
|
| 298 | + |
|
| 299 | + // Some optional parameters are supplied |
|
| 300 | + } else { |
|
| 301 | + $elements = explode(';', $cookieString); |
|
| 302 | + $pos = strpos($elements[0], '='); |
|
| 303 | + $cookie['name'] = trim(substr($elements[0], 0, $pos)); |
|
| 304 | + $cookie['value'] = trim(substr($elements[0], $pos + 1)); |
|
| 305 | + |
|
| 306 | + for ($i = 1; $i < count($elements); $i++) { |
|
| 307 | + if (false === strpos($elements[$i], '=')) { |
|
| 308 | + $elName = trim($elements[$i]); |
|
| 309 | + $elValue = null; |
|
| 310 | + } else { |
|
| 311 | + list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i])); |
|
| 312 | + } |
|
| 313 | + $elName = strtolower($elName); |
|
| 314 | + if ('secure' == $elName) { |
|
| 315 | + $cookie['secure'] = true; |
|
| 316 | + } elseif ('expires' == $elName) { |
|
| 317 | + $cookie['expires'] = str_replace('"', '', $elValue); |
|
| 318 | + } elseif ('path' == $elName || 'domain' == $elName) { |
|
| 319 | + $cookie[$elName] = urldecode($elValue); |
|
| 320 | + } else { |
|
| 321 | + $cookie[$elName] = $elValue; |
|
| 322 | + } |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + $this->cookies[] = $cookie; |
|
| 326 | + } |
|
| 327 | 327 | |
| 328 | 328 | /** |
| 329 | 329 | * Appends a string to the response body |
| 330 | 330 | * @param string |
| 331 | 331 | */ |
| 332 | - public function appendBody($bodyChunk) |
|
| 333 | - { |
|
| 334 | - $this->body .= $bodyChunk; |
|
| 335 | - } |
|
| 332 | + public function appendBody($bodyChunk) |
|
| 333 | + { |
|
| 334 | + $this->body .= $bodyChunk; |
|
| 335 | + } |
|
| 336 | 336 | |
| 337 | 337 | /** |
| 338 | 338 | * Returns the status code |
| 339 | 339 | * @return integer |
| 340 | 340 | */ |
| 341 | - public function getStatus() |
|
| 342 | - { |
|
| 343 | - return $this->code; |
|
| 344 | - } |
|
| 341 | + public function getStatus() |
|
| 342 | + { |
|
| 343 | + return $this->code; |
|
| 344 | + } |
|
| 345 | 345 | |
| 346 | 346 | /** |
| 347 | 347 | * Returns the reason phrase |
| 348 | 348 | * @return string |
| 349 | 349 | */ |
| 350 | - public function getReasonPhrase() |
|
| 351 | - { |
|
| 352 | - return $this->reasonPhrase; |
|
| 353 | - } |
|
| 350 | + public function getReasonPhrase() |
|
| 351 | + { |
|
| 352 | + return $this->reasonPhrase; |
|
| 353 | + } |
|
| 354 | 354 | |
| 355 | 355 | /** |
| 356 | 356 | * Whether response is a redirect that can be automatically handled by HTTP_Request2 |
| 357 | 357 | * @return bool |
| 358 | 358 | */ |
| 359 | - public function isRedirect() |
|
| 360 | - { |
|
| 361 | - return in_array($this->code, array(300, 301, 302, 303, 307)) |
|
| 362 | - && isset($this->headers['location']); |
|
| 363 | - } |
|
| 359 | + public function isRedirect() |
|
| 360 | + { |
|
| 361 | + return in_array($this->code, array(300, 301, 302, 303, 307)) |
|
| 362 | + && isset($this->headers['location']); |
|
| 363 | + } |
|
| 364 | 364 | |
| 365 | 365 | /** |
| 366 | 366 | * Returns either the named header or all response headers |
@@ -370,25 +370,25 @@ discard block |
||
| 370 | 370 | * not present), array of all response headers if |
| 371 | 371 | * $headerName is null |
| 372 | 372 | */ |
| 373 | - public function getHeader($headerName = null) |
|
| 374 | - { |
|
| 375 | - if (null === $headerName) { |
|
| 376 | - return $this->headers; |
|
| 377 | - } else { |
|
| 378 | - $headerName = strtolower($headerName); |
|
| 379 | - return isset($this->headers[$headerName])? $this->headers[$headerName]: null; |
|
| 380 | - } |
|
| 381 | - } |
|
| 373 | + public function getHeader($headerName = null) |
|
| 374 | + { |
|
| 375 | + if (null === $headerName) { |
|
| 376 | + return $this->headers; |
|
| 377 | + } else { |
|
| 378 | + $headerName = strtolower($headerName); |
|
| 379 | + return isset($this->headers[$headerName])? $this->headers[$headerName]: null; |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | 383 | /** |
| 384 | 384 | * Returns cookies set in response |
| 385 | 385 | * |
| 386 | 386 | * @return array |
| 387 | 387 | */ |
| 388 | - public function getCookies() |
|
| 389 | - { |
|
| 390 | - return $this->cookies; |
|
| 391 | - } |
|
| 388 | + public function getCookies() |
|
| 389 | + { |
|
| 390 | + return $this->cookies; |
|
| 391 | + } |
|
| 392 | 392 | |
| 393 | 393 | /** |
| 394 | 394 | * Returns the body of the response |
@@ -396,49 +396,49 @@ discard block |
||
| 396 | 396 | * @return string |
| 397 | 397 | * @throws HTTP_Request2_Exception if body cannot be decoded |
| 398 | 398 | */ |
| 399 | - public function getBody() |
|
| 400 | - { |
|
| 401 | - if (!$this->bodyEncoded || |
|
| 402 | - !in_array(strtolower($this->getHeader('content-encoding')), array('gzip', 'deflate')) |
|
| 403 | - ) { |
|
| 404 | - return $this->body; |
|
| 405 | - |
|
| 406 | - } else { |
|
| 407 | - if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) { |
|
| 408 | - $oldEncoding = mb_internal_encoding(); |
|
| 409 | - mb_internal_encoding('iso-8859-1'); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - try { |
|
| 413 | - switch (strtolower($this->getHeader('content-encoding'))) { |
|
| 414 | - case 'gzip': |
|
| 415 | - $decoded = self::decodeGzip($this->body); |
|
| 416 | - break; |
|
| 417 | - case 'deflate': |
|
| 418 | - $decoded = self::decodeDeflate($this->body); |
|
| 419 | - } |
|
| 420 | - } catch (Exception $e) { |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - if (!empty($oldEncoding)) { |
|
| 424 | - mb_internal_encoding($oldEncoding); |
|
| 425 | - } |
|
| 426 | - if (!empty($e)) { |
|
| 427 | - throw $e; |
|
| 428 | - } |
|
| 429 | - return $decoded; |
|
| 430 | - } |
|
| 431 | - } |
|
| 399 | + public function getBody() |
|
| 400 | + { |
|
| 401 | + if (!$this->bodyEncoded || |
|
| 402 | + !in_array(strtolower($this->getHeader('content-encoding')), array('gzip', 'deflate')) |
|
| 403 | + ) { |
|
| 404 | + return $this->body; |
|
| 405 | + |
|
| 406 | + } else { |
|
| 407 | + if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) { |
|
| 408 | + $oldEncoding = mb_internal_encoding(); |
|
| 409 | + mb_internal_encoding('iso-8859-1'); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + try { |
|
| 413 | + switch (strtolower($this->getHeader('content-encoding'))) { |
|
| 414 | + case 'gzip': |
|
| 415 | + $decoded = self::decodeGzip($this->body); |
|
| 416 | + break; |
|
| 417 | + case 'deflate': |
|
| 418 | + $decoded = self::decodeDeflate($this->body); |
|
| 419 | + } |
|
| 420 | + } catch (Exception $e) { |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + if (!empty($oldEncoding)) { |
|
| 424 | + mb_internal_encoding($oldEncoding); |
|
| 425 | + } |
|
| 426 | + if (!empty($e)) { |
|
| 427 | + throw $e; |
|
| 428 | + } |
|
| 429 | + return $decoded; |
|
| 430 | + } |
|
| 431 | + } |
|
| 432 | 432 | |
| 433 | 433 | /** |
| 434 | 434 | * Get the HTTP version of the response |
| 435 | 435 | * |
| 436 | 436 | * @return string |
| 437 | 437 | */ |
| 438 | - public function getVersion() |
|
| 439 | - { |
|
| 440 | - return $this->version; |
|
| 441 | - } |
|
| 438 | + public function getVersion() |
|
| 439 | + { |
|
| 440 | + return $this->version; |
|
| 441 | + } |
|
| 442 | 442 | |
| 443 | 443 | /** |
| 444 | 444 | * Decodes the message-body encoded by gzip |
@@ -452,89 +452,89 @@ discard block |
||
| 452 | 452 | * @throws HTTP_Request2_Exception |
| 453 | 453 | * @link http://tools.ietf.org/html/rfc1952 |
| 454 | 454 | */ |
| 455 | - public static function decodeGzip($data) |
|
| 456 | - { |
|
| 457 | - $length = strlen($data); |
|
| 458 | - // If it doesn't look like gzip-encoded data, don't bother |
|
| 459 | - if (18 > $length || strcmp(substr($data, 0, 2), "\x1f\x8b")) { |
|
| 460 | - return $data; |
|
| 461 | - } |
|
| 462 | - if (!function_exists('gzinflate')) { |
|
| 463 | - throw new HTTP_Request2_Exception('Unable to decode body: gzip extension not available'); |
|
| 464 | - } |
|
| 465 | - $method = ord(substr($data, 2, 1)); |
|
| 466 | - if (8 != $method) { |
|
| 467 | - throw new HTTP_Request2_Exception('Error parsing gzip header: unknown compression method'); |
|
| 468 | - } |
|
| 469 | - $flags = ord(substr($data, 3, 1)); |
|
| 470 | - if ($flags & 224) { |
|
| 471 | - throw new HTTP_Request2_Exception('Error parsing gzip header: reserved bits are set'); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - // header is 10 bytes minimum. may be longer, though. |
|
| 475 | - $headerLength = 10; |
|
| 476 | - // extra fields, need to skip 'em |
|
| 477 | - if ($flags & 4) { |
|
| 478 | - if ($length - $headerLength - 2 < 8) { |
|
| 479 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 480 | - } |
|
| 481 | - $extraLength = unpack('v', substr($data, 10, 2)); |
|
| 482 | - if ($length - $headerLength - 2 - $extraLength[1] < 8) { |
|
| 483 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 484 | - } |
|
| 485 | - $headerLength += $extraLength[1] + 2; |
|
| 486 | - } |
|
| 487 | - // file name, need to skip that |
|
| 488 | - if ($flags & 8) { |
|
| 489 | - if ($length - $headerLength - 1 < 8) { |
|
| 490 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 491 | - } |
|
| 492 | - $filenameLength = strpos(substr($data, $headerLength), chr(0)); |
|
| 493 | - if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) { |
|
| 494 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 495 | - } |
|
| 496 | - $headerLength += $filenameLength + 1; |
|
| 497 | - } |
|
| 498 | - // comment, need to skip that also |
|
| 499 | - if ($flags & 16) { |
|
| 500 | - if ($length - $headerLength - 1 < 8) { |
|
| 501 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 502 | - } |
|
| 503 | - $commentLength = strpos(substr($data, $headerLength), chr(0)); |
|
| 504 | - if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) { |
|
| 505 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 506 | - } |
|
| 507 | - $headerLength += $commentLength + 1; |
|
| 508 | - } |
|
| 509 | - // have a CRC for header. let's check |
|
| 510 | - if ($flags & 2) { |
|
| 511 | - if ($length - $headerLength - 2 < 8) { |
|
| 512 | - throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 513 | - } |
|
| 514 | - $crcReal = 0xffff & crc32(substr($data, 0, $headerLength)); |
|
| 515 | - $crcStored = unpack('v', substr($data, $headerLength, 2)); |
|
| 516 | - if ($crcReal != $crcStored[1]) { |
|
| 517 | - throw new HTTP_Request2_Exception('Header CRC check failed'); |
|
| 518 | - } |
|
| 519 | - $headerLength += 2; |
|
| 520 | - } |
|
| 521 | - // unpacked data CRC and size at the end of encoded data |
|
| 522 | - $tmp = unpack('V2', substr($data, -8)); |
|
| 523 | - $dataCrc = $tmp[1]; |
|
| 524 | - $dataSize = $tmp[2]; |
|
| 525 | - |
|
| 526 | - // finally, call the gzinflate() function |
|
| 527 | - // don't pass $dataSize to gzinflate, see bugs #13135, #14370 |
|
| 528 | - $unpacked = gzinflate(substr($data, $headerLength, -8)); |
|
| 529 | - if (false === $unpacked) { |
|
| 530 | - throw new HTTP_Request2_Exception('gzinflate() call failed'); |
|
| 531 | - } elseif ($dataSize != strlen($unpacked)) { |
|
| 532 | - throw new HTTP_Request2_Exception('Data size check failed'); |
|
| 533 | - } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) { |
|
| 534 | - throw new HTTP_Request2_Exception('Data CRC check failed'); |
|
| 535 | - } |
|
| 536 | - return $unpacked; |
|
| 537 | - } |
|
| 455 | + public static function decodeGzip($data) |
|
| 456 | + { |
|
| 457 | + $length = strlen($data); |
|
| 458 | + // If it doesn't look like gzip-encoded data, don't bother |
|
| 459 | + if (18 > $length || strcmp(substr($data, 0, 2), "\x1f\x8b")) { |
|
| 460 | + return $data; |
|
| 461 | + } |
|
| 462 | + if (!function_exists('gzinflate')) { |
|
| 463 | + throw new HTTP_Request2_Exception('Unable to decode body: gzip extension not available'); |
|
| 464 | + } |
|
| 465 | + $method = ord(substr($data, 2, 1)); |
|
| 466 | + if (8 != $method) { |
|
| 467 | + throw new HTTP_Request2_Exception('Error parsing gzip header: unknown compression method'); |
|
| 468 | + } |
|
| 469 | + $flags = ord(substr($data, 3, 1)); |
|
| 470 | + if ($flags & 224) { |
|
| 471 | + throw new HTTP_Request2_Exception('Error parsing gzip header: reserved bits are set'); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + // header is 10 bytes minimum. may be longer, though. |
|
| 475 | + $headerLength = 10; |
|
| 476 | + // extra fields, need to skip 'em |
|
| 477 | + if ($flags & 4) { |
|
| 478 | + if ($length - $headerLength - 2 < 8) { |
|
| 479 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 480 | + } |
|
| 481 | + $extraLength = unpack('v', substr($data, 10, 2)); |
|
| 482 | + if ($length - $headerLength - 2 - $extraLength[1] < 8) { |
|
| 483 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 484 | + } |
|
| 485 | + $headerLength += $extraLength[1] + 2; |
|
| 486 | + } |
|
| 487 | + // file name, need to skip that |
|
| 488 | + if ($flags & 8) { |
|
| 489 | + if ($length - $headerLength - 1 < 8) { |
|
| 490 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 491 | + } |
|
| 492 | + $filenameLength = strpos(substr($data, $headerLength), chr(0)); |
|
| 493 | + if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) { |
|
| 494 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 495 | + } |
|
| 496 | + $headerLength += $filenameLength + 1; |
|
| 497 | + } |
|
| 498 | + // comment, need to skip that also |
|
| 499 | + if ($flags & 16) { |
|
| 500 | + if ($length - $headerLength - 1 < 8) { |
|
| 501 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 502 | + } |
|
| 503 | + $commentLength = strpos(substr($data, $headerLength), chr(0)); |
|
| 504 | + if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) { |
|
| 505 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 506 | + } |
|
| 507 | + $headerLength += $commentLength + 1; |
|
| 508 | + } |
|
| 509 | + // have a CRC for header. let's check |
|
| 510 | + if ($flags & 2) { |
|
| 511 | + if ($length - $headerLength - 2 < 8) { |
|
| 512 | + throw new HTTP_Request2_Exception('Error parsing gzip header: data too short'); |
|
| 513 | + } |
|
| 514 | + $crcReal = 0xffff & crc32(substr($data, 0, $headerLength)); |
|
| 515 | + $crcStored = unpack('v', substr($data, $headerLength, 2)); |
|
| 516 | + if ($crcReal != $crcStored[1]) { |
|
| 517 | + throw new HTTP_Request2_Exception('Header CRC check failed'); |
|
| 518 | + } |
|
| 519 | + $headerLength += 2; |
|
| 520 | + } |
|
| 521 | + // unpacked data CRC and size at the end of encoded data |
|
| 522 | + $tmp = unpack('V2', substr($data, -8)); |
|
| 523 | + $dataCrc = $tmp[1]; |
|
| 524 | + $dataSize = $tmp[2]; |
|
| 525 | + |
|
| 526 | + // finally, call the gzinflate() function |
|
| 527 | + // don't pass $dataSize to gzinflate, see bugs #13135, #14370 |
|
| 528 | + $unpacked = gzinflate(substr($data, $headerLength, -8)); |
|
| 529 | + if (false === $unpacked) { |
|
| 530 | + throw new HTTP_Request2_Exception('gzinflate() call failed'); |
|
| 531 | + } elseif ($dataSize != strlen($unpacked)) { |
|
| 532 | + throw new HTTP_Request2_Exception('Data size check failed'); |
|
| 533 | + } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) { |
|
| 534 | + throw new HTTP_Request2_Exception('Data CRC check failed'); |
|
| 535 | + } |
|
| 536 | + return $unpacked; |
|
| 537 | + } |
|
| 538 | 538 | |
| 539 | 539 | /** |
| 540 | 540 | * Decodes the message-body encoded by deflate |
@@ -543,17 +543,17 @@ discard block |
||
| 543 | 543 | * @return string decoded data |
| 544 | 544 | * @throws HTTP_Request2_Exception |
| 545 | 545 | */ |
| 546 | - public static function decodeDeflate($data) |
|
| 547 | - { |
|
| 548 | - if (!function_exists('gzuncompress')) { |
|
| 549 | - throw new HTTP_Request2_Exception('Unable to decode body: gzip extension not available'); |
|
| 550 | - } |
|
| 551 | - // RFC 2616 defines 'deflate' encoding as zlib format from RFC 1950, |
|
| 552 | - // while many applications send raw deflate stream from RFC 1951. |
|
| 553 | - // We should check for presence of zlib header and use gzuncompress() or |
|
| 554 | - // gzinflate() as needed. See bug #15305 |
|
| 555 | - $header = unpack('n', substr($data, 0, 2)); |
|
| 556 | - return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data); |
|
| 557 | - } |
|
| 546 | + public static function decodeDeflate($data) |
|
| 547 | + { |
|
| 548 | + if (!function_exists('gzuncompress')) { |
|
| 549 | + throw new HTTP_Request2_Exception('Unable to decode body: gzip extension not available'); |
|
| 550 | + } |
|
| 551 | + // RFC 2616 defines 'deflate' encoding as zlib format from RFC 1950, |
|
| 552 | + // while many applications send raw deflate stream from RFC 1951. |
|
| 553 | + // We should check for presence of zlib header and use gzuncompress() or |
|
| 554 | + // gzinflate() as needed. See bug #15305 |
|
| 555 | + $header = unpack('n', substr($data, 0, 2)); |
|
| 556 | + return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data); |
|
| 557 | + } |
|
| 558 | 558 | } |
| 559 | 559 | ?> |
| 560 | 560 | \ No newline at end of file |
@@ -158,7 +158,7 @@ discard block |
||
| 158 | 158 | // the request |
| 159 | 159 | 300 => 'Multiple Choices', |
| 160 | 160 | 301 => 'Moved Permanently', |
| 161 | - 302 => 'Found', // 1.1 |
|
| 161 | + 302 => 'Found', // 1.1 |
|
| 162 | 162 | 303 => 'See Other', |
| 163 | 163 | 304 => 'Not Modified', |
| 164 | 164 | 305 => 'Use Proxy', |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | } elseif (!empty(self::$phrases[$this->code])) { |
| 217 | 217 | $this->reasonPhrase = self::$phrases[$this->code]; |
| 218 | 218 | } |
| 219 | - $this->bodyEncoded = (bool)$bodyEncoded; |
|
| 219 | + $this->bodyEncoded = (bool) $bodyEncoded; |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | /** |
@@ -236,9 +236,8 @@ discard block |
||
| 236 | 236 | // empty string signals the end of headers, process the received ones |
| 237 | 237 | if ('' == $headerLine) { |
| 238 | 238 | if (!empty($this->headers['set-cookie'])) { |
| 239 | - $cookies = is_array($this->headers['set-cookie'])? |
|
| 240 | - $this->headers['set-cookie']: |
|
| 241 | - array($this->headers['set-cookie']); |
|
| 239 | + $cookies = is_array($this->headers['set-cookie']) ? |
|
| 240 | + $this->headers['set-cookie'] : array($this->headers['set-cookie']); |
|
| 242 | 241 | foreach ($cookies as $cookieString) { |
| 243 | 242 | $this->parseCookie($cookieString); |
| 244 | 243 | } |
@@ -376,7 +375,7 @@ discard block |
||
| 376 | 375 | return $this->headers; |
| 377 | 376 | } else { |
| 378 | 377 | $headerName = strtolower($headerName); |
| 379 | - return isset($this->headers[$headerName])? $this->headers[$headerName]: null; |
|
| 378 | + return isset($this->headers[$headerName]) ? $this->headers[$headerName] : null; |
|
| 380 | 379 | } |
| 381 | 380 | } |
| 382 | 381 | |
@@ -553,7 +552,7 @@ discard block |
||
| 553 | 552 | // We should check for presence of zlib header and use gzuncompress() or |
| 554 | 553 | // gzinflate() as needed. See bug #15305 |
| 555 | 554 | $header = unpack('n', substr($data, 0, 2)); |
| 556 | - return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data); |
|
| 555 | + return (0 == $header[1] % 31) ? gzuncompress($data) : gzinflate($data); |
|
| 557 | 556 | } |
| 558 | 557 | } |
| 559 | 558 | ?> |
| 560 | 559 | \ No newline at end of file |
@@ -34,495 +34,495 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class Net_Socket extends PEAR { |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Socket file pointer. |
|
| 39 | - * @var resource $fp |
|
| 40 | - */ |
|
| 41 | - var $fp = null; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Whether the socket is blocking. Defaults to true. |
|
| 45 | - * @var boolean $blocking |
|
| 46 | - */ |
|
| 47 | - var $blocking = true; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Whether the socket is persistent. Defaults to false. |
|
| 51 | - * @var boolean $persistent |
|
| 52 | - */ |
|
| 53 | - var $persistent = false; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The IP address to connect to. |
|
| 57 | - * @var string $addr |
|
| 58 | - */ |
|
| 59 | - var $addr = ''; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * The port number to connect to. |
|
| 63 | - * @var integer $port |
|
| 64 | - */ |
|
| 65 | - var $port = 0; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Number of seconds to wait on socket connections before assuming |
|
| 69 | - * there's no more data. Defaults to no timeout. |
|
| 70 | - * @var integer $timeout |
|
| 71 | - */ |
|
| 72 | - var $timeout = false; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Number of bytes to read at a time in readLine() and |
|
| 76 | - * readAll(). Defaults to 2048. |
|
| 77 | - * @var integer $lineLength |
|
| 78 | - */ |
|
| 79 | - var $lineLength = 2048; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Connect to the specified port. If called when the socket is |
|
| 83 | - * already connected, it disconnects and connects again. |
|
| 84 | - * |
|
| 85 | - * @param string $addr IP address or host name. |
|
| 86 | - * @param integer $port TCP port number. |
|
| 87 | - * @param boolean $persistent (optional) Whether the connection is |
|
| 88 | - * persistent (kept open between requests |
|
| 89 | - * by the web server). |
|
| 90 | - * @param integer $timeout (optional) How long to wait for data. |
|
| 91 | - * @param array $options See options for stream_context_create. |
|
| 92 | - * |
|
| 93 | - * @access public |
|
| 94 | - * |
|
| 95 | - * @return boolean | PEAR_Error True on success or a PEAR_Error on failure. |
|
| 96 | - */ |
|
| 97 | - function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null) |
|
| 98 | - { |
|
| 99 | - if (is_resource($this->fp)) { |
|
| 100 | - @fclose($this->fp); |
|
| 101 | - $this->fp = null; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - if (!$addr) { |
|
| 105 | - return $this->raiseError('$addr cannot be empty'); |
|
| 106 | - } elseif (strspn($addr, '.0123456789') == strlen($addr) || |
|
| 107 | - strstr($addr, '/') !== false) { |
|
| 108 | - $this->addr = $addr; |
|
| 109 | - } else { |
|
| 110 | - $this->addr = @gethostbyname($addr); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $this->port = $port % 65536; |
|
| 114 | - |
|
| 115 | - if ($persistent !== null) { |
|
| 116 | - $this->persistent = $persistent; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - if ($timeout !== null) { |
|
| 120 | - $this->timeout = $timeout; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen'; |
|
| 124 | - $errno = 0; |
|
| 125 | - $errstr = ''; |
|
| 126 | - if ($options && function_exists('stream_context_create')) { |
|
| 127 | - if ($this->timeout) { |
|
| 128 | - $timeout = $this->timeout; |
|
| 129 | - } else { |
|
| 130 | - $timeout = 0; |
|
| 131 | - } |
|
| 132 | - $context = stream_context_create($options); |
|
| 133 | - $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context); |
|
| 134 | - } else { |
|
| 135 | - if ($this->timeout) { |
|
| 136 | - $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout); |
|
| 137 | - } else { |
|
| 138 | - $fp = @$openfunc($this->addr, $this->port, $errno, $errstr); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - if (!$fp) { |
|
| 143 | - return $this->raiseError($errstr, $errno); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - $this->fp = $fp; |
|
| 147 | - |
|
| 148 | - return $this->setBlocking($this->blocking); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Disconnects from the peer, closes the socket. |
|
| 153 | - * |
|
| 154 | - * @access public |
|
| 155 | - * @return mixed true on success or an error object otherwise |
|
| 156 | - */ |
|
| 157 | - function disconnect() |
|
| 158 | - { |
|
| 159 | - if (!is_resource($this->fp)) { |
|
| 160 | - return $this->raiseError('not connected'); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - @fclose($this->fp); |
|
| 164 | - $this->fp = null; |
|
| 165 | - return true; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Find out if the socket is in blocking mode. |
|
| 170 | - * |
|
| 171 | - * @access public |
|
| 172 | - * @return boolean The current blocking mode. |
|
| 173 | - */ |
|
| 174 | - function isBlocking() |
|
| 175 | - { |
|
| 176 | - return $this->blocking; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Sets whether the socket connection should be blocking or |
|
| 181 | - * not. A read call to a non-blocking socket will return immediately |
|
| 182 | - * if there is no data available, whereas it will block until there |
|
| 183 | - * is data for blocking sockets. |
|
| 184 | - * |
|
| 185 | - * @param boolean $mode True for blocking sockets, false for nonblocking. |
|
| 186 | - * @access public |
|
| 187 | - * @return mixed true on success or an error object otherwise |
|
| 188 | - */ |
|
| 189 | - function setBlocking($mode) |
|
| 190 | - { |
|
| 191 | - if (!is_resource($this->fp)) { |
|
| 192 | - return $this->raiseError('not connected'); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - $this->blocking = $mode; |
|
| 196 | - socket_set_blocking($this->fp, $this->blocking); |
|
| 197 | - return true; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Sets the timeout value on socket descriptor, |
|
| 202 | - * expressed in the sum of seconds and microseconds |
|
| 203 | - * |
|
| 204 | - * @param integer $seconds Seconds. |
|
| 205 | - * @param integer $microseconds Microseconds. |
|
| 206 | - * @access public |
|
| 207 | - * @return mixed true on success or an error object otherwise |
|
| 208 | - */ |
|
| 209 | - function setTimeout($seconds, $microseconds) |
|
| 210 | - { |
|
| 211 | - if (!is_resource($this->fp)) { |
|
| 212 | - return $this->raiseError('not connected'); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return socket_set_timeout($this->fp, $seconds, $microseconds); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Returns information about an existing socket resource. |
|
| 220 | - * Currently returns four entries in the result array: |
|
| 221 | - * |
|
| 222 | - * <p> |
|
| 223 | - * timed_out (bool) - The socket timed out waiting for data<br> |
|
| 224 | - * blocked (bool) - The socket was blocked<br> |
|
| 225 | - * eof (bool) - Indicates EOF event<br> |
|
| 226 | - * unread_bytes (int) - Number of bytes left in the socket buffer<br> |
|
| 227 | - * </p> |
|
| 228 | - * |
|
| 229 | - * @access public |
|
| 230 | - * @return mixed Array containing information about existing socket resource or an error object otherwise |
|
| 231 | - */ |
|
| 232 | - function getStatus() |
|
| 233 | - { |
|
| 234 | - if (!is_resource($this->fp)) { |
|
| 235 | - return $this->raiseError('not connected'); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return socket_get_status($this->fp); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Get a specified line of data |
|
| 243 | - * |
|
| 244 | - * @access public |
|
| 245 | - * @return $size bytes of data from the socket, or a PEAR_Error if |
|
| 246 | - * not connected. |
|
| 247 | - */ |
|
| 248 | - function gets($size) |
|
| 249 | - { |
|
| 250 | - if (!is_resource($this->fp)) { |
|
| 251 | - return $this->raiseError('not connected'); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return @fgets($this->fp, $size); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Read a specified amount of data. This is guaranteed to return, |
|
| 259 | - * and has the added benefit of getting everything in one fread() |
|
| 260 | - * chunk; if you know the size of the data you're getting |
|
| 261 | - * beforehand, this is definitely the way to go. |
|
| 262 | - * |
|
| 263 | - * @param integer $size The number of bytes to read from the socket. |
|
| 264 | - * @access public |
|
| 265 | - * @return $size bytes of data from the socket, or a PEAR_Error if |
|
| 266 | - * not connected. |
|
| 267 | - */ |
|
| 268 | - function read($size) |
|
| 269 | - { |
|
| 270 | - if (!is_resource($this->fp)) { |
|
| 271 | - return $this->raiseError('not connected'); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - return @fread($this->fp, $size); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Write a specified amount of data. |
|
| 279 | - * |
|
| 280 | - * @param string $data Data to write. |
|
| 281 | - * @param integer $blocksize Amount of data to write at once. |
|
| 282 | - * NULL means all at once. |
|
| 283 | - * |
|
| 284 | - * @access public |
|
| 285 | - * @return mixed true on success or an error object otherwise |
|
| 286 | - */ |
|
| 287 | - function write($data, $blocksize = null) |
|
| 288 | - { |
|
| 289 | - if (!is_resource($this->fp)) { |
|
| 290 | - return $this->raiseError('not connected'); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - if (is_null($blocksize) && !OS_WINDOWS) { |
|
| 294 | - return fwrite($this->fp, $data); |
|
| 295 | - } else { |
|
| 296 | - if (is_null($blocksize)) { |
|
| 297 | - $blocksize = 1024; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - $pos = 0; |
|
| 301 | - $size = strlen($data); |
|
| 302 | - while ($pos < $size) { |
|
| 303 | - $written = @fwrite($this->fp, substr($data, $pos, $blocksize)); |
|
| 304 | - if ($written === false) { |
|
| 305 | - return false; |
|
| 306 | - } |
|
| 307 | - $pos += $written; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - return $pos; |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Write a line of data to the socket, followed by a trailing "\r\n". |
|
| 316 | - * |
|
| 317 | - * @access public |
|
| 318 | - * @return mixed fputs result, or an error |
|
| 319 | - */ |
|
| 320 | - function writeLine($data) |
|
| 321 | - { |
|
| 322 | - if (!is_resource($this->fp)) { |
|
| 323 | - return $this->raiseError('not connected'); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - return fwrite($this->fp, $data . "\r\n"); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Tests for end-of-file on a socket descriptor. |
|
| 331 | - * |
|
| 332 | - * @access public |
|
| 333 | - * @return bool |
|
| 334 | - */ |
|
| 335 | - function eof() |
|
| 336 | - { |
|
| 337 | - return (is_resource($this->fp) && feof($this->fp)); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Reads a byte of data |
|
| 342 | - * |
|
| 343 | - * @access public |
|
| 344 | - * @return 1 byte of data from the socket, or a PEAR_Error if |
|
| 345 | - * not connected. |
|
| 346 | - */ |
|
| 347 | - function readByte() |
|
| 348 | - { |
|
| 349 | - if (!is_resource($this->fp)) { |
|
| 350 | - return $this->raiseError('not connected'); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - return ord(@fread($this->fp, 1)); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Reads a word of data |
|
| 358 | - * |
|
| 359 | - * @access public |
|
| 360 | - * @return 1 word of data from the socket, or a PEAR_Error if |
|
| 361 | - * not connected. |
|
| 362 | - */ |
|
| 363 | - function readWord() |
|
| 364 | - { |
|
| 365 | - if (!is_resource($this->fp)) { |
|
| 366 | - return $this->raiseError('not connected'); |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - $buf = @fread($this->fp, 2); |
|
| 370 | - return (ord($buf[0]) + (ord($buf[1]) << 8)); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Reads an int of data |
|
| 375 | - * |
|
| 376 | - * @access public |
|
| 377 | - * @return integer 1 int of data from the socket, or a PEAR_Error if |
|
| 378 | - * not connected. |
|
| 379 | - */ |
|
| 380 | - function readInt() |
|
| 381 | - { |
|
| 382 | - if (!is_resource($this->fp)) { |
|
| 383 | - return $this->raiseError('not connected'); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - $buf = @fread($this->fp, 4); |
|
| 387 | - return (ord($buf[0]) + (ord($buf[1]) << 8) + |
|
| 388 | - (ord($buf[2]) << 16) + (ord($buf[3]) << 24)); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Reads a zero-terminated string of data |
|
| 393 | - * |
|
| 394 | - * @access public |
|
| 395 | - * @return string, or a PEAR_Error if |
|
| 396 | - * not connected. |
|
| 397 | - */ |
|
| 398 | - function readString() |
|
| 399 | - { |
|
| 400 | - if (!is_resource($this->fp)) { |
|
| 401 | - return $this->raiseError('not connected'); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - $string = ''; |
|
| 405 | - while (($char = @fread($this->fp, 1)) != "\x00") { |
|
| 406 | - $string .= $char; |
|
| 407 | - } |
|
| 408 | - return $string; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * Reads an IP Address and returns it in a dot formated string |
|
| 413 | - * |
|
| 414 | - * @access public |
|
| 415 | - * @return Dot formated string, or a PEAR_Error if |
|
| 416 | - * not connected. |
|
| 417 | - */ |
|
| 418 | - function readIPAddress() |
|
| 419 | - { |
|
| 420 | - if (!is_resource($this->fp)) { |
|
| 421 | - return $this->raiseError('not connected'); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - $buf = @fread($this->fp, 4); |
|
| 425 | - return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]), |
|
| 426 | - ord($buf[2]), ord($buf[3])); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Read until either the end of the socket or a newline, whichever |
|
| 431 | - * comes first. Strips the trailing newline from the returned data. |
|
| 432 | - * |
|
| 433 | - * @access public |
|
| 434 | - * @return All available data up to a newline, without that |
|
| 435 | - * newline, or until the end of the socket, or a PEAR_Error if |
|
| 436 | - * not connected. |
|
| 437 | - */ |
|
| 438 | - function readLine() |
|
| 439 | - { |
|
| 440 | - if (!is_resource($this->fp)) { |
|
| 441 | - return $this->raiseError('not connected'); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - $line = ''; |
|
| 445 | - $timeout = time() + $this->timeout; |
|
| 446 | - while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { |
|
| 447 | - $line .= @fgets($this->fp, $this->lineLength); |
|
| 448 | - if (substr($line, -1) == "\n") { |
|
| 449 | - return rtrim($line, "\r\n"); |
|
| 450 | - } |
|
| 451 | - } |
|
| 452 | - return $line; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Read until the socket closes, or until there is no more data in |
|
| 457 | - * the inner PHP buffer. If the inner buffer is empty, in blocking |
|
| 458 | - * mode we wait for at least 1 byte of data. Therefore, in |
|
| 459 | - * blocking mode, if there is no data at all to be read, this |
|
| 460 | - * function will never exit (unless the socket is closed on the |
|
| 461 | - * remote end). |
|
| 462 | - * |
|
| 463 | - * @access public |
|
| 464 | - * |
|
| 465 | - * @return string All data until the socket closes, or a PEAR_Error if |
|
| 466 | - * not connected. |
|
| 467 | - */ |
|
| 468 | - function readAll() |
|
| 469 | - { |
|
| 470 | - if (!is_resource($this->fp)) { |
|
| 471 | - return $this->raiseError('not connected'); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - $data = ''; |
|
| 475 | - while (!feof($this->fp)) { |
|
| 476 | - $data .= @fread($this->fp, $this->lineLength); |
|
| 477 | - } |
|
| 478 | - return $data; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - /** |
|
| 482 | - * Runs the equivalent of the select() system call on the socket |
|
| 483 | - * with a timeout specified by tv_sec and tv_usec. |
|
| 484 | - * |
|
| 485 | - * @param integer $state Which of read/write/error to check for. |
|
| 486 | - * @param integer $tv_sec Number of seconds for timeout. |
|
| 487 | - * @param integer $tv_usec Number of microseconds for timeout. |
|
| 488 | - * |
|
| 489 | - * @access public |
|
| 490 | - * @return False if select fails, integer describing which of read/write/error |
|
| 491 | - * are ready, or PEAR_Error if not connected. |
|
| 492 | - */ |
|
| 493 | - function select($state, $tv_sec, $tv_usec = 0) |
|
| 494 | - { |
|
| 495 | - if (!is_resource($this->fp)) { |
|
| 496 | - return $this->raiseError('not connected'); |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - $read = null; |
|
| 500 | - $write = null; |
|
| 501 | - $except = null; |
|
| 502 | - if ($state & NET_SOCKET_READ) { |
|
| 503 | - $read[] = $this->fp; |
|
| 504 | - } |
|
| 505 | - if ($state & NET_SOCKET_WRITE) { |
|
| 506 | - $write[] = $this->fp; |
|
| 507 | - } |
|
| 508 | - if ($state & NET_SOCKET_ERROR) { |
|
| 509 | - $except[] = $this->fp; |
|
| 510 | - } |
|
| 511 | - if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) { |
|
| 512 | - return false; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - $result = 0; |
|
| 516 | - if (count($read)) { |
|
| 517 | - $result |= NET_SOCKET_READ; |
|
| 518 | - } |
|
| 519 | - if (count($write)) { |
|
| 520 | - $result |= NET_SOCKET_WRITE; |
|
| 521 | - } |
|
| 522 | - if (count($except)) { |
|
| 523 | - $result |= NET_SOCKET_ERROR; |
|
| 524 | - } |
|
| 525 | - return $result; |
|
| 526 | - } |
|
| 37 | + /** |
|
| 38 | + * Socket file pointer. |
|
| 39 | + * @var resource $fp |
|
| 40 | + */ |
|
| 41 | + var $fp = null; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Whether the socket is blocking. Defaults to true. |
|
| 45 | + * @var boolean $blocking |
|
| 46 | + */ |
|
| 47 | + var $blocking = true; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Whether the socket is persistent. Defaults to false. |
|
| 51 | + * @var boolean $persistent |
|
| 52 | + */ |
|
| 53 | + var $persistent = false; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The IP address to connect to. |
|
| 57 | + * @var string $addr |
|
| 58 | + */ |
|
| 59 | + var $addr = ''; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * The port number to connect to. |
|
| 63 | + * @var integer $port |
|
| 64 | + */ |
|
| 65 | + var $port = 0; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Number of seconds to wait on socket connections before assuming |
|
| 69 | + * there's no more data. Defaults to no timeout. |
|
| 70 | + * @var integer $timeout |
|
| 71 | + */ |
|
| 72 | + var $timeout = false; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Number of bytes to read at a time in readLine() and |
|
| 76 | + * readAll(). Defaults to 2048. |
|
| 77 | + * @var integer $lineLength |
|
| 78 | + */ |
|
| 79 | + var $lineLength = 2048; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Connect to the specified port. If called when the socket is |
|
| 83 | + * already connected, it disconnects and connects again. |
|
| 84 | + * |
|
| 85 | + * @param string $addr IP address or host name. |
|
| 86 | + * @param integer $port TCP port number. |
|
| 87 | + * @param boolean $persistent (optional) Whether the connection is |
|
| 88 | + * persistent (kept open between requests |
|
| 89 | + * by the web server). |
|
| 90 | + * @param integer $timeout (optional) How long to wait for data. |
|
| 91 | + * @param array $options See options for stream_context_create. |
|
| 92 | + * |
|
| 93 | + * @access public |
|
| 94 | + * |
|
| 95 | + * @return boolean | PEAR_Error True on success or a PEAR_Error on failure. |
|
| 96 | + */ |
|
| 97 | + function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null) |
|
| 98 | + { |
|
| 99 | + if (is_resource($this->fp)) { |
|
| 100 | + @fclose($this->fp); |
|
| 101 | + $this->fp = null; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + if (!$addr) { |
|
| 105 | + return $this->raiseError('$addr cannot be empty'); |
|
| 106 | + } elseif (strspn($addr, '.0123456789') == strlen($addr) || |
|
| 107 | + strstr($addr, '/') !== false) { |
|
| 108 | + $this->addr = $addr; |
|
| 109 | + } else { |
|
| 110 | + $this->addr = @gethostbyname($addr); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $this->port = $port % 65536; |
|
| 114 | + |
|
| 115 | + if ($persistent !== null) { |
|
| 116 | + $this->persistent = $persistent; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + if ($timeout !== null) { |
|
| 120 | + $this->timeout = $timeout; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen'; |
|
| 124 | + $errno = 0; |
|
| 125 | + $errstr = ''; |
|
| 126 | + if ($options && function_exists('stream_context_create')) { |
|
| 127 | + if ($this->timeout) { |
|
| 128 | + $timeout = $this->timeout; |
|
| 129 | + } else { |
|
| 130 | + $timeout = 0; |
|
| 131 | + } |
|
| 132 | + $context = stream_context_create($options); |
|
| 133 | + $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context); |
|
| 134 | + } else { |
|
| 135 | + if ($this->timeout) { |
|
| 136 | + $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout); |
|
| 137 | + } else { |
|
| 138 | + $fp = @$openfunc($this->addr, $this->port, $errno, $errstr); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + if (!$fp) { |
|
| 143 | + return $this->raiseError($errstr, $errno); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + $this->fp = $fp; |
|
| 147 | + |
|
| 148 | + return $this->setBlocking($this->blocking); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Disconnects from the peer, closes the socket. |
|
| 153 | + * |
|
| 154 | + * @access public |
|
| 155 | + * @return mixed true on success or an error object otherwise |
|
| 156 | + */ |
|
| 157 | + function disconnect() |
|
| 158 | + { |
|
| 159 | + if (!is_resource($this->fp)) { |
|
| 160 | + return $this->raiseError('not connected'); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + @fclose($this->fp); |
|
| 164 | + $this->fp = null; |
|
| 165 | + return true; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Find out if the socket is in blocking mode. |
|
| 170 | + * |
|
| 171 | + * @access public |
|
| 172 | + * @return boolean The current blocking mode. |
|
| 173 | + */ |
|
| 174 | + function isBlocking() |
|
| 175 | + { |
|
| 176 | + return $this->blocking; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Sets whether the socket connection should be blocking or |
|
| 181 | + * not. A read call to a non-blocking socket will return immediately |
|
| 182 | + * if there is no data available, whereas it will block until there |
|
| 183 | + * is data for blocking sockets. |
|
| 184 | + * |
|
| 185 | + * @param boolean $mode True for blocking sockets, false for nonblocking. |
|
| 186 | + * @access public |
|
| 187 | + * @return mixed true on success or an error object otherwise |
|
| 188 | + */ |
|
| 189 | + function setBlocking($mode) |
|
| 190 | + { |
|
| 191 | + if (!is_resource($this->fp)) { |
|
| 192 | + return $this->raiseError('not connected'); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + $this->blocking = $mode; |
|
| 196 | + socket_set_blocking($this->fp, $this->blocking); |
|
| 197 | + return true; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Sets the timeout value on socket descriptor, |
|
| 202 | + * expressed in the sum of seconds and microseconds |
|
| 203 | + * |
|
| 204 | + * @param integer $seconds Seconds. |
|
| 205 | + * @param integer $microseconds Microseconds. |
|
| 206 | + * @access public |
|
| 207 | + * @return mixed true on success or an error object otherwise |
|
| 208 | + */ |
|
| 209 | + function setTimeout($seconds, $microseconds) |
|
| 210 | + { |
|
| 211 | + if (!is_resource($this->fp)) { |
|
| 212 | + return $this->raiseError('not connected'); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return socket_set_timeout($this->fp, $seconds, $microseconds); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Returns information about an existing socket resource. |
|
| 220 | + * Currently returns four entries in the result array: |
|
| 221 | + * |
|
| 222 | + * <p> |
|
| 223 | + * timed_out (bool) - The socket timed out waiting for data<br> |
|
| 224 | + * blocked (bool) - The socket was blocked<br> |
|
| 225 | + * eof (bool) - Indicates EOF event<br> |
|
| 226 | + * unread_bytes (int) - Number of bytes left in the socket buffer<br> |
|
| 227 | + * </p> |
|
| 228 | + * |
|
| 229 | + * @access public |
|
| 230 | + * @return mixed Array containing information about existing socket resource or an error object otherwise |
|
| 231 | + */ |
|
| 232 | + function getStatus() |
|
| 233 | + { |
|
| 234 | + if (!is_resource($this->fp)) { |
|
| 235 | + return $this->raiseError('not connected'); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + return socket_get_status($this->fp); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Get a specified line of data |
|
| 243 | + * |
|
| 244 | + * @access public |
|
| 245 | + * @return $size bytes of data from the socket, or a PEAR_Error if |
|
| 246 | + * not connected. |
|
| 247 | + */ |
|
| 248 | + function gets($size) |
|
| 249 | + { |
|
| 250 | + if (!is_resource($this->fp)) { |
|
| 251 | + return $this->raiseError('not connected'); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return @fgets($this->fp, $size); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Read a specified amount of data. This is guaranteed to return, |
|
| 259 | + * and has the added benefit of getting everything in one fread() |
|
| 260 | + * chunk; if you know the size of the data you're getting |
|
| 261 | + * beforehand, this is definitely the way to go. |
|
| 262 | + * |
|
| 263 | + * @param integer $size The number of bytes to read from the socket. |
|
| 264 | + * @access public |
|
| 265 | + * @return $size bytes of data from the socket, or a PEAR_Error if |
|
| 266 | + * not connected. |
|
| 267 | + */ |
|
| 268 | + function read($size) |
|
| 269 | + { |
|
| 270 | + if (!is_resource($this->fp)) { |
|
| 271 | + return $this->raiseError('not connected'); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + return @fread($this->fp, $size); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Write a specified amount of data. |
|
| 279 | + * |
|
| 280 | + * @param string $data Data to write. |
|
| 281 | + * @param integer $blocksize Amount of data to write at once. |
|
| 282 | + * NULL means all at once. |
|
| 283 | + * |
|
| 284 | + * @access public |
|
| 285 | + * @return mixed true on success or an error object otherwise |
|
| 286 | + */ |
|
| 287 | + function write($data, $blocksize = null) |
|
| 288 | + { |
|
| 289 | + if (!is_resource($this->fp)) { |
|
| 290 | + return $this->raiseError('not connected'); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + if (is_null($blocksize) && !OS_WINDOWS) { |
|
| 294 | + return fwrite($this->fp, $data); |
|
| 295 | + } else { |
|
| 296 | + if (is_null($blocksize)) { |
|
| 297 | + $blocksize = 1024; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + $pos = 0; |
|
| 301 | + $size = strlen($data); |
|
| 302 | + while ($pos < $size) { |
|
| 303 | + $written = @fwrite($this->fp, substr($data, $pos, $blocksize)); |
|
| 304 | + if ($written === false) { |
|
| 305 | + return false; |
|
| 306 | + } |
|
| 307 | + $pos += $written; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + return $pos; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Write a line of data to the socket, followed by a trailing "\r\n". |
|
| 316 | + * |
|
| 317 | + * @access public |
|
| 318 | + * @return mixed fputs result, or an error |
|
| 319 | + */ |
|
| 320 | + function writeLine($data) |
|
| 321 | + { |
|
| 322 | + if (!is_resource($this->fp)) { |
|
| 323 | + return $this->raiseError('not connected'); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + return fwrite($this->fp, $data . "\r\n"); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Tests for end-of-file on a socket descriptor. |
|
| 331 | + * |
|
| 332 | + * @access public |
|
| 333 | + * @return bool |
|
| 334 | + */ |
|
| 335 | + function eof() |
|
| 336 | + { |
|
| 337 | + return (is_resource($this->fp) && feof($this->fp)); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Reads a byte of data |
|
| 342 | + * |
|
| 343 | + * @access public |
|
| 344 | + * @return 1 byte of data from the socket, or a PEAR_Error if |
|
| 345 | + * not connected. |
|
| 346 | + */ |
|
| 347 | + function readByte() |
|
| 348 | + { |
|
| 349 | + if (!is_resource($this->fp)) { |
|
| 350 | + return $this->raiseError('not connected'); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + return ord(@fread($this->fp, 1)); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Reads a word of data |
|
| 358 | + * |
|
| 359 | + * @access public |
|
| 360 | + * @return 1 word of data from the socket, or a PEAR_Error if |
|
| 361 | + * not connected. |
|
| 362 | + */ |
|
| 363 | + function readWord() |
|
| 364 | + { |
|
| 365 | + if (!is_resource($this->fp)) { |
|
| 366 | + return $this->raiseError('not connected'); |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + $buf = @fread($this->fp, 2); |
|
| 370 | + return (ord($buf[0]) + (ord($buf[1]) << 8)); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Reads an int of data |
|
| 375 | + * |
|
| 376 | + * @access public |
|
| 377 | + * @return integer 1 int of data from the socket, or a PEAR_Error if |
|
| 378 | + * not connected. |
|
| 379 | + */ |
|
| 380 | + function readInt() |
|
| 381 | + { |
|
| 382 | + if (!is_resource($this->fp)) { |
|
| 383 | + return $this->raiseError('not connected'); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + $buf = @fread($this->fp, 4); |
|
| 387 | + return (ord($buf[0]) + (ord($buf[1]) << 8) + |
|
| 388 | + (ord($buf[2]) << 16) + (ord($buf[3]) << 24)); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Reads a zero-terminated string of data |
|
| 393 | + * |
|
| 394 | + * @access public |
|
| 395 | + * @return string, or a PEAR_Error if |
|
| 396 | + * not connected. |
|
| 397 | + */ |
|
| 398 | + function readString() |
|
| 399 | + { |
|
| 400 | + if (!is_resource($this->fp)) { |
|
| 401 | + return $this->raiseError('not connected'); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + $string = ''; |
|
| 405 | + while (($char = @fread($this->fp, 1)) != "\x00") { |
|
| 406 | + $string .= $char; |
|
| 407 | + } |
|
| 408 | + return $string; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * Reads an IP Address and returns it in a dot formated string |
|
| 413 | + * |
|
| 414 | + * @access public |
|
| 415 | + * @return Dot formated string, or a PEAR_Error if |
|
| 416 | + * not connected. |
|
| 417 | + */ |
|
| 418 | + function readIPAddress() |
|
| 419 | + { |
|
| 420 | + if (!is_resource($this->fp)) { |
|
| 421 | + return $this->raiseError('not connected'); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + $buf = @fread($this->fp, 4); |
|
| 425 | + return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]), |
|
| 426 | + ord($buf[2]), ord($buf[3])); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Read until either the end of the socket or a newline, whichever |
|
| 431 | + * comes first. Strips the trailing newline from the returned data. |
|
| 432 | + * |
|
| 433 | + * @access public |
|
| 434 | + * @return All available data up to a newline, without that |
|
| 435 | + * newline, or until the end of the socket, or a PEAR_Error if |
|
| 436 | + * not connected. |
|
| 437 | + */ |
|
| 438 | + function readLine() |
|
| 439 | + { |
|
| 440 | + if (!is_resource($this->fp)) { |
|
| 441 | + return $this->raiseError('not connected'); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + $line = ''; |
|
| 445 | + $timeout = time() + $this->timeout; |
|
| 446 | + while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { |
|
| 447 | + $line .= @fgets($this->fp, $this->lineLength); |
|
| 448 | + if (substr($line, -1) == "\n") { |
|
| 449 | + return rtrim($line, "\r\n"); |
|
| 450 | + } |
|
| 451 | + } |
|
| 452 | + return $line; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Read until the socket closes, or until there is no more data in |
|
| 457 | + * the inner PHP buffer. If the inner buffer is empty, in blocking |
|
| 458 | + * mode we wait for at least 1 byte of data. Therefore, in |
|
| 459 | + * blocking mode, if there is no data at all to be read, this |
|
| 460 | + * function will never exit (unless the socket is closed on the |
|
| 461 | + * remote end). |
|
| 462 | + * |
|
| 463 | + * @access public |
|
| 464 | + * |
|
| 465 | + * @return string All data until the socket closes, or a PEAR_Error if |
|
| 466 | + * not connected. |
|
| 467 | + */ |
|
| 468 | + function readAll() |
|
| 469 | + { |
|
| 470 | + if (!is_resource($this->fp)) { |
|
| 471 | + return $this->raiseError('not connected'); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + $data = ''; |
|
| 475 | + while (!feof($this->fp)) { |
|
| 476 | + $data .= @fread($this->fp, $this->lineLength); |
|
| 477 | + } |
|
| 478 | + return $data; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * Runs the equivalent of the select() system call on the socket |
|
| 483 | + * with a timeout specified by tv_sec and tv_usec. |
|
| 484 | + * |
|
| 485 | + * @param integer $state Which of read/write/error to check for. |
|
| 486 | + * @param integer $tv_sec Number of seconds for timeout. |
|
| 487 | + * @param integer $tv_usec Number of microseconds for timeout. |
|
| 488 | + * |
|
| 489 | + * @access public |
|
| 490 | + * @return False if select fails, integer describing which of read/write/error |
|
| 491 | + * are ready, or PEAR_Error if not connected. |
|
| 492 | + */ |
|
| 493 | + function select($state, $tv_sec, $tv_usec = 0) |
|
| 494 | + { |
|
| 495 | + if (!is_resource($this->fp)) { |
|
| 496 | + return $this->raiseError('not connected'); |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + $read = null; |
|
| 500 | + $write = null; |
|
| 501 | + $except = null; |
|
| 502 | + if ($state & NET_SOCKET_READ) { |
|
| 503 | + $read[] = $this->fp; |
|
| 504 | + } |
|
| 505 | + if ($state & NET_SOCKET_WRITE) { |
|
| 506 | + $write[] = $this->fp; |
|
| 507 | + } |
|
| 508 | + if ($state & NET_SOCKET_ERROR) { |
|
| 509 | + $except[] = $this->fp; |
|
| 510 | + } |
|
| 511 | + if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) { |
|
| 512 | + return false; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + $result = 0; |
|
| 516 | + if (count($read)) { |
|
| 517 | + $result |= NET_SOCKET_READ; |
|
| 518 | + } |
|
| 519 | + if (count($write)) { |
|
| 520 | + $result |= NET_SOCKET_WRITE; |
|
| 521 | + } |
|
| 522 | + if (count($except)) { |
|
| 523 | + $result |= NET_SOCKET_ERROR; |
|
| 524 | + } |
|
| 525 | + return $result; |
|
| 526 | + } |
|
| 527 | 527 | |
| 528 | 528 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | require_once 'PEAR.php'; |
| 23 | 23 | |
| 24 | -define('NET_SOCKET_READ', 1); |
|
| 24 | +define('NET_SOCKET_READ', 1); |
|
| 25 | 25 | define('NET_SOCKET_WRITE', 2); |
| 26 | 26 | define('NET_SOCKET_ERROR', 3); |
| 27 | 27 | |
@@ -402,7 +402,7 @@ discard block |
||
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | $string = ''; |
| 405 | - while (($char = @fread($this->fp, 1)) != "\x00") { |
|
| 405 | + while (($char = @fread($this->fp, 1)) != "\x00") { |
|
| 406 | 406 | $string .= $char; |
| 407 | 407 | } |
| 408 | 408 | return $string; |
@@ -28,11 +28,11 @@ discard block |
||
| 28 | 28 | /**#@+ |
| 29 | 29 | * ERROR constants |
| 30 | 30 | */ |
| 31 | -define('PEAR_ERROR_RETURN', 1); |
|
| 32 | -define('PEAR_ERROR_PRINT', 2); |
|
| 33 | -define('PEAR_ERROR_TRIGGER', 4); |
|
| 34 | -define('PEAR_ERROR_DIE', 8); |
|
| 35 | -define('PEAR_ERROR_CALLBACK', 16); |
|
| 31 | +define('PEAR_ERROR_RETURN', 1); |
|
| 32 | +define('PEAR_ERROR_PRINT', 2); |
|
| 33 | +define('PEAR_ERROR_TRIGGER', 4); |
|
| 34 | +define('PEAR_ERROR_DIE', 8); |
|
| 35 | +define('PEAR_ERROR_CALLBACK', 16); |
|
| 36 | 36 | /** |
| 37 | 37 | * WARNING: obsolete |
| 38 | 38 | * @deprecated |
@@ -44,12 +44,12 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | if (substr(PHP_OS, 0, 3) == 'WIN') { |
| 46 | 46 | define('OS_WINDOWS', true); |
| 47 | - define('OS_UNIX', false); |
|
| 48 | - define('PEAR_OS', 'Windows'); |
|
| 47 | + define('OS_UNIX', false); |
|
| 48 | + define('PEAR_OS', 'Windows'); |
|
| 49 | 49 | } else { |
| 50 | 50 | define('OS_WINDOWS', false); |
| 51 | - define('OS_UNIX', true); |
|
| 52 | - define('PEAR_OS', 'Unix'); // blatant assumption |
|
| 51 | + define('OS_UNIX', true); |
|
| 52 | + define('PEAR_OS', 'Unix'); // blatant assumption |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | // instant backwards compatibility |
@@ -460,9 +460,9 @@ discard block |
||
| 460 | 460 | // $error_code is a non-empty array here; |
| 461 | 461 | // we walk through it trying to unset all |
| 462 | 462 | // values |
| 463 | - foreach($error_code as $key => $error) { |
|
| 463 | + foreach ($error_code as $key => $error) { |
|
| 464 | 464 | if ($this->_checkDelExpect($error)) { |
| 465 | - $deleted = true; |
|
| 465 | + $deleted = true; |
|
| 466 | 466 | } else { |
| 467 | 467 | $deleted = false; |
| 468 | 468 | } |
@@ -756,7 +756,7 @@ discard block |
||
| 756 | 756 | } else { |
| 757 | 757 | $suffix = '.so'; |
| 758 | 758 | } |
| 759 | - return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix); |
|
| 759 | + return @dl('php_' . $ext . $suffix) || @dl($ext . $suffix); |
|
| 760 | 760 | } |
| 761 | 761 | return true; |
| 762 | 762 | } |
@@ -1063,13 +1063,12 @@ discard block |
||
| 1063 | 1063 | if ($this->mode & PEAR_ERROR_CALLBACK) { |
| 1064 | 1064 | if (is_array($this->callback)) { |
| 1065 | 1065 | $callback = (is_object($this->callback[0]) ? |
| 1066 | - strtolower(get_class($this->callback[0])) : |
|
| 1067 | - $this->callback[0]) . '::' . |
|
| 1066 | + strtolower(get_class($this->callback[0])) : $this->callback[0]) . '::' . |
|
| 1068 | 1067 | $this->callback[1]; |
| 1069 | 1068 | } else { |
| 1070 | 1069 | $callback = $this->callback; |
| 1071 | 1070 | } |
| 1072 | - return sprintf('[%s: message="%s" code=%d mode=callback '. |
|
| 1071 | + return sprintf('[%s: message="%s" code=%d mode=callback ' . |
|
| 1073 | 1072 | 'callback=%s prefix="%s" info="%s"]', |
| 1074 | 1073 | strtolower(get_class($this)), $this->message, $this->code, |
| 1075 | 1074 | $callback, $this->error_message_prefix, |
@@ -1087,7 +1086,7 @@ discard block |
||
| 1087 | 1086 | if ($this->mode & PEAR_ERROR_RETURN) { |
| 1088 | 1087 | $modes[] = 'return'; |
| 1089 | 1088 | } |
| 1090 | - return sprintf('[%s: message="%s" code=%d mode=%s level=%s '. |
|
| 1089 | + return sprintf('[%s: message="%s" code=%d mode=%s level=%s ' . |
|
| 1091 | 1090 | 'prefix="%s" info="%s"]', |
| 1092 | 1091 | strtolower(get_class($this)), $this->message, $this->code, |
| 1093 | 1092 | implode("|", $modes), $levels[$this->level], |
@@ -1,29 +1,29 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * PEAR, the PHP Extension and Application Repository |
|
| 4 | - * |
|
| 5 | - * PEAR class and PEAR_Error class |
|
| 6 | - * |
|
| 7 | - * PHP versions 4 and 5 |
|
| 8 | - * |
|
| 9 | - * LICENSE: This source file is subject to version 3.0 of the PHP license |
|
| 10 | - * that is available through the world-wide-web at the following URI: |
|
| 11 | - * http://www.php.net/license/3_0.txt. If you did not receive a copy of |
|
| 12 | - * the PHP License and are unable to obtain it through the web, please |
|
| 13 | - * send a note to [email protected] so we can mail you a copy immediately. |
|
| 14 | - * |
|
| 15 | - * @category pear |
|
| 16 | - * @package PEAR |
|
| 17 | - * @author Sterling Hughes <[email protected]> |
|
| 18 | - * @author Stig Bakken <[email protected]> |
|
| 19 | - * @author Tomas V.V.Cox <[email protected]> |
|
| 20 | - * @author Greg Beaver <[email protected]> |
|
| 21 | - * @copyright 1997-2006 The PHP Group |
|
| 22 | - * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
|
| 23 | - * @version CVS: $Id: PEAR.php,v 1.101 2006/04/25 02:41:03 cellog Exp $ |
|
| 24 | - * @link http://pear.php.net/package/PEAR |
|
| 25 | - * @since File available since Release 0.1 |
|
| 26 | - */ |
|
| 3 | + * PEAR, the PHP Extension and Application Repository |
|
| 4 | + * |
|
| 5 | + * PEAR class and PEAR_Error class |
|
| 6 | + * |
|
| 7 | + * PHP versions 4 and 5 |
|
| 8 | + * |
|
| 9 | + * LICENSE: This source file is subject to version 3.0 of the PHP license |
|
| 10 | + * that is available through the world-wide-web at the following URI: |
|
| 11 | + * http://www.php.net/license/3_0.txt. If you did not receive a copy of |
|
| 12 | + * the PHP License and are unable to obtain it through the web, please |
|
| 13 | + * send a note to [email protected] so we can mail you a copy immediately. |
|
| 14 | + * |
|
| 15 | + * @category pear |
|
| 16 | + * @package PEAR |
|
| 17 | + * @author Sterling Hughes <[email protected]> |
|
| 18 | + * @author Stig Bakken <[email protected]> |
|
| 19 | + * @author Tomas V.V.Cox <[email protected]> |
|
| 20 | + * @author Greg Beaver <[email protected]> |
|
| 21 | + * @copyright 1997-2006 The PHP Group |
|
| 22 | + * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
|
| 23 | + * @version CVS: $Id: PEAR.php,v 1.101 2006/04/25 02:41:03 cellog Exp $ |
|
| 24 | + * @link http://pear.php.net/package/PEAR |
|
| 25 | + * @since File available since Release 0.1 |
|
| 26 | + */ |
|
| 27 | 27 | |
| 28 | 28 | /**#@+ |
| 29 | 29 | * ERROR constants |
@@ -40,25 +40,25 @@ discard block |
||
| 40 | 40 | define('PEAR_ERROR_EXCEPTION', 32); |
| 41 | 41 | /**#@-*/ |
| 42 | 42 | define('PEAR_ZE2', (function_exists('version_compare') && |
| 43 | - version_compare(zend_version(), "2-dev", "ge"))); |
|
| 43 | + version_compare(zend_version(), "2-dev", "ge"))); |
|
| 44 | 44 | |
| 45 | 45 | if (substr(PHP_OS, 0, 3) == 'WIN') { |
| 46 | - define('OS_WINDOWS', true); |
|
| 47 | - define('OS_UNIX', false); |
|
| 48 | - define('PEAR_OS', 'Windows'); |
|
| 46 | + define('OS_WINDOWS', true); |
|
| 47 | + define('OS_UNIX', false); |
|
| 48 | + define('PEAR_OS', 'Windows'); |
|
| 49 | 49 | } else { |
| 50 | - define('OS_WINDOWS', false); |
|
| 51 | - define('OS_UNIX', true); |
|
| 52 | - define('PEAR_OS', 'Unix'); // blatant assumption |
|
| 50 | + define('OS_WINDOWS', false); |
|
| 51 | + define('OS_UNIX', true); |
|
| 52 | + define('PEAR_OS', 'Unix'); // blatant assumption |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | // instant backwards compatibility |
| 56 | 56 | if (!defined('PATH_SEPARATOR')) { |
| 57 | - if (OS_WINDOWS) { |
|
| 58 | - define('PATH_SEPARATOR', ';'); |
|
| 59 | - } else { |
|
| 60 | - define('PATH_SEPARATOR', ':'); |
|
| 61 | - } |
|
| 57 | + if (OS_WINDOWS) { |
|
| 58 | + define('PATH_SEPARATOR', ';'); |
|
| 59 | + } else { |
|
| 60 | + define('PATH_SEPARATOR', ':'); |
|
| 61 | + } |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN; |
@@ -101,704 +101,704 @@ discard block |
||
| 101 | 101 | */ |
| 102 | 102 | class PEAR |
| 103 | 103 | { |
| 104 | - // {{{ properties |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Whether to enable internal debug messages. |
|
| 108 | - * |
|
| 109 | - * @var bool |
|
| 110 | - * @access private |
|
| 111 | - */ |
|
| 112 | - var $_debug = false; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Default error mode for this object. |
|
| 116 | - * |
|
| 117 | - * @var int |
|
| 118 | - * @access private |
|
| 119 | - */ |
|
| 120 | - var $_default_error_mode = null; |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Default error options used for this object when error mode |
|
| 124 | - * is PEAR_ERROR_TRIGGER. |
|
| 125 | - * |
|
| 126 | - * @var int |
|
| 127 | - * @access private |
|
| 128 | - */ |
|
| 129 | - var $_default_error_options = null; |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Default error handler (callback) for this object, if error mode is |
|
| 133 | - * PEAR_ERROR_CALLBACK. |
|
| 134 | - * |
|
| 135 | - * @var string |
|
| 136 | - * @access private |
|
| 137 | - */ |
|
| 138 | - var $_default_error_handler = ''; |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Which class to use for error objects. |
|
| 142 | - * |
|
| 143 | - * @var string |
|
| 144 | - * @access private |
|
| 145 | - */ |
|
| 146 | - var $_error_class = 'PEAR_Error'; |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * An array of expected errors. |
|
| 150 | - * |
|
| 151 | - * @var array |
|
| 152 | - * @access private |
|
| 153 | - */ |
|
| 154 | - var $_expected_errors = array(); |
|
| 155 | - |
|
| 156 | - // }}} |
|
| 157 | - |
|
| 158 | - // {{{ constructor |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Constructor. Registers this object in |
|
| 162 | - * $_PEAR_destructor_object_list for destructor emulation if a |
|
| 163 | - * destructor object exists. |
|
| 164 | - * |
|
| 165 | - * @param string $error_class (optional) which class to use for |
|
| 166 | - * error objects, defaults to PEAR_Error. |
|
| 167 | - * @access public |
|
| 168 | - * @return void |
|
| 169 | - */ |
|
| 170 | - function PEAR($error_class = null) |
|
| 171 | - { |
|
| 172 | - $classname = strtolower(get_class($this)); |
|
| 173 | - if ($this->_debug) { |
|
| 174 | - print "PEAR constructor called, class=$classname\n"; |
|
| 175 | - } |
|
| 176 | - if ($error_class !== null) { |
|
| 177 | - $this->_error_class = $error_class; |
|
| 178 | - } |
|
| 179 | - while ($classname && strcasecmp($classname, "pear")) { |
|
| 180 | - $destructor = "_$classname"; |
|
| 181 | - if (method_exists($this, $destructor)) { |
|
| 182 | - global $_PEAR_destructor_object_list; |
|
| 183 | - $_PEAR_destructor_object_list[] = &$this; |
|
| 184 | - if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) { |
|
| 185 | - register_shutdown_function("_PEAR_call_destructors"); |
|
| 186 | - $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true; |
|
| 187 | - } |
|
| 188 | - break; |
|
| 189 | - } else { |
|
| 190 | - $classname = get_parent_class($classname); |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // }}} |
|
| 196 | - // {{{ destructor |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Destructor (the emulated type of...). Does nothing right now, |
|
| 200 | - * but is included for forward compatibility, so subclass |
|
| 201 | - * destructors should always call it. |
|
| 202 | - * |
|
| 203 | - * See the note in the class desciption about output from |
|
| 204 | - * destructors. |
|
| 205 | - * |
|
| 206 | - * @access public |
|
| 207 | - * @return void |
|
| 208 | - */ |
|
| 209 | - function _PEAR() { |
|
| 210 | - if ($this->_debug) { |
|
| 211 | - printf("PEAR destructor called, class=%s\n", strtolower(get_class($this))); |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - // }}} |
|
| 216 | - // {{{ getStaticProperty() |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * If you have a class that's mostly/entirely static, and you need static |
|
| 220 | - * properties, you can use this method to simulate them. Eg. in your method(s) |
|
| 221 | - * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar'); |
|
| 222 | - * You MUST use a reference, or they will not persist! |
|
| 223 | - * |
|
| 224 | - * @access public |
|
| 225 | - * @param string $class The calling classname, to prevent clashes |
|
| 226 | - * @param string $var The variable to retrieve. |
|
| 227 | - * @return mixed A reference to the variable. If not set it will be |
|
| 228 | - * auto initialised to NULL. |
|
| 229 | - */ |
|
| 230 | - function &getStaticProperty($class, $var) |
|
| 231 | - { |
|
| 232 | - static $properties; |
|
| 233 | - if (!isset($properties[$class])) { |
|
| 234 | - $properties[$class] = array(); |
|
| 235 | - } |
|
| 236 | - if (!array_key_exists($var, $properties[$class])) { |
|
| 237 | - $properties[$class][$var] = null; |
|
| 238 | - } |
|
| 239 | - return $properties[$class][$var]; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - // }}} |
|
| 243 | - // {{{ registerShutdownFunc() |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Use this function to register a shutdown method for static |
|
| 247 | - * classes. |
|
| 248 | - * |
|
| 249 | - * @access public |
|
| 250 | - * @param mixed $func The function name (or array of class/method) to call |
|
| 251 | - * @param mixed $args The arguments to pass to the function |
|
| 252 | - * @return void |
|
| 253 | - */ |
|
| 254 | - function registerShutdownFunc($func, $args = array()) |
|
| 255 | - { |
|
| 256 | - // if we are called statically, there is a potential |
|
| 257 | - // that no shutdown func is registered. Bug #6445 |
|
| 258 | - if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) { |
|
| 259 | - register_shutdown_function("_PEAR_call_destructors"); |
|
| 260 | - $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true; |
|
| 261 | - } |
|
| 262 | - $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - // }}} |
|
| 266 | - // {{{ isError() |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Tell whether a value is a PEAR error. |
|
| 270 | - * |
|
| 271 | - * @param mixed $data the value to test |
|
| 272 | - * @param int $code if $data is an error object, return true |
|
| 273 | - * only if $code is a string and |
|
| 274 | - * $obj->getMessage() == $code or |
|
| 275 | - * $code is an integer and $obj->getCode() == $code |
|
| 276 | - * @access public |
|
| 277 | - * @return bool true if parameter is an error |
|
| 278 | - */ |
|
| 279 | - function isError($data, $code = null) |
|
| 280 | - { |
|
| 281 | - if (is_a($data, 'PEAR_Error')) { |
|
| 282 | - if (is_null($code)) { |
|
| 283 | - return true; |
|
| 284 | - } elseif (is_string($code)) { |
|
| 285 | - return $data->getMessage() == $code; |
|
| 286 | - } else { |
|
| 287 | - return $data->getCode() == $code; |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - return false; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - // }}} |
|
| 294 | - // {{{ setErrorHandling() |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Sets how errors generated by this object should be handled. |
|
| 298 | - * Can be invoked both in objects and statically. If called |
|
| 299 | - * statically, setErrorHandling sets the default behaviour for all |
|
| 300 | - * PEAR objects. If called in an object, setErrorHandling sets |
|
| 301 | - * the default behaviour for that object. |
|
| 302 | - * |
|
| 303 | - * @param int $mode |
|
| 304 | - * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, |
|
| 305 | - * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE, |
|
| 306 | - * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION. |
|
| 307 | - * |
|
| 308 | - * @param mixed $options |
|
| 309 | - * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one |
|
| 310 | - * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). |
|
| 311 | - * |
|
| 312 | - * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected |
|
| 313 | - * to be the callback function or method. A callback |
|
| 314 | - * function is a string with the name of the function, a |
|
| 315 | - * callback method is an array of two elements: the element |
|
| 316 | - * at index 0 is the object, and the element at index 1 is |
|
| 317 | - * the name of the method to call in the object. |
|
| 318 | - * |
|
| 319 | - * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is |
|
| 320 | - * a printf format string used when printing the error |
|
| 321 | - * message. |
|
| 322 | - * |
|
| 323 | - * @access public |
|
| 324 | - * @return void |
|
| 325 | - * @see PEAR_ERROR_RETURN |
|
| 326 | - * @see PEAR_ERROR_PRINT |
|
| 327 | - * @see PEAR_ERROR_TRIGGER |
|
| 328 | - * @see PEAR_ERROR_DIE |
|
| 329 | - * @see PEAR_ERROR_CALLBACK |
|
| 330 | - * @see PEAR_ERROR_EXCEPTION |
|
| 331 | - * |
|
| 332 | - * @since PHP 4.0.5 |
|
| 333 | - */ |
|
| 334 | - |
|
| 335 | - function setErrorHandling($mode = null, $options = null) |
|
| 336 | - { |
|
| 337 | - if (isset($this) && is_a($this, 'PEAR')) { |
|
| 338 | - $setmode = &$this->_default_error_mode; |
|
| 339 | - $setoptions = &$this->_default_error_options; |
|
| 340 | - } else { |
|
| 341 | - $setmode = &$GLOBALS['_PEAR_default_error_mode']; |
|
| 342 | - $setoptions = &$GLOBALS['_PEAR_default_error_options']; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - switch ($mode) { |
|
| 346 | - case PEAR_ERROR_EXCEPTION: |
|
| 347 | - case PEAR_ERROR_RETURN: |
|
| 348 | - case PEAR_ERROR_PRINT: |
|
| 349 | - case PEAR_ERROR_TRIGGER: |
|
| 350 | - case PEAR_ERROR_DIE: |
|
| 351 | - case null: |
|
| 352 | - $setmode = $mode; |
|
| 353 | - $setoptions = $options; |
|
| 354 | - break; |
|
| 355 | - |
|
| 356 | - case PEAR_ERROR_CALLBACK: |
|
| 357 | - $setmode = $mode; |
|
| 358 | - // class/object method callback |
|
| 359 | - if (is_callable($options)) { |
|
| 360 | - $setoptions = $options; |
|
| 361 | - } else { |
|
| 362 | - trigger_error("invalid error callback", E_USER_WARNING); |
|
| 363 | - } |
|
| 364 | - break; |
|
| 365 | - |
|
| 366 | - default: |
|
| 367 | - trigger_error("invalid error mode", E_USER_WARNING); |
|
| 368 | - break; |
|
| 369 | - } |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - // }}} |
|
| 373 | - // {{{ expectError() |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * This method is used to tell which errors you expect to get. |
|
| 377 | - * Expected errors are always returned with error mode |
|
| 378 | - * PEAR_ERROR_RETURN. Expected error codes are stored in a stack, |
|
| 379 | - * and this method pushes a new element onto it. The list of |
|
| 380 | - * expected errors are in effect until they are popped off the |
|
| 381 | - * stack with the popExpect() method. |
|
| 382 | - * |
|
| 383 | - * Note that this method can not be called statically |
|
| 384 | - * |
|
| 385 | - * @param mixed $code a single error code or an array of error codes to expect |
|
| 386 | - * |
|
| 387 | - * @return int the new depth of the "expected errors" stack |
|
| 388 | - * @access public |
|
| 389 | - */ |
|
| 390 | - function expectError($code = '*') |
|
| 391 | - { |
|
| 392 | - if (is_array($code)) { |
|
| 393 | - array_push($this->_expected_errors, $code); |
|
| 394 | - } else { |
|
| 395 | - array_push($this->_expected_errors, array($code)); |
|
| 396 | - } |
|
| 397 | - return sizeof($this->_expected_errors); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - // }}} |
|
| 401 | - // {{{ popExpect() |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * This method pops one element off the expected error codes |
|
| 405 | - * stack. |
|
| 406 | - * |
|
| 407 | - * @return array the list of error codes that were popped |
|
| 408 | - */ |
|
| 409 | - function popExpect() |
|
| 410 | - { |
|
| 411 | - return array_pop($this->_expected_errors); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - // }}} |
|
| 415 | - // {{{ _checkDelExpect() |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * This method checks unsets an error code if available |
|
| 419 | - * |
|
| 420 | - * @param mixed error code |
|
| 421 | - * @return bool true if the error code was unset, false otherwise |
|
| 422 | - * @access private |
|
| 423 | - * @since PHP 4.3.0 |
|
| 424 | - */ |
|
| 425 | - function _checkDelExpect($error_code) |
|
| 426 | - { |
|
| 427 | - $deleted = false; |
|
| 428 | - |
|
| 429 | - foreach ($this->_expected_errors AS $key => $error_array) { |
|
| 430 | - if (in_array($error_code, $error_array)) { |
|
| 431 | - unset($this->_expected_errors[$key][array_search($error_code, $error_array)]); |
|
| 432 | - $deleted = true; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - // clean up empty arrays |
|
| 436 | - if (0 == count($this->_expected_errors[$key])) { |
|
| 437 | - unset($this->_expected_errors[$key]); |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - return $deleted; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - // }}} |
|
| 444 | - // {{{ delExpect() |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * This method deletes all occurences of the specified element from |
|
| 448 | - * the expected error codes stack. |
|
| 449 | - * |
|
| 450 | - * @param mixed $error_code error code that should be deleted |
|
| 451 | - * @return mixed list of error codes that were deleted or error |
|
| 452 | - * @access public |
|
| 453 | - * @since PHP 4.3.0 |
|
| 454 | - */ |
|
| 455 | - function delExpect($error_code) |
|
| 456 | - { |
|
| 457 | - $deleted = false; |
|
| 458 | - |
|
| 459 | - if ((is_array($error_code) && (0 != count($error_code)))) { |
|
| 460 | - // $error_code is a non-empty array here; |
|
| 461 | - // we walk through it trying to unset all |
|
| 462 | - // values |
|
| 463 | - foreach($error_code as $key => $error) { |
|
| 464 | - if ($this->_checkDelExpect($error)) { |
|
| 465 | - $deleted = true; |
|
| 466 | - } else { |
|
| 467 | - $deleted = false; |
|
| 468 | - } |
|
| 469 | - } |
|
| 470 | - return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME |
|
| 471 | - } elseif (!empty($error_code)) { |
|
| 472 | - // $error_code comes alone, trying to unset it |
|
| 473 | - if ($this->_checkDelExpect($error_code)) { |
|
| 474 | - return true; |
|
| 475 | - } else { |
|
| 476 | - return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME |
|
| 477 | - } |
|
| 478 | - } else { |
|
| 479 | - // $error_code is empty |
|
| 480 | - return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - // }}} |
|
| 485 | - // {{{ raiseError() |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * This method is a wrapper that returns an instance of the |
|
| 489 | - * configured error class with this object's default error |
|
| 490 | - * handling applied. If the $mode and $options parameters are not |
|
| 491 | - * specified, the object's defaults are used. |
|
| 492 | - * |
|
| 493 | - * @param mixed $message a text error message or a PEAR error object |
|
| 494 | - * |
|
| 495 | - * @param int $code a numeric error code (it is up to your class |
|
| 496 | - * to define these if you want to use codes) |
|
| 497 | - * |
|
| 498 | - * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, |
|
| 499 | - * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE, |
|
| 500 | - * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION. |
|
| 501 | - * |
|
| 502 | - * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter |
|
| 503 | - * specifies the PHP-internal error level (one of |
|
| 504 | - * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). |
|
| 505 | - * If $mode is PEAR_ERROR_CALLBACK, this |
|
| 506 | - * parameter specifies the callback function or |
|
| 507 | - * method. In other error modes this parameter |
|
| 508 | - * is ignored. |
|
| 509 | - * |
|
| 510 | - * @param string $userinfo If you need to pass along for example debug |
|
| 511 | - * information, this parameter is meant for that. |
|
| 512 | - * |
|
| 513 | - * @param string $error_class The returned error object will be |
|
| 514 | - * instantiated from this class, if specified. |
|
| 515 | - * |
|
| 516 | - * @param bool $skipmsg If true, raiseError will only pass error codes, |
|
| 517 | - * the error message parameter will be dropped. |
|
| 518 | - * |
|
| 519 | - * @access public |
|
| 520 | - * @return object a PEAR error object |
|
| 521 | - * @see PEAR::setErrorHandling |
|
| 522 | - * @since PHP 4.0.5 |
|
| 523 | - */ |
|
| 524 | - function &raiseError($message = null, |
|
| 525 | - $code = null, |
|
| 526 | - $mode = null, |
|
| 527 | - $options = null, |
|
| 528 | - $userinfo = null, |
|
| 529 | - $error_class = null, |
|
| 530 | - $skipmsg = false) |
|
| 531 | - { |
|
| 532 | - // The error is yet a PEAR error object |
|
| 533 | - if (is_object($message)) { |
|
| 534 | - $code = $message->getCode(); |
|
| 535 | - $userinfo = $message->getUserInfo(); |
|
| 536 | - $error_class = $message->getType(); |
|
| 537 | - $message->error_message_prefix = ''; |
|
| 538 | - $message = $message->getMessage(); |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) { |
|
| 542 | - if ($exp[0] == "*" || |
|
| 543 | - (is_int(reset($exp)) && in_array($code, $exp)) || |
|
| 544 | - (is_string(reset($exp)) && in_array($message, $exp))) { |
|
| 545 | - $mode = PEAR_ERROR_RETURN; |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - // No mode given, try global ones |
|
| 549 | - if ($mode === null) { |
|
| 550 | - // Class error handler |
|
| 551 | - if (isset($this) && isset($this->_default_error_mode)) { |
|
| 552 | - $mode = $this->_default_error_mode; |
|
| 553 | - $options = $this->_default_error_options; |
|
| 554 | - // Global error handler |
|
| 555 | - } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) { |
|
| 556 | - $mode = $GLOBALS['_PEAR_default_error_mode']; |
|
| 557 | - $options = $GLOBALS['_PEAR_default_error_options']; |
|
| 558 | - } |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - if ($error_class !== null) { |
|
| 562 | - $ec = $error_class; |
|
| 563 | - } elseif (isset($this) && isset($this->_error_class)) { |
|
| 564 | - $ec = $this->_error_class; |
|
| 565 | - } else { |
|
| 566 | - $ec = 'PEAR_Error'; |
|
| 567 | - } |
|
| 568 | - if ($skipmsg) { |
|
| 569 | - $a = &new $ec($code, $mode, $options, $userinfo); |
|
| 570 | - return $a; |
|
| 571 | - } else { |
|
| 572 | - $a = &new $ec($message, $code, $mode, $options, $userinfo); |
|
| 573 | - return $a; |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - // }}} |
|
| 578 | - // {{{ throwError() |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Simpler form of raiseError with fewer options. In most cases |
|
| 582 | - * message, code and userinfo are enough. |
|
| 583 | - * |
|
| 584 | - * @param string $message |
|
| 585 | - * |
|
| 586 | - */ |
|
| 587 | - function &throwError($message = null, |
|
| 588 | - $code = null, |
|
| 589 | - $userinfo = null) |
|
| 590 | - { |
|
| 591 | - if (isset($this) && is_a($this, 'PEAR')) { |
|
| 592 | - $a = &$this->raiseError($message, $code, null, null, $userinfo); |
|
| 593 | - return $a; |
|
| 594 | - } else { |
|
| 595 | - $a = &PEAR::raiseError($message, $code, null, null, $userinfo); |
|
| 596 | - return $a; |
|
| 597 | - } |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - // }}} |
|
| 601 | - function staticPushErrorHandling($mode, $options = null) |
|
| 602 | - { |
|
| 603 | - $stack = &$GLOBALS['_PEAR_error_handler_stack']; |
|
| 604 | - $def_mode = &$GLOBALS['_PEAR_default_error_mode']; |
|
| 605 | - $def_options = &$GLOBALS['_PEAR_default_error_options']; |
|
| 606 | - $stack[] = array($def_mode, $def_options); |
|
| 607 | - switch ($mode) { |
|
| 608 | - case PEAR_ERROR_EXCEPTION: |
|
| 609 | - case PEAR_ERROR_RETURN: |
|
| 610 | - case PEAR_ERROR_PRINT: |
|
| 611 | - case PEAR_ERROR_TRIGGER: |
|
| 612 | - case PEAR_ERROR_DIE: |
|
| 613 | - case null: |
|
| 614 | - $def_mode = $mode; |
|
| 615 | - $def_options = $options; |
|
| 616 | - break; |
|
| 617 | - |
|
| 618 | - case PEAR_ERROR_CALLBACK: |
|
| 619 | - $def_mode = $mode; |
|
| 620 | - // class/object method callback |
|
| 621 | - if (is_callable($options)) { |
|
| 622 | - $def_options = $options; |
|
| 623 | - } else { |
|
| 624 | - trigger_error("invalid error callback", E_USER_WARNING); |
|
| 625 | - } |
|
| 626 | - break; |
|
| 627 | - |
|
| 628 | - default: |
|
| 629 | - trigger_error("invalid error mode", E_USER_WARNING); |
|
| 630 | - break; |
|
| 631 | - } |
|
| 632 | - $stack[] = array($mode, $options); |
|
| 633 | - return true; |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - function staticPopErrorHandling() |
|
| 637 | - { |
|
| 638 | - $stack = &$GLOBALS['_PEAR_error_handler_stack']; |
|
| 639 | - $setmode = &$GLOBALS['_PEAR_default_error_mode']; |
|
| 640 | - $setoptions = &$GLOBALS['_PEAR_default_error_options']; |
|
| 641 | - array_pop($stack); |
|
| 642 | - list($mode, $options) = $stack[sizeof($stack) - 1]; |
|
| 643 | - array_pop($stack); |
|
| 644 | - switch ($mode) { |
|
| 645 | - case PEAR_ERROR_EXCEPTION: |
|
| 646 | - case PEAR_ERROR_RETURN: |
|
| 647 | - case PEAR_ERROR_PRINT: |
|
| 648 | - case PEAR_ERROR_TRIGGER: |
|
| 649 | - case PEAR_ERROR_DIE: |
|
| 650 | - case null: |
|
| 651 | - $setmode = $mode; |
|
| 652 | - $setoptions = $options; |
|
| 653 | - break; |
|
| 654 | - |
|
| 655 | - case PEAR_ERROR_CALLBACK: |
|
| 656 | - $setmode = $mode; |
|
| 657 | - // class/object method callback |
|
| 658 | - if (is_callable($options)) { |
|
| 659 | - $setoptions = $options; |
|
| 660 | - } else { |
|
| 661 | - trigger_error("invalid error callback", E_USER_WARNING); |
|
| 662 | - } |
|
| 663 | - break; |
|
| 664 | - |
|
| 665 | - default: |
|
| 666 | - trigger_error("invalid error mode", E_USER_WARNING); |
|
| 667 | - break; |
|
| 668 | - } |
|
| 669 | - return true; |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - // {{{ pushErrorHandling() |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * Push a new error handler on top of the error handler options stack. With this |
|
| 676 | - * you can easily override the actual error handler for some code and restore |
|
| 677 | - * it later with popErrorHandling. |
|
| 678 | - * |
|
| 679 | - * @param mixed $mode (same as setErrorHandling) |
|
| 680 | - * @param mixed $options (same as setErrorHandling) |
|
| 681 | - * |
|
| 682 | - * @return bool Always true |
|
| 683 | - * |
|
| 684 | - * @see PEAR::setErrorHandling |
|
| 685 | - */ |
|
| 686 | - function pushErrorHandling($mode, $options = null) |
|
| 687 | - { |
|
| 688 | - $stack = &$GLOBALS['_PEAR_error_handler_stack']; |
|
| 689 | - if (isset($this) && is_a($this, 'PEAR')) { |
|
| 690 | - $def_mode = &$this->_default_error_mode; |
|
| 691 | - $def_options = &$this->_default_error_options; |
|
| 692 | - } else { |
|
| 693 | - $def_mode = &$GLOBALS['_PEAR_default_error_mode']; |
|
| 694 | - $def_options = &$GLOBALS['_PEAR_default_error_options']; |
|
| 695 | - } |
|
| 696 | - $stack[] = array($def_mode, $def_options); |
|
| 697 | - |
|
| 698 | - if (isset($this) && is_a($this, 'PEAR')) { |
|
| 699 | - $this->setErrorHandling($mode, $options); |
|
| 700 | - } else { |
|
| 701 | - PEAR::setErrorHandling($mode, $options); |
|
| 702 | - } |
|
| 703 | - $stack[] = array($mode, $options); |
|
| 704 | - return true; |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - // }}} |
|
| 708 | - // {{{ popErrorHandling() |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * Pop the last error handler used |
|
| 712 | - * |
|
| 713 | - * @return bool Always true |
|
| 714 | - * |
|
| 715 | - * @see PEAR::pushErrorHandling |
|
| 716 | - */ |
|
| 717 | - function popErrorHandling() |
|
| 718 | - { |
|
| 719 | - $stack = &$GLOBALS['_PEAR_error_handler_stack']; |
|
| 720 | - array_pop($stack); |
|
| 721 | - list($mode, $options) = $stack[sizeof($stack) - 1]; |
|
| 722 | - array_pop($stack); |
|
| 723 | - if (isset($this) && is_a($this, 'PEAR')) { |
|
| 724 | - $this->setErrorHandling($mode, $options); |
|
| 725 | - } else { |
|
| 726 | - PEAR::setErrorHandling($mode, $options); |
|
| 727 | - } |
|
| 728 | - return true; |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - // }}} |
|
| 732 | - // {{{ loadExtension() |
|
| 733 | - |
|
| 734 | - /** |
|
| 735 | - * OS independant PHP extension load. Remember to take care |
|
| 736 | - * on the correct extension name for case sensitive OSes. |
|
| 737 | - * |
|
| 738 | - * @param string $ext The extension name |
|
| 739 | - * @return bool Success or not on the dl() call |
|
| 740 | - */ |
|
| 741 | - function loadExtension($ext) |
|
| 742 | - { |
|
| 743 | - if (!extension_loaded($ext)) { |
|
| 744 | - // if either returns true dl() will produce a FATAL error, stop that |
|
| 745 | - if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) { |
|
| 746 | - return false; |
|
| 747 | - } |
|
| 748 | - if (OS_WINDOWS) { |
|
| 749 | - $suffix = '.dll'; |
|
| 750 | - } elseif (PHP_OS == 'HP-UX') { |
|
| 751 | - $suffix = '.sl'; |
|
| 752 | - } elseif (PHP_OS == 'AIX') { |
|
| 753 | - $suffix = '.a'; |
|
| 754 | - } elseif (PHP_OS == 'OSX') { |
|
| 755 | - $suffix = '.bundle'; |
|
| 756 | - } else { |
|
| 757 | - $suffix = '.so'; |
|
| 758 | - } |
|
| 759 | - return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix); |
|
| 760 | - } |
|
| 761 | - return true; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - // }}} |
|
| 104 | + // {{{ properties |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Whether to enable internal debug messages. |
|
| 108 | + * |
|
| 109 | + * @var bool |
|
| 110 | + * @access private |
|
| 111 | + */ |
|
| 112 | + var $_debug = false; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Default error mode for this object. |
|
| 116 | + * |
|
| 117 | + * @var int |
|
| 118 | + * @access private |
|
| 119 | + */ |
|
| 120 | + var $_default_error_mode = null; |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Default error options used for this object when error mode |
|
| 124 | + * is PEAR_ERROR_TRIGGER. |
|
| 125 | + * |
|
| 126 | + * @var int |
|
| 127 | + * @access private |
|
| 128 | + */ |
|
| 129 | + var $_default_error_options = null; |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Default error handler (callback) for this object, if error mode is |
|
| 133 | + * PEAR_ERROR_CALLBACK. |
|
| 134 | + * |
|
| 135 | + * @var string |
|
| 136 | + * @access private |
|
| 137 | + */ |
|
| 138 | + var $_default_error_handler = ''; |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Which class to use for error objects. |
|
| 142 | + * |
|
| 143 | + * @var string |
|
| 144 | + * @access private |
|
| 145 | + */ |
|
| 146 | + var $_error_class = 'PEAR_Error'; |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * An array of expected errors. |
|
| 150 | + * |
|
| 151 | + * @var array |
|
| 152 | + * @access private |
|
| 153 | + */ |
|
| 154 | + var $_expected_errors = array(); |
|
| 155 | + |
|
| 156 | + // }}} |
|
| 157 | + |
|
| 158 | + // {{{ constructor |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Constructor. Registers this object in |
|
| 162 | + * $_PEAR_destructor_object_list for destructor emulation if a |
|
| 163 | + * destructor object exists. |
|
| 164 | + * |
|
| 165 | + * @param string $error_class (optional) which class to use for |
|
| 166 | + * error objects, defaults to PEAR_Error. |
|
| 167 | + * @access public |
|
| 168 | + * @return void |
|
| 169 | + */ |
|
| 170 | + function PEAR($error_class = null) |
|
| 171 | + { |
|
| 172 | + $classname = strtolower(get_class($this)); |
|
| 173 | + if ($this->_debug) { |
|
| 174 | + print "PEAR constructor called, class=$classname\n"; |
|
| 175 | + } |
|
| 176 | + if ($error_class !== null) { |
|
| 177 | + $this->_error_class = $error_class; |
|
| 178 | + } |
|
| 179 | + while ($classname && strcasecmp($classname, "pear")) { |
|
| 180 | + $destructor = "_$classname"; |
|
| 181 | + if (method_exists($this, $destructor)) { |
|
| 182 | + global $_PEAR_destructor_object_list; |
|
| 183 | + $_PEAR_destructor_object_list[] = &$this; |
|
| 184 | + if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) { |
|
| 185 | + register_shutdown_function("_PEAR_call_destructors"); |
|
| 186 | + $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true; |
|
| 187 | + } |
|
| 188 | + break; |
|
| 189 | + } else { |
|
| 190 | + $classname = get_parent_class($classname); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // }}} |
|
| 196 | + // {{{ destructor |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Destructor (the emulated type of...). Does nothing right now, |
|
| 200 | + * but is included for forward compatibility, so subclass |
|
| 201 | + * destructors should always call it. |
|
| 202 | + * |
|
| 203 | + * See the note in the class desciption about output from |
|
| 204 | + * destructors. |
|
| 205 | + * |
|
| 206 | + * @access public |
|
| 207 | + * @return void |
|
| 208 | + */ |
|
| 209 | + function _PEAR() { |
|
| 210 | + if ($this->_debug) { |
|
| 211 | + printf("PEAR destructor called, class=%s\n", strtolower(get_class($this))); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + // }}} |
|
| 216 | + // {{{ getStaticProperty() |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * If you have a class that's mostly/entirely static, and you need static |
|
| 220 | + * properties, you can use this method to simulate them. Eg. in your method(s) |
|
| 221 | + * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar'); |
|
| 222 | + * You MUST use a reference, or they will not persist! |
|
| 223 | + * |
|
| 224 | + * @access public |
|
| 225 | + * @param string $class The calling classname, to prevent clashes |
|
| 226 | + * @param string $var The variable to retrieve. |
|
| 227 | + * @return mixed A reference to the variable. If not set it will be |
|
| 228 | + * auto initialised to NULL. |
|
| 229 | + */ |
|
| 230 | + function &getStaticProperty($class, $var) |
|
| 231 | + { |
|
| 232 | + static $properties; |
|
| 233 | + if (!isset($properties[$class])) { |
|
| 234 | + $properties[$class] = array(); |
|
| 235 | + } |
|
| 236 | + if (!array_key_exists($var, $properties[$class])) { |
|
| 237 | + $properties[$class][$var] = null; |
|
| 238 | + } |
|
| 239 | + return $properties[$class][$var]; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + // }}} |
|
| 243 | + // {{{ registerShutdownFunc() |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Use this function to register a shutdown method for static |
|
| 247 | + * classes. |
|
| 248 | + * |
|
| 249 | + * @access public |
|
| 250 | + * @param mixed $func The function name (or array of class/method) to call |
|
| 251 | + * @param mixed $args The arguments to pass to the function |
|
| 252 | + * @return void |
|
| 253 | + */ |
|
| 254 | + function registerShutdownFunc($func, $args = array()) |
|
| 255 | + { |
|
| 256 | + // if we are called statically, there is a potential |
|
| 257 | + // that no shutdown func is registered. Bug #6445 |
|
| 258 | + if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) { |
|
| 259 | + register_shutdown_function("_PEAR_call_destructors"); |
|
| 260 | + $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true; |
|
| 261 | + } |
|
| 262 | + $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + // }}} |
|
| 266 | + // {{{ isError() |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Tell whether a value is a PEAR error. |
|
| 270 | + * |
|
| 271 | + * @param mixed $data the value to test |
|
| 272 | + * @param int $code if $data is an error object, return true |
|
| 273 | + * only if $code is a string and |
|
| 274 | + * $obj->getMessage() == $code or |
|
| 275 | + * $code is an integer and $obj->getCode() == $code |
|
| 276 | + * @access public |
|
| 277 | + * @return bool true if parameter is an error |
|
| 278 | + */ |
|
| 279 | + function isError($data, $code = null) |
|
| 280 | + { |
|
| 281 | + if (is_a($data, 'PEAR_Error')) { |
|
| 282 | + if (is_null($code)) { |
|
| 283 | + return true; |
|
| 284 | + } elseif (is_string($code)) { |
|
| 285 | + return $data->getMessage() == $code; |
|
| 286 | + } else { |
|
| 287 | + return $data->getCode() == $code; |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + return false; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + // }}} |
|
| 294 | + // {{{ setErrorHandling() |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Sets how errors generated by this object should be handled. |
|
| 298 | + * Can be invoked both in objects and statically. If called |
|
| 299 | + * statically, setErrorHandling sets the default behaviour for all |
|
| 300 | + * PEAR objects. If called in an object, setErrorHandling sets |
|
| 301 | + * the default behaviour for that object. |
|
| 302 | + * |
|
| 303 | + * @param int $mode |
|
| 304 | + * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, |
|
| 305 | + * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE, |
|
| 306 | + * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION. |
|
| 307 | + * |
|
| 308 | + * @param mixed $options |
|
| 309 | + * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one |
|
| 310 | + * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). |
|
| 311 | + * |
|
| 312 | + * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected |
|
| 313 | + * to be the callback function or method. A callback |
|
| 314 | + * function is a string with the name of the function, a |
|
| 315 | + * callback method is an array of two elements: the element |
|
| 316 | + * at index 0 is the object, and the element at index 1 is |
|
| 317 | + * the name of the method to call in the object. |
|
| 318 | + * |
|
| 319 | + * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is |
|
| 320 | + * a printf format string used when printing the error |
|
| 321 | + * message. |
|
| 322 | + * |
|
| 323 | + * @access public |
|
| 324 | + * @return void |
|
| 325 | + * @see PEAR_ERROR_RETURN |
|
| 326 | + * @see PEAR_ERROR_PRINT |
|
| 327 | + * @see PEAR_ERROR_TRIGGER |
|
| 328 | + * @see PEAR_ERROR_DIE |
|
| 329 | + * @see PEAR_ERROR_CALLBACK |
|
| 330 | + * @see PEAR_ERROR_EXCEPTION |
|
| 331 | + * |
|
| 332 | + * @since PHP 4.0.5 |
|
| 333 | + */ |
|
| 334 | + |
|
| 335 | + function setErrorHandling($mode = null, $options = null) |
|
| 336 | + { |
|
| 337 | + if (isset($this) && is_a($this, 'PEAR')) { |
|
| 338 | + $setmode = &$this->_default_error_mode; |
|
| 339 | + $setoptions = &$this->_default_error_options; |
|
| 340 | + } else { |
|
| 341 | + $setmode = &$GLOBALS['_PEAR_default_error_mode']; |
|
| 342 | + $setoptions = &$GLOBALS['_PEAR_default_error_options']; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + switch ($mode) { |
|
| 346 | + case PEAR_ERROR_EXCEPTION: |
|
| 347 | + case PEAR_ERROR_RETURN: |
|
| 348 | + case PEAR_ERROR_PRINT: |
|
| 349 | + case PEAR_ERROR_TRIGGER: |
|
| 350 | + case PEAR_ERROR_DIE: |
|
| 351 | + case null: |
|
| 352 | + $setmode = $mode; |
|
| 353 | + $setoptions = $options; |
|
| 354 | + break; |
|
| 355 | + |
|
| 356 | + case PEAR_ERROR_CALLBACK: |
|
| 357 | + $setmode = $mode; |
|
| 358 | + // class/object method callback |
|
| 359 | + if (is_callable($options)) { |
|
| 360 | + $setoptions = $options; |
|
| 361 | + } else { |
|
| 362 | + trigger_error("invalid error callback", E_USER_WARNING); |
|
| 363 | + } |
|
| 364 | + break; |
|
| 365 | + |
|
| 366 | + default: |
|
| 367 | + trigger_error("invalid error mode", E_USER_WARNING); |
|
| 368 | + break; |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + // }}} |
|
| 373 | + // {{{ expectError() |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * This method is used to tell which errors you expect to get. |
|
| 377 | + * Expected errors are always returned with error mode |
|
| 378 | + * PEAR_ERROR_RETURN. Expected error codes are stored in a stack, |
|
| 379 | + * and this method pushes a new element onto it. The list of |
|
| 380 | + * expected errors are in effect until they are popped off the |
|
| 381 | + * stack with the popExpect() method. |
|
| 382 | + * |
|
| 383 | + * Note that this method can not be called statically |
|
| 384 | + * |
|
| 385 | + * @param mixed $code a single error code or an array of error codes to expect |
|
| 386 | + * |
|
| 387 | + * @return int the new depth of the "expected errors" stack |
|
| 388 | + * @access public |
|
| 389 | + */ |
|
| 390 | + function expectError($code = '*') |
|
| 391 | + { |
|
| 392 | + if (is_array($code)) { |
|
| 393 | + array_push($this->_expected_errors, $code); |
|
| 394 | + } else { |
|
| 395 | + array_push($this->_expected_errors, array($code)); |
|
| 396 | + } |
|
| 397 | + return sizeof($this->_expected_errors); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + // }}} |
|
| 401 | + // {{{ popExpect() |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * This method pops one element off the expected error codes |
|
| 405 | + * stack. |
|
| 406 | + * |
|
| 407 | + * @return array the list of error codes that were popped |
|
| 408 | + */ |
|
| 409 | + function popExpect() |
|
| 410 | + { |
|
| 411 | + return array_pop($this->_expected_errors); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + // }}} |
|
| 415 | + // {{{ _checkDelExpect() |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * This method checks unsets an error code if available |
|
| 419 | + * |
|
| 420 | + * @param mixed error code |
|
| 421 | + * @return bool true if the error code was unset, false otherwise |
|
| 422 | + * @access private |
|
| 423 | + * @since PHP 4.3.0 |
|
| 424 | + */ |
|
| 425 | + function _checkDelExpect($error_code) |
|
| 426 | + { |
|
| 427 | + $deleted = false; |
|
| 428 | + |
|
| 429 | + foreach ($this->_expected_errors AS $key => $error_array) { |
|
| 430 | + if (in_array($error_code, $error_array)) { |
|
| 431 | + unset($this->_expected_errors[$key][array_search($error_code, $error_array)]); |
|
| 432 | + $deleted = true; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + // clean up empty arrays |
|
| 436 | + if (0 == count($this->_expected_errors[$key])) { |
|
| 437 | + unset($this->_expected_errors[$key]); |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + return $deleted; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + // }}} |
|
| 444 | + // {{{ delExpect() |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * This method deletes all occurences of the specified element from |
|
| 448 | + * the expected error codes stack. |
|
| 449 | + * |
|
| 450 | + * @param mixed $error_code error code that should be deleted |
|
| 451 | + * @return mixed list of error codes that were deleted or error |
|
| 452 | + * @access public |
|
| 453 | + * @since PHP 4.3.0 |
|
| 454 | + */ |
|
| 455 | + function delExpect($error_code) |
|
| 456 | + { |
|
| 457 | + $deleted = false; |
|
| 458 | + |
|
| 459 | + if ((is_array($error_code) && (0 != count($error_code)))) { |
|
| 460 | + // $error_code is a non-empty array here; |
|
| 461 | + // we walk through it trying to unset all |
|
| 462 | + // values |
|
| 463 | + foreach($error_code as $key => $error) { |
|
| 464 | + if ($this->_checkDelExpect($error)) { |
|
| 465 | + $deleted = true; |
|
| 466 | + } else { |
|
| 467 | + $deleted = false; |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | + return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME |
|
| 471 | + } elseif (!empty($error_code)) { |
|
| 472 | + // $error_code comes alone, trying to unset it |
|
| 473 | + if ($this->_checkDelExpect($error_code)) { |
|
| 474 | + return true; |
|
| 475 | + } else { |
|
| 476 | + return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME |
|
| 477 | + } |
|
| 478 | + } else { |
|
| 479 | + // $error_code is empty |
|
| 480 | + return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + // }}} |
|
| 485 | + // {{{ raiseError() |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * This method is a wrapper that returns an instance of the |
|
| 489 | + * configured error class with this object's default error |
|
| 490 | + * handling applied. If the $mode and $options parameters are not |
|
| 491 | + * specified, the object's defaults are used. |
|
| 492 | + * |
|
| 493 | + * @param mixed $message a text error message or a PEAR error object |
|
| 494 | + * |
|
| 495 | + * @param int $code a numeric error code (it is up to your class |
|
| 496 | + * to define these if you want to use codes) |
|
| 497 | + * |
|
| 498 | + * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, |
|
| 499 | + * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE, |
|
| 500 | + * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION. |
|
| 501 | + * |
|
| 502 | + * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter |
|
| 503 | + * specifies the PHP-internal error level (one of |
|
| 504 | + * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). |
|
| 505 | + * If $mode is PEAR_ERROR_CALLBACK, this |
|
| 506 | + * parameter specifies the callback function or |
|
| 507 | + * method. In other error modes this parameter |
|
| 508 | + * is ignored. |
|
| 509 | + * |
|
| 510 | + * @param string $userinfo If you need to pass along for example debug |
|
| 511 | + * information, this parameter is meant for that. |
|
| 512 | + * |
|
| 513 | + * @param string $error_class The returned error object will be |
|
| 514 | + * instantiated from this class, if specified. |
|
| 515 | + * |
|
| 516 | + * @param bool $skipmsg If true, raiseError will only pass error codes, |
|
| 517 | + * the error message parameter will be dropped. |
|
| 518 | + * |
|
| 519 | + * @access public |
|
| 520 | + * @return object a PEAR error object |
|
| 521 | + * @see PEAR::setErrorHandling |
|
| 522 | + * @since PHP 4.0.5 |
|
| 523 | + */ |
|
| 524 | + function &raiseError($message = null, |
|
| 525 | + $code = null, |
|
| 526 | + $mode = null, |
|
| 527 | + $options = null, |
|
| 528 | + $userinfo = null, |
|
| 529 | + $error_class = null, |
|
| 530 | + $skipmsg = false) |
|
| 531 | + { |
|
| 532 | + // The error is yet a PEAR error object |
|
| 533 | + if (is_object($message)) { |
|
| 534 | + $code = $message->getCode(); |
|
| 535 | + $userinfo = $message->getUserInfo(); |
|
| 536 | + $error_class = $message->getType(); |
|
| 537 | + $message->error_message_prefix = ''; |
|
| 538 | + $message = $message->getMessage(); |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) { |
|
| 542 | + if ($exp[0] == "*" || |
|
| 543 | + (is_int(reset($exp)) && in_array($code, $exp)) || |
|
| 544 | + (is_string(reset($exp)) && in_array($message, $exp))) { |
|
| 545 | + $mode = PEAR_ERROR_RETURN; |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + // No mode given, try global ones |
|
| 549 | + if ($mode === null) { |
|
| 550 | + // Class error handler |
|
| 551 | + if (isset($this) && isset($this->_default_error_mode)) { |
|
| 552 | + $mode = $this->_default_error_mode; |
|
| 553 | + $options = $this->_default_error_options; |
|
| 554 | + // Global error handler |
|
| 555 | + } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) { |
|
| 556 | + $mode = $GLOBALS['_PEAR_default_error_mode']; |
|
| 557 | + $options = $GLOBALS['_PEAR_default_error_options']; |
|
| 558 | + } |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + if ($error_class !== null) { |
|
| 562 | + $ec = $error_class; |
|
| 563 | + } elseif (isset($this) && isset($this->_error_class)) { |
|
| 564 | + $ec = $this->_error_class; |
|
| 565 | + } else { |
|
| 566 | + $ec = 'PEAR_Error'; |
|
| 567 | + } |
|
| 568 | + if ($skipmsg) { |
|
| 569 | + $a = &new $ec($code, $mode, $options, $userinfo); |
|
| 570 | + return $a; |
|
| 571 | + } else { |
|
| 572 | + $a = &new $ec($message, $code, $mode, $options, $userinfo); |
|
| 573 | + return $a; |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + // }}} |
|
| 578 | + // {{{ throwError() |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Simpler form of raiseError with fewer options. In most cases |
|
| 582 | + * message, code and userinfo are enough. |
|
| 583 | + * |
|
| 584 | + * @param string $message |
|
| 585 | + * |
|
| 586 | + */ |
|
| 587 | + function &throwError($message = null, |
|
| 588 | + $code = null, |
|
| 589 | + $userinfo = null) |
|
| 590 | + { |
|
| 591 | + if (isset($this) && is_a($this, 'PEAR')) { |
|
| 592 | + $a = &$this->raiseError($message, $code, null, null, $userinfo); |
|
| 593 | + return $a; |
|
| 594 | + } else { |
|
| 595 | + $a = &PEAR::raiseError($message, $code, null, null, $userinfo); |
|
| 596 | + return $a; |
|
| 597 | + } |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + // }}} |
|
| 601 | + function staticPushErrorHandling($mode, $options = null) |
|
| 602 | + { |
|
| 603 | + $stack = &$GLOBALS['_PEAR_error_handler_stack']; |
|
| 604 | + $def_mode = &$GLOBALS['_PEAR_default_error_mode']; |
|
| 605 | + $def_options = &$GLOBALS['_PEAR_default_error_options']; |
|
| 606 | + $stack[] = array($def_mode, $def_options); |
|
| 607 | + switch ($mode) { |
|
| 608 | + case PEAR_ERROR_EXCEPTION: |
|
| 609 | + case PEAR_ERROR_RETURN: |
|
| 610 | + case PEAR_ERROR_PRINT: |
|
| 611 | + case PEAR_ERROR_TRIGGER: |
|
| 612 | + case PEAR_ERROR_DIE: |
|
| 613 | + case null: |
|
| 614 | + $def_mode = $mode; |
|
| 615 | + $def_options = $options; |
|
| 616 | + break; |
|
| 617 | + |
|
| 618 | + case PEAR_ERROR_CALLBACK: |
|
| 619 | + $def_mode = $mode; |
|
| 620 | + // class/object method callback |
|
| 621 | + if (is_callable($options)) { |
|
| 622 | + $def_options = $options; |
|
| 623 | + } else { |
|
| 624 | + trigger_error("invalid error callback", E_USER_WARNING); |
|
| 625 | + } |
|
| 626 | + break; |
|
| 627 | + |
|
| 628 | + default: |
|
| 629 | + trigger_error("invalid error mode", E_USER_WARNING); |
|
| 630 | + break; |
|
| 631 | + } |
|
| 632 | + $stack[] = array($mode, $options); |
|
| 633 | + return true; |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + function staticPopErrorHandling() |
|
| 637 | + { |
|
| 638 | + $stack = &$GLOBALS['_PEAR_error_handler_stack']; |
|
| 639 | + $setmode = &$GLOBALS['_PEAR_default_error_mode']; |
|
| 640 | + $setoptions = &$GLOBALS['_PEAR_default_error_options']; |
|
| 641 | + array_pop($stack); |
|
| 642 | + list($mode, $options) = $stack[sizeof($stack) - 1]; |
|
| 643 | + array_pop($stack); |
|
| 644 | + switch ($mode) { |
|
| 645 | + case PEAR_ERROR_EXCEPTION: |
|
| 646 | + case PEAR_ERROR_RETURN: |
|
| 647 | + case PEAR_ERROR_PRINT: |
|
| 648 | + case PEAR_ERROR_TRIGGER: |
|
| 649 | + case PEAR_ERROR_DIE: |
|
| 650 | + case null: |
|
| 651 | + $setmode = $mode; |
|
| 652 | + $setoptions = $options; |
|
| 653 | + break; |
|
| 654 | + |
|
| 655 | + case PEAR_ERROR_CALLBACK: |
|
| 656 | + $setmode = $mode; |
|
| 657 | + // class/object method callback |
|
| 658 | + if (is_callable($options)) { |
|
| 659 | + $setoptions = $options; |
|
| 660 | + } else { |
|
| 661 | + trigger_error("invalid error callback", E_USER_WARNING); |
|
| 662 | + } |
|
| 663 | + break; |
|
| 664 | + |
|
| 665 | + default: |
|
| 666 | + trigger_error("invalid error mode", E_USER_WARNING); |
|
| 667 | + break; |
|
| 668 | + } |
|
| 669 | + return true; |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + // {{{ pushErrorHandling() |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * Push a new error handler on top of the error handler options stack. With this |
|
| 676 | + * you can easily override the actual error handler for some code and restore |
|
| 677 | + * it later with popErrorHandling. |
|
| 678 | + * |
|
| 679 | + * @param mixed $mode (same as setErrorHandling) |
|
| 680 | + * @param mixed $options (same as setErrorHandling) |
|
| 681 | + * |
|
| 682 | + * @return bool Always true |
|
| 683 | + * |
|
| 684 | + * @see PEAR::setErrorHandling |
|
| 685 | + */ |
|
| 686 | + function pushErrorHandling($mode, $options = null) |
|
| 687 | + { |
|
| 688 | + $stack = &$GLOBALS['_PEAR_error_handler_stack']; |
|
| 689 | + if (isset($this) && is_a($this, 'PEAR')) { |
|
| 690 | + $def_mode = &$this->_default_error_mode; |
|
| 691 | + $def_options = &$this->_default_error_options; |
|
| 692 | + } else { |
|
| 693 | + $def_mode = &$GLOBALS['_PEAR_default_error_mode']; |
|
| 694 | + $def_options = &$GLOBALS['_PEAR_default_error_options']; |
|
| 695 | + } |
|
| 696 | + $stack[] = array($def_mode, $def_options); |
|
| 697 | + |
|
| 698 | + if (isset($this) && is_a($this, 'PEAR')) { |
|
| 699 | + $this->setErrorHandling($mode, $options); |
|
| 700 | + } else { |
|
| 701 | + PEAR::setErrorHandling($mode, $options); |
|
| 702 | + } |
|
| 703 | + $stack[] = array($mode, $options); |
|
| 704 | + return true; |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + // }}} |
|
| 708 | + // {{{ popErrorHandling() |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Pop the last error handler used |
|
| 712 | + * |
|
| 713 | + * @return bool Always true |
|
| 714 | + * |
|
| 715 | + * @see PEAR::pushErrorHandling |
|
| 716 | + */ |
|
| 717 | + function popErrorHandling() |
|
| 718 | + { |
|
| 719 | + $stack = &$GLOBALS['_PEAR_error_handler_stack']; |
|
| 720 | + array_pop($stack); |
|
| 721 | + list($mode, $options) = $stack[sizeof($stack) - 1]; |
|
| 722 | + array_pop($stack); |
|
| 723 | + if (isset($this) && is_a($this, 'PEAR')) { |
|
| 724 | + $this->setErrorHandling($mode, $options); |
|
| 725 | + } else { |
|
| 726 | + PEAR::setErrorHandling($mode, $options); |
|
| 727 | + } |
|
| 728 | + return true; |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + // }}} |
|
| 732 | + // {{{ loadExtension() |
|
| 733 | + |
|
| 734 | + /** |
|
| 735 | + * OS independant PHP extension load. Remember to take care |
|
| 736 | + * on the correct extension name for case sensitive OSes. |
|
| 737 | + * |
|
| 738 | + * @param string $ext The extension name |
|
| 739 | + * @return bool Success or not on the dl() call |
|
| 740 | + */ |
|
| 741 | + function loadExtension($ext) |
|
| 742 | + { |
|
| 743 | + if (!extension_loaded($ext)) { |
|
| 744 | + // if either returns true dl() will produce a FATAL error, stop that |
|
| 745 | + if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) { |
|
| 746 | + return false; |
|
| 747 | + } |
|
| 748 | + if (OS_WINDOWS) { |
|
| 749 | + $suffix = '.dll'; |
|
| 750 | + } elseif (PHP_OS == 'HP-UX') { |
|
| 751 | + $suffix = '.sl'; |
|
| 752 | + } elseif (PHP_OS == 'AIX') { |
|
| 753 | + $suffix = '.a'; |
|
| 754 | + } elseif (PHP_OS == 'OSX') { |
|
| 755 | + $suffix = '.bundle'; |
|
| 756 | + } else { |
|
| 757 | + $suffix = '.so'; |
|
| 758 | + } |
|
| 759 | + return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix); |
|
| 760 | + } |
|
| 761 | + return true; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + // }}} |
|
| 765 | 765 | } |
| 766 | 766 | |
| 767 | 767 | // {{{ _PEAR_call_destructors() |
| 768 | 768 | |
| 769 | 769 | function _PEAR_call_destructors() |
| 770 | 770 | { |
| 771 | - global $_PEAR_destructor_object_list; |
|
| 772 | - if (is_array($_PEAR_destructor_object_list) && |
|
| 773 | - sizeof($_PEAR_destructor_object_list)) |
|
| 774 | - { |
|
| 775 | - reset($_PEAR_destructor_object_list); |
|
| 776 | - if (PEAR::getStaticProperty('PEAR', 'destructlifo')) { |
|
| 777 | - $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list); |
|
| 778 | - } |
|
| 779 | - while (list($k, $objref) = each($_PEAR_destructor_object_list)) { |
|
| 780 | - $classname = get_class($objref); |
|
| 781 | - while ($classname) { |
|
| 782 | - $destructor = "_$classname"; |
|
| 783 | - if (method_exists($objref, $destructor)) { |
|
| 784 | - $objref->$destructor(); |
|
| 785 | - break; |
|
| 786 | - } else { |
|
| 787 | - $classname = get_parent_class($classname); |
|
| 788 | - } |
|
| 789 | - } |
|
| 790 | - } |
|
| 791 | - // Empty the object list to ensure that destructors are |
|
| 792 | - // not called more than once. |
|
| 793 | - $_PEAR_destructor_object_list = array(); |
|
| 794 | - } |
|
| 795 | - |
|
| 796 | - // Now call the shutdown functions |
|
| 797 | - if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) { |
|
| 798 | - foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) { |
|
| 799 | - call_user_func_array($value[0], $value[1]); |
|
| 800 | - } |
|
| 801 | - } |
|
| 771 | + global $_PEAR_destructor_object_list; |
|
| 772 | + if (is_array($_PEAR_destructor_object_list) && |
|
| 773 | + sizeof($_PEAR_destructor_object_list)) |
|
| 774 | + { |
|
| 775 | + reset($_PEAR_destructor_object_list); |
|
| 776 | + if (PEAR::getStaticProperty('PEAR', 'destructlifo')) { |
|
| 777 | + $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list); |
|
| 778 | + } |
|
| 779 | + while (list($k, $objref) = each($_PEAR_destructor_object_list)) { |
|
| 780 | + $classname = get_class($objref); |
|
| 781 | + while ($classname) { |
|
| 782 | + $destructor = "_$classname"; |
|
| 783 | + if (method_exists($objref, $destructor)) { |
|
| 784 | + $objref->$destructor(); |
|
| 785 | + break; |
|
| 786 | + } else { |
|
| 787 | + $classname = get_parent_class($classname); |
|
| 788 | + } |
|
| 789 | + } |
|
| 790 | + } |
|
| 791 | + // Empty the object list to ensure that destructors are |
|
| 792 | + // not called more than once. |
|
| 793 | + $_PEAR_destructor_object_list = array(); |
|
| 794 | + } |
|
| 795 | + |
|
| 796 | + // Now call the shutdown functions |
|
| 797 | + if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) { |
|
| 798 | + foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) { |
|
| 799 | + call_user_func_array($value[0], $value[1]); |
|
| 800 | + } |
|
| 801 | + } |
|
| 802 | 802 | } |
| 803 | 803 | |
| 804 | 804 | // }}} |
@@ -821,281 +821,281 @@ discard block |
||
| 821 | 821 | */ |
| 822 | 822 | class PEAR_Error |
| 823 | 823 | { |
| 824 | - // {{{ properties |
|
| 825 | - |
|
| 826 | - var $error_message_prefix = ''; |
|
| 827 | - var $mode = PEAR_ERROR_RETURN; |
|
| 828 | - var $level = E_USER_NOTICE; |
|
| 829 | - var $code = -1; |
|
| 830 | - var $message = ''; |
|
| 831 | - var $userinfo = ''; |
|
| 832 | - var $backtrace = null; |
|
| 833 | - |
|
| 834 | - // }}} |
|
| 835 | - // {{{ constructor |
|
| 836 | - |
|
| 837 | - /** |
|
| 838 | - * PEAR_Error constructor |
|
| 839 | - * |
|
| 840 | - * @param string $message message |
|
| 841 | - * |
|
| 842 | - * @param int $code (optional) error code |
|
| 843 | - * |
|
| 844 | - * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN, |
|
| 845 | - * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER, |
|
| 846 | - * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION |
|
| 847 | - * |
|
| 848 | - * @param mixed $options (optional) error level, _OR_ in the case of |
|
| 849 | - * PEAR_ERROR_CALLBACK, the callback function or object/method |
|
| 850 | - * tuple. |
|
| 851 | - * |
|
| 852 | - * @param string $userinfo (optional) additional user/debug info |
|
| 853 | - * |
|
| 854 | - * @access public |
|
| 855 | - * |
|
| 856 | - */ |
|
| 857 | - function PEAR_Error($message = 'unknown error', $code = null, |
|
| 858 | - $mode = null, $options = null, $userinfo = null) |
|
| 859 | - { |
|
| 860 | - if ($mode === null) { |
|
| 861 | - $mode = PEAR_ERROR_RETURN; |
|
| 862 | - } |
|
| 863 | - $this->message = $message; |
|
| 864 | - $this->code = $code; |
|
| 865 | - $this->mode = $mode; |
|
| 866 | - $this->userinfo = $userinfo; |
|
| 867 | - if (!PEAR::getStaticProperty('PEAR_Error', 'skiptrace')) { |
|
| 868 | - $this->backtrace = debug_backtrace(); |
|
| 869 | - if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) { |
|
| 870 | - unset($this->backtrace[0]['object']); |
|
| 871 | - } |
|
| 872 | - } |
|
| 873 | - if ($mode & PEAR_ERROR_CALLBACK) { |
|
| 874 | - $this->level = E_USER_NOTICE; |
|
| 875 | - $this->callback = $options; |
|
| 876 | - } else { |
|
| 877 | - if ($options === null) { |
|
| 878 | - $options = E_USER_NOTICE; |
|
| 879 | - } |
|
| 880 | - $this->level = $options; |
|
| 881 | - $this->callback = null; |
|
| 882 | - } |
|
| 883 | - if ($this->mode & PEAR_ERROR_PRINT) { |
|
| 884 | - if (is_null($options) || is_int($options)) { |
|
| 885 | - $format = "%s"; |
|
| 886 | - } else { |
|
| 887 | - $format = $options; |
|
| 888 | - } |
|
| 889 | - printf($format, $this->getMessage()); |
|
| 890 | - } |
|
| 891 | - if ($this->mode & PEAR_ERROR_TRIGGER) { |
|
| 892 | - trigger_error($this->getMessage(), $this->level); |
|
| 893 | - } |
|
| 894 | - if ($this->mode & PEAR_ERROR_DIE) { |
|
| 895 | - $msg = $this->getMessage(); |
|
| 896 | - if (is_null($options) || is_int($options)) { |
|
| 897 | - $format = "%s"; |
|
| 898 | - if (substr($msg, -1) != "\n") { |
|
| 899 | - $msg .= "\n"; |
|
| 900 | - } |
|
| 901 | - } else { |
|
| 902 | - $format = $options; |
|
| 903 | - } |
|
| 904 | - die(sprintf($format, $msg)); |
|
| 905 | - } |
|
| 906 | - if ($this->mode & PEAR_ERROR_CALLBACK) { |
|
| 907 | - if (is_callable($this->callback)) { |
|
| 908 | - call_user_func($this->callback, $this); |
|
| 909 | - } |
|
| 910 | - } |
|
| 911 | - if ($this->mode & PEAR_ERROR_EXCEPTION) { |
|
| 912 | - trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING); |
|
| 913 | - eval('$e = new Exception($this->message, $this->code);throw($e);'); |
|
| 914 | - } |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - // }}} |
|
| 918 | - // {{{ getMode() |
|
| 919 | - |
|
| 920 | - /** |
|
| 921 | - * Get the error mode from an error object. |
|
| 922 | - * |
|
| 923 | - * @return int error mode |
|
| 924 | - * @access public |
|
| 925 | - */ |
|
| 926 | - function getMode() { |
|
| 927 | - return $this->mode; |
|
| 928 | - } |
|
| 929 | - |
|
| 930 | - // }}} |
|
| 931 | - // {{{ getCallback() |
|
| 932 | - |
|
| 933 | - /** |
|
| 934 | - * Get the callback function/method from an error object. |
|
| 935 | - * |
|
| 936 | - * @return mixed callback function or object/method array |
|
| 937 | - * @access public |
|
| 938 | - */ |
|
| 939 | - function getCallback() { |
|
| 940 | - return $this->callback; |
|
| 941 | - } |
|
| 942 | - |
|
| 943 | - // }}} |
|
| 944 | - // {{{ getMessage() |
|
| 945 | - |
|
| 946 | - |
|
| 947 | - /** |
|
| 948 | - * Get the error message from an error object. |
|
| 949 | - * |
|
| 950 | - * @return string full error message |
|
| 951 | - * @access public |
|
| 952 | - */ |
|
| 953 | - function getMessage() |
|
| 954 | - { |
|
| 955 | - return ($this->error_message_prefix . $this->message); |
|
| 956 | - } |
|
| 957 | - |
|
| 958 | - |
|
| 959 | - // }}} |
|
| 960 | - // {{{ getCode() |
|
| 961 | - |
|
| 962 | - /** |
|
| 963 | - * Get error code from an error object |
|
| 964 | - * |
|
| 965 | - * @return int error code |
|
| 966 | - * @access public |
|
| 967 | - */ |
|
| 968 | - function getCode() |
|
| 969 | - { |
|
| 970 | - return $this->code; |
|
| 971 | - } |
|
| 972 | - |
|
| 973 | - // }}} |
|
| 974 | - // {{{ getType() |
|
| 975 | - |
|
| 976 | - /** |
|
| 977 | - * Get the name of this error/exception. |
|
| 978 | - * |
|
| 979 | - * @return string error/exception name (type) |
|
| 980 | - * @access public |
|
| 981 | - */ |
|
| 982 | - function getType() |
|
| 983 | - { |
|
| 984 | - return get_class($this); |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - // }}} |
|
| 988 | - // {{{ getUserInfo() |
|
| 989 | - |
|
| 990 | - /** |
|
| 991 | - * Get additional user-supplied information. |
|
| 992 | - * |
|
| 993 | - * @return string user-supplied information |
|
| 994 | - * @access public |
|
| 995 | - */ |
|
| 996 | - function getUserInfo() |
|
| 997 | - { |
|
| 998 | - return $this->userinfo; |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - // }}} |
|
| 1002 | - // {{{ getDebugInfo() |
|
| 1003 | - |
|
| 1004 | - /** |
|
| 1005 | - * Get additional debug information supplied by the application. |
|
| 1006 | - * |
|
| 1007 | - * @return string debug information |
|
| 1008 | - * @access public |
|
| 1009 | - */ |
|
| 1010 | - function getDebugInfo() |
|
| 1011 | - { |
|
| 1012 | - return $this->getUserInfo(); |
|
| 1013 | - } |
|
| 1014 | - |
|
| 1015 | - // }}} |
|
| 1016 | - // {{{ getBacktrace() |
|
| 1017 | - |
|
| 1018 | - /** |
|
| 1019 | - * Get the call backtrace from where the error was generated. |
|
| 1020 | - * Supported with PHP 4.3.0 or newer. |
|
| 1021 | - * |
|
| 1022 | - * @param int $frame (optional) what frame to fetch |
|
| 1023 | - * @return array Backtrace, or NULL if not available. |
|
| 1024 | - * @access public |
|
| 1025 | - */ |
|
| 1026 | - function getBacktrace($frame = null) |
|
| 1027 | - { |
|
| 1028 | - if (defined('PEAR_IGNORE_BACKTRACE')) { |
|
| 1029 | - return null; |
|
| 1030 | - } |
|
| 1031 | - if ($frame === null) { |
|
| 1032 | - return $this->backtrace; |
|
| 1033 | - } |
|
| 1034 | - return $this->backtrace[$frame]; |
|
| 1035 | - } |
|
| 1036 | - |
|
| 1037 | - // }}} |
|
| 1038 | - // {{{ addUserInfo() |
|
| 1039 | - |
|
| 1040 | - function addUserInfo($info) |
|
| 1041 | - { |
|
| 1042 | - if (empty($this->userinfo)) { |
|
| 1043 | - $this->userinfo = $info; |
|
| 1044 | - } else { |
|
| 1045 | - $this->userinfo .= " ** $info"; |
|
| 1046 | - } |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - // }}} |
|
| 1050 | - // {{{ toString() |
|
| 1051 | - |
|
| 1052 | - /** |
|
| 1053 | - * Make a string representation of this object. |
|
| 1054 | - * |
|
| 1055 | - * @return string a string with an object summary |
|
| 1056 | - * @access public |
|
| 1057 | - */ |
|
| 1058 | - function toString() { |
|
| 1059 | - $modes = array(); |
|
| 1060 | - $levels = array(E_USER_NOTICE => 'notice', |
|
| 1061 | - E_USER_WARNING => 'warning', |
|
| 1062 | - E_USER_ERROR => 'error'); |
|
| 1063 | - if ($this->mode & PEAR_ERROR_CALLBACK) { |
|
| 1064 | - if (is_array($this->callback)) { |
|
| 1065 | - $callback = (is_object($this->callback[0]) ? |
|
| 1066 | - strtolower(get_class($this->callback[0])) : |
|
| 1067 | - $this->callback[0]) . '::' . |
|
| 1068 | - $this->callback[1]; |
|
| 1069 | - } else { |
|
| 1070 | - $callback = $this->callback; |
|
| 1071 | - } |
|
| 1072 | - return sprintf('[%s: message="%s" code=%d mode=callback '. |
|
| 1073 | - 'callback=%s prefix="%s" info="%s"]', |
|
| 1074 | - strtolower(get_class($this)), $this->message, $this->code, |
|
| 1075 | - $callback, $this->error_message_prefix, |
|
| 1076 | - $this->userinfo); |
|
| 1077 | - } |
|
| 1078 | - if ($this->mode & PEAR_ERROR_PRINT) { |
|
| 1079 | - $modes[] = 'print'; |
|
| 1080 | - } |
|
| 1081 | - if ($this->mode & PEAR_ERROR_TRIGGER) { |
|
| 1082 | - $modes[] = 'trigger'; |
|
| 1083 | - } |
|
| 1084 | - if ($this->mode & PEAR_ERROR_DIE) { |
|
| 1085 | - $modes[] = 'die'; |
|
| 1086 | - } |
|
| 1087 | - if ($this->mode & PEAR_ERROR_RETURN) { |
|
| 1088 | - $modes[] = 'return'; |
|
| 1089 | - } |
|
| 1090 | - return sprintf('[%s: message="%s" code=%d mode=%s level=%s '. |
|
| 1091 | - 'prefix="%s" info="%s"]', |
|
| 1092 | - strtolower(get_class($this)), $this->message, $this->code, |
|
| 1093 | - implode("|", $modes), $levels[$this->level], |
|
| 1094 | - $this->error_message_prefix, |
|
| 1095 | - $this->userinfo); |
|
| 1096 | - } |
|
| 1097 | - |
|
| 1098 | - // }}} |
|
| 824 | + // {{{ properties |
|
| 825 | + |
|
| 826 | + var $error_message_prefix = ''; |
|
| 827 | + var $mode = PEAR_ERROR_RETURN; |
|
| 828 | + var $level = E_USER_NOTICE; |
|
| 829 | + var $code = -1; |
|
| 830 | + var $message = ''; |
|
| 831 | + var $userinfo = ''; |
|
| 832 | + var $backtrace = null; |
|
| 833 | + |
|
| 834 | + // }}} |
|
| 835 | + // {{{ constructor |
|
| 836 | + |
|
| 837 | + /** |
|
| 838 | + * PEAR_Error constructor |
|
| 839 | + * |
|
| 840 | + * @param string $message message |
|
| 841 | + * |
|
| 842 | + * @param int $code (optional) error code |
|
| 843 | + * |
|
| 844 | + * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN, |
|
| 845 | + * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER, |
|
| 846 | + * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION |
|
| 847 | + * |
|
| 848 | + * @param mixed $options (optional) error level, _OR_ in the case of |
|
| 849 | + * PEAR_ERROR_CALLBACK, the callback function or object/method |
|
| 850 | + * tuple. |
|
| 851 | + * |
|
| 852 | + * @param string $userinfo (optional) additional user/debug info |
|
| 853 | + * |
|
| 854 | + * @access public |
|
| 855 | + * |
|
| 856 | + */ |
|
| 857 | + function PEAR_Error($message = 'unknown error', $code = null, |
|
| 858 | + $mode = null, $options = null, $userinfo = null) |
|
| 859 | + { |
|
| 860 | + if ($mode === null) { |
|
| 861 | + $mode = PEAR_ERROR_RETURN; |
|
| 862 | + } |
|
| 863 | + $this->message = $message; |
|
| 864 | + $this->code = $code; |
|
| 865 | + $this->mode = $mode; |
|
| 866 | + $this->userinfo = $userinfo; |
|
| 867 | + if (!PEAR::getStaticProperty('PEAR_Error', 'skiptrace')) { |
|
| 868 | + $this->backtrace = debug_backtrace(); |
|
| 869 | + if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) { |
|
| 870 | + unset($this->backtrace[0]['object']); |
|
| 871 | + } |
|
| 872 | + } |
|
| 873 | + if ($mode & PEAR_ERROR_CALLBACK) { |
|
| 874 | + $this->level = E_USER_NOTICE; |
|
| 875 | + $this->callback = $options; |
|
| 876 | + } else { |
|
| 877 | + if ($options === null) { |
|
| 878 | + $options = E_USER_NOTICE; |
|
| 879 | + } |
|
| 880 | + $this->level = $options; |
|
| 881 | + $this->callback = null; |
|
| 882 | + } |
|
| 883 | + if ($this->mode & PEAR_ERROR_PRINT) { |
|
| 884 | + if (is_null($options) || is_int($options)) { |
|
| 885 | + $format = "%s"; |
|
| 886 | + } else { |
|
| 887 | + $format = $options; |
|
| 888 | + } |
|
| 889 | + printf($format, $this->getMessage()); |
|
| 890 | + } |
|
| 891 | + if ($this->mode & PEAR_ERROR_TRIGGER) { |
|
| 892 | + trigger_error($this->getMessage(), $this->level); |
|
| 893 | + } |
|
| 894 | + if ($this->mode & PEAR_ERROR_DIE) { |
|
| 895 | + $msg = $this->getMessage(); |
|
| 896 | + if (is_null($options) || is_int($options)) { |
|
| 897 | + $format = "%s"; |
|
| 898 | + if (substr($msg, -1) != "\n") { |
|
| 899 | + $msg .= "\n"; |
|
| 900 | + } |
|
| 901 | + } else { |
|
| 902 | + $format = $options; |
|
| 903 | + } |
|
| 904 | + die(sprintf($format, $msg)); |
|
| 905 | + } |
|
| 906 | + if ($this->mode & PEAR_ERROR_CALLBACK) { |
|
| 907 | + if (is_callable($this->callback)) { |
|
| 908 | + call_user_func($this->callback, $this); |
|
| 909 | + } |
|
| 910 | + } |
|
| 911 | + if ($this->mode & PEAR_ERROR_EXCEPTION) { |
|
| 912 | + trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING); |
|
| 913 | + eval('$e = new Exception($this->message, $this->code);throw($e);'); |
|
| 914 | + } |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + // }}} |
|
| 918 | + // {{{ getMode() |
|
| 919 | + |
|
| 920 | + /** |
|
| 921 | + * Get the error mode from an error object. |
|
| 922 | + * |
|
| 923 | + * @return int error mode |
|
| 924 | + * @access public |
|
| 925 | + */ |
|
| 926 | + function getMode() { |
|
| 927 | + return $this->mode; |
|
| 928 | + } |
|
| 929 | + |
|
| 930 | + // }}} |
|
| 931 | + // {{{ getCallback() |
|
| 932 | + |
|
| 933 | + /** |
|
| 934 | + * Get the callback function/method from an error object. |
|
| 935 | + * |
|
| 936 | + * @return mixed callback function or object/method array |
|
| 937 | + * @access public |
|
| 938 | + */ |
|
| 939 | + function getCallback() { |
|
| 940 | + return $this->callback; |
|
| 941 | + } |
|
| 942 | + |
|
| 943 | + // }}} |
|
| 944 | + // {{{ getMessage() |
|
| 945 | + |
|
| 946 | + |
|
| 947 | + /** |
|
| 948 | + * Get the error message from an error object. |
|
| 949 | + * |
|
| 950 | + * @return string full error message |
|
| 951 | + * @access public |
|
| 952 | + */ |
|
| 953 | + function getMessage() |
|
| 954 | + { |
|
| 955 | + return ($this->error_message_prefix . $this->message); |
|
| 956 | + } |
|
| 957 | + |
|
| 958 | + |
|
| 959 | + // }}} |
|
| 960 | + // {{{ getCode() |
|
| 961 | + |
|
| 962 | + /** |
|
| 963 | + * Get error code from an error object |
|
| 964 | + * |
|
| 965 | + * @return int error code |
|
| 966 | + * @access public |
|
| 967 | + */ |
|
| 968 | + function getCode() |
|
| 969 | + { |
|
| 970 | + return $this->code; |
|
| 971 | + } |
|
| 972 | + |
|
| 973 | + // }}} |
|
| 974 | + // {{{ getType() |
|
| 975 | + |
|
| 976 | + /** |
|
| 977 | + * Get the name of this error/exception. |
|
| 978 | + * |
|
| 979 | + * @return string error/exception name (type) |
|
| 980 | + * @access public |
|
| 981 | + */ |
|
| 982 | + function getType() |
|
| 983 | + { |
|
| 984 | + return get_class($this); |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + // }}} |
|
| 988 | + // {{{ getUserInfo() |
|
| 989 | + |
|
| 990 | + /** |
|
| 991 | + * Get additional user-supplied information. |
|
| 992 | + * |
|
| 993 | + * @return string user-supplied information |
|
| 994 | + * @access public |
|
| 995 | + */ |
|
| 996 | + function getUserInfo() |
|
| 997 | + { |
|
| 998 | + return $this->userinfo; |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + // }}} |
|
| 1002 | + // {{{ getDebugInfo() |
|
| 1003 | + |
|
| 1004 | + /** |
|
| 1005 | + * Get additional debug information supplied by the application. |
|
| 1006 | + * |
|
| 1007 | + * @return string debug information |
|
| 1008 | + * @access public |
|
| 1009 | + */ |
|
| 1010 | + function getDebugInfo() |
|
| 1011 | + { |
|
| 1012 | + return $this->getUserInfo(); |
|
| 1013 | + } |
|
| 1014 | + |
|
| 1015 | + // }}} |
|
| 1016 | + // {{{ getBacktrace() |
|
| 1017 | + |
|
| 1018 | + /** |
|
| 1019 | + * Get the call backtrace from where the error was generated. |
|
| 1020 | + * Supported with PHP 4.3.0 or newer. |
|
| 1021 | + * |
|
| 1022 | + * @param int $frame (optional) what frame to fetch |
|
| 1023 | + * @return array Backtrace, or NULL if not available. |
|
| 1024 | + * @access public |
|
| 1025 | + */ |
|
| 1026 | + function getBacktrace($frame = null) |
|
| 1027 | + { |
|
| 1028 | + if (defined('PEAR_IGNORE_BACKTRACE')) { |
|
| 1029 | + return null; |
|
| 1030 | + } |
|
| 1031 | + if ($frame === null) { |
|
| 1032 | + return $this->backtrace; |
|
| 1033 | + } |
|
| 1034 | + return $this->backtrace[$frame]; |
|
| 1035 | + } |
|
| 1036 | + |
|
| 1037 | + // }}} |
|
| 1038 | + // {{{ addUserInfo() |
|
| 1039 | + |
|
| 1040 | + function addUserInfo($info) |
|
| 1041 | + { |
|
| 1042 | + if (empty($this->userinfo)) { |
|
| 1043 | + $this->userinfo = $info; |
|
| 1044 | + } else { |
|
| 1045 | + $this->userinfo .= " ** $info"; |
|
| 1046 | + } |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + // }}} |
|
| 1050 | + // {{{ toString() |
|
| 1051 | + |
|
| 1052 | + /** |
|
| 1053 | + * Make a string representation of this object. |
|
| 1054 | + * |
|
| 1055 | + * @return string a string with an object summary |
|
| 1056 | + * @access public |
|
| 1057 | + */ |
|
| 1058 | + function toString() { |
|
| 1059 | + $modes = array(); |
|
| 1060 | + $levels = array(E_USER_NOTICE => 'notice', |
|
| 1061 | + E_USER_WARNING => 'warning', |
|
| 1062 | + E_USER_ERROR => 'error'); |
|
| 1063 | + if ($this->mode & PEAR_ERROR_CALLBACK) { |
|
| 1064 | + if (is_array($this->callback)) { |
|
| 1065 | + $callback = (is_object($this->callback[0]) ? |
|
| 1066 | + strtolower(get_class($this->callback[0])) : |
|
| 1067 | + $this->callback[0]) . '::' . |
|
| 1068 | + $this->callback[1]; |
|
| 1069 | + } else { |
|
| 1070 | + $callback = $this->callback; |
|
| 1071 | + } |
|
| 1072 | + return sprintf('[%s: message="%s" code=%d mode=callback '. |
|
| 1073 | + 'callback=%s prefix="%s" info="%s"]', |
|
| 1074 | + strtolower(get_class($this)), $this->message, $this->code, |
|
| 1075 | + $callback, $this->error_message_prefix, |
|
| 1076 | + $this->userinfo); |
|
| 1077 | + } |
|
| 1078 | + if ($this->mode & PEAR_ERROR_PRINT) { |
|
| 1079 | + $modes[] = 'print'; |
|
| 1080 | + } |
|
| 1081 | + if ($this->mode & PEAR_ERROR_TRIGGER) { |
|
| 1082 | + $modes[] = 'trigger'; |
|
| 1083 | + } |
|
| 1084 | + if ($this->mode & PEAR_ERROR_DIE) { |
|
| 1085 | + $modes[] = 'die'; |
|
| 1086 | + } |
|
| 1087 | + if ($this->mode & PEAR_ERROR_RETURN) { |
|
| 1088 | + $modes[] = 'return'; |
|
| 1089 | + } |
|
| 1090 | + return sprintf('[%s: message="%s" code=%d mode=%s level=%s '. |
|
| 1091 | + 'prefix="%s" info="%s"]', |
|
| 1092 | + strtolower(get_class($this)), $this->message, $this->code, |
|
| 1093 | + implode("|", $modes), $levels[$this->level], |
|
| 1094 | + $this->error_message_prefix, |
|
| 1095 | + $this->userinfo); |
|
| 1096 | + } |
|
| 1097 | + |
|
| 1098 | + // }}} |
|
| 1099 | 1099 | } |
| 1100 | 1100 | |
| 1101 | 1101 | /* |
@@ -8,24 +8,24 @@ |
||
| 8 | 8 | |
| 9 | 9 | $version_select = '<select size="1" name="idn_version"><option value="2003">IDNA 2003</option><option value="2008"'; |
| 10 | 10 | if ($idn_version == 2008) { |
| 11 | - $version_select .= ' selected="selected"'; |
|
| 11 | + $version_select .= ' selected="selected"'; |
|
| 12 | 12 | } |
| 13 | 13 | $version_select .= '>IDNA 2008</option></select>'; |
| 14 | 14 | |
| 15 | 15 | if (isset($_REQUEST['encode'])) { |
| 16 | - $decoded = isset($_REQUEST['decoded']) ? stripslashes($_REQUEST['decoded']) : ''; |
|
| 17 | - $encoded = $IDN->encode($decoded); |
|
| 16 | + $decoded = isset($_REQUEST['decoded']) ? stripslashes($_REQUEST['decoded']) : ''; |
|
| 17 | + $encoded = $IDN->encode($decoded); |
|
| 18 | 18 | } |
| 19 | 19 | if (isset($_REQUEST['decode'])) { |
| 20 | - $encoded = isset($_REQUEST['encoded']) ? stripslashes($_REQUEST['encoded']) : ''; |
|
| 21 | - $decoded = $IDN->decode($encoded); |
|
| 20 | + $encoded = isset($_REQUEST['encoded']) ? stripslashes($_REQUEST['encoded']) : ''; |
|
| 21 | + $decoded = $IDN->decode($encoded); |
|
| 22 | 22 | } |
| 23 | 23 | $lang = 'en'; |
| 24 | 24 | if (isset($_REQUEST['lang'])) { |
| 25 | - if ('de' == $_REQUEST['lang'] || 'en' == $_REQUEST['lang']) { |
|
| 26 | - $lang = $_REQUEST['lang']; |
|
| 27 | - $add .= '<input type="hidden" name="lang" value="'.$lang.'" />'."\n"; |
|
| 28 | - } |
|
| 25 | + if ('de' == $_REQUEST['lang'] || 'en' == $_REQUEST['lang']) { |
|
| 26 | + $lang = $_REQUEST['lang']; |
|
| 27 | + $add .= '<input type="hidden" name="lang" value="'.$lang.'" />'."\n"; |
|
| 28 | + } |
|
| 29 | 29 | } |
| 30 | 30 | ?> |
| 31 | 31 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
@@ -24,7 +24,7 @@ |
||
| 24 | 24 | if (isset($_REQUEST['lang'])) { |
| 25 | 25 | if ('de' == $_REQUEST['lang'] || 'en' == $_REQUEST['lang']) { |
| 26 | 26 | $lang = $_REQUEST['lang']; |
| 27 | - $add .= '<input type="hidden" name="lang" value="'.$lang.'" />'."\n"; |
|
| 27 | + $add .= '<input type="hidden" name="lang" value="' . $lang . '" />' . "\n"; |
|
| 28 | 28 | } |
| 29 | 29 | } |
| 30 | 30 | ?> |
@@ -1,11 +1,11 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * transcode wrapper functions |
|
| 4 | - * @package IDNA Convert |
|
| 5 | - * @subpackage charset transcoding |
|
| 6 | - * @author Matthias Sommerfeld, <[email protected]> |
|
| 7 | - * @version 0.1.0 |
|
| 8 | - */ |
|
| 3 | + * transcode wrapper functions |
|
| 4 | + * @package IDNA Convert |
|
| 5 | + * @subpackage charset transcoding |
|
| 6 | + * @author Matthias Sommerfeld, <[email protected]> |
|
| 7 | + * @version 0.1.0 |
|
| 8 | + */ |
|
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * Convert a string from any of various encodings to UTF-8 |
@@ -18,29 +18,29 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | function encode_utf8($string = '', $encoding = 'iso-8859-1', $safe_mode = false) |
| 20 | 20 | { |
| 21 | - $safe = ($safe_mode) ? $string : false; |
|
| 22 | - if (strtoupper($encoding) == 'UTF-8' || strtoupper($encoding) == 'UTF8') { |
|
| 23 | - return $string; |
|
| 24 | - } elseif (strtoupper($encoding) == 'ISO-8859-1') { |
|
| 25 | - return utf8_encode($string); |
|
| 26 | - } elseif (strtoupper($encoding) == 'WINDOWS-1252') { |
|
| 27 | - return utf8_encode(map_w1252_iso8859_1($string)); |
|
| 28 | - } elseif (strtoupper($encoding) == 'UNICODE-1-1-UTF-7') { |
|
| 29 | - $encoding = 'utf-7'; |
|
| 30 | - } |
|
| 31 | - if (function_exists('mb_convert_encoding')) { |
|
| 32 | - $conv = @mb_convert_encoding($string, 'UTF-8', strtoupper($encoding)); |
|
| 33 | - if ($conv) return $conv; |
|
| 34 | - } |
|
| 35 | - if (function_exists('iconv')) { |
|
| 36 | - $conv = @iconv(strtoupper($encoding), 'UTF-8', $string); |
|
| 37 | - if ($conv) return $conv; |
|
| 38 | - } |
|
| 39 | - if (function_exists('libiconv')) { |
|
| 40 | - $conv = @libiconv(strtoupper($encoding), 'UTF-8', $string); |
|
| 41 | - if ($conv) return $conv; |
|
| 42 | - } |
|
| 43 | - return $safe; |
|
| 21 | + $safe = ($safe_mode) ? $string : false; |
|
| 22 | + if (strtoupper($encoding) == 'UTF-8' || strtoupper($encoding) == 'UTF8') { |
|
| 23 | + return $string; |
|
| 24 | + } elseif (strtoupper($encoding) == 'ISO-8859-1') { |
|
| 25 | + return utf8_encode($string); |
|
| 26 | + } elseif (strtoupper($encoding) == 'WINDOWS-1252') { |
|
| 27 | + return utf8_encode(map_w1252_iso8859_1($string)); |
|
| 28 | + } elseif (strtoupper($encoding) == 'UNICODE-1-1-UTF-7') { |
|
| 29 | + $encoding = 'utf-7'; |
|
| 30 | + } |
|
| 31 | + if (function_exists('mb_convert_encoding')) { |
|
| 32 | + $conv = @mb_convert_encoding($string, 'UTF-8', strtoupper($encoding)); |
|
| 33 | + if ($conv) return $conv; |
|
| 34 | + } |
|
| 35 | + if (function_exists('iconv')) { |
|
| 36 | + $conv = @iconv(strtoupper($encoding), 'UTF-8', $string); |
|
| 37 | + if ($conv) return $conv; |
|
| 38 | + } |
|
| 39 | + if (function_exists('libiconv')) { |
|
| 40 | + $conv = @libiconv(strtoupper($encoding), 'UTF-8', $string); |
|
| 41 | + if ($conv) return $conv; |
|
| 42 | + } |
|
| 43 | + return $safe; |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
@@ -54,30 +54,30 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | function decode_utf8($string = '', $encoding = 'iso-8859-1', $safe_mode = false) |
| 56 | 56 | { |
| 57 | - $safe = ($safe_mode) ? $string : false; |
|
| 58 | - if (!$encoding) $encoding = 'ISO-8859-1'; |
|
| 59 | - if (strtoupper($encoding) == 'UTF-8' || strtoupper($encoding) == 'UTF8') { |
|
| 60 | - return $string; |
|
| 61 | - } elseif (strtoupper($encoding) == 'ISO-8859-1') { |
|
| 62 | - return utf8_decode($string); |
|
| 63 | - } elseif (strtoupper($encoding) == 'WINDOWS-1252') { |
|
| 64 | - return map_iso8859_1_w1252(utf8_decode($string)); |
|
| 65 | - } elseif (strtoupper($encoding) == 'UNICODE-1-1-UTF-7') { |
|
| 66 | - $encoding = 'utf-7'; |
|
| 67 | - } |
|
| 68 | - if (function_exists('mb_convert_encoding')) { |
|
| 69 | - $conv = @mb_convert_encoding($string, strtoupper($encoding), 'UTF-8'); |
|
| 70 | - if ($conv) return $conv; |
|
| 71 | - } |
|
| 72 | - if (function_exists('iconv')) { |
|
| 73 | - $conv = @iconv('UTF-8', strtoupper($encoding), $string); |
|
| 74 | - if ($conv) return $conv; |
|
| 75 | - } |
|
| 76 | - if (function_exists('libiconv')) { |
|
| 77 | - $conv = @libiconv('UTF-8', strtoupper($encoding), $string); |
|
| 78 | - if ($conv) return $conv; |
|
| 79 | - } |
|
| 80 | - return $safe; |
|
| 57 | + $safe = ($safe_mode) ? $string : false; |
|
| 58 | + if (!$encoding) $encoding = 'ISO-8859-1'; |
|
| 59 | + if (strtoupper($encoding) == 'UTF-8' || strtoupper($encoding) == 'UTF8') { |
|
| 60 | + return $string; |
|
| 61 | + } elseif (strtoupper($encoding) == 'ISO-8859-1') { |
|
| 62 | + return utf8_decode($string); |
|
| 63 | + } elseif (strtoupper($encoding) == 'WINDOWS-1252') { |
|
| 64 | + return map_iso8859_1_w1252(utf8_decode($string)); |
|
| 65 | + } elseif (strtoupper($encoding) == 'UNICODE-1-1-UTF-7') { |
|
| 66 | + $encoding = 'utf-7'; |
|
| 67 | + } |
|
| 68 | + if (function_exists('mb_convert_encoding')) { |
|
| 69 | + $conv = @mb_convert_encoding($string, strtoupper($encoding), 'UTF-8'); |
|
| 70 | + if ($conv) return $conv; |
|
| 71 | + } |
|
| 72 | + if (function_exists('iconv')) { |
|
| 73 | + $conv = @iconv('UTF-8', strtoupper($encoding), $string); |
|
| 74 | + if ($conv) return $conv; |
|
| 75 | + } |
|
| 76 | + if (function_exists('libiconv')) { |
|
| 77 | + $conv = @libiconv('UTF-8', strtoupper($encoding), $string); |
|
| 78 | + if ($conv) return $conv; |
|
| 79 | + } |
|
| 80 | + return $safe; |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | /** |
@@ -89,22 +89,22 @@ discard block |
||
| 89 | 89 | */ |
| 90 | 90 | function map_w1252_iso8859_1($string = '') |
| 91 | 91 | { |
| 92 | - if ($string == '') return ''; |
|
| 93 | - $return = ''; |
|
| 94 | - for ($i = 0; $i < strlen($string); ++$i) { |
|
| 95 | - $c = ord($string{$i}); |
|
| 96 | - switch ($c) { |
|
| 97 | - case 129: $return .= chr(252); break; |
|
| 98 | - case 132: $return .= chr(228); break; |
|
| 99 | - case 142: $return .= chr(196); break; |
|
| 100 | - case 148: $return .= chr(246); break; |
|
| 101 | - case 153: $return .= chr(214); break; |
|
| 102 | - case 154: $return .= chr(220); break; |
|
| 103 | - case 225: $return .= chr(223); break; |
|
| 104 | - default: $return .= chr($c); break; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - return $return; |
|
| 92 | + if ($string == '') return ''; |
|
| 93 | + $return = ''; |
|
| 94 | + for ($i = 0; $i < strlen($string); ++$i) { |
|
| 95 | + $c = ord($string{$i}); |
|
| 96 | + switch ($c) { |
|
| 97 | + case 129: $return .= chr(252); break; |
|
| 98 | + case 132: $return .= chr(228); break; |
|
| 99 | + case 142: $return .= chr(196); break; |
|
| 100 | + case 148: $return .= chr(246); break; |
|
| 101 | + case 153: $return .= chr(214); break; |
|
| 102 | + case 154: $return .= chr(220); break; |
|
| 103 | + case 225: $return .= chr(223); break; |
|
| 104 | + default: $return .= chr($c); break; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + return $return; |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
@@ -116,22 +116,22 @@ discard block |
||
| 116 | 116 | */ |
| 117 | 117 | function map_iso8859_1_w1252($string = '') |
| 118 | 118 | { |
| 119 | - if ($string == '') return ''; |
|
| 120 | - $return = ''; |
|
| 121 | - for ($i = 0; $i < strlen($string); ++$i) { |
|
| 122 | - $c = ord($string{$i}); |
|
| 123 | - switch ($c) { |
|
| 124 | - case 196: $return .= chr(142); break; |
|
| 125 | - case 214: $return .= chr(153); break; |
|
| 126 | - case 220: $return .= chr(154); break; |
|
| 127 | - case 223: $return .= chr(225); break; |
|
| 128 | - case 228: $return .= chr(132); break; |
|
| 129 | - case 246: $return .= chr(148); break; |
|
| 130 | - case 252: $return .= chr(129); break; |
|
| 131 | - default: $return .= chr($c); break; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - return $return; |
|
| 119 | + if ($string == '') return ''; |
|
| 120 | + $return = ''; |
|
| 121 | + for ($i = 0; $i < strlen($string); ++$i) { |
|
| 122 | + $c = ord($string{$i}); |
|
| 123 | + switch ($c) { |
|
| 124 | + case 196: $return .= chr(142); break; |
|
| 125 | + case 214: $return .= chr(153); break; |
|
| 126 | + case 220: $return .= chr(154); break; |
|
| 127 | + case 223: $return .= chr(225); break; |
|
| 128 | + case 228: $return .= chr(132); break; |
|
| 129 | + case 246: $return .= chr(148); break; |
|
| 130 | + case 252: $return .= chr(129); break; |
|
| 131 | + default: $return .= chr($c); break; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + return $return; |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | ?> |
| 138 | 138 | \ No newline at end of file |
@@ -30,15 +30,21 @@ discard block |
||
| 30 | 30 | } |
| 31 | 31 | if (function_exists('mb_convert_encoding')) { |
| 32 | 32 | $conv = @mb_convert_encoding($string, 'UTF-8', strtoupper($encoding)); |
| 33 | - if ($conv) return $conv; |
|
| 33 | + if ($conv) { |
|
| 34 | + return $conv; |
|
| 35 | + } |
|
| 34 | 36 | } |
| 35 | 37 | if (function_exists('iconv')) { |
| 36 | 38 | $conv = @iconv(strtoupper($encoding), 'UTF-8', $string); |
| 37 | - if ($conv) return $conv; |
|
| 39 | + if ($conv) { |
|
| 40 | + return $conv; |
|
| 41 | + } |
|
| 38 | 42 | } |
| 39 | 43 | if (function_exists('libiconv')) { |
| 40 | 44 | $conv = @libiconv(strtoupper($encoding), 'UTF-8', $string); |
| 41 | - if ($conv) return $conv; |
|
| 45 | + if ($conv) { |
|
| 46 | + return $conv; |
|
| 47 | + } |
|
| 42 | 48 | } |
| 43 | 49 | return $safe; |
| 44 | 50 | } |
@@ -55,7 +61,9 @@ discard block |
||
| 55 | 61 | function decode_utf8($string = '', $encoding = 'iso-8859-1', $safe_mode = false) |
| 56 | 62 | { |
| 57 | 63 | $safe = ($safe_mode) ? $string : false; |
| 58 | - if (!$encoding) $encoding = 'ISO-8859-1'; |
|
| 64 | + if (!$encoding) { |
|
| 65 | + $encoding = 'ISO-8859-1'; |
|
| 66 | + } |
|
| 59 | 67 | if (strtoupper($encoding) == 'UTF-8' || strtoupper($encoding) == 'UTF8') { |
| 60 | 68 | return $string; |
| 61 | 69 | } elseif (strtoupper($encoding) == 'ISO-8859-1') { |
@@ -67,15 +75,21 @@ discard block |
||
| 67 | 75 | } |
| 68 | 76 | if (function_exists('mb_convert_encoding')) { |
| 69 | 77 | $conv = @mb_convert_encoding($string, strtoupper($encoding), 'UTF-8'); |
| 70 | - if ($conv) return $conv; |
|
| 78 | + if ($conv) { |
|
| 79 | + return $conv; |
|
| 80 | + } |
|
| 71 | 81 | } |
| 72 | 82 | if (function_exists('iconv')) { |
| 73 | 83 | $conv = @iconv('UTF-8', strtoupper($encoding), $string); |
| 74 | - if ($conv) return $conv; |
|
| 84 | + if ($conv) { |
|
| 85 | + return $conv; |
|
| 86 | + } |
|
| 75 | 87 | } |
| 76 | 88 | if (function_exists('libiconv')) { |
| 77 | 89 | $conv = @libiconv('UTF-8', strtoupper($encoding), $string); |
| 78 | - if ($conv) return $conv; |
|
| 90 | + if ($conv) { |
|
| 91 | + return $conv; |
|
| 92 | + } |
|
| 79 | 93 | } |
| 80 | 94 | return $safe; |
| 81 | 95 | } |
@@ -89,7 +103,9 @@ discard block |
||
| 89 | 103 | */ |
| 90 | 104 | function map_w1252_iso8859_1($string = '') |
| 91 | 105 | { |
| 92 | - if ($string == '') return ''; |
|
| 106 | + if ($string == '') { |
|
| 107 | + return ''; |
|
| 108 | + } |
|
| 93 | 109 | $return = ''; |
| 94 | 110 | for ($i = 0; $i < strlen($string); ++$i) { |
| 95 | 111 | $c = ord($string{$i}); |
@@ -116,7 +132,9 @@ discard block |
||
| 116 | 132 | */ |
| 117 | 133 | function map_iso8859_1_w1252($string = '') |
| 118 | 134 | { |
| 119 | - if ($string == '') return ''; |
|
| 135 | + if ($string == '') { |
|
| 136 | + return ''; |
|
| 137 | + } |
|
| 120 | 138 | $return = ''; |
| 121 | 139 | for ($i = 0; $i < strlen($string); ++$i) { |
| 122 | 140 | $c = ord($string{$i}); |
@@ -16,285 +16,285 @@ |
||
| 16 | 16 | * @version 0.0.6 2009-05-10 |
| 17 | 17 | */ |
| 18 | 18 | class uctc { |
| 19 | - private static $mechs = array('ucs4', /*'ucs4le', 'ucs4be', */'ucs4array', /*'utf16', 'utf16le', 'utf16be', */'utf8', 'utf7', 'utf7imap'); |
|
| 20 | - private static $allow_overlong = false; |
|
| 21 | - private static $safe_mode; |
|
| 22 | - private static $safe_char; |
|
| 19 | + private static $mechs = array('ucs4', /*'ucs4le', 'ucs4be', */'ucs4array', /*'utf16', 'utf16le', 'utf16be', */'utf8', 'utf7', 'utf7imap'); |
|
| 20 | + private static $allow_overlong = false; |
|
| 21 | + private static $safe_mode; |
|
| 22 | + private static $safe_char; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The actual conversion routine |
|
| 26 | - * |
|
| 27 | - * @param mixed $data The data to convert, usually a string, array when converting from UCS-4 array |
|
| 28 | - * @param string $from Original encoding of the data |
|
| 29 | - * @param string $to Target encoding of the data |
|
| 30 | - * @param bool $safe_mode SafeMode tries to correct invalid codepoints |
|
| 31 | - * @return mixed False on failure, String or array on success, depending on target encoding |
|
| 32 | - * @access public |
|
| 33 | - * @since 0.0.1 |
|
| 34 | - */ |
|
| 35 | - public static function convert($data, $from, $to, $safe_mode = false, $safe_char = 0xFFFC) |
|
| 36 | - { |
|
| 37 | - self::$safe_mode = ($safe_mode) ? true : false; |
|
| 38 | - self::$safe_char = ($safe_char) ? $safe_char : 0xFFFC; |
|
| 39 | - if (self::$safe_mode) self::$allow_overlong = true; |
|
| 40 | - if (!in_array($from, self::$mechs)) throw new Exception('Invalid input format specified'); |
|
| 41 | - if (!in_array($to, self::$mechs)) throw new Exception('Invalid output format specified'); |
|
| 42 | - if ($from != 'ucs4array') eval('$data = self::'.$from.'_ucs4array($data);'); |
|
| 43 | - if ($to != 'ucs4array') eval('$data = self::ucs4array_'.$to.'($data);'); |
|
| 44 | - return $data; |
|
| 45 | - } |
|
| 24 | + /** |
|
| 25 | + * The actual conversion routine |
|
| 26 | + * |
|
| 27 | + * @param mixed $data The data to convert, usually a string, array when converting from UCS-4 array |
|
| 28 | + * @param string $from Original encoding of the data |
|
| 29 | + * @param string $to Target encoding of the data |
|
| 30 | + * @param bool $safe_mode SafeMode tries to correct invalid codepoints |
|
| 31 | + * @return mixed False on failure, String or array on success, depending on target encoding |
|
| 32 | + * @access public |
|
| 33 | + * @since 0.0.1 |
|
| 34 | + */ |
|
| 35 | + public static function convert($data, $from, $to, $safe_mode = false, $safe_char = 0xFFFC) |
|
| 36 | + { |
|
| 37 | + self::$safe_mode = ($safe_mode) ? true : false; |
|
| 38 | + self::$safe_char = ($safe_char) ? $safe_char : 0xFFFC; |
|
| 39 | + if (self::$safe_mode) self::$allow_overlong = true; |
|
| 40 | + if (!in_array($from, self::$mechs)) throw new Exception('Invalid input format specified'); |
|
| 41 | + if (!in_array($to, self::$mechs)) throw new Exception('Invalid output format specified'); |
|
| 42 | + if ($from != 'ucs4array') eval('$data = self::'.$from.'_ucs4array($data);'); |
|
| 43 | + if ($to != 'ucs4array') eval('$data = self::ucs4array_'.$to.'($data);'); |
|
| 44 | + return $data; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * This converts an UTF-8 encoded string to its UCS-4 representation |
|
| 49 | - * |
|
| 50 | - * @param string $input The UTF-8 string to convert |
|
| 51 | - * @return array Array of 32bit values representing each codepoint |
|
| 52 | - * @access private |
|
| 53 | - */ |
|
| 54 | - private static function utf8_ucs4array($input) |
|
| 55 | - { |
|
| 56 | - $output = array(); |
|
| 57 | - $out_len = 0; |
|
| 58 | - $inp_len = strlen($input); |
|
| 59 | - $mode = 'next'; |
|
| 60 | - $test = 'none'; |
|
| 61 | - for ($k = 0; $k < $inp_len; ++$k) { |
|
| 62 | - $v = ord($input{$k}); // Extract byte from input string |
|
| 47 | + /** |
|
| 48 | + * This converts an UTF-8 encoded string to its UCS-4 representation |
|
| 49 | + * |
|
| 50 | + * @param string $input The UTF-8 string to convert |
|
| 51 | + * @return array Array of 32bit values representing each codepoint |
|
| 52 | + * @access private |
|
| 53 | + */ |
|
| 54 | + private static function utf8_ucs4array($input) |
|
| 55 | + { |
|
| 56 | + $output = array(); |
|
| 57 | + $out_len = 0; |
|
| 58 | + $inp_len = strlen($input); |
|
| 59 | + $mode = 'next'; |
|
| 60 | + $test = 'none'; |
|
| 61 | + for ($k = 0; $k < $inp_len; ++$k) { |
|
| 62 | + $v = ord($input{$k}); // Extract byte from input string |
|
| 63 | 63 | |
| 64 | - if ($v < 128) { // We found an ASCII char - put into stirng as is |
|
| 65 | - $output[$out_len] = $v; |
|
| 66 | - ++$out_len; |
|
| 67 | - if ('add' == $mode) { |
|
| 68 | - if (self::$safe_mode) { |
|
| 69 | - $output[$out_len-2] = self::$safe_char; |
|
| 70 | - $mode = 'next'; |
|
| 71 | - } else { |
|
| 72 | - throw new Exception('Conversion from UTF-8 to UCS-4 failed: malformed input at byte '.$k); |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - continue; |
|
| 76 | - } |
|
| 77 | - if ('next' == $mode) { // Try to find the next start byte; determine the width of the Unicode char |
|
| 78 | - $start_byte = $v; |
|
| 79 | - $mode = 'add'; |
|
| 80 | - $test = 'range'; |
|
| 81 | - if ($v >> 5 == 6) { // &110xxxxx 10xxxxx |
|
| 82 | - $next_byte = 0; // Tells, how many times subsequent bitmasks must rotate 6bits to the left |
|
| 83 | - $v = ($v - 192) << 6; |
|
| 84 | - } elseif ($v >> 4 == 14) { // &1110xxxx 10xxxxxx 10xxxxxx |
|
| 85 | - $next_byte = 1; |
|
| 86 | - $v = ($v - 224) << 12; |
|
| 87 | - } elseif ($v >> 3 == 30) { // &11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
|
| 88 | - $next_byte = 2; |
|
| 89 | - $v = ($v - 240) << 18; |
|
| 90 | - } elseif (self::$safe_mode) { |
|
| 91 | - $mode = 'next'; |
|
| 92 | - $output[$out_len] = self::$safe_char; |
|
| 93 | - ++$out_len; |
|
| 94 | - continue; |
|
| 95 | - } else { |
|
| 96 | - throw new Exception('This might be UTF-8, but I don\'t understand it at byte '.$k); |
|
| 97 | - } |
|
| 98 | - if ($inp_len-$k-$next_byte < 2) { |
|
| 99 | - $output[$out_len] = self::$safe_char; |
|
| 100 | - $mode = 'no'; |
|
| 101 | - continue; |
|
| 102 | - } |
|
| 64 | + if ($v < 128) { // We found an ASCII char - put into stirng as is |
|
| 65 | + $output[$out_len] = $v; |
|
| 66 | + ++$out_len; |
|
| 67 | + if ('add' == $mode) { |
|
| 68 | + if (self::$safe_mode) { |
|
| 69 | + $output[$out_len-2] = self::$safe_char; |
|
| 70 | + $mode = 'next'; |
|
| 71 | + } else { |
|
| 72 | + throw new Exception('Conversion from UTF-8 to UCS-4 failed: malformed input at byte '.$k); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + continue; |
|
| 76 | + } |
|
| 77 | + if ('next' == $mode) { // Try to find the next start byte; determine the width of the Unicode char |
|
| 78 | + $start_byte = $v; |
|
| 79 | + $mode = 'add'; |
|
| 80 | + $test = 'range'; |
|
| 81 | + if ($v >> 5 == 6) { // &110xxxxx 10xxxxx |
|
| 82 | + $next_byte = 0; // Tells, how many times subsequent bitmasks must rotate 6bits to the left |
|
| 83 | + $v = ($v - 192) << 6; |
|
| 84 | + } elseif ($v >> 4 == 14) { // &1110xxxx 10xxxxxx 10xxxxxx |
|
| 85 | + $next_byte = 1; |
|
| 86 | + $v = ($v - 224) << 12; |
|
| 87 | + } elseif ($v >> 3 == 30) { // &11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
|
| 88 | + $next_byte = 2; |
|
| 89 | + $v = ($v - 240) << 18; |
|
| 90 | + } elseif (self::$safe_mode) { |
|
| 91 | + $mode = 'next'; |
|
| 92 | + $output[$out_len] = self::$safe_char; |
|
| 93 | + ++$out_len; |
|
| 94 | + continue; |
|
| 95 | + } else { |
|
| 96 | + throw new Exception('This might be UTF-8, but I don\'t understand it at byte '.$k); |
|
| 97 | + } |
|
| 98 | + if ($inp_len-$k-$next_byte < 2) { |
|
| 99 | + $output[$out_len] = self::$safe_char; |
|
| 100 | + $mode = 'no'; |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - if ('add' == $mode) { |
|
| 105 | - $output[$out_len] = (int) $v; |
|
| 106 | - ++$out_len; |
|
| 107 | - continue; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - if ('add' == $mode) { |
|
| 111 | - if (!self::$allow_overlong && $test == 'range') { |
|
| 112 | - $test = 'none'; |
|
| 113 | - if (($v < 0xA0 && $start_byte == 0xE0) || ($v < 0x90 && $start_byte == 0xF0) || ($v > 0x8F && $start_byte == 0xF4)) { |
|
| 114 | - throw new Exception('Bogus UTF-8 character detected (out of legal range) at byte '.$k); |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - if ($v >> 6 == 2) { // Bit mask must be 10xxxxxx |
|
| 118 | - $v = ($v-128) << ($next_byte*6); |
|
| 119 | - $output[($out_len-1)] += $v; |
|
| 120 | - --$next_byte; |
|
| 121 | - } else { |
|
| 122 | - if (self::$safe_mode) { |
|
| 123 | - $output[$out_len-1] = ord(self::$safe_char); |
|
| 124 | - $k--; |
|
| 125 | - $mode = 'next'; |
|
| 126 | - continue; |
|
| 127 | - } else { |
|
| 128 | - throw new Exception('Conversion from UTF-8 to UCS-4 failed: malformed input at byte '.$k); |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - if ($next_byte < 0) { |
|
| 132 | - $mode = 'next'; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - } // for |
|
| 136 | - return $output; |
|
| 137 | - } |
|
| 104 | + if ('add' == $mode) { |
|
| 105 | + $output[$out_len] = (int) $v; |
|
| 106 | + ++$out_len; |
|
| 107 | + continue; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + if ('add' == $mode) { |
|
| 111 | + if (!self::$allow_overlong && $test == 'range') { |
|
| 112 | + $test = 'none'; |
|
| 113 | + if (($v < 0xA0 && $start_byte == 0xE0) || ($v < 0x90 && $start_byte == 0xF0) || ($v > 0x8F && $start_byte == 0xF4)) { |
|
| 114 | + throw new Exception('Bogus UTF-8 character detected (out of legal range) at byte '.$k); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + if ($v >> 6 == 2) { // Bit mask must be 10xxxxxx |
|
| 118 | + $v = ($v-128) << ($next_byte*6); |
|
| 119 | + $output[($out_len-1)] += $v; |
|
| 120 | + --$next_byte; |
|
| 121 | + } else { |
|
| 122 | + if (self::$safe_mode) { |
|
| 123 | + $output[$out_len-1] = ord(self::$safe_char); |
|
| 124 | + $k--; |
|
| 125 | + $mode = 'next'; |
|
| 126 | + continue; |
|
| 127 | + } else { |
|
| 128 | + throw new Exception('Conversion from UTF-8 to UCS-4 failed: malformed input at byte '.$k); |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + if ($next_byte < 0) { |
|
| 132 | + $mode = 'next'; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + } // for |
|
| 136 | + return $output; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Convert UCS-4 string into UTF-8 string |
|
| 141 | - * See utf8_ucs4array() for details |
|
| 142 | - * @access private |
|
| 143 | - */ |
|
| 144 | - private static function ucs4array_utf8($input) |
|
| 145 | - { |
|
| 146 | - $output = ''; |
|
| 147 | - foreach ($input as $v) { |
|
| 148 | - if ($v < 128) { // 7bit are transferred literally |
|
| 149 | - $output .= chr($v); |
|
| 150 | - } elseif ($v < (1 << 11)) { // 2 bytes |
|
| 151 | - $output .= chr(192+($v >> 6)).chr(128+($v & 63)); |
|
| 152 | - } elseif ($v < (1 << 16)) { // 3 bytes |
|
| 153 | - $output .= chr(224+($v >> 12)).chr(128+(($v >> 6) & 63)).chr(128+($v & 63)); |
|
| 154 | - } elseif ($v < (1 << 21)) { // 4 bytes |
|
| 155 | - $output .= chr(240+($v >> 18)).chr(128+(($v >> 12) & 63)).chr(128+(($v >> 6) & 63)).chr(128+($v & 63)); |
|
| 156 | - } elseif (self::$safe_mode) { |
|
| 157 | - $output .= self::$safe_char; |
|
| 158 | - } else { |
|
| 159 | - throw new Exception('Conversion from UCS-4 to UTF-8 failed: malformed input at byte '.$k); |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - return $output; |
|
| 163 | - } |
|
| 139 | + /** |
|
| 140 | + * Convert UCS-4 string into UTF-8 string |
|
| 141 | + * See utf8_ucs4array() for details |
|
| 142 | + * @access private |
|
| 143 | + */ |
|
| 144 | + private static function ucs4array_utf8($input) |
|
| 145 | + { |
|
| 146 | + $output = ''; |
|
| 147 | + foreach ($input as $v) { |
|
| 148 | + if ($v < 128) { // 7bit are transferred literally |
|
| 149 | + $output .= chr($v); |
|
| 150 | + } elseif ($v < (1 << 11)) { // 2 bytes |
|
| 151 | + $output .= chr(192+($v >> 6)).chr(128+($v & 63)); |
|
| 152 | + } elseif ($v < (1 << 16)) { // 3 bytes |
|
| 153 | + $output .= chr(224+($v >> 12)).chr(128+(($v >> 6) & 63)).chr(128+($v & 63)); |
|
| 154 | + } elseif ($v < (1 << 21)) { // 4 bytes |
|
| 155 | + $output .= chr(240+($v >> 18)).chr(128+(($v >> 12) & 63)).chr(128+(($v >> 6) & 63)).chr(128+($v & 63)); |
|
| 156 | + } elseif (self::$safe_mode) { |
|
| 157 | + $output .= self::$safe_char; |
|
| 158 | + } else { |
|
| 159 | + throw new Exception('Conversion from UCS-4 to UTF-8 failed: malformed input at byte '.$k); |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + return $output; |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - private static function utf7imap_ucs4array($input) |
|
| 166 | - { |
|
| 167 | - return self::utf7_ucs4array(str_replace(',', '/', $input), '&'); |
|
| 168 | - } |
|
| 165 | + private static function utf7imap_ucs4array($input) |
|
| 166 | + { |
|
| 167 | + return self::utf7_ucs4array(str_replace(',', '/', $input), '&'); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - private static function utf7_ucs4array($input, $sc = '+') |
|
| 171 | - { |
|
| 172 | - $output = array(); |
|
| 173 | - $out_len = 0; |
|
| 174 | - $inp_len = strlen($input); |
|
| 175 | - $mode = 'd'; |
|
| 176 | - $b64 = ''; |
|
| 170 | + private static function utf7_ucs4array($input, $sc = '+') |
|
| 171 | + { |
|
| 172 | + $output = array(); |
|
| 173 | + $out_len = 0; |
|
| 174 | + $inp_len = strlen($input); |
|
| 175 | + $mode = 'd'; |
|
| 176 | + $b64 = ''; |
|
| 177 | 177 | |
| 178 | - for ($k = 0; $k < $inp_len; ++$k) { |
|
| 179 | - $c = $input{$k}; |
|
| 180 | - if (0 == ord($c)) continue; // Ignore zero bytes |
|
| 181 | - if ('b' == $mode) { |
|
| 182 | - // Sequence got terminated |
|
| 183 | - if (!preg_match('![A-Za-z0-9/'.preg_quote($sc, '!').']!', $c)) { |
|
| 184 | - if ('-' == $c) { |
|
| 185 | - if ($b64 == '') { |
|
| 186 | - $output[$out_len] = ord($sc); |
|
| 187 | - $out_len++; |
|
| 188 | - $mode = 'd'; |
|
| 189 | - continue; |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - $tmp = base64_decode($b64); |
|
| 193 | - $tmp = substr($tmp, -1 * (strlen($tmp) % 2)); |
|
| 194 | - for ($i = 0; $i < strlen($tmp); $i++) { |
|
| 195 | - if ($i % 2) { |
|
| 196 | - $output[$out_len] += ord($tmp{$i}); |
|
| 197 | - $out_len++; |
|
| 198 | - } else { |
|
| 199 | - $output[$out_len] = ord($tmp{$i}) << 8; |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - $mode = 'd'; |
|
| 203 | - $b64 = ''; |
|
| 204 | - continue; |
|
| 205 | - } else { |
|
| 206 | - $b64 .= $c; |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - if ('d' == $mode) { |
|
| 210 | - if ($sc == $c) { |
|
| 211 | - $mode = 'b'; |
|
| 212 | - continue; |
|
| 213 | - } |
|
| 214 | - $output[$out_len] = ord($c); |
|
| 215 | - $out_len++; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - return $output; |
|
| 219 | - } |
|
| 178 | + for ($k = 0; $k < $inp_len; ++$k) { |
|
| 179 | + $c = $input{$k}; |
|
| 180 | + if (0 == ord($c)) continue; // Ignore zero bytes |
|
| 181 | + if ('b' == $mode) { |
|
| 182 | + // Sequence got terminated |
|
| 183 | + if (!preg_match('![A-Za-z0-9/'.preg_quote($sc, '!').']!', $c)) { |
|
| 184 | + if ('-' == $c) { |
|
| 185 | + if ($b64 == '') { |
|
| 186 | + $output[$out_len] = ord($sc); |
|
| 187 | + $out_len++; |
|
| 188 | + $mode = 'd'; |
|
| 189 | + continue; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + $tmp = base64_decode($b64); |
|
| 193 | + $tmp = substr($tmp, -1 * (strlen($tmp) % 2)); |
|
| 194 | + for ($i = 0; $i < strlen($tmp); $i++) { |
|
| 195 | + if ($i % 2) { |
|
| 196 | + $output[$out_len] += ord($tmp{$i}); |
|
| 197 | + $out_len++; |
|
| 198 | + } else { |
|
| 199 | + $output[$out_len] = ord($tmp{$i}) << 8; |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + $mode = 'd'; |
|
| 203 | + $b64 = ''; |
|
| 204 | + continue; |
|
| 205 | + } else { |
|
| 206 | + $b64 .= $c; |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + if ('d' == $mode) { |
|
| 210 | + if ($sc == $c) { |
|
| 211 | + $mode = 'b'; |
|
| 212 | + continue; |
|
| 213 | + } |
|
| 214 | + $output[$out_len] = ord($c); |
|
| 215 | + $out_len++; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + return $output; |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - private static function ucs4array_utf7imap($input) |
|
| 222 | - { |
|
| 223 | - return str_replace('/', ',', self::ucs4array_utf7($input, '&')); |
|
| 224 | - } |
|
| 221 | + private static function ucs4array_utf7imap($input) |
|
| 222 | + { |
|
| 223 | + return str_replace('/', ',', self::ucs4array_utf7($input, '&')); |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - private static function ucs4array_utf7($input, $sc = '+') |
|
| 227 | - { |
|
| 228 | - $output = ''; |
|
| 229 | - $mode = 'd'; |
|
| 230 | - $b64 = ''; |
|
| 231 | - while (true) { |
|
| 232 | - $v = (!empty($input)) ? array_shift($input) : false; |
|
| 233 | - $is_direct = (false !== $v) ? (0x20 <= $v && $v <= 0x7e && $v != ord($sc)) : true; |
|
| 234 | - if ($mode == 'b') { |
|
| 235 | - if ($is_direct) { |
|
| 236 | - if ($b64 == chr(0).$sc) { |
|
| 237 | - $output .= $sc.'-'; |
|
| 238 | - $b64 = ''; |
|
| 239 | - } elseif ($b64) { |
|
| 240 | - $output .= $sc.str_replace('=', '', base64_encode($b64)).'-'; |
|
| 241 | - $b64 = ''; |
|
| 242 | - } |
|
| 243 | - $mode = 'd'; |
|
| 244 | - } elseif (false !== $v) { |
|
| 245 | - $b64 .= chr(($v >> 8) & 255). chr($v & 255); |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - if ($mode == 'd' && false !== $v) { |
|
| 249 | - if ($is_direct) { |
|
| 250 | - $output .= chr($v); |
|
| 251 | - } else { |
|
| 252 | - $b64 = chr(($v >> 8) & 255). chr($v & 255); |
|
| 253 | - $mode = 'b'; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - if (false === $v && $b64 == '') break; |
|
| 257 | - } |
|
| 258 | - return $output; |
|
| 259 | - } |
|
| 226 | + private static function ucs4array_utf7($input, $sc = '+') |
|
| 227 | + { |
|
| 228 | + $output = ''; |
|
| 229 | + $mode = 'd'; |
|
| 230 | + $b64 = ''; |
|
| 231 | + while (true) { |
|
| 232 | + $v = (!empty($input)) ? array_shift($input) : false; |
|
| 233 | + $is_direct = (false !== $v) ? (0x20 <= $v && $v <= 0x7e && $v != ord($sc)) : true; |
|
| 234 | + if ($mode == 'b') { |
|
| 235 | + if ($is_direct) { |
|
| 236 | + if ($b64 == chr(0).$sc) { |
|
| 237 | + $output .= $sc.'-'; |
|
| 238 | + $b64 = ''; |
|
| 239 | + } elseif ($b64) { |
|
| 240 | + $output .= $sc.str_replace('=', '', base64_encode($b64)).'-'; |
|
| 241 | + $b64 = ''; |
|
| 242 | + } |
|
| 243 | + $mode = 'd'; |
|
| 244 | + } elseif (false !== $v) { |
|
| 245 | + $b64 .= chr(($v >> 8) & 255). chr($v & 255); |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + if ($mode == 'd' && false !== $v) { |
|
| 249 | + if ($is_direct) { |
|
| 250 | + $output .= chr($v); |
|
| 251 | + } else { |
|
| 252 | + $b64 = chr(($v >> 8) & 255). chr($v & 255); |
|
| 253 | + $mode = 'b'; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + if (false === $v && $b64 == '') break; |
|
| 257 | + } |
|
| 258 | + return $output; |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * Convert UCS-4 array into UCS-4 string (Little Endian at the moment) |
|
| 263 | - * @access private |
|
| 264 | - */ |
|
| 265 | - private static function ucs4array_ucs4($input) |
|
| 266 | - { |
|
| 267 | - $output = ''; |
|
| 268 | - foreach ($input as $v) { |
|
| 269 | - $output .= chr(($v >> 24) & 255).chr(($v >> 16) & 255).chr(($v >> 8) & 255).chr($v & 255); |
|
| 270 | - } |
|
| 271 | - return $output; |
|
| 272 | - } |
|
| 261 | + /** |
|
| 262 | + * Convert UCS-4 array into UCS-4 string (Little Endian at the moment) |
|
| 263 | + * @access private |
|
| 264 | + */ |
|
| 265 | + private static function ucs4array_ucs4($input) |
|
| 266 | + { |
|
| 267 | + $output = ''; |
|
| 268 | + foreach ($input as $v) { |
|
| 269 | + $output .= chr(($v >> 24) & 255).chr(($v >> 16) & 255).chr(($v >> 8) & 255).chr($v & 255); |
|
| 270 | + } |
|
| 271 | + return $output; |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * Convert UCS-4 string (LE in the moment) into UCS-4 garray |
|
| 276 | - * @access private |
|
| 277 | - */ |
|
| 278 | - private static function ucs4_ucs4array($input) |
|
| 279 | - { |
|
| 280 | - $output = array(); |
|
| 274 | + /** |
|
| 275 | + * Convert UCS-4 string (LE in the moment) into UCS-4 garray |
|
| 276 | + * @access private |
|
| 277 | + */ |
|
| 278 | + private static function ucs4_ucs4array($input) |
|
| 279 | + { |
|
| 280 | + $output = array(); |
|
| 281 | 281 | |
| 282 | - $inp_len = strlen($input); |
|
| 283 | - // Input length must be dividable by 4 |
|
| 284 | - if ($inp_len % 4) { |
|
| 285 | - throw new Exception('Input UCS4 string is broken'); |
|
| 286 | - } |
|
| 287 | - // Empty input - return empty output |
|
| 288 | - if (!$inp_len) return $output; |
|
| 282 | + $inp_len = strlen($input); |
|
| 283 | + // Input length must be dividable by 4 |
|
| 284 | + if ($inp_len % 4) { |
|
| 285 | + throw new Exception('Input UCS4 string is broken'); |
|
| 286 | + } |
|
| 287 | + // Empty input - return empty output |
|
| 288 | + if (!$inp_len) return $output; |
|
| 289 | 289 | |
| 290 | - for ($i = 0, $out_len = -1; $i < $inp_len; ++$i) { |
|
| 291 | - if (!($i % 4)) { // Increment output position every 4 input bytes |
|
| 292 | - $out_len++; |
|
| 293 | - $output[$out_len] = 0; |
|
| 294 | - } |
|
| 295 | - $output[$out_len] += ord($input{$i}) << (8 * (3 - ($i % 4) ) ); |
|
| 296 | - } |
|
| 297 | - return $output; |
|
| 298 | - } |
|
| 290 | + for ($i = 0, $out_len = -1; $i < $inp_len; ++$i) { |
|
| 291 | + if (!($i % 4)) { // Increment output position every 4 input bytes |
|
| 292 | + $out_len++; |
|
| 293 | + $output[$out_len] = 0; |
|
| 294 | + } |
|
| 295 | + $output[$out_len] += ord($input{$i}) << (8 * (3 - ($i % 4) ) ); |
|
| 296 | + } |
|
| 297 | + return $output; |
|
| 298 | + } |
|
| 299 | 299 | } |
| 300 | 300 | ?> |
| 301 | 301 | \ No newline at end of file |
@@ -39,8 +39,8 @@ discard block |
||
| 39 | 39 | if (self::$safe_mode) self::$allow_overlong = true; |
| 40 | 40 | if (!in_array($from, self::$mechs)) throw new Exception('Invalid input format specified'); |
| 41 | 41 | if (!in_array($to, self::$mechs)) throw new Exception('Invalid output format specified'); |
| 42 | - if ($from != 'ucs4array') eval('$data = self::'.$from.'_ucs4array($data);'); |
|
| 43 | - if ($to != 'ucs4array') eval('$data = self::ucs4array_'.$to.'($data);'); |
|
| 42 | + if ($from != 'ucs4array') eval('$data = self::' . $from . '_ucs4array($data);'); |
|
| 43 | + if ($to != 'ucs4array') eval('$data = self::ucs4array_' . $to . '($data);'); |
|
| 44 | 44 | return $data; |
| 45 | 45 | } |
| 46 | 46 | |
@@ -66,10 +66,10 @@ discard block |
||
| 66 | 66 | ++$out_len; |
| 67 | 67 | if ('add' == $mode) { |
| 68 | 68 | if (self::$safe_mode) { |
| 69 | - $output[$out_len-2] = self::$safe_char; |
|
| 69 | + $output[$out_len - 2] = self::$safe_char; |
|
| 70 | 70 | $mode = 'next'; |
| 71 | 71 | } else { |
| 72 | - throw new Exception('Conversion from UTF-8 to UCS-4 failed: malformed input at byte '.$k); |
|
| 72 | + throw new Exception('Conversion from UTF-8 to UCS-4 failed: malformed input at byte ' . $k); |
|
| 73 | 73 | } |
| 74 | 74 | } |
| 75 | 75 | continue; |
@@ -93,9 +93,9 @@ discard block |
||
| 93 | 93 | ++$out_len; |
| 94 | 94 | continue; |
| 95 | 95 | } else { |
| 96 | - throw new Exception('This might be UTF-8, but I don\'t understand it at byte '.$k); |
|
| 96 | + throw new Exception('This might be UTF-8, but I don\'t understand it at byte ' . $k); |
|
| 97 | 97 | } |
| 98 | - if ($inp_len-$k-$next_byte < 2) { |
|
| 98 | + if ($inp_len - $k - $next_byte < 2) { |
|
| 99 | 99 | $output[$out_len] = self::$safe_char; |
| 100 | 100 | $mode = 'no'; |
| 101 | 101 | continue; |
@@ -111,21 +111,21 @@ discard block |
||
| 111 | 111 | if (!self::$allow_overlong && $test == 'range') { |
| 112 | 112 | $test = 'none'; |
| 113 | 113 | if (($v < 0xA0 && $start_byte == 0xE0) || ($v < 0x90 && $start_byte == 0xF0) || ($v > 0x8F && $start_byte == 0xF4)) { |
| 114 | - throw new Exception('Bogus UTF-8 character detected (out of legal range) at byte '.$k); |
|
| 114 | + throw new Exception('Bogus UTF-8 character detected (out of legal range) at byte ' . $k); |
|
| 115 | 115 | } |
| 116 | 116 | } |
| 117 | 117 | if ($v >> 6 == 2) { // Bit mask must be 10xxxxxx |
| 118 | - $v = ($v-128) << ($next_byte*6); |
|
| 119 | - $output[($out_len-1)] += $v; |
|
| 118 | + $v = ($v - 128) << ($next_byte * 6); |
|
| 119 | + $output[($out_len - 1)] += $v; |
|
| 120 | 120 | --$next_byte; |
| 121 | 121 | } else { |
| 122 | 122 | if (self::$safe_mode) { |
| 123 | - $output[$out_len-1] = ord(self::$safe_char); |
|
| 123 | + $output[$out_len - 1] = ord(self::$safe_char); |
|
| 124 | 124 | $k--; |
| 125 | 125 | $mode = 'next'; |
| 126 | 126 | continue; |
| 127 | 127 | } else { |
| 128 | - throw new Exception('Conversion from UTF-8 to UCS-4 failed: malformed input at byte '.$k); |
|
| 128 | + throw new Exception('Conversion from UTF-8 to UCS-4 failed: malformed input at byte ' . $k); |
|
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | 131 | if ($next_byte < 0) { |
@@ -148,15 +148,15 @@ discard block |
||
| 148 | 148 | if ($v < 128) { // 7bit are transferred literally |
| 149 | 149 | $output .= chr($v); |
| 150 | 150 | } elseif ($v < (1 << 11)) { // 2 bytes |
| 151 | - $output .= chr(192+($v >> 6)).chr(128+($v & 63)); |
|
| 151 | + $output .= chr(192 + ($v >> 6)) . chr(128 + ($v & 63)); |
|
| 152 | 152 | } elseif ($v < (1 << 16)) { // 3 bytes |
| 153 | - $output .= chr(224+($v >> 12)).chr(128+(($v >> 6) & 63)).chr(128+($v & 63)); |
|
| 153 | + $output .= chr(224 + ($v >> 12)) . chr(128 + (($v >> 6) & 63)) . chr(128 + ($v & 63)); |
|
| 154 | 154 | } elseif ($v < (1 << 21)) { // 4 bytes |
| 155 | - $output .= chr(240+($v >> 18)).chr(128+(($v >> 12) & 63)).chr(128+(($v >> 6) & 63)).chr(128+($v & 63)); |
|
| 155 | + $output .= chr(240 + ($v >> 18)) . chr(128 + (($v >> 12) & 63)) . chr(128 + (($v >> 6) & 63)) . chr(128 + ($v & 63)); |
|
| 156 | 156 | } elseif (self::$safe_mode) { |
| 157 | 157 | $output .= self::$safe_char; |
| 158 | 158 | } else { |
| 159 | - throw new Exception('Conversion from UCS-4 to UTF-8 failed: malformed input at byte '.$k); |
|
| 159 | + throw new Exception('Conversion from UCS-4 to UTF-8 failed: malformed input at byte ' . $k); |
|
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | return $output; |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | if (0 == ord($c)) continue; // Ignore zero bytes |
| 181 | 181 | if ('b' == $mode) { |
| 182 | 182 | // Sequence got terminated |
| 183 | - if (!preg_match('![A-Za-z0-9/'.preg_quote($sc, '!').']!', $c)) { |
|
| 183 | + if (!preg_match('![A-Za-z0-9/' . preg_quote($sc, '!') . ']!', $c)) { |
|
| 184 | 184 | if ('-' == $c) { |
| 185 | 185 | if ($b64 == '') { |
| 186 | 186 | $output[$out_len] = ord($sc); |
@@ -233,23 +233,23 @@ discard block |
||
| 233 | 233 | $is_direct = (false !== $v) ? (0x20 <= $v && $v <= 0x7e && $v != ord($sc)) : true; |
| 234 | 234 | if ($mode == 'b') { |
| 235 | 235 | if ($is_direct) { |
| 236 | - if ($b64 == chr(0).$sc) { |
|
| 237 | - $output .= $sc.'-'; |
|
| 236 | + if ($b64 == chr(0) . $sc) { |
|
| 237 | + $output .= $sc . '-'; |
|
| 238 | 238 | $b64 = ''; |
| 239 | 239 | } elseif ($b64) { |
| 240 | - $output .= $sc.str_replace('=', '', base64_encode($b64)).'-'; |
|
| 240 | + $output .= $sc . str_replace('=', '', base64_encode($b64)) . '-'; |
|
| 241 | 241 | $b64 = ''; |
| 242 | 242 | } |
| 243 | 243 | $mode = 'd'; |
| 244 | 244 | } elseif (false !== $v) { |
| 245 | - $b64 .= chr(($v >> 8) & 255). chr($v & 255); |
|
| 245 | + $b64 .= chr(($v >> 8) & 255) . chr($v & 255); |
|
| 246 | 246 | } |
| 247 | 247 | } |
| 248 | 248 | if ($mode == 'd' && false !== $v) { |
| 249 | 249 | if ($is_direct) { |
| 250 | 250 | $output .= chr($v); |
| 251 | 251 | } else { |
| 252 | - $b64 = chr(($v >> 8) & 255). chr($v & 255); |
|
| 252 | + $b64 = chr(($v >> 8) & 255) . chr($v & 255); |
|
| 253 | 253 | $mode = 'b'; |
| 254 | 254 | } |
| 255 | 255 | } |
@@ -266,7 +266,7 @@ discard block |
||
| 266 | 266 | { |
| 267 | 267 | $output = ''; |
| 268 | 268 | foreach ($input as $v) { |
| 269 | - $output .= chr(($v >> 24) & 255).chr(($v >> 16) & 255).chr(($v >> 8) & 255).chr($v & 255); |
|
| 269 | + $output .= chr(($v >> 24) & 255) . chr(($v >> 16) & 255) . chr(($v >> 8) & 255) . chr($v & 255); |
|
| 270 | 270 | } |
| 271 | 271 | return $output; |
| 272 | 272 | } |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | $out_len++; |
| 293 | 293 | $output[$out_len] = 0; |
| 294 | 294 | } |
| 295 | - $output[$out_len] += ord($input{$i}) << (8 * (3 - ($i % 4) ) ); |
|
| 295 | + $output[$out_len] += ord($input{$i}) << (8 * (3 - ($i % 4))); |
|
| 296 | 296 | } |
| 297 | 297 | return $output; |
| 298 | 298 | } |
@@ -36,11 +36,21 @@ discard block |
||
| 36 | 36 | { |
| 37 | 37 | self::$safe_mode = ($safe_mode) ? true : false; |
| 38 | 38 | self::$safe_char = ($safe_char) ? $safe_char : 0xFFFC; |
| 39 | - if (self::$safe_mode) self::$allow_overlong = true; |
|
| 40 | - if (!in_array($from, self::$mechs)) throw new Exception('Invalid input format specified'); |
|
| 41 | - if (!in_array($to, self::$mechs)) throw new Exception('Invalid output format specified'); |
|
| 42 | - if ($from != 'ucs4array') eval('$data = self::'.$from.'_ucs4array($data);'); |
|
| 43 | - if ($to != 'ucs4array') eval('$data = self::ucs4array_'.$to.'($data);'); |
|
| 39 | + if (self::$safe_mode) { |
|
| 40 | + self::$allow_overlong = true; |
|
| 41 | + } |
|
| 42 | + if (!in_array($from, self::$mechs)) { |
|
| 43 | + throw new Exception('Invalid input format specified'); |
|
| 44 | + } |
|
| 45 | + if (!in_array($to, self::$mechs)) { |
|
| 46 | + throw new Exception('Invalid output format specified'); |
|
| 47 | + } |
|
| 48 | + if ($from != 'ucs4array') { |
|
| 49 | + eval('$data = self::'.$from.'_ucs4array($data);'); |
|
| 50 | + } |
|
| 51 | + if ($to != 'ucs4array') { |
|
| 52 | + eval('$data = self::ucs4array_'.$to.'($data);'); |
|
| 53 | + } |
|
| 44 | 54 | return $data; |
| 45 | 55 | } |
| 46 | 56 | |
@@ -177,7 +187,10 @@ discard block |
||
| 177 | 187 | |
| 178 | 188 | for ($k = 0; $k < $inp_len; ++$k) { |
| 179 | 189 | $c = $input{$k}; |
| 180 | - if (0 == ord($c)) continue; // Ignore zero bytes |
|
| 190 | + if (0 == ord($c)) { |
|
| 191 | + continue; |
|
| 192 | + } |
|
| 193 | + // Ignore zero bytes |
|
| 181 | 194 | if ('b' == $mode) { |
| 182 | 195 | // Sequence got terminated |
| 183 | 196 | if (!preg_match('![A-Za-z0-9/'.preg_quote($sc, '!').']!', $c)) { |
@@ -253,7 +266,9 @@ discard block |
||
| 253 | 266 | $mode = 'b'; |
| 254 | 267 | } |
| 255 | 268 | } |
| 256 | - if (false === $v && $b64 == '') break; |
|
| 269 | + if (false === $v && $b64 == '') { |
|
| 270 | + break; |
|
| 271 | + } |
|
| 257 | 272 | } |
| 258 | 273 | return $output; |
| 259 | 274 | } |
@@ -285,7 +300,9 @@ discard block |
||
| 285 | 300 | throw new Exception('Input UCS4 string is broken'); |
| 286 | 301 | } |
| 287 | 302 | // Empty input - return empty output |
| 288 | - if (!$inp_len) return $output; |
|
| 303 | + if (!$inp_len) { |
|
| 304 | + return $output; |
|
| 305 | + } |
|
| 289 | 306 | |
| 290 | 307 | for ($i = 0, $out_len = -1; $i < $inp_len; ++$i) { |
| 291 | 308 | if (!($i % 4)) { // Increment output position every 4 input bytes |