]> git.p6c8.net - jirafeau_project.git/blob - install.php
[FEATURE] Remove redundant constants
[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 action =
210 "<?php echo basename(__FILE__); ?>" method = "post"> <input type =
211 "hidden" name = "jirafeau" value =
212 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
213 "step" value = "1" /><fieldset> <legend><?php echo t('Language');
214 ?></legend> <table> <tr> <td class = "info" colspan =
215 "2"><?php echo
216 t
217 ('Jirafeau is internationalised. Choose a specific langage or ' .
218 'choose Automatic (langage is provided by user\'s browser).');
219 ?></td> </tr> <tr> <td class = "label"><label for = "select_lang"
220 ><?php echo t('Choose the default language') . ':';
221 ?></label></td>
222 <td class = "field">
223 <select name = "lang" id = "select_lang">
224 <?php foreach ($languages_list as $key => $item)
225 {
226 echo '<option value="'.$key.'"'.($key ==
227 $cfg['lang'] ? ' selected="selected"'
228 : '').'>'.$item.'</option>'.NL;
229 }
230 ?></select>
231 </td>
232 </tr>
233 <tr class = "nav">
234 <td></td>
235 <td class = "nav next"><input type = "submit" name = "next" value =
236 "<?php echo t('Next step'); ?>" /></td> </tr> </table>
237 </fieldset> </form> </div> <?php
238 break;
239
240 case 2:
241 ?><h2><?php printf (t('Installation of Jirafeau') . ' - ' . t('step') .
242 ' %d ' . t('out of') . ' %d', 2, 4);
243 ?></h2> <div id = "install"> <form action =
244 "<?php echo basename(__FILE__); ?>" method = "post"> <input type =
245 "hidden" name = "jirafeau" value =
246 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
247 "step" value = "2" /><fieldset> <legend><?php
248 echo t('Administration password');
249 ?></legend> <table> <tr> <td class = "info" colspan =
250 "2"><?php echo
251 t
252 ('Jirafeau has an administration interface (through admin.php). ' .
253 'You can set a password to access the interface or leave it empty ' .
254 'to disable the interface.');
255 ?></td> </tr> <tr> <td class = "label"><label for = "select_password"
256 ><?php echo t('Administration password') . ':';
257 ?></label></td>
258 <td class = "field"><input type = "password" name = "admin_password"
259 id = "admin_password" size = "40" /></td>
260 </tr>
261 <tr class = "nav">
262 <td></td>
263 <td class = "nav next">
264 <input type = "submit"
265 class = "navleft" name = "previous" value = "<?php
266 echo t('Previous step'); ?>" />
267 <input type = "submit" name = "next" value =
268 "<?php echo t('Next step'); ?>" /></td> </tr> </table>
269 </fieldset> </form> </div> <?php
270 break;
271
272 case 3:
273 ?><h2><?php printf (t('Installation of Jirafeau') . ' - ' . t('step') .
274 ' %d ' . t('out of') . ' %d', 3, 4);
275 ?></h2> <div id = "install"> <form action =
276 "<?php echo basename(__FILE__); ?>" method = "post"> <input type =
277 "hidden" name = "jirafeau" value =
278 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
279 "step" value =
280 "3" /><fieldset> <legend><?php echo t('Information');
281 ?></legend> <table> <tr> <td class = "info" colspan =
282 "2"><?php echo
283 t
284 ('The base address of Jirafeau is the first part of the URL, until ' .
285 '(and including) the last slash. For example: ' .
286 '"http://www.example.com/". Do not forget the trailing slash!');
287 ?></td> </tr> <tr> <td class = "label"><label for = "input_web_root"
288 ><?php echo t('Base address') . ':';
289 ?></label></td>
290 <td class = "field"><input type = "text" name = "web_root"
291 id = "input_web_root" value = "<?php
292 echo (empty($cfg['web_root']) ?
293 'http://' . $_SERVER['HTTP_HOST'] . str_replace(basename(__FILE__),
294 '', $_SERVER['REQUEST_URI']) : $cfg['web_root']);
295 ?>" size = "40" /></td>
296 </tr> <tr> <td class = "info" colspan = "2"><?php
297 echo t('The data directory is where your files and information about' .
298 ' your files will be stored. You should put it outside your web ' .
299 'site, or at least restrict the access to this directory. Do not ' .
300 'forget the trailing slash!');
301 ?></td> </tr> <tr> <td class = "label"><label for = "input_var_root"
302 ><?php echo t('Data directory') . ':';
303 ?></label></td>
304 <td class = "field"><input type = "text" name = "var_root"
305 id = "input_var_root" value = "<?php
306 if(empty($cfg['var_root'])) {
307 $alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
308 'abcdefghijklmnopqrstuvwxyz' . '0123456789';
309 $len_alphanum = strlen($alphanum);
310 $var = 'var-';
311 for($i = 0; $i <JIRAFEAU_VAR_RAND_LENGTH; $i++) {
312 $var .= substr($alphanum, mt_rand(0, $len_alphanum - 1), 1);
313 }
314 echo JIRAFEAU_ROOT . $var . '/';
315 }
316 else
317 echo $cfg['var_root'];
318 ?>" size = "40" /></td>
319 </tr> <tr> <td colspan = "2"><input type = "submit"
320 class = "navleft" name = "previous" value = "<?php
321 echo t('Previous step'); ?>" />
322 <input type = "submit" class = "navright" name = "next" value = "
323 <?php echo t('Next step'); ?>" />
324 </td> </tr> </table> </fieldset>
325 </form> </div> <?php
326 break;
327
328 case 4:
329 ?><h2><?php printf (t('Installation of Jirafeau') . ' - ' . t('step') .
330 ' %d ' . t('out of') . ' %d', 4, 4);
331 ?></h2> <div id = "install"> <form action =
332 "<?php echo basename(__FILE__); ?>" method = "post"> <input type =
333 "hidden" name = "jirafeau" value =
334 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
335 "step" value =
336 "4" /><fieldset> <legend><?php echo t('Finalisation');
337 ?></legend> <table> <tr> <td class = "info" colspan =
338 "2"><?php echo
339 t ('Jirafeau is setting the website according to the configuration ' .
340 'you provided.');
341 ?></td> </tr> <tr> <td class = "nav previous"><input type =
342 "submit" name = "previous" value =
343 "
344 <?php
345 echo t('Previous step');
346 ?>" /></td> <td></td> </tr>
347 </table> </fieldset> </form> </div>
348 <?php
349 $err = jirafeau_check_var_dir ($cfg['var_root']);
350 if ($err['has_error'])
351 {
352 echo '<div class="error"><p>'.$err['why'].'<br />'.NL;
353 ?><form action = "<?php echo basename(__FILE__); ?>" method =
354 "post"> <input type = "hidden" name = "jirafeau" value =
355 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
356 "step" value = "4" /><input type = "submit" name =
357 "retry" value =
358 "<?php echo t('Retry this step'); ?>" /></form>
359 <?php echo '</p></div>';
360 }
361 else
362 {
363 $cfg['installation_done'] = true;
364 jirafeau_export_cfg ($cfg);
365 echo '<div class="message"><p>' .
366 t('Jirafeau is now fully operational') . ':' .
367 '<br /><a href="' . $cfg['web_root'] . '">' .
368 $cfg['web_root'].'</a></p></div>';
369 }
370 break;
371 }
372
373 require (JIRAFEAU_ROOT . 'lib/template/footer.php');

patrick-canterino.de