| 1 |
<?php |
| 2 |
|
| 3 |
namespace BackupGuard; |
| 4 |
|
| 5 |
require_once(dirname(__FILE__).'/RequestHandler.php'); |
| 6 |
|
| 7 |
class Stream extends RequestHandler |
| 8 |
{ |
| 9 |
public function __construct($url) |
| 10 |
{ |
| 11 |
parent::__construct($url); |
| 12 |
|
| 13 |
$this->addHeader('Content-type: application/x-www-form-urlencoded'); |
| 14 |
} |
| 15 |
|
| 16 |
public function post() |
| 17 |
{ |
| 18 |
return $this->sendRequest('POST'); |
| 19 |
} |
| 20 |
|
| 21 |
public function get() |
| 22 |
{ |
| 23 |
return $this->sendRequest('GET'); |
| 24 |
} |
| 25 |
|
| 26 |
private function sendRequest($type) |
| 27 |
{ |
| 28 |
//prepare headers |
| 29 |
$headers = ''; |
| 30 |
if (count($this->headers)) { |
| 31 |
$headers = implode("\r\n", $this->headers); |
| 32 |
$headers .= "\r\n"; |
| 33 |
} |
| 34 |
|
| 35 |
//set http request method |
| 36 |
$opts = array( |
| 37 |
'http' => array( |
| 38 |
'method' => $type, |
| 39 |
'user_agent' => 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0', |
| 40 |
'ignore_errors' => '1' |
| 41 |
) |
| 42 |
); |
| 43 |
|
| 44 |
//set headers |
| 45 |
if ($headers) { |
| 46 |
$opts['http']['header'] = $headers; |
| 47 |
} |
| 48 |
|
| 49 |
$url = $this->url; |
| 50 |
|
| 51 |
//set params |
| 52 |
if (count($this->params)) { |
| 53 |
$data = http_build_query($this->params, '', '&'); |
| 54 |
|
| 55 |
if ($type == 'GET') { //set params inside url |
| 56 |
$url = rtrim($url, '/').'/'; |
| 57 |
|
| 58 |
if ($this->getWithQueryParams) { //standard get url, with query params |
| 59 |
$url .= '?'.http_build_query($this->params, '', '&'); |
| 60 |
} |
| 61 |
else { //mvs-styled get url |
| 62 |
$url .= implode('/', array_values($this->params)); |
| 63 |
} |
| 64 |
} |
| 65 |
else if ($type == 'POST') { |
| 66 |
$opts['http']['content'] = $data; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$context = @stream_context_create($opts); |
| 71 |
$fp = @fopen($url, 'r', false, $context); |
| 72 |
|
| 73 |
if (isset($http_response_header)) { |
| 74 |
$this->setResponseInfo($http_response_header); |
| 75 |
} |
| 76 |
|
| 77 |
if ($fp) { |
| 78 |
$this->body = @stream_get_contents($fp); |
| 79 |
@fclose($fp); |
| 80 |
} |
| 81 |
|
| 82 |
return $this->parseResponse(); |
| 83 |
} |
| 84 |
|
| 85 |
private function parseHTTPHeaders($headers) |
| 86 |
{ |
| 87 |
$head = array(); |
| 88 |
foreach($headers as $k => $v) { |
| 89 |
$t = explode(':', $v, 2); |
| 90 |
if (isset($t[1])) { |
| 91 |
$head[trim($t[0])] = trim($t[1]); |
| 92 |
} |
| 93 |
else { |
| 94 |
$head[] = $v; |
| 95 |
if (preg_match("#HTTP/[0-9\.]+\s+([0-9]+)#", $v, $out)) { |
| 96 |
$head['reponse_code'] = intval($out[1]); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
return $head; |
| 101 |
} |
| 102 |
|
| 103 |
private function setResponseInfo($info) |
| 104 |
{ |
| 105 |
$info = $this->parseHTTPHeaders($info); |
| 106 |
|
| 107 |
if (isset($info['reponse_code'])) { |
| 108 |
$this->httpCode = $info['reponse_code']; |
| 109 |
} |
| 110 |
|
| 111 |
if (isset($info['Content-Type'])) { |
| 112 |
$this->contentType = $info['Content-Type']; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|