3 use Friendica\Core\System;
6 * Ein fies zusammengehackter PHP-Diaspory-Client, der direkt von diesem abgeschaut ist:
7 * https://github.com/Javafant/diaspy/blob/master/client.py
13 function __construct($pod) {
14 $this->token_regex = '/content="(.*?)" name="csrf-token/';
17 $this->cookiejar = tempnam(System::getTempPath(), 'cookies');
20 function __destruct() {
21 if (file_exists($this->cookiejar))
22 unlink($this->cookiejar);
25 function _fetch_token() {
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);
34 $output = curl_exec ($ch);
37 // Token holen und zurückgeben
38 preg_match($this->token_regex, $output, $matches);
42 function login($username, $password) {
44 'user[username]' => $username,
45 'user[password]' => $password,
46 'authenticity_token' => $this->_fetch_token()
49 $poststr = http_build_query($datatopost);
51 // Adresse per cURL abrufen
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);
63 $info = curl_getinfo($ch);
66 if($info['http_code'] != 302) {
67 throw new Exception('Login error '.print_r($info, true));
70 // Das Objekt zurückgeben, damit man Aurufe verketten kann.
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)
84 'Content-Type: application/json',
85 'accept: application/json',
86 'x-csrf-token: '.$this->_fetch_token()
89 // Adresse per cURL abrufen
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);
102 $info = curl_getinfo($ch);
105 if($info['http_code'] != 201) {
106 throw new Exception('Post error '.print_r($info, true));
109 // Ende der möglichen Kette, gib mal "true" zurück.