]> git.mxchange.org Git - friendica-addons.git/blob - diaspora/diasphp.php
Merge pull request 'CLD2: Use ISO-639-1 for the language detection' (#1433) from...
[friendica-addons.git] / diaspora / diasphp.php
1 <?php
2
3 use Friendica\Core\System;
4
5 /**
6  * Ein fies zusammengehackter PHP-Diaspory-Client, der direkt von diesem abgeschaut ist:
7  * https://github.com/Javafant/diaspy/blob/master/client.py
8  */
9
10 class Diasphp {
11         private $cookiejar;
12
13         function __construct($pod) {
14                 $this->token_regex = '/content="(.*?)" name="csrf-token/';
15
16                 $this->pod = $pod;
17                 $this->cookiejar = tempnam(System::getTempPath(), 'cookies');
18         }
19
20         function __destruct() {
21                 if (file_exists($this->cookiejar))
22                         unlink($this->cookiejar);
23         }
24
25         function _fetch_token() {
26                 $ch = curl_init();
27
28                 curl_setopt ($ch, CURLOPT_URL, $this->pod . "/stream");
29                 curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
30                 curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
31                 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
32                 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
33
34                 $output = curl_exec ($ch);
35                 curl_close($ch);
36
37                 // Token holen und zurückgeben
38                 preg_match($this->token_regex, $output, $matches);
39                 return $matches[1];
40         }
41
42         function login($username, $password) {
43                 $datatopost = array(
44                         'user[username]' => $username,
45                         'user[password]' => $password,
46                         'authenticity_token' => $this->_fetch_token()
47                 );
48
49                 $poststr = http_build_query($datatopost);
50
51                 // Adresse per cURL abrufen
52                 $ch = curl_init();
53
54                 curl_setopt ($ch, CURLOPT_URL, $this->pod . "/users/sign_in");
55                 curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
56                 curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
57                 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, false);
58                 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
59                 curl_setopt ($ch, CURLOPT_POST, true);
60                 curl_setopt ($ch, CURLOPT_POSTFIELDS, $poststr);
61
62                 curl_exec ($ch);
63                 $info = curl_getinfo($ch);
64                 curl_close($ch);
65
66                 if($info['http_code'] != 302) {
67                         throw new Exception('Login error '.print_r($info, true));
68                 }
69
70                 // Das Objekt zurückgeben, damit man Aurufe verketten kann.
71                 return $this;
72         }
73
74         function post($text, $provider = "diasphp") {
75                 // post-daten vorbereiten
76                 $datatopost = json_encode(array(
77                                 'aspect_ids' => 'public',
78                                 'status_message' => array('text' => $text,
79                                                         'provider_display_name' => $provider)
80                 ));
81
82                 // header vorbereiten
83                 $headers = array(
84                         'Content-Type: application/json',
85                         'accept: application/json',
86                         'x-csrf-token: '.$this->_fetch_token()
87                 );
88
89                 // Adresse per cURL abrufen
90                 $ch = curl_init();
91
92                 curl_setopt ($ch, CURLOPT_URL, $this->pod . "/status_messages");
93                 curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
94                 curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
95                 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, false);
96                 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
97                 curl_setopt ($ch, CURLOPT_POST, true);
98                 curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
99                 curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
100
101                 curl_exec ($ch);
102                 $info = curl_getinfo($ch);
103                 curl_close($ch);
104
105                 if($info['http_code'] != 201) {
106                         throw new Exception('Post error '.print_r($info, true));
107                 }
108
109                 // Ende der möglichen Kette, gib mal "true" zurück.
110                 return true;
111         }
112 }