add option to disable setup check
[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     if ($config->disableSetup) {
21         header('HTTP/1.0 403 Forbidden');
22         header('Content-type: text/plain');
23         echo "Setup check is disabled.\n";
24         exit(1);
25     }
26     $messages[][] = array('ok', 'Base configuration is ok');
27
28     if ($config->access === true) {
29         $messages[][] = array('ok', 'Everyone may access the API');
30     } else if ($config->access === false) {
31         $messages[][] = array('err', 'API access is disabled');
32     } else {
33         $messages[][] = array(
34             'ok',
35             count($config->access) . ' users may access the API'
36         );
37     }
38
39     foreach ($config->cfgFiles as $cfgFile) {
40         $messages[][] = array(
41             'info', 'Possible config file: ' . $cfgFile
42         );
43     }
44     if ($config->cfgFileExists) {
45         $messages[][] = array(
46             'ok', 'Configuration file loaded'
47         );
48     } else {
49         $messages[][] = array(
50             'info', 'No configuration file found'
51         );
52     }
53 } catch (\Exception $e) {
54     $messages[][] = array('err', $e->getMessage());
55 }
56
57 $adapter = array(
58     'Cutycapt'
59 );
60 foreach ($adapter as $classpart) {
61     $class = '\\phancap\\Adapter_' . $classpart;
62     $adapter = new $class();
63     $adapter->setConfig($config);
64     $errors = $adapter->isAvailable();
65     if ($errors === true) {
66         $messages[][] = array(
67             'ok', 'Adapter ' . $classpart . ' is available'
68         );
69     } else {
70         foreach ($errors as $msg) {
71             $messages['Adapter: '. $classpart][] = array('err', $msg);
72         }
73     }
74 }
75
76 $out = <<<HTM
77 <?xml version="1.0" encoding="utf-8"?>
78 <html>
79  <head>
80   <title>phancap setup check</title>
81   <style type="text/css">
82     li:before {
83         text-align: center;
84         display: inline-block;
85         width: 1em;
86         padding: 0 0.5ex;
87         margin-right: 0.5ex;
88     }
89     li.ok:before {
90         content: '✔';
91         color: green;
92     }
93     li.err:before {
94         content: "✘";
95         color: white;
96         background-color: red;
97     }
98     li.info:before {
99         content: "i";
100         font-weight: bold;
101         color: blue;
102     }
103   </style>
104  </head>
105  <body>
106   <h1>phancap setup check</h1>
107 <ul>
108 HTM;
109 foreach ($messages as $key => $messages) {
110     if (!is_numeric($key)) {
111         $out .= '<li>' . htmlspecialchars($key)
112             . '<ul>';
113     }
114     foreach ($messages as $data) {
115         list($state, $message) = $data;
116         $out .= '<li class="' . $state . '">';
117         $out .= htmlspecialchars($message);
118         $out .= '</li>' . "\n";
119     }
120     if (!is_numeric($key)) {
121         $out .= '</ul></li>' . "\n";
122     }
123 }
124 $out .= <<<HTM
125   </ul>
126   <p>
127    <a href="./">back</a> to the index
128   </p>
129  </body>
130 </html>
131 HTM;
132
133 header('HTTP/1.0 200 OK');
134 echo $out;
135 ?>