3 use Friendica\Core\System;
6 * Super-skeletal class to interact with Diaspora.
8 * @author Meitar Moscovitz <meitarm@gmail.com>
9 * Modifications by Michael Vogel <heluecht@pirati.ca>
12 class Diaspora_Connection {
16 private $tls = true; //< Whether to use an SSL/TLS connection or not.
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.
25 public $provider = '*Diaspora Connection';
27 public function __construct($diaspora_handle = '', $password = '') {
28 if (!empty($diaspora_handle)) {
29 $this->setDiasporaID($diaspora_handle);
31 if (!empty($password)) {
32 $this->setPassword($password);
35 $this->cookiejar = tempnam(System::getTempPath(), 'cookies');
39 public function __destruct() {
40 if (file_exists($this->cookiejar)) {
41 unlink($this->cookiejar);
45 public function setDebugLog($log_file) {
46 $this->debug_log = $log_file;
49 public function setDiasporaID($id) {
50 $parts = explode('@', $id);
51 $this->user = $parts[0];
52 if (count($parts) > 1) {
53 $this->host = $parts[1];
59 public function getDiasporaID() {
60 return $this->user . '@' . $this->host;
63 public function getPodURL() {
64 return $this->getScheme() . '://' . $this->host;
67 public function setPassword($passwd) {
68 $this->password = $passwd;
71 public function setSecureTransport($is_secure) {
72 $this->tls = (bool) $is_secure;
75 private function getScheme() {
76 return ($this->tls) ? 'https' : 'http';
79 private function doHttpRequest($url, $data = [], $headers = []) {
80 if (0 === strpos($url, '/')) {
81 $url = $this->getScheme() . '://' . $this->host . $url;
84 $ch = curl_init($url);
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);
92 curl_setopt($ch, CURLOPT_HEADER, true);
93 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
96 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
98 if (!empty($headers)) {
99 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
102 curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
103 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
105 // Are we doing a special kind of HTTP request?
106 switch ($this->http_method) {
108 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->http_method);
111 curl_setopt($ch, CURLOPT_POST, true);
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);
123 // Maybe update CSRF token
124 $token = $this->parseAuthenticityToken($this->last_http_result->response);
126 $this->csrf_token = $token;
129 return $this->last_http_result;
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
138 private function parseAuthenticityToken($str) {
140 preg_match('/<meta (?:name="csrf-token" content="(.*?)"|content="(.*?)" name="csrf-token")/', $str, $m);
141 if (empty($m[1]) && !empty($m[2])) {
143 } elseif (!empty($m[1])) {
146 return !empty($token) ? $token : false;
149 private function readJsonResponse($response) {
150 $lines = explode("\r\n", $response);
152 $lines, array_search('', $lines) + 1 // empty, as "\r\n" was explode()'d
154 $http_body = array_pop($x);
155 return json_decode($http_body);
158 public function logIn() {
159 $this->doHttpRequest('/users/sign_in');
162 'user[username]' => $this->user,
163 'user[password]' => $this->password,
164 'authenticity_token' => $this->csrf_token
166 $this->doHttpRequest('/users/sign_in', $params);
167 $this->doHttpRequest('/stream');
168 return (200 === $this->last_http_result->info['http_code']) ? true : false;
171 public function getAspects() {
172 $this->doHttpRequest('/bookmarklet');
174 preg_match('/"aspects"\:(\[.+?\])/', $this->last_http_result->response, $m);
175 return !empty($m[1]) ? json_decode($m[1]) : false;
178 public function getServices() {
179 $this->doHttpRequest('/bookmarklet');
181 preg_match('/"configured_services"\:(\[.+?\])/', $this->last_http_result->response, $m);
182 return !empty($m[1]) ? json_decode($m[1]) : false;
185 public function getNotifications($notification_type = '', $show = '') {
186 $url = '/notifications?format=json';
188 if (!empty($notification_type)) {
189 $url .= "&type=$notification_type";
192 if ('unread' === $show) {
193 $url .= '&show=unread';
196 $this->doHttpRequest($url);
197 return $this->readJsonResponse($this->last_http_result->response);
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);
206 public function postStatusMessage($msg, $aspect_ids = 'all_aspects', $additional_data = []) {
208 'aspect_ids' => $aspect_ids,
209 'status_message' => [
211 'provider_display_name' => $this->provider
215 if (!empty($additional_data)) {
216 $data += $additional_data;
220 'Content-Type: application/json',
221 'Accept: application/json',
222 'X-CSRF-Token: ' . $this->csrf_token
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.
231 } elseif (200 !== $this->last_http_result->info['http_code']) {
232 $resp = $this->readJsonResponse($this->last_http_result->response);
237 public function postPhoto($file) {
239 'photo[pending]' => 'true',
240 'qqfile' => basename($file)
242 $query_string = '?' . http_build_query($params);
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',
250 if ($size = @filesize($file)) {
251 $headers[] = "Content-Length: $size";
253 $data = file_get_contents($file);
254 $this->doHttpRequest('/photos' . $query_string, $data, $headers);
255 return $this->readJsonResponse($this->last_http_result->response);
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;
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;