]> git.p6c8.net - jirafeau_project.git/blob - install.php
[TASK] CGL: Remove trailing whitespaces
[jirafeau_project.git] / install.php
1 <?php
2 /*
3 * Jirafeau, your web file repository
4 * Copyright (C) 2008 Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
5 * Copyright (C) 2015 Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20 define ('JIRAFEAU_ROOT', dirname (__FILE__) . '/');
21
22 define ('JIRAFEAU_CFG', JIRAFEAU_ROOT . 'lib/config.local.php');
23 define ('JIRAFEAU_VAR_RAND_LENGTH', 15);
24
25 require (JIRAFEAU_ROOT . 'lib/settings.php');
26 require (JIRAFEAU_ROOT . 'lib/functions.php');
27 require (JIRAFEAU_ROOT . 'lib/lang.php');
28
29 /**
30 * Prepend used functions
31 **/
32
33 function
34 jirafeau_quoted ($str)
35 {
36 return QUOTE . str_replace (QUOTE, "\'", $str) . QUOTE;
37 }
38
39 function
40 jirafeau_export_cfg ($cfg)
41 {
42 $content = '<?php' . NL;
43 $content .= '/* ' . t ('This file was generated by the install process. ' .
44 'You can edit it. Please see config.original.php to understand the ' .
45 'configuration items.') . ' */' . NL;
46 $content .= '$cfg = ' . var_export($cfg, true) . ';';
47
48 $fileWrite = file_put_contents(JIRAFEAU_CFG, $content);
49
50 if (false === $fileWrite) {
51 jirafeau_fatal_error(t('Can not write local configuration file'));
52 }
53 }
54
55 function
56 jirafeau_mkdir ($path)
57 {
58 return !(!file_exists ($path) && !@mkdir ($path, 0755));
59 }
60
61 /**
62 * Returns true whether the path is writable or we manage to make it
63 * so, which essentially is the same thing.
64 * @param $path is the file or directory to be tested.
65 * @return true if $path is writable.
66 */
67 function
68 jirafeau_is_writable ($path)
69 {
70 /* "@" gets rid of error messages. */
71 return is_writable ($path) || @chmod ($path, 0777);
72 }
73
74 function
75 jirafeau_check_var_dir ($path)
76 {
77 $mkdir_str1 = t('The following directory could not be created') . ':';
78 $mkdir_str2 = t('You should create this directory manually.');
79 $write_str1 = t('The following directory is not writable') . ':';
80 $write_str2 = t('You should give the write permission to the web server on ' .
81 'this directory.');
82 $solution_str = t('Here is a solution') . ':';
83
84 if (!jirafeau_mkdir ($path) || !jirafeau_is_writable ($path))
85 return array ('has_error' => true,
86 'why' => $mkdir_str1 . '<br /><code>' .
87 $path . '</code><br />' . $solution_str .
88 '<br />' . $mkdir_str2);
89
90 foreach (array ('files', 'links', 'async', 'alias') as $subdir)
91 {
92 $subpath = $path.$subdir;
93
94 if (!jirafeau_mkdir ($subpath) || !jirafeau_is_writable ($subpath))
95 return array ('has_error' => true,
96 'why' => $mkdir_str1 . '<br /><code>' .
97 $subpath . '</code><br />' . $solution_str .
98 '<br />' . $mkdir_str2);
99 }
100
101 return array ('has_error' => false, 'why' => '');
102 }
103
104 function
105 jirafeau_add_ending_slash ($path)
106 {
107 return $path . ((substr ($path, -1) == '/') ? '' : '/');
108 }
109
110 function
111 jirafeau_fatal_error($errorText)
112 {
113 echo '<div class="error"><h2>Error</h2><p>' . $errorText . '</p></div>';
114 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
115 exit;
116 }
117
118 /**
119 * Check installation
120 **/
121
122 // Is the installation process done already?
123 // Then there is nothing to do here → redirect to the main page.
124 if ($cfg['installation_done'] === true)
125 {
126 header('Location: index.php');
127 exit;
128 }
129
130 /**
131 * Prepare installation process
132 **/
133
134 require (JIRAFEAU_ROOT . 'lib/template/header.php');
135
136 // does the local configuration file exist?
137 if (!file_exists (JIRAFEAU_CFG))
138 {
139 // show an error if it is not possible to create the file
140 if (!@touch (JIRAFEAU_CFG))
141 {
142 jirafeau_fatal_error(
143 t('The local configuration file could not be created. Create a ' .
144 '<code>lib/config.local.php</code> file and give the write ' .
145 'permission to the web server (preferred solution), or give the ' .
146 'write permission to the web server on the <code>lib</code> ' .
147 'directory.')
148 );
149 }
150 }
151
152 // is the local configuration writable?
153 if (!is_writable (JIRAFEAU_CFG) && !@chmod (JIRAFEAU_CFG, '0666'))
154 {
155 jirafeau_fatal_error(
156 t('The local configuration is not writable by the web server. ' .
157 'Give the write permission to the web server on the ' .
158 '<code>lib/config.local.php</code> file.')
159 );
160 }
161
162 /**
163 * Run trough each installation step
164 **/
165
166 if (isset ($_POST['step']) && isset ($_POST['next']))
167 {
168 switch ($_POST['step'])
169 {
170 case 1:
171 $cfg['lang'] = $_POST['lang'];
172 jirafeau_export_cfg ($cfg);
173 break;
174
175 case 2:
176 $cfg['admin_password'] = hash('sha256', $_POST['admin_password']);
177 jirafeau_export_cfg ($cfg);
178 break;
179
180 case 3:
181 $cfg['web_root'] = jirafeau_add_ending_slash ($_POST['web_root']);
182 $cfg['var_root'] = jirafeau_add_ending_slash ($_POST['var_root']);
183 jirafeau_export_cfg ($cfg);
184 break;
185
186 case 4:
187 $cfg['web_root'] = jirafeau_add_ending_slash ($_POST['web_root']);
188 $cfg['var_root'] = jirafeau_add_ending_slash ($_POST['var_root']);
189 jirafeau_export_cfg ($cfg);
190 break;
191 }
192
193 }
194
195 $current = 1;
196 if (isset ($_POST['next']))
197 $current = $_POST['step'] + 1;
198 else if (isset ($_POST['previous']))
199 $current = $_POST['step'] - 1;
200 else if (isset ($_POST['retry']))
201 $current = $_POST['step'];
202
203 switch ($current)
204 {
205 case 1:
206 default:
207 ?><h2><?php printf (t('Installation of Jirafeau') . ' - ' . t('step') .
208 ' %d ' . t('out of') . ' %d', 1, 4);
209 ?></h2> <div id = "install"> <form method="post"> <input type =
210 "hidden" name = "jirafeau" value =
211 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
212 "step" value = "1" /><fieldset> <legend><?php echo t('Language');
213 ?></legend> <table> <tr> <td class = "info" colspan =
214 "2"><?php echo
215 t
216 ('Jirafeau is internationalised. Choose a specific langage or ' .
217 'choose Automatic (langage is provided by user\'s browser).');
218 ?></td> </tr> <tr> <td class = "label"><label for = "select_lang"
219 ><?php echo t('Choose the default language') . ':';
220 ?></label></td>
221 <td class = "field">
222 <select name = "lang" id = "select_lang">
223 <?php foreach ($languages_list as $key => $item)
224 {
225 echo '<option value="'.$key.'"'.($key ==
226 $cfg['lang'] ? ' selected="selected"'
227 : '').'>'.$item.'</option>'.NL;
228 }
229 ?></select>
230 </td>
231 </tr>
232 <tr class = "nav">
233 <td></td>
234 <td class = "nav next"><input type = "submit" name = "next" value =
235 "<?php echo t('Next step'); ?>" /></td> </tr> </table>
236 </fieldset> </form> </div> <?php
237 break;
238
239 case 2:
240 ?><h2><?php printf (t('Installation of Jirafeau') . ' - ' . t('step') .
241 ' %d ' . t('out of') . ' %d', 2, 4);
242 ?></h2> <div id = "install"> <form method="post"> <input type =
243 "hidden" name = "jirafeau" value =
244 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
245 "step" value = "2" /><fieldset> <legend><?php
246 echo t('Administration password');
247 ?></legend> <table> <tr> <td class = "info" colspan =
248 "2"><?php echo
249 t
250 ('Jirafeau has an administration interface (through admin.php). ' .
251 'You can set a password to access the interface or leave it empty ' .
252 'to disable the interface.');
253 ?></td> </tr> <tr> <td class = "label"><label for = "select_password"
254 ><?php echo t('Administration password') . ':';
255 ?></label></td>
256 <td class = "field"><input type = "password" name = "admin_password"
257 id = "admin_password" size = "40" /></td>
258 </tr>
259 <tr class = "nav">
260 <td></td>
261 <td class = "nav next">
262 <input type = "submit"
263 class = "navleft" name = "previous" value = "<?php
264 echo t('Previous step'); ?>" />
265 <input type = "submit" name = "next" value =
266 "<?php echo t('Next step'); ?>" /></td> </tr> </table>
267 </fieldset> </form> </div> <?php
268 break;
269
270 case 3:
271 ?><h2><?php printf (t('Installation of Jirafeau') . ' - ' . t('step') .
272 ' %d ' . t('out of') . ' %d', 3, 4);
273 ?></h2> <div id = "install"> <form method="post"> <input type =
274 "hidden" name = "jirafeau" value =
275 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
276 "step" value =
277 "3" /><fieldset> <legend><?php echo t('Information');
278 ?></legend> <table> <tr> <td class = "info" colspan =
279 "2"><?php echo
280 t
281 ('The base address of Jirafeau is the first part of the URL, until ' .
282 '(and including) the last slash. For example: ' .
283 '"http://www.example.com/". Do not forget the trailing slash!');
284 ?></td> </tr> <tr> <td class = "label"><label for = "input_web_root"
285 ><?php echo t('Base address') . ':';
286 ?></label></td>
287 <td class = "field"><input type = "text" name = "web_root"
288 id = "input_web_root" value = "<?php
289 echo (empty($cfg['web_root']) ?
290 'http://' . $_SERVER['HTTP_HOST'] . str_replace(basename(__FILE__),
291 '', $_SERVER['REQUEST_URI']) : $cfg['web_root']);
292 ?>" size = "40" /></td>
293 </tr> <tr> <td class = "info" colspan = "2"><?php
294 echo t('The data directory is where your files and information about' .
295 ' your files will be stored. You should put it outside your web ' .
296 'site, or at least restrict the access to this directory. Do not ' .
297 'forget the trailing slash!');
298 ?></td> </tr> <tr> <td class = "label"><label for = "input_var_root"
299 ><?php echo t('Data directory') . ':';
300 ?></label></td>
301 <td class = "field"><input type = "text" name = "var_root"
302 id = "input_var_root" value = "<?php
303 if(empty($cfg['var_root'])) {
304 $alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
305 'abcdefghijklmnopqrstuvwxyz' . '0123456789';
306 $len_alphanum = strlen($alphanum);
307 $var = 'var-';
308 for($i = 0; $i <JIRAFEAU_VAR_RAND_LENGTH; $i++) {
309 $var .= substr($alphanum, mt_rand(0, $len_alphanum - 1), 1);
310 }
311 echo JIRAFEAU_ROOT . $var . '/';
312 }
313 else
314 echo $cfg['var_root'];
315 ?>" size = "40" /></td>
316 </tr> <tr> <td colspan = "2"><input type = "submit"
317 class = "navleft" name = "previous" value = "<?php
318 echo t('Previous step'); ?>" />
319 <input type = "submit" class = "navright" name = "next" value = "
320 <?php echo t('Next step'); ?>" />
321 </td> </tr> </table> </fieldset>
322 </form> </div> <?php
323 break;
324
325 case 4:
326 ?><h2><?php printf (t('Installation of Jirafeau') . ' - ' . t('step') .
327 ' %d ' . t('out of') . ' %d', 4, 4);
328 ?></h2> <div id = "install"> <form method="post"> <input type =
329 "hidden" name = "jirafeau" value =
330 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
331 "step" value =
332 "4" /><fieldset> <legend><?php echo t('Finalisation');
333 ?></legend> <table> <tr> <td class = "info" colspan =
334 "2"><?php echo
335 t ('Jirafeau is setting the website according to the configuration ' .
336 'you provided.');
337 ?></td> </tr> <tr> <td class = "nav previous"><input type =
338 "submit" name = "previous" value =
339 "
340 <?php
341 echo t('Previous step');
342 ?>" /></td> <td></td> </tr>
343 </table> </fieldset> </form> </div>
344 <?php
345 $err = jirafeau_check_var_dir ($cfg['var_root']);
346 if ($err['has_error'])
347 {
348 echo '<div class="error"><p>'.$err['why'].'<br />'.NL;
349 ?><form method="post"> <input type = "hidden" name = "jirafeau" value =
350 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
351 "step" value = "4" /><input type = "submit" name =
352 "retry" value =
353 "<?php echo t('Retry this step'); ?>" /></form>
354 <?php echo '</p></div>';
355 }
356 else
357 {
358 $cfg['installation_done'] = true;
359 jirafeau_export_cfg ($cfg);
360 echo '<div class="message"><p>' .
361 t('Jirafeau is now fully operational') . ':' .
362 '<br /><a href="' . $cfg['web_root'] . '">' .
363 $cfg['web_root'].'</a></p></div>';
364 }
365 break;
366 }
367
368 require (JIRAFEAU_ROOT . 'lib/template/footer.php');

patrick-canterino.de