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

patrick-canterino.de