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