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