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