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

patrick-canterino.de