mastodon.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace HalcyonSuite\HalcyonForMastodon;
  3. require_once('mastodon-api/mastodon.php');
  4. class Mastodon extends \Mastodon_api {
  5. function __construct(){
  6. $appSettings = parse_ini_file('../config/config.ini',true);
  7. $this->datadir = "../data";
  8. $this->clientName = $appSettings["App"]["api_client_name"];
  9. $this->clientRedirectUris = $appSettings["App"]["api_client_website"].'/auth';
  10. $this->clientWebsite = $appSettings["App"]["api_client_website"];
  11. $this->clientScopes = array('read','write','follow');
  12. $this->instances = array();
  13. $this->readInstances();
  14. }
  15. private function newInstance($domain) {
  16. $res = $this->create_app($this->clientName,$this->clientScopes,$this->clientRedirectUris."?&host=".substr($domain,8),$this->clientWebsite);
  17. if(isset($res['html']['client_id'])) {
  18. $this->instances[$domain] = $res['html'];
  19. file_put_contents($this->datadir."/".substr($domain,8).".txt",json_encode(array("client_id" => $res['html']['client_id'],"client_secret" => $res['html']['client_secret'])));
  20. }
  21. else {
  22. header("Location: /login/?cause=domain");
  23. die();
  24. }
  25. }
  26. public function selectInstance($domain) {
  27. $this->set_url($domain);
  28. if(!$this->instanceExists($domain)) {
  29. $this->newInstance($domain);
  30. }
  31. $this->set_client($this->instances[$domain]['client_id'],$this->instances[$domain]['client_secret']);
  32. }
  33. public function getInstance($domain) {
  34. $this->set_url($domain);
  35. if (!$this->instanceExists($domain)) {
  36. $this->newInstance($domain);
  37. }
  38. return array('client_id' => $this->instances[$domain]['client_id'],'client_secret' => $this->instances[$domain]['client_secret']);
  39. }
  40. public function instanceExists($domain) {
  41. return isset($this->instances[$domain]);
  42. }
  43. private function readInstances() {
  44. $instlist = array_diff(scandir($this->datadir),array("..",".",".htaccess"));
  45. foreach($instlist as $index => $item) {
  46. $itemname = "https://".substr($item,0,-4);
  47. $this->instances[$itemname] = json_decode(file_get_contents($this->datadir."/".$item),true);
  48. }
  49. }
  50. }
  51. ?>