Use short form array syntax everywhere
[friendica-addons.git] / diaspora / Diaspora_Connection.php
1 <?php
2 /**
3  * Super-skeletal class to interact with Diaspora.
4  *
5  * @author Meitar Moscovitz <meitarm@gmail.com>
6  * Modifications by Michael Vogel <heluecht@pirati.ca>
7  */
8
9 class Diaspora_Connection {
10         private $user;
11         private $host;
12         private $password;
13         private $tls = true; //< Whether to use an SSL/TLS connection or not.
14
15         private $last_http_result; //< Result of last cURL transaction.
16         private $csrf_token; //< Authenticity token retrieved from last HTTP response.
17         private $http_method; //< Which HTTP verb to use for the next HTTP request.
18         private $cookiejar;
19
20         private $debug_log;
21
22         public $provider = '*Diaspora Connection';
23
24         public function __construct($diaspora_handle = '', $password = '') {
25                 if (!empty($diaspora_handle)) {
26                         $this->setDiasporaID($diaspora_handle);
27                 }
28                 if (!empty($password)) {
29                         $this->setPassword($password);
30                 }
31
32                 $this->cookiejar = tempnam(sys_get_temp_dir(), 'cookies');
33                 return $this;
34         }
35
36         public function __destruct() {
37                 if (file_exists($this->cookiejar)) {
38                         unlink($this->cookiejar);
39                 }
40         }
41
42         public function setDebugLog($log_file) {
43                 $this->debug_log = $log_file;
44         }
45
46         public function setDiasporaID($id) {
47                 $parts = explode('@', $id);
48                 $this->user = $parts[0];
49                 $this->host = $parts[1];
50         }
51
52         public function getDiasporaID() {
53                 return $this->user . '@' . $this->host;
54         }
55
56         public function getPodURL() {
57                 return $this->getScheme() . '://' . $this->host;
58         }
59
60         public function setPassword($passwd) {
61                 $this->password = $passwd;
62         }
63
64         public function setSecureTransport($is_secure) {
65                 $this->tls = (bool) $is_secure;
66         }
67
68         private function getScheme() {
69                 return ($this->tls) ? 'https' : 'http';
70         }
71
72         private function doHttpRequest($url, $data = [], $headers = []) {
73                 if (0 === strpos($url, '/')) {
74                         $url = $this->getScheme() . '://' . $this->host . $url;
75                 }
76
77                 $ch = curl_init($url);
78
79                 if ($this->debug_log) {
80                         curl_setopt($ch, CURLOPT_VERBOSE, true);
81                         $fh = fopen($this->debug_log, 'a');
82                         curl_setopt($ch, CURLOPT_STDERR, $fh);
83                 }
84
85                 curl_setopt($ch, CURLOPT_HEADER, true);
86                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
87
88                 if (!empty($data)) {
89                         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
90                 }
91                 if (!empty($headers)) {
92                         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
93                 }
94
95                 curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
96                 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
97
98                 // Are we doing a special kind of HTTP request?
99                 switch ($this->http_method) {
100                         case 'DELETE':
101                                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->http_method);
102                                 break;
103                         case 'POST':
104                                 curl_setopt($ch, CURLOPT_POST, true);
105                                 break;
106                 }
107
108                 $this->last_http_result = new stdClass();
109                 $this->last_http_result->response = curl_exec($ch);
110                 $this->last_http_result->info = curl_getinfo($ch);
111                 curl_close($ch);
112                 if (isset($fh)) {
113                         fclose($fh);
114                 }
115
116                 // Maybe update CSRF token
117                 $token = $this->parseAuthenticityToken($this->last_http_result->response);
118                 if ($token) {
119                         $this->csrf_token = $token;
120                 }
121
122                 return $this->last_http_result;
123         }
124
125         private function doHttpDelete($url, $data = [], $headers = []) {
126                 $this->http_method = 'DELETE';
127                 $this->doHttpRequest($url, $data, $headers);
128                 $this->http_method = null; // reset for next request
129         }
130
131         private function parseAuthenticityToken($str) {
132                 $m = [];
133                 preg_match('/<meta (?:name="csrf-token" content="(.*?)"|content="(.*?)" name="csrf-token")/', $str, $m);
134                 if (empty($m[1]) && !empty($m[2])) {
135                         $token = $m[2];
136                 } elseif (!empty($m[1])) {
137                         $token = $m[1];
138                 }
139                 return (!empty($token)) ? $token : false;
140         }
141
142         private function readJsonResponse($response) {
143                 $lines = explode("\r\n", $response);
144                 $x = array_splice(
145                         $lines, array_search('', $lines) + 1 // empty, as "\r\n" was explode()'d
146                 );
147                 $http_body = array_pop($x);
148                 return json_decode($http_body);
149         }
150
151         public function logIn() {
152                 $this->doHttpRequest('/users/sign_in');
153
154                 $params = [
155                         'user[username]' => $this->user,
156                         'user[password]' => $this->password,
157                         'authenticity_token' => $this->csrf_token
158                 ];
159                 $this->doHttpRequest('/users/sign_in', $params);
160                 $this->doHttpRequest('/stream');
161                 return (200 === $this->last_http_result->info['http_code']) ? true : false;
162         }
163
164         public function getAspects() {
165                 $this->doHttpRequest('/bookmarklet');
166                 $m = [];
167                 preg_match('/"aspects"\:(\[.+?\])/', $this->last_http_result->response, $m);
168                 return (!empty($m[1])) ? json_decode($m[1]) : false;
169         }
170
171         public function getServices() {
172                 $this->doHttpRequest('/bookmarklet');
173                 $m = [];
174                 preg_match('/"configured_services"\:(\[.+?\])/', $this->last_http_result->response, $m);
175                 return (!empty($m[1])) ? json_decode($m[1]) : false;
176         }
177
178         public function getNotifications($notification_type = '', $show = '') {
179                 $url = '/notifications?format=json';
180
181                 if (!empty($notification_type)) {
182                         $url .= "&type=$notification_type";
183                 }
184
185                 if ('unread' === $show) {
186                         $url .= '&show=unread';
187                 }
188
189                 $this->doHttpRequest($url);
190                 return $this->readJsonResponse($this->last_http_result->response);
191         }
192
193         public function getComments($post_id) {
194                 $url = "/posts/$post_id/comments?format=json";
195                 $this->doHttpRequest($url);
196                 return $this->readJsonResponse($this->last_http_result->response);
197         }
198
199         public function postStatusMessage($msg, $aspect_ids = 'all_aspects', $additional_data = []) {
200                 $data = [
201                         'aspect_ids' => $aspect_ids,
202                         'status_message' => [
203                                 'text' => $msg,
204                                 'provider_display_name' => $this->provider
205                         ]
206                 ];
207
208                 if (!empty($additional_data)) {
209                         $data += $additional_data;
210                 }
211
212                 $headers = [
213                         'Content-Type: application/json',
214                         'Accept: application/json',
215                         'X-CSRF-Token: ' . $this->csrf_token
216                 ];
217
218                 $this->http_method = 'POST';
219                 $this->doHttpRequest('/status_messages', json_encode($data), $headers);
220                 $this->http_method = null; // reset for next request
221                 if (201 !== $this->last_http_result->info['http_code']) {
222                         // TODO: Handle error.
223                         return false;
224                 } elseif (200 !== $this->last_http_result->info['http_code']) {
225                         $resp = $this->readJsonResponse($this->last_http_result->response);
226                         return $resp->id;
227                 }
228         }
229
230         public function postPhoto($file) {
231                 $params = [
232                         'photo[pending]' => 'true',
233                         'qqfile' => basename($file)
234                 ];
235                 $query_string = '?' . http_build_query($params);
236                 $headers = [
237                         'Accept: application/json',
238                         'X-Requested-With: XMLHttpRequest',
239                         'X-CSRF-Token: ' . $this->csrf_token,
240                         'X-File-Name: ' . basename($file),
241                         'Content-Type: application/octet-stream',
242                 ];
243                 if ($size = @filesize($file)) {
244                         $headers[] = "Content-Length: $size";
245                 }
246                 $data = file_get_contents($file);
247                 $this->doHttpRequest('/photos' . $query_string, $data, $headers);
248                 return $this->readJsonResponse($this->last_http_result->response);
249         }
250
251         public function deletePost($id) {
252                 $headers = ['X-CSRF-Token: ' . $this->csrf_token];
253                 $this->doHttpDelete("/posts/$id", [], $headers);
254                 return (204 === $this->last_http_result->info['http_code']) ? true : false;
255         }
256
257         public function deleteComment($id) {
258                 $headers = ['X-CSRF-Token: ' . $this->csrf_token];
259                 $this->doHttpDelete("/comments/$id", [], $headers);
260                 return (204 === $this->last_http_result->info['http_code']) ? true : false;
261         }
262
263 }