mastodon.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 urn:ietf:wg:oauth:2.0:oob';
  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,$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. throw new Exception("Invalid instance");
  23. }
  24. }
  25. public function selectInstance($domain) {
  26. $this->set_url($domain);
  27. if(!$this->instanceExists($domain)) {
  28. $this->newInstance($domain);
  29. }
  30. $this->set_client($this->instances[$domain]['client_id'],$this->instances[$domain]['client_secret']);
  31. }
  32. public function getInstance($domain) {
  33. $this->set_url($domain);
  34. if (!$this->instanceExists($domain)) {
  35. $this->newInstance($domain);
  36. }
  37. return array('client_id' => $this->instances[$domain]['client_id'],'client_secret' => $this->instances[$domain]['client_secret']);
  38. }
  39. public function instanceExists($domain) {
  40. return isset($this->instances[$domain]);
  41. }
  42. private function readInstances() {
  43. $instlist = array_diff(scandir($this->datadir),array("..",".",".htaccess"));
  44. foreach($instlist as $index => $item) {
  45. $itemname = "https://".substr($item,0,-4);
  46. $this->instances[$itemname] = json_decode(file_get_contents($this->datadir."/".$item),true);
  47. }
  48. }
  49. }
  50. ?>