12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001 |
- <?php
-
-
-
- class Requests {
-
-
- const POST = 'POST';
-
-
-
- const PUT = 'PUT';
-
-
-
- const GET = 'GET';
-
-
-
- const HEAD = 'HEAD';
-
-
-
- const DELETE = 'DELETE';
-
-
-
- const OPTIONS = 'OPTIONS';
-
-
-
- const TRACE = 'TRACE';
-
-
-
- const PATCH = 'PATCH';
-
-
-
- const BUFFER_SIZE = 1160;
-
-
-
- const VERSION = '1.8.1';
-
-
-
- protected static $transports = array();
-
-
-
- public static $transport = array();
-
-
-
- protected static $certificate_path;
-
-
-
- private function __construct() {}
-
-
-
- public static function autoloader($class) {
-
- if (strpos($class, 'Requests') !== 0) {
- return;
- }
-
- $file = str_replace('_', '/', $class);
- if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
- require_once dirname(__FILE__) . '/' . $file . '.php';
- }
- }
-
-
-
- public static function register_autoloader() {
- spl_autoload_register(array('Requests', 'autoloader'));
- }
-
-
-
- public static function add_transport($transport) {
- if (empty(self::$transports)) {
- self::$transports = array(
- 'Requests_Transport_cURL',
- 'Requests_Transport_fsockopen',
- );
- }
-
- self::$transports = array_merge(self::$transports, array($transport));
- }
-
-
-
- protected static function get_transport($capabilities = array()) {
-
-
-
- ksort($capabilities);
- $cap_string = serialize($capabilities);
-
-
- if (isset(self::$transport[$cap_string]) && self::$transport[$cap_string] !== null) {
- $class = self::$transport[$cap_string];
- return new $class();
- }
-
-
- if (empty(self::$transports)) {
- self::$transports = array(
- 'Requests_Transport_cURL',
- 'Requests_Transport_fsockopen',
- );
- }
-
-
- foreach (self::$transports as $class) {
- if (!class_exists($class)) {
- continue;
- }
-
- $result = call_user_func(array($class, 'test'), $capabilities);
- if ($result) {
- self::$transport[$cap_string] = $class;
- break;
- }
- }
- if (self::$transport[$cap_string] === null) {
- throw new Requests_Exception('No working transports found', 'notransport', self::$transports);
- }
-
- $class = self::$transport[$cap_string];
- return new $class();
- }
-
-
-
-
-
- public static function get($url, $headers = array(), $options = array()) {
- return self::request($url, $headers, null, self::GET, $options);
- }
-
-
-
- public static function head($url, $headers = array(), $options = array()) {
- return self::request($url, $headers, null, self::HEAD, $options);
- }
-
-
-
- public static function delete($url, $headers = array(), $options = array()) {
- return self::request($url, $headers, null, self::DELETE, $options);
- }
-
-
-
- public static function trace($url, $headers = array(), $options = array()) {
- return self::request($url, $headers, null, self::TRACE, $options);
- }
-
-
-
-
-
-
- public static function post($url, $headers = array(), $data = array(), $options = array()) {
- return self::request($url, $headers, $data, self::POST, $options);
- }
-
-
- public static function put($url, $headers = array(), $data = array(), $options = array()) {
- return self::request($url, $headers, $data, self::PUT, $options);
- }
-
-
-
- public static function options($url, $headers = array(), $data = array(), $options = array()) {
- return self::request($url, $headers, $data, self::OPTIONS, $options);
- }
-
-
-
- public static function patch($url, $headers, $data = array(), $options = array()) {
- return self::request($url, $headers, $data, self::PATCH, $options);
- }
-
-
-
-
- public static function request($url, $headers = array(), $data = array(), $type = self::GET, $options = array()) {
- if (empty($options['type'])) {
- $options['type'] = $type;
- }
- $options = array_merge(self::get_default_options(), $options);
-
- self::set_defaults($url, $headers, $data, $type, $options);
-
- $options['hooks']->dispatch('requests.before_request', array(&$url, &$headers, &$data, &$type, &$options));
-
- if (!empty($options['transport'])) {
- $transport = $options['transport'];
-
- if (is_string($options['transport'])) {
- $transport = new $transport();
- }
- }
- else {
- $need_ssl = (stripos($url, 'https://') === 0);
- $capabilities = array('ssl' => $need_ssl);
- $transport = self::get_transport($capabilities);
- }
- $response = $transport->request($url, $headers, $data, $options);
-
- $options['hooks']->dispatch('requests.before_parse', array(&$response, $url, $headers, $data, $type, $options));
-
- return self::parse_response($response, $url, $headers, $data, $options);
- }
-
-
-
- public static function request_multiple($requests, $options = array()) {
- $options = array_merge(self::get_default_options(true), $options);
-
- if (!empty($options['hooks'])) {
- $options['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple'));
- if (!empty($options['complete'])) {
- $options['hooks']->register('multiple.request.complete', $options['complete']);
- }
- }
-
- foreach ($requests as $id => &$request) {
- if (!isset($request['headers'])) {
- $request['headers'] = array();
- }
- if (!isset($request['data'])) {
- $request['data'] = array();
- }
- if (!isset($request['type'])) {
- $request['type'] = self::GET;
- }
- if (!isset($request['options'])) {
- $request['options'] = $options;
- $request['options']['type'] = $request['type'];
- }
- else {
- if (empty($request['options']['type'])) {
- $request['options']['type'] = $request['type'];
- }
- $request['options'] = array_merge($options, $request['options']);
- }
-
- self::set_defaults($request['url'], $request['headers'], $request['data'], $request['type'], $request['options']);
-
-
- if ($request['options']['hooks'] !== $options['hooks']) {
- $request['options']['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple'));
- if (!empty($request['options']['complete'])) {
- $request['options']['hooks']->register('multiple.request.complete', $request['options']['complete']);
- }
- }
- }
- unset($request);
-
- if (!empty($options['transport'])) {
- $transport = $options['transport'];
-
- if (is_string($options['transport'])) {
- $transport = new $transport();
- }
- }
- else {
- $transport = self::get_transport();
- }
- $responses = $transport->request_multiple($requests, $options);
-
- foreach ($responses as $id => &$response) {
-
-
- if (is_string($response)) {
- $request = $requests[$id];
- self::parse_multiple($response, $request);
- $request['options']['hooks']->dispatch('multiple.request.complete', array(&$response, $id));
- }
- }
-
- return $responses;
- }
-
-
-
- protected static function get_default_options($multirequest = false) {
- $defaults = array(
- 'timeout' => 10,
- 'connect_timeout' => 10,
- 'useragent' => 'php-requests/' . self::VERSION,
- 'protocol_version' => 1.1,
- 'redirected' => 0,
- 'redirects' => 10,
- 'follow_redirects' => true,
- 'blocking' => true,
- 'type' => self::GET,
- 'filename' => false,
- 'auth' => false,
- 'proxy' => false,
- 'cookies' => false,
- 'max_bytes' => false,
- 'idn' => true,
- 'hooks' => null,
- 'transport' => null,
- 'verify' => self::get_certificate_path(),
- 'verifyname' => true,
- );
- if ($multirequest !== false) {
- $defaults['complete'] = null;
- }
- return $defaults;
- }
-
-
-
- public static function get_certificate_path() {
- if (!empty(self::$certificate_path)) {
- return self::$certificate_path;
- }
-
- return dirname(__FILE__) . '/Requests/Transport/cacert.pem';
- }
-
-
-
- public static function set_certificate_path($path) {
- self::$certificate_path = $path;
- }
-
-
-
- protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) {
- if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) {
- throw new Requests_Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url);
- }
-
- if (empty($options['hooks'])) {
- $options['hooks'] = new Requests_Hooks();
- }
-
- if (is_array($options['auth'])) {
- $options['auth'] = new Requests_Auth_Basic($options['auth']);
- }
- if ($options['auth'] !== false) {
- $options['auth']->register($options['hooks']);
- }
-
- if (is_string($options['proxy']) || is_array($options['proxy'])) {
- $options['proxy'] = new Requests_Proxy_HTTP($options['proxy']);
- }
- if ($options['proxy'] !== false) {
- $options['proxy']->register($options['hooks']);
- }
-
- if (is_array($options['cookies'])) {
- $options['cookies'] = new Requests_Cookie_Jar($options['cookies']);
- }
- elseif (empty($options['cookies'])) {
- $options['cookies'] = new Requests_Cookie_Jar();
- }
- if ($options['cookies'] !== false) {
- $options['cookies']->register($options['hooks']);
- }
-
- if ($options['idn'] !== false) {
- $iri = new Requests_IRI($url);
- $iri->host = Requests_IDNAEncoder::encode($iri->ihost);
- $url = $iri->uri;
- }
-
-
- $type = strtoupper($type);
-
- if (!isset($options['data_format'])) {
- if (in_array($type, array(self::HEAD, self::GET, self::DELETE), true)) {
- $options['data_format'] = 'query';
- }
- else {
- $options['data_format'] = 'body';
- }
- }
- }
-
-
-
- protected static function parse_response($headers, $url, $req_headers, $req_data, $options) {
- $return = new Requests_Response();
- if (!$options['blocking']) {
- return $return;
- }
-
- $return->raw = $headers;
- $return->url = (string) $url;
- $return->body = '';
-
- if (!$options['filename']) {
- $pos = strpos($headers, "\r\n\r\n");
- if ($pos === false) {
-
- throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator');
- }
-
- $headers = substr($return->raw, 0, $pos);
-
- $body = substr($return->raw, $pos + 4);
- if (!empty($body)) {
- $return->body = $body;
- }
- }
-
- $headers = str_replace("\r\n", "\n", $headers);
-
- $headers = preg_replace('/\n[ \t]/', ' ', $headers);
- $headers = explode("\n", $headers);
- preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)#i', array_shift($headers), $matches);
- if (empty($matches)) {
- throw new Requests_Exception('Response could not be parsed', 'noversion', $headers);
- }
- $return->protocol_version = (float) $matches[1];
- $return->status_code = (int) $matches[2];
- if ($return->status_code >= 200 && $return->status_code < 300) {
- $return->success = true;
- }
-
- foreach ($headers as $header) {
- list($key, $value) = explode(':', $header, 2);
- $value = trim($value);
- preg_replace('#(\s+)#i', ' ', $value);
- $return->headers[$key] = $value;
- }
- if (isset($return->headers['transfer-encoding'])) {
- $return->body = self::decode_chunked($return->body);
- unset($return->headers['transfer-encoding']);
- }
- if (isset($return->headers['content-encoding'])) {
- $return->body = self::decompress($return->body);
- }
-
-
- if (isset($return->headers['connection'])) {
- unset($return->headers['connection']);
- }
-
- $options['hooks']->dispatch('requests.before_redirect_check', array(&$return, $req_headers, $req_data, $options));
-
- if ($return->is_redirect() && $options['follow_redirects'] === true) {
- if (isset($return->headers['location']) && $options['redirected'] < $options['redirects']) {
- if ($return->status_code === 303) {
- $options['type'] = self::GET;
- }
- $options['redirected']++;
- $location = $return->headers['location'];
- if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) {
-
- $location = Requests_IRI::absolutize($url, $location);
- $location = $location->uri;
- }
-
- $hook_args = array(
- &$location,
- &$req_headers,
- &$req_data,
- &$options,
- $return,
- );
- $options['hooks']->dispatch('requests.before_redirect', $hook_args);
- $redirected = self::request($location, $req_headers, $req_data, $options['type'], $options);
- $redirected->history[] = $return;
- return $redirected;
- }
- elseif ($options['redirected'] >= $options['redirects']) {
- throw new Requests_Exception('Too many redirects', 'toomanyredirects', $return);
- }
- }
-
- $return->redirects = $options['redirected'];
-
- $options['hooks']->dispatch('requests.after_request', array(&$return, $req_headers, $req_data, $options));
- return $return;
- }
-
-
-
- public static function parse_multiple(&$response, $request) {
- try {
- $url = $request['url'];
- $headers = $request['headers'];
- $data = $request['data'];
- $options = $request['options'];
- $response = self::parse_response($response, $url, $headers, $data, $options);
- }
- catch (Requests_Exception $e) {
- $response = $e;
- }
- }
-
-
-
- protected static function decode_chunked($data) {
- if (!preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', trim($data))) {
- return $data;
- }
-
- $decoded = '';
- $encoded = $data;
-
- while (true) {
- $is_chunked = (bool) preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', $encoded, $matches);
- if (!$is_chunked) {
-
- return $data;
- }
-
- $length = hexdec(trim($matches[1]));
- if ($length === 0) {
-
- return $decoded;
- }
-
- $chunk_length = strlen($matches[0]);
- $decoded .= substr($encoded, $chunk_length, $length);
- $encoded = substr($encoded, $chunk_length + $length + 2);
-
- if (trim($encoded) === '0' || empty($encoded)) {
- return $decoded;
- }
- }
-
-
-
- }
-
-
-
-
- public static function flatten($array) {
- $return = array();
- foreach ($array as $key => $value) {
- $return[] = sprintf('%s: %s', $key, $value);
- }
- return $return;
- }
-
-
-
- public static function flattern($array) {
- return self::flatten($array);
- }
-
-
-
- public static function decompress($data) {
- if (substr($data, 0, 2) !== "\x1f\x8b" && substr($data, 0, 2) !== "\x78\x9c") {
-
- return $data;
- }
-
- if (function_exists('gzdecode')) {
-
- $decoded = @gzdecode($data);
- if ($decoded !== false) {
- return $decoded;
- }
- }
-
- if (function_exists('gzinflate')) {
- $decoded = @gzinflate($data);
- if ($decoded !== false) {
- return $decoded;
- }
- }
-
- $decoded = self::compatible_gzinflate($data);
- if ($decoded !== false) {
- return $decoded;
- }
-
- if (function_exists('gzuncompress')) {
- $decoded = @gzuncompress($data);
- if ($decoded !== false) {
- return $decoded;
- }
- }
-
- return $data;
- }
-
-
-
- public static function compatible_gzinflate($gz_data) {
-
-
- if (substr($gz_data, 0, 3) === "\x1f\x8b\x08") {
- $i = 10;
- $flg = ord(substr($gz_data, 3, 1));
- if ($flg > 0) {
- if ($flg & 4) {
- list($xlen) = unpack('v', substr($gz_data, $i, 2));
- $i += 2 + $xlen;
- }
- if ($flg & 8) {
- $i = strpos($gz_data, "\0", $i) + 1;
- }
- if ($flg & 16) {
- $i = strpos($gz_data, "\0", $i) + 1;
- }
- if ($flg & 2) {
- $i += 2;
- }
- }
- $decompressed = self::compatible_gzinflate(substr($gz_data, $i));
- if ($decompressed !== false) {
- return $decompressed;
- }
- }
-
-
-
-
-
-
-
-
-
- $huffman_encoded = false;
-
-
- list(, $first_nibble) = unpack('h', $gz_data);
-
-
- list(, $first_two_bytes) = unpack('n', $gz_data);
-
- if ($first_nibble === 0x08 && ($first_two_bytes % 0x1F) === 0) {
- $huffman_encoded = true;
- }
-
- if ($huffman_encoded) {
- $decompressed = @gzinflate(substr($gz_data, 2));
- if ($decompressed !== false) {
- return $decompressed;
- }
- }
-
- if (substr($gz_data, 0, 4) === "\x50\x4b\x03\x04") {
-
-
-
-
-
-
- list(, $general_purpose_flag) = unpack('v', substr($gz_data, 6, 2));
-
-
-
-
- $zip_compressed_on_the_fly = ((0x08 & $general_purpose_flag) === 0x08);
-
- if (!$zip_compressed_on_the_fly) {
-
- return $gz_data;
- }
-
-
-
- $first_file_start = array_sum(unpack('v2', substr($gz_data, 26, 4)));
- $decompressed = @gzinflate(substr($gz_data, 30 + $first_file_start));
- if ($decompressed !== false) {
- return $decompressed;
- }
- return false;
- }
-
-
- $decompressed = @gzinflate($gz_data);
- if ($decompressed !== false) {
- return $decompressed;
- }
-
-
-
- $decompressed = @gzinflate(substr($gz_data, 2));
- if ($decompressed !== false) {
- return $decompressed;
- }
-
- return false;
- }
-
- public static function match_domain($host, $reference) {
-
- if ($host === $reference) {
- return true;
- }
-
-
-
-
- $parts = explode('.', $host);
- if (ip2long($host) === false && count($parts) >= 3) {
- $parts[0] = '*';
- $wildcard = implode('.', $parts);
- if ($wildcard === $reference) {
- return true;
- }
- }
-
- return false;
- }
- }
|