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

patrick-canterino.de