]> git.mxchange.org Git - friendica-addons.git/blob - diaspora/diasphp.php
diaspora: New posting connector for Diaspora
[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         function __construct($pod) {
9                 $this->token_regex = '/content="(.*?)" name="csrf-token/';
10                 
11                 $this->pod = $pod;
12                 $this->cookiejar = tempnam(sys_get_temp_dir(), 'cookies');
13         }
14
15         function _fetch_token() {
16                 $ch = curl_init();
17                 
18                 curl_setopt ($ch, CURLOPT_URL, $this->pod . "/stream");
19                 curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
20                 curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
21                 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
22                 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
23                 
24                 $output = curl_exec ($ch);
25                 curl_close($ch);
26                 
27                 // Token holen und zurückgeben
28                 preg_match($this->token_regex, $output, $matches);
29                 return $matches[1];
30         }
31         
32         function login($username, $password) {
33                 $datatopost = array(
34                         'user[username]' => $username,
35                         'user[password]' => $password,
36                         'authenticity_token' => $this->_fetch_token()
37                 );
38                 
39                 $poststr = http_build_query($datatopost);
40                 
41                 // Adresse per cURL abrufen
42                 $ch = curl_init();
43                 
44                 curl_setopt ($ch, CURLOPT_URL, $this->pod . "/users/sign_in");
45                 curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
46                 curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
47                 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, false);
48                 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
49                 curl_setopt ($ch, CURLOPT_POST, true);
50                 curl_setopt ($ch, CURLOPT_POSTFIELDS, $poststr);
51                 
52                 curl_exec ($ch);
53                 $info = curl_getinfo($ch);
54                 curl_close($ch);
55                 
56                 if($info['http_code'] != 302) {
57                         throw new Exception('Login error '.print_r($info, true));
58                 }
59
60                 // Das Objekt zurückgeben, damit man Aurufe verketten kann.
61                 return $this;
62         }
63         
64         function post($text) {
65                 // post-daten vorbereiten
66                 $datatopost = json_encode(array(
67                                 'aspect_ids' => 'public',
68                                 'status_message' => array('text' => $text)
69                 ));
70                 
71                 // header vorbereiten
72                 $headers = array(
73                         'Content-Type: application/json',
74                         'accept: application/json',
75                         'x-csrf-token: '.$this->_fetch_token()
76                 );
77                 
78                 // Adresse per cURL abrufen
79                 $ch = curl_init();
80                 
81                 curl_setopt ($ch, CURLOPT_URL, $this->pod . "/status_messages");
82                 curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
83                 curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
84                 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, false);
85                 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
86                 curl_setopt ($ch, CURLOPT_POST, true);
87                 curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
88                 curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
89                 
90                 curl_exec ($ch);
91                 $info = curl_getinfo($ch);
92                 curl_close($ch);
93                 
94                 if($info['http_code'] != 201) {
95                         throw new Exception('Post error '.print_r($info, true));
96                 }
97                 
98                 // Ende der möglichen Kette, gib mal "true" zurück.
99                 return true;
100         }
101 }
102 ?>