1: <?php
2: namespace Datadepo\Api;
3:
4: class ApiWrapper
5: {
6:
7:
8: protected $settingsDatadepo;
9:
10:
11: protected $settingsAccount;
12:
13: 14: 15: 16:
17: public function __construct($settingsDatadepo, $settingsAccount)
18: {
19: $this->settingsDatadepo = $settingsDatadepo;
20: $this->settingsAccount = $settingsAccount;
21: }
22:
23: 24: 25: 26: 27: 28:
29: public function request($type, $actionType, array $params = array())
30: {
31: $url = $this->settingsDatadepo['apiUrl'] . '/' . $this->settingsDatadepo['version']. '/' . $type . '/' . $actionType . '/';
32: list($socket) = $this->createFsock($url, $params);
33: $contents = "";
34: while (!feof($socket)) {
35: $contents .= fgets($socket, 4096);
36: }
37: fclose($socket);
38:
39: $json = json_decode($contents);
40: if (!$json instanceof \stdClass) {
41: throw new ApiException('Unrecognized response from datadepo');
42: }
43: return $json;
44: }
45:
46: 47: 48: 49:
50: public function analyzeResponse($json)
51: {
52: if (isset($json->url)) {
53: return 'url';
54: }
55: elseif (isset($json->suspend)) {
56: return 'suspend';
57: }
58: return NULL;
59: }
60:
61: 62: 63: 64:
65: public function download($url, $filePath)
66: {
67: list($socket) = $this->createFsock($url);
68:
69:
70: umask(0000);
71: $fp = fopen($filePath, 'w');
72: while (!feof($socket)) {
73: fwrite($fp, fread($socket, 4096));
74: }
75: fclose($fp);
76: fclose($socket);
77: chmod($filePath, 0777);
78: }
79:
80:
81:
82:
83:
84: 85: 86: 87: 88: 89:
90: protected function createFsock($url, $params = FALSE)
91: {
92: $url = parse_url($url);
93: if (!isset($url['host']) || !isset($url['path'])) {
94: throw new ApiException('Invalid URL');
95: }
96:
97: $host = $url['host'];
98: $path = $url['path'] . (isset($url['query']) ? '?' . $url['query'] : '');
99: $socket = fsockopen($host, 80, $errno, $errstr, $this->settingsDatadepo['connectTimeout']);
100: if (!$socket) {
101: throw new ApiException($errstr);
102: }
103: stream_set_timeout($socket, $this->settingsDatadepo['connectTimeout']);
104:
105:
106: $postData = FALSE;
107: if ($params !== FALSE) {
108: foreach (array('user', 'pwd') as $name) {
109: if (isset($this->settingsAccount[$name])) {
110: $params[$name] = $this->settingsAccount[$name];
111: }
112: }
113: $postData = http_build_query($params);
114: }
115:
116: $http = "POST ".$path." HTTP/1.0\r\n";
117: $http .= "Host: ".$host."\r\n";
118: if (isset($_SERVER['HTTP_USER_AGENT'])) {
119: $http .= "User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n";
120: }
121: $http .= "Content-Type: application/x-www-form-urlencoded\r\n";
122: if ($postData) {
123: $http .= "Content-length: " . strlen($postData) . "\r\n";
124: }
125: $http .= "Connection: close\r\n\r\n";
126: if ($postData) {
127: $http .= $postData . "\r\n\r\n";
128: }
129: fwrite($socket, $http);
130:
131:
132: $header = '';
133:
134: do {
135: $c = fgets ( $socket, 4096 );
136: if ($c === FALSE) {
137: throw new ApiException('Connection timeout');
138: }
139: $header .= $c;
140: }
141: while (strpos($header, "\r\n\r\n") === FALSE);
142: return array($socket, $header);
143: }
144:
145: }