mastodon.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if($domain == base64_decode("aHR0cHM6Ly9nYWIuY29t") || $domain == base64_decode("aHR0cHM6Ly9nYWIuYWk=")) {
  35. header("Location: /login/?cause=domain");
  36. die();
  37. }
  38. $this->set_url($domain);
  39. if (!$this->instanceExists($domain)) {
  40. $this->newInstance($domain);
  41. }
  42. return array('client_id' => $this->instances[$domain]['client_id'],'client_secret' => $this->instances[$domain]['client_secret']);
  43. }
  44. public function instanceExists($domain) {
  45. return isset($this->instances[$domain]);
  46. }
  47. private function readInstances() {
  48. $instlist = array_diff(scandir($this->datadir),array("..",".",".htaccess"));
  49. foreach($instlist as $index => $item) {
  50. $itemname = "https://".substr($item,0,-4);
  51. $this->instances[$itemname] = json_decode(file_get_contents($this->datadir."/".$item),true);
  52. }
  53. }
  54. }
  55. ?>