2014-01-12 19:30:01 -05:00
|
|
|
<?php
|
2021-11-04 16:32:16 -04:00
|
|
|
|
|
|
|
use Friendica\Core\System;
|
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
/**
|
|
|
|
* Ein fies zusammengehackter PHP-Diaspory-Client, der direkt von diesem abgeschaut ist:
|
|
|
|
* https://github.com/Javafant/diaspy/blob/master/client.py
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Diasphp {
|
2015-01-09 01:07:07 -05:00
|
|
|
private $cookiejar;
|
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
function __construct($pod) {
|
|
|
|
$this->token_regex = '/content="(.*?)" name="csrf-token/';
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
$this->pod = $pod;
|
2021-11-04 16:32:16 -04:00
|
|
|
$this->cookiejar = tempnam(System::getTempPath(), 'cookies');
|
2014-01-12 19:30:01 -05:00
|
|
|
}
|
|
|
|
|
2015-01-09 01:07:07 -05:00
|
|
|
function __destruct() {
|
|
|
|
if (file_exists($this->cookiejar))
|
|
|
|
unlink($this->cookiejar);
|
|
|
|
}
|
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
function _fetch_token() {
|
|
|
|
$ch = curl_init();
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
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);
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
$output = curl_exec ($ch);
|
|
|
|
curl_close($ch);
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
// Token holen und zurückgeben
|
|
|
|
preg_match($this->token_regex, $output, $matches);
|
|
|
|
return $matches[1];
|
|
|
|
}
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
function login($username, $password) {
|
|
|
|
$datatopost = array(
|
|
|
|
'user[username]' => $username,
|
|
|
|
'user[password]' => $password,
|
|
|
|
'authenticity_token' => $this->_fetch_token()
|
|
|
|
);
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
$poststr = http_build_query($datatopost);
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
// Adresse per cURL abrufen
|
|
|
|
$ch = curl_init();
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
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);
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
curl_exec ($ch);
|
|
|
|
$info = curl_getinfo($ch);
|
|
|
|
curl_close($ch);
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
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;
|
|
|
|
}
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-04-22 09:28:23 -04:00
|
|
|
function post($text, $provider = "diasphp") {
|
2014-01-12 19:30:01 -05:00
|
|
|
// post-daten vorbereiten
|
|
|
|
$datatopost = json_encode(array(
|
|
|
|
'aspect_ids' => 'public',
|
2014-04-22 09:28:23 -04:00
|
|
|
'status_message' => array('text' => $text,
|
|
|
|
'provider_display_name' => $provider)
|
2014-01-12 19:30:01 -05:00
|
|
|
));
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
// header vorbereiten
|
|
|
|
$headers = array(
|
|
|
|
'Content-Type: application/json',
|
|
|
|
'accept: application/json',
|
|
|
|
'x-csrf-token: '.$this->_fetch_token()
|
|
|
|
);
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
// Adresse per cURL abrufen
|
|
|
|
$ch = curl_init();
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
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);
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
curl_exec ($ch);
|
|
|
|
$info = curl_getinfo($ch);
|
|
|
|
curl_close($ch);
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
if($info['http_code'] != 201) {
|
|
|
|
throw new Exception('Post error '.print_r($info, true));
|
|
|
|
}
|
2014-03-16 14:13:57 -04:00
|
|
|
|
2014-01-12 19:30:01 -05:00
|
|
|
// Ende der möglichen Kette, gib mal "true" zurück.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|