da93207cdbcc44b59e37fa03651f3c866013a48e
[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 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
79 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
80  <head>
81   <title>phancap setup check</title>
82   <link rel="stylesheet" href="css/bootstrap.min.css"/>
83   <link rel="stylesheet" href="css/bootstrap-theme.min.css"/>
84   <link rel="stylesheet" href="css/phancap.css"/>
85   <meta name="viewport" content="width=device-width, initial-scale=1"/>
86   <style type="text/css">
87     /*
88     li:before {
89         text-align: center;
90         display: inline-block;
91         width: 1em;
92         padding: 0 0.5ex;
93         margin-right: 0.5ex;
94     }
95     li.ok:before {
96         content: '✔';
97         color: green;
98     }
99     li.err:before {
100         content: "✘";
101         color: white;
102         background-color: red;
103     }
104     li.info:before {
105         content: "i";
106         font-weight: bold;
107         color: blue;
108     }
109     */
110   </style>
111  </head>
112  <body>
113   <div class="container">
114    <div class="row">
115     <div class="col-md-2"></div>
116     <div class="col-md-8">
117
118      <div class="page-header">
119       <h1>phancap setup check</h1>
120      </div>
121
122      <ul class="list-group">
123 HTM;
124 $stateMap = array(
125     'ok' => 'success',
126     'info' => 'info',
127     'err' => 'danger'
128 );
129 foreach ($messages as $key => $messages) {
130     if (!is_numeric($key)) {
131         $out .= '<li class="list-group-item">' . htmlspecialchars($key)
132             . '<ul class="list-group">';
133     }
134     foreach ($messages as $data) {
135         list($state, $message) = $data;
136         $out .= '<li class="list-group-item list-group-item-' . $stateMap[$state] . '">';
137         $out .= htmlspecialchars($message);
138         $out .= '</li>' . "\n";
139     }
140     if (!is_numeric($key)) {
141         $out .= '</ul></li>' . "\n";
142     }
143 }
144 $out .= <<<HTM
145      </ul>
146      <p>
147       <a href="./">back</a> to the index
148      </p>
149     </div>
150    </div>
151   </div>
152  </body>
153 </html>
154 HTM;
155
156 header('HTTP/1.0 200 OK');
157 echo $out;
158 ?>