]> git.p6c8.net - jirafeau_mojo42.git/blob - install.php
b6b322d5a38334a26b2037643dae79367880c92e
[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
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 function jirafeau_export_cfg($cfg)
30 {
31 $content = '<?php' . NL;
32 $content .= '/* This file was generated by the install process. ' .
33 'You can edit it. Please see config.original.php to understand the ' .
34 'configuration items. */' . NL;
35 $content .= '$cfg = ' . var_export($cfg, true) . ';';
36
37 $fileWrite = file_put_contents(JIRAFEAU_CFG, $content);
38
39 if (false === $fileWrite) {
40 jirafeau_fatal_error(t('Can not write local configuration file'));
41 }
42 }
43
44 function jirafeau_mkdir($path)
45 {
46 return !(!file_exists($path) && !@mkdir($path, 0755));
47 }
48
49 /**
50 * Returns true whether the path is writable or we manage to make it
51 * so, which essentially is the same thing.
52 * @param $path is the file or directory to be tested.
53 * @return true if $path is writable.
54 */
55 function jirafeau_is_writable($path)
56 {
57 /* "@" gets rid of error messages. */
58 return is_writable($path) || @chmod($path, 0777);
59 }
60
61 function jirafeau_check_var_dir($path)
62 {
63 $mkdir_str1 = t('CANNOT_CREATE_DIR') . ':';
64 $mkdir_str2 = t('MANUAL_CREATE');
65 $write_str1 = t('DIR_NOT_W') . ':';
66 $write_str2 = t('You should give the write permission to the web server on ' .
67 'this directory.');
68 $solution_str = t('HERE_SOLUTION') . ':';
69
70 if (!jirafeau_mkdir($path) || !jirafeau_is_writable($path)) {
71 return array('has_error' => true,
72 'why' => $mkdir_str1 . '<br /><code>' .
73 $path . '</code><br />' . $solution_str .
74 '<br />' . $mkdir_str2);
75 }
76
77 foreach (array('files', 'links', 'async') as $subdir) {
78 $subpath = $path.$subdir;
79
80 if (!jirafeau_mkdir($subpath) || !jirafeau_is_writable($subpath)) {
81 return array('has_error' => true,
82 'why' => $mkdir_str1 . '<br /><code>' .
83 $subpath . '</code><br />' . $solution_str .
84 '<br />' . $mkdir_str2);
85 }
86 }
87
88 return array('has_error' => false, 'why' => '');
89 }
90
91 function jirafeau_add_ending_slash($path)
92 {
93 return $path . ((substr($path, -1) == '/') ? '' : '/');
94 }
95
96 /**
97 * Check installation
98 **/
99
100 // Is the installation process done already?
101 // Then there is nothing to do here → redirect to the main page.
102 if ($cfg['installation_done'] === true) {
103 header('Location: index.php');
104 exit;
105 }
106
107 /**
108 * Prepare installation process
109 **/
110
111 require(JIRAFEAU_ROOT . 'lib/template/header.php');
112
113 // does the local configuration file exist?
114 if (!file_exists(JIRAFEAU_CFG)) {
115 // show an error if it is not possible to create the file
116 if (!@touch(JIRAFEAU_CFG)) {
117 jirafeau_fatal_error(
118 t('The local configuration file could not be created. Create a ' .
119 '<code>lib/config.local.php</code> file and give the write ' .
120 'permission to the web server (preferred solution), or give the ' .
121 'write permission to the web server on the <code>lib</code> ' .
122 'directory.')
123 );
124 }
125 }
126
127 // is the local configuration writable?
128 if (!is_writable(JIRAFEAU_CFG) && !@chmod(JIRAFEAU_CFG, '0666')) {
129 jirafeau_fatal_error(
130 t('The local configuration is not writable by the web server. ' .
131 'Give the write permission to the web server on the ' .
132 '<code>lib/config.local.php</code> file.')
133 );
134 }
135
136 /**
137 * Run trough each installation step
138 **/
139
140 if (isset($_POST['step']) && isset($_POST['next'])) {
141 switch ($_POST['step']) {
142 case 1:
143 if (strlen($_POST['admin_password'])) {
144 $cfg['admin_password'] = hash('sha256', $_POST['admin_password']);
145 } else {
146 $cfg['admin_password'] = '';
147 }
148 jirafeau_export_cfg($cfg);
149 break;
150
151 case 2:
152 $cfg['web_root'] = jirafeau_add_ending_slash($_POST['web_root']);
153 $cfg['var_root'] = jirafeau_add_ending_slash($_POST['var_root']);
154 jirafeau_export_cfg($cfg);
155 break;
156
157 case 3:
158 $cfg['web_root'] = jirafeau_add_ending_slash($_POST['web_root']);
159 $cfg['var_root'] = jirafeau_add_ending_slash($_POST['var_root']);
160 jirafeau_export_cfg($cfg);
161 break;
162 }
163 }
164
165 $current = 1;
166 if (isset($_POST['next'])) {
167 $current = $_POST['step'] + 1;
168 } elseif (isset($_POST['previous'])) {
169 $current = $_POST['step'] - 1;
170 } elseif (isset($_POST['retry'])) {
171 $current = $_POST['step'];
172 }
173
174 switch ($current) {
175 case 1:
176 default:
177 ?><h2><?php printf(t('JI_INSTALL') . ' - ' . t('STEP') .
178 ' %d ' . t('OUT_OF') . ' %d', 1, 3);
179 ?></h2> <div id = "install"> <form method="post"> <input type =
180 "hidden" name = "jirafeau" value =
181 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
182 "step" value = "1" /><fieldset> <legend><?php
183 echo t('ADMIN_PSW');
184 ?></legend> <table> <tr> <td class = "info" colspan =
185 "2"><?php echo t('ADMIN_INTERFACE_INFO');
186 ?></td> </tr> <tr> <td class = "label"><label for = "select_password"
187 ><?php echo t('ADMIN_PSW') . ':';
188 ?></label></td>
189 <td class = "field"><input type = "password" name = "admin_password"
190 id = "admin_password" size = "40" /></td>
191 </tr>
192 <tr class = "nav">
193 <td></td>
194 <td class = "nav next">
195 <input type = "submit"
196 class = "navleft" name = "previous" value = "<?php
197 echo t('PREV_STEP'); ?>" />
198 <input type = "submit" name = "next" value =
199 "<?php echo t('NEXT_STEP'); ?>" /></td> </tr> </table>
200 </fieldset> </form> </div> <?php
201 break;
202
203 case 2:
204 ?><h2><?php printf(t('JI_INSTALL') . ' - ' . t('STEP') .
205 ' %d ' . t('OUT_OF') . ' %d', 2, 3);
206 ?></h2> <div id = "install"> <form method="post"> <input type =
207 "hidden" name = "jirafeau" value =
208 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
209 "step" value =
210 "2" /><fieldset> <legend><?php echo t('INFO');
211 ?></legend> <table> <tr> <td class = "info" colspan =
212 "2"><?php echo t('BASE_ADDR_INFO');
213 ?></td> </tr> <tr> <td class = "label"><label for = "input_web_root"
214 ><?php echo t('BASE_ADDR') . ':';
215 ?></label></td>
216 <td class = "field"><input type = "text" name = "web_root"
217 id = "input_web_root" value = "<?php
218 echo(empty($cfg['web_root']) ?
219 $_SERVER['HTTP_HOST'] . str_replace(
220 basename(__FILE__),
221 '',
222 $_SERVER['REQUEST_URI']
223 ) : $cfg['web_root']);
224 ?>" size = "40" /></td>
225 </tr> <tr> <td class = "info" colspan = "2"><?php
226 echo t('DATA_DIR_EXPLAINATION');
227 ?></td> </tr> <tr> <td class = "label"><label for = "input_var_root"
228 ><?php echo t('DATA_DIR') . ':';
229 ?></label></td>
230 <td class = "field"><input type = "text" name = "var_root"
231 id = "input_var_root" value = "<?php
232 if (empty($cfg['var_root'])) {
233 $alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
234 'abcdefghijklmnopqrstuvwxyz' . '0123456789';
235 $len_alphanum = strlen($alphanum);
236 $var = 'var-';
237 for ($i = 0; $i <JIRAFEAU_VAR_RAND_LENGTH; $i++) {
238 $var .= substr($alphanum, mt_rand(0, $len_alphanum - 1), 1);
239 }
240 echo JIRAFEAU_ROOT . $var . '/';
241 } else {
242 echo $cfg['var_root'];
243 }
244 ?>" size = "40" /></td>
245 </tr> <tr> <td colspan = "2"><input type = "submit"
246 class = "navleft" name = "previous" value = "<?php
247 echo t('PREV_STEP'); ?>" />
248 <input type = "submit" class = "navright" name = "next" value =
249 "<?php echo t('NEXT_STEP'); ?>" />
250 </td> </tr> </table> </fieldset>
251 </form> </div> <?php
252 break;
253
254 case 3:
255 ?><h2><?php printf(t('JI_INSTALL') . ' - ' . t('STEP') .
256 ' %d ' . t('OUT_OF') . ' %d', 3, 3);
257 ?></h2> <div id = "install"> <form method="post"> <input type =
258 "hidden" name = "jirafeau" value =
259 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
260 "step" value =
261 "3" /><fieldset> <legend><?php echo t('FINALIZATION');
262 ?></legend> <table> <tr> <td class = "info" colspan =
263 "2"><?php echo t('SETTING_UP');
264 ?></td> </tr> <tr> <td class = "nav previous"><input type =
265 "submit" name = "previous" value = " <?php echo t('PREV_STEP');
266 ?>" /></td> <td></td> </tr>
267 </table> </fieldset> </form> </div>
268 <?php
269 $err = jirafeau_check_var_dir($cfg['var_root']);
270 if ($err['has_error']) {
271 echo '<div class="error"><p>'.$err['why'].'<br />'.NL; ?><form method="post"> <input type = "hidden" name = "jirafeau" value =
272 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
273 "step" value = "3" /><input type = "submit" name =
274 "retry" value =
275 "<?php echo t('RETRY_STEP'); ?>" /></form>
276 <?php echo '</p></div>';
277 } else {
278 $cfg['installation_done'] = true;
279 jirafeau_export_cfg($cfg);
280 echo '<div class="message"><p>' .
281 t('JI_FONCTIONAL') . ':' .
282 '<br /><a href="./">' .
283 $cfg['web_root'].'</a></p></div>';
284 }
285 break;
286 }
287
288 require(JIRAFEAU_ROOT . 'lib/template/footer.php');

patrick-canterino.de