make user URL optional in connect command
[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         $sx = simplexml_load_file($server);
45         if ($sx === false) {
46             Log::err('Error loading URL: ' . $server);
47             exit(1);
48         }
49         $sx->registerXPathNamespace('h', 'http://www.w3.org/1999/xhtml');
50
51         $auths = $sx->xpath(
52             '/h:html/h:head/h:link[@rel="authorization_endpoint" and @href]'
53         );
54         if (!count($auths)) {
55             Log::err('No authorization endpoint found');
56             exit(1);
57         }
58         $this->authorization = (string) $auths[0]['href'];
59
60         $tokens = $sx->xpath(
61             '/h:html/h:head/h:link[@rel="token_endpoint" and @href]'
62         );
63         if (!count($tokens)) {
64             Log::err('No token endpoint found');
65             exit(1);
66         }
67         $this->token = (string) $tokens[0]['href'];
68
69         $mps = $sx->xpath(
70             '/h:html/h:head/h:link[@rel="micropub" and @href]'
71         );
72         if (!count($mps)) {
73             Log::err('No micropub endpoint found');
74             exit(1);
75         }
76         $this->micropub = (string) $mps[0]['href'];
77     }
78
79     public function load($server)
80     {
81         $file = $this->getCacheFilePath($server, false);
82         if ($file === false || !file_exists($file)) {
83             return false;
84         }
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 . '"');
89                 exit(1);
90             }
91             $this->$prop = $val;
92         }
93         return true;
94     }
95
96     public function save($server)
97     {
98         $file = $this->getCacheFilePath($server, true);
99         if ($file === false) {
100             return false;
101         }
102
103         file_put_contents(
104             $file,
105             'micropub=' . $this->micropub . "\n"
106             . 'media=' . $this->media . "\n"
107             . 'token=' . $this->token . "\n"
108              . 'authorization=' . $this->authorization . "\n"
109         );
110     }
111
112     public function getCacheFilePath($server, $create = false)
113     {
114         if (isset($_SERVER['XDG_CACHE_HOME'])
115             && $_SERVER['XDG_CACHE_HOME'] != ''
116         ) {
117             $cacheBaseDir = $_SERVER['XDG_CACHE_HOME'];
118         } else {
119             if (!isset($_SERVER['HOME']) || $_SERVER['HOME'] == '') {
120                 Log::err('Cannot determine home directory');
121                 return false;
122             }
123             $cacheBaseDir = $_SERVER['HOME'] . '/.cache';
124         }
125
126         $cacheDir = $cacheBaseDir . '/shpub';
127         if (!is_dir($cacheDir) && $create) {
128             mkdir($cacheDir, 0700, true);
129         }
130         $file = $cacheDir . '/' . urlencode($server) . '.ini';
131         return $file;
132     }
133 }
134 ?>