setup: 200 ok only before echo
[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 $out = <<<HTM
56 <?xml version="1.0" encoding="utf-8"?>
57 <html>
58  <head>
59   <title>phancap setup check</title>
60   <style type="text/css">
61     li.ok:before {
62         content: '✔';
63         color: green;
64         padding: 0 0.5ex;
65         margin-right: 0.5ex;
66     }
67     li.err:before {
68         content: "✘";
69         color: white;
70         background-color: red;
71         padding: 0 0.5ex;
72         margin-right: 0.5ex;
73     }
74   </style>
75  </head>
76  <body>
77   <h1>phancap setup check</h1>
78 <ul>
79 HTM;
80 foreach ($messages as $key => $messages) {
81     if (!is_numeric($key)) {
82         $out .= '<li>' . htmlspecialchars($key)
83             . '<ul>';
84     }
85     foreach ($messages as $data) {
86         list($state, $message) = $data;
87         $out .= '<li class="' . $state . '">';
88         $out .= htmlspecialchars($message);
89         $out .= '</li>' . "\n";
90     }
91     if (!is_numeric($key)) {
92         $out .= '</ul></li>' . "\n";
93     }
94 }
95 $out .= <<<HTM
96   </ul>
97   <p>
98    <a href="./">back</a> to the index
99   </p>
100  </body>
101 </html>
102 HTM;
103
104 header('HTTP/1.0 200 OK');
105 echo $out;
106 ?>