2018-03-11 11:07:57 +00:00
|
|
|
<?php
|
|
|
|
namespace HalcyonSuite\HalcyonForMastodon;
|
|
|
|
require_once('mastodon-api/mastodon.php');
|
|
|
|
class Mastodon extends \Mastodon_api {
|
|
|
|
function __construct(){
|
2018-07-25 16:31:54 +01:00
|
|
|
$appSettings = parse_ini_file('../config/config.ini',true);
|
2018-03-11 11:07:57 +00:00
|
|
|
$this->datadir = "../data";
|
|
|
|
$this->clientName = $appSettings["App"]["api_client_name"];
|
2019-02-25 17:18:21 +00:00
|
|
|
$this->clientRedirectUris = $appSettings["App"]["api_client_website"].'/auth';
|
2018-03-11 11:07:57 +00:00
|
|
|
$this->clientWebsite = $appSettings["App"]["api_client_website"];
|
|
|
|
$this->clientScopes = array('read','write','follow');
|
|
|
|
$this->instances = array();
|
|
|
|
$this->readInstances();
|
|
|
|
}
|
|
|
|
private function newInstance($domain) {
|
2019-02-25 17:18:21 +00:00
|
|
|
$res = $this->create_app($this->clientName,$this->clientScopes,$this->clientRedirectUris."?&host=".substr($domain,8),$this->clientWebsite);
|
2018-03-11 11:07:57 +00:00
|
|
|
if(isset($res['html']['client_id'])) {
|
|
|
|
$this->instances[$domain] = $res['html'];
|
|
|
|
file_put_contents($this->datadir."/".substr($domain,8).".txt",json_encode(array("client_id" => $res['html']['client_id'],"client_secret" => $res['html']['client_secret'])));
|
|
|
|
}
|
|
|
|
else {
|
2019-03-01 15:17:03 +00:00
|
|
|
header("Location: /login/?cause=domain");
|
|
|
|
die();
|
2018-03-11 11:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public function selectInstance($domain) {
|
|
|
|
$this->set_url($domain);
|
|
|
|
if(!$this->instanceExists($domain)) {
|
|
|
|
$this->newInstance($domain);
|
|
|
|
}
|
|
|
|
$this->set_client($this->instances[$domain]['client_id'],$this->instances[$domain]['client_secret']);
|
|
|
|
}
|
|
|
|
public function getInstance($domain) {
|
2019-06-20 15:50:42 +01:00
|
|
|
if($domain == base64_decode("aHR0cHM6Ly9nYWIuY29t") || $domain == base64_decode("aHR0cHM6Ly9nYWIuYWk=")) {
|
|
|
|
header("Location: /login/?cause=domain");
|
|
|
|
die();
|
|
|
|
}
|
2018-03-11 11:07:57 +00:00
|
|
|
$this->set_url($domain);
|
|
|
|
if (!$this->instanceExists($domain)) {
|
|
|
|
$this->newInstance($domain);
|
|
|
|
}
|
|
|
|
return array('client_id' => $this->instances[$domain]['client_id'],'client_secret' => $this->instances[$domain]['client_secret']);
|
|
|
|
}
|
|
|
|
public function instanceExists($domain) {
|
|
|
|
return isset($this->instances[$domain]);
|
|
|
|
}
|
|
|
|
private function readInstances() {
|
|
|
|
$instlist = array_diff(scandir($this->datadir),array("..",".",".htaccess"));
|
|
|
|
foreach($instlist as $index => $item) {
|
|
|
|
$itemname = "https://".substr($item,0,-4);
|
|
|
|
$this->instances[$itemname] = json_decode(file_get_contents($this->datadir."/".$item),true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-25 16:31:54 +01:00
|
|
|
?>
|