7 * Micropub endpoint URL
14 * Micropub media endpoint URL
21 * Access token endpoint URL
28 * Authorization endpoint URL
32 public $authorization;
34 public function incomplete()
36 return $this->authorization === null
37 || $this->token === null
38 || $this->micropub === null;
41 public function discover($server)
43 //TODO: discovery via link headers
44 $sx = simplexml_load_file($server);
46 Log::err('Error loading URL: ' . $server);
49 $sx->registerXPathNamespace('h', 'http://www.w3.org/1999/xhtml');
52 '/h:html/h:head/h:link[@rel="authorization_endpoint" and @href]'
55 Log::err('No authorization endpoint found');
58 $this->authorization = (string) $auths[0]['href'];
61 '/h:html/h:head/h:link[@rel="token_endpoint" and @href]'
63 if (!count($tokens)) {
64 Log::err('No token endpoint found');
67 $this->token = (string) $tokens[0]['href'];
70 '/h:html/h:head/h:link[@rel="micropub" and @href]'
73 Log::err('No micropub endpoint found');
76 $this->micropub = (string) $mps[0]['href'];
79 public function load($server)
81 $file = $this->getCacheFilePath($server, false);
82 if ($file === false || !file_exists($file)) {
85 $data = parse_ini_file($file);
86 foreach ($data as $prop => $val) {
87 if (!property_exists($this, $prop)) {
88 Log::err('Invalid cache config key "' . $prop . '"');
96 public function save($server)
98 $file = $this->getCacheFilePath($server, true);
99 if ($file === false) {
105 'micropub=' . $this->micropub . "\n"
106 . 'media=' . $this->media . "\n"
107 . 'token=' . $this->token . "\n"
108 . 'authorization=' . $this->authorization . "\n"
112 public function getCacheFilePath($server, $create = false)
114 if (isset($_SERVER['XDG_CACHE_HOME'])
115 && $_SERVER['XDG_CACHE_HOME'] != ''
117 $cacheBaseDir = $_SERVER['XDG_CACHE_HOME'];
119 if (!isset($_SERVER['HOME']) || $_SERVER['HOME'] == '') {
120 Log::err('Cannot determine home directory');
123 $cacheBaseDir = $_SERVER['HOME'] . '/.cache';
126 $cacheDir = $cacheBaseDir . '/shpub';
127 if (!is_dir($cacheDir) && $create) {
128 mkdir($cacheDir, 0700, true);
130 $file = $cacheDir . '/' . urlencode($server) . '.ini';