Do not crash list view when seeing a fully empty git repository
[phorkie.git] / www / setup.php
1 <?php
2 /**
3  * Check if all is setup correctly
4  */
5 namespace phorkie;
6 header('HTTP/1.0 500 Internal Server Error');
7
8 $reqWritePermissions = false;
9 require_once 'www-header.php';
10
11 if (!$GLOBALS['phorkie']['cfg']['setupcheck']) {
12     header('HTTP/1.0 403 Forbidden');
13     header('Content-type: text/plain');
14     echo "Setup check is disabled\n";
15     exit(1);
16 }
17
18 $messages = SetupCheck::run();
19 $errors = 0;
20 foreach ($messages as $arMessage) {
21     list($type, $message) = $arMessage;
22     $type == 'error' && ++$errors;
23 }
24 if ($errors == 0) {
25     header('HTTP/1.0 200 OK');
26 }
27 header('Content-type: text/html');
28
29 if ($errors == 0) {
30     $messages[] =  array('ok', 'All fine');
31 }
32
33 $out = <<<HTM
34 <?xml version="1.0" encoding="utf-8"?>
35 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
36 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
37  <head>
38   <title>phorkie setup check</title>
39   <meta charset="utf-8" />
40   <link rel="stylesheet" href="css/bootstrap.min.css"/>
41   <link rel="stylesheet" href="css/font-awesome.css"/>
42   <link rel="stylesheet" href="css/phorkie.css"/>
43   <link rel="icon" href="favicon.ico"/>
44   <meta name="viewport" content="width=device-width, initial-scale=1"/>
45   <style type="text/css">
46     /**/
47     li:before {
48         text-align: center;
49         display: inline-block;
50         width: 1em;
51         padding: 0 0.5ex;
52         margin-right: 0.5ex;
53     }
54     li.list-group-item-success:before {
55         content: '✔';
56         color: green;
57     }
58     li.list-group-item-danger:before {
59         content: "✘";
60         color: white;
61         background-color: red;
62     }
63     li.list-group-item-info:before {
64         content: "i";
65         font-weight: bold;
66         color: blue;
67     }
68 /**/
69   </style>
70  </head>
71  <body>
72   <div class="container">
73    <div class="row">
74     <div class="span12">
75
76      <div class="page-header">
77       <h1>phorkie setup check</h1>
78      </div>
79      <h3>Check results</h3>
80
81      <ul class="list-group">
82 HTM;
83 $stateMap = array(
84     'ok'    => 'success',
85     'info'  => 'info',
86     'error' => 'danger'
87 );
88 foreach ($messages as $arMessage) {
89     list($type, $message) = $arMessage;
90     $out .= '<li class="list-group-item list-group-item-'
91         . $stateMap[$type] . '">';
92     $out .= htmlspecialchars($message);
93     $out .= '</li>' . "\n";
94 }
95 $out .= <<<HTM
96      </ul>
97 HTM;
98
99 if (array_sum($GLOBALS['phorkie']['cfgfiles']) == 0) {
100     //no config file loaded
101     reset($GLOBALS['phorkie']['cfgfiles']);
102     list($cfgFilePath, ) = each($GLOBALS['phorkie']['cfgfiles']);
103
104     $cfgFilePath = Tools::foldPath($cfgFilePath);
105     $cfgFileTemplate = htmlspecialchars(
106         file_get_contents(__DIR__ . '/../data/config.php.dist')
107     );
108     $cfgFileLines = count(explode("\n", $cfgFileTemplate));
109
110     $out .= <<<HTM
111      <h3 id="configfile">Configuration file</h3>
112      <p>
113       Phorkie did not find a configuration file.
114       Please create one at
115      </p>
116      <pre>$cfgFilePath</pre>
117      <p>
118       from the following template:
119      </p>
120      <textarea style="width:99%; background-color: #F5F5F5" rows="$cfgFileLines">$cfgFileTemplate</textarea>
121      <p>
122       Remove the leading <tt>//</tt> from a line if you want to adjust it.
123      </p>
124 HTM;
125 }
126
127 $out .= <<<HTM
128      <p style="margin-top: 4ex">
129       <a href="./"><i class="icon-arrow-left"></i> back</a> to the index
130      </p>
131     </div>
132    </div>
133   </div>
134
135   <div class="container footer">
136    <a href="https://cweiske.de/phorkie.htm">phorkie</a>,
137    the self-hosted, git-based pastebin software is available under the
138    <a href="http://www.gnu.org/licenses/agpl-3.0.html">
139     <abbr title="GNU Affero General Public License">AGPL</abbr></a>.
140   </div>
141
142  </body>
143 </html>
144 HTM;
145 echo $out;
146 ?>