Load HTML as HTML, resolve relative endpoint URLs
[shpub.git] / src / shpub / Config / Endpoints.php
1 <?php
2 namespace shpub;
3
4 class Config_Endpoints
5 {
6     /**
7      * Micropub endpoint URL
8      *
9      * @var string
10      */
11     public $micropub;
12
13     /**
14      * Micropub media endpoint URL
15      *
16      * @var string
17      */
18     public $media;
19
20     /**
21      * Access token endpoint URL
22      *
23      * @var string
24      */
25     public $token;
26
27     /**
28      * Authorization endpoint URL
29      *
30      * @var string
31      */
32     public $authorization;
33
34     public function incomplete()
35     {
36         return $this->authorization === null
37             || $this->token === null
38             || $this->micropub === null;
39     }
40
41     public function discover($server)
42     {
43         //TODO: discovery via link headers
44         $baseUrl = new \Net_URL2($server);
45
46         $doc = new \DOMDocument();
47         $doc->loadHTMLFile($server);
48         $sx = simplexml_import_dom($doc);
49         if ($sx === false) {
50             Log::err('Error loading URL: ' . $server);
51             exit(1);
52         }
53         //$sx->registerXPathNamespace('h', 'http://www.w3.org/1999/xhtml');
54
55         $auths = $sx->xpath(
56             '/html/head/link[@rel="authorization_endpoint" and @href]'
57         );
58         if (!count($auths)) {
59             Log::err('No authorization endpoint found at ' . $server);
60             exit(1);
61         }
62         $this->authorization = (string) $baseUrl->resolve(
63             (string) $auths[0]['href']
64         );
65
66         $tokens = $sx->xpath(
67             '/html/head/link[@rel="token_endpoint" and @href]'
68         );
69         if (!count($tokens)) {
70             Log::err('No token endpoint found');
71             exit(1);
72         }
73         $this->token = (string) $baseUrl->resolve(
74             (string) $tokens[0]['href']
75         );
76
77         $mps = $sx->xpath(
78             '/html/head/link[@rel="micropub" and @href]'
79         );
80         if (!count($mps)) {
81             Log::err('No micropub endpoint found');
82             exit(1);
83         }
84         $this->micropub = (string) $baseUrl->resolve(
85             (string) $mps[0]['href']
86         );
87     }
88
89     public function load($server)
90     {
91         $file = $this->getCacheFilePath($server, false);
92         if ($file === false || !file_exists($file)) {
93             return false;
94         }
95         $data = parse_ini_file($file);
96         foreach ($data as $prop => $val) {
97             if (!property_exists($this, $prop)) {
98                 Log::err('Invalid cache config key "' . $prop . '"');
99                 exit(1);
100             }
101             $this->$prop = $val;
102         }
103         return true;
104     }
105
106     public function save($server)
107     {
108         $file = $this->getCacheFilePath($server, true);
109         if ($file === false) {
110             return false;
111         }
112
113         file_put_contents(
114             $file,
115             'micropub=' . $this->micropub . "\n"
116             . 'media=' . $this->media . "\n"
117             . 'token=' . $this->token . "\n"
118              . 'authorization=' . $this->authorization . "\n"
119         );
120     }
121
122     public function getCacheFilePath($server, $create = false)
123     {
124         if (isset($_SERVER['XDG_CACHE_HOME'])
125             && $_SERVER['XDG_CACHE_HOME'] != ''
126         ) {
127             $cacheBaseDir = $_SERVER['XDG_CACHE_HOME'];
128         } else {
129             if (!isset($_SERVER['HOME']) || $_SERVER['HOME'] == '') {
130                 Log::err('Cannot determine home directory');
131                 return false;
132             }
133             $cacheBaseDir = $_SERVER['HOME'] . '/.cache';
134         }
135
136         $cacheDir = $cacheBaseDir . '/shpub';
137         if (!is_dir($cacheDir) && $create) {
138             mkdir($cacheDir, 0700, true);
139         }
140         $file = $cacheDir . '/' . urlencode($server) . '.ini';
141         return $file;
142     }
143 }
144 ?>