add setup check for access rights
[phancap.git] / www / setup.php
1 <?php
2 namespace phancap;
3 /**
4  * Check if everything is setup
5  */
6 header('HTTP/1.0 500 Internal Server Error');
7
8 if (file_exists(__DIR__ . '/../src/phancap/Autoloader.php')) {
9     include_once __DIR__ . '/../src/phancap/Autoloader.php';
10     Autoloader::register();
11 } else {
12     include_once 'phancap/Autoloader.php';
13 }
14
15 $messages = array();
16
17 $config = new Config();
18 try {
19     $config->load();
20     $messages[][] = array('ok', 'Base configuration is ok');
21
22     if ($config->access === true) {
23         $messages[][] = array('ok', 'Everyone may access the API');
24     } else if ($config->access === false) {
25         $messages[][] = array('err', 'API access is disabled');
26     } else {
27         $messages[][] = array(
28             'ok',
29             count($config->access) . ' users may access the API'
30         );
31     }
32 } catch (\Exception $e) {
33     $messages[][] = array('err', $e->getMessage());
34 }
35
36 $adapter = array(
37     'Cutycapt'
38 );
39 foreach ($adapter as $classpart) {
40     $class = '\\phancap\\Adapter_' . $classpart;
41     $adapter = new $class();
42     $adapter->setConfig($config);
43     $errors = $adapter->isAvailable();
44     if ($errors === true) {
45         $messages[][] = array(
46             'ok', 'Adapter ' . $classpart . ' is available'
47         );
48     } else {
49         foreach ($errors as $msg) {
50             $messages['Adapter: '. $classpart][] = array('err', $msg);
51         }
52     }
53 }
54
55 header('HTTP/1.0 200 OK');
56
57 $out = <<<HTM
58 <?xml version="1.0" encoding="utf-8"?>
59 <html>
60  <head>
61   <title>phancap setup check</title>
62   <style type="text/css">
63     li.ok:before {
64         content: '✔';
65         color: green;
66         padding: 0 0.5ex;
67         margin-right: 0.5ex;
68     }
69     li.err:before {
70         content: "✘";
71         color: white;
72         background-color: red;
73         padding: 0 0.5ex;
74         margin-right: 0.5ex;
75     }
76   </style>
77  </head>
78  <body>
79   <h1>phancap setup check</h1>
80 <ul>
81 HTM;
82 foreach ($messages as $key => $messages) {
83     if (!is_numeric($key)) {
84         $out .= '<li>' . htmlspecialchars($key)
85             . '<ul>';
86     }
87     foreach ($messages as $data) {
88         list($state, $message) = $data;
89         $out .= '<li class="' . $state . '">';
90         $out .= htmlspecialchars($message);
91         $out .= '</li>' . "\n";
92     }
93     if (!is_numeric($key)) {
94         $out .= '</ul></li>' . "\n";
95     }
96 }
97 $out .= <<<HTM
98   </ul>
99   <p>
100    <a href="./">back</a> to the index
101   </p>
102  </body>
103 </html>
104 HTM;
105 echo $out;
106 ?>