]> git.p6c8.net - jirafeau.git/blob - install.php
Security fix, bug fix and project name change.
[jirafeau.git] / install.php
1 <?php
2 /*
3 * Jirafeau, your web file repository
4 * Copyright (C) 2008 Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
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.php');
28
29 function jirafeau_quoted($str) {
30 return QUOTE . str_replace(QUOTE, "\'", $str) . QUOTE;
31 }
32
33 function jirafeau_export_cfg($cfg) {
34 $handle = fopen(JIRAFEAU_CFG, 'w');
35 fwrite($handle, '<?php' . NL);
36 fwrite($handle, '/* ' . _('This file was generated by the install process. You can edit it. Please see config.php to understand the configuration items.') . ' */' . NL);
37 foreach($cfg as $key => $item) {
38 fwrite($handle, '$cfg[' . jirafeau_quoted($key) . '] = ');
39 if(is_bool($item)) {
40 fwrite($handle, ($item ? 'true' : 'false'));
41 } else if(is_string($item)) {
42 fwrite($handle, jirafeau_quoted($item));
43 } else {
44 fwrite($handle, 'null');
45 }
46 fwrite($handle, ';' . NL);
47 }
48 fwrite($handle, '?>'); // no newline at the end of the file to be able to send headers
49 fclose($handle);
50 }
51
52 function jirafeau_mkdir($path) {
53 if(!file_exists($path)) {
54 if(!@mkdir($path, 0755)) {
55 return false;
56 }
57 }
58 return true;
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 jirafeau_is_writable($path) {
68 return is_writable($path) || @chmod($path, 0777); // "@" gets rid of error messages.
69 }
70
71 function jirafeau_check_var_dir($path) {
72 $mkdir_str1 = _('The following directory could not be created:');
73 $mkdir_str2 = _('You should create this directory by hand.');
74 $write_str1 = _('The following directory is not writable:');
75 $write_str2 = _('You should give the write right to the web server on this directory.');
76 $solution_str = _('Here is a solution:');
77
78 if(!jirafeau_mkdir($path)) {
79 return array(
80 'has_error' => true,
81 'why' => $mkdir_str1 . '<br /><code>' . $path . '</code><br />' . $solution_str . '<br />' . $mkdir_str2
82 );
83 }
84
85 if(!jirafeau_is_writable($path)) {
86 return array(
87 'has_error' => true,
88 'why' => $write_str1 . '<br /><code>' . $path . '</code><br />' . $solution_str . '<br />' . $write_str2
89 );
90 }
91
92 foreach(array('files', 'links', 'trash') as $subdir) {
93 $subpath = $path . $subdir;
94
95 if(!jirafeau_mkdir($subpath)) {
96 return array(
97 'has_error' => true,
98 'why' => $mkdir_str1 . '<br /><code>' . $subpath . '</code><br />' . $solution_str . '<br />' . $mkdir_str2
99 );
100 }
101
102 if(!jirafeau_is_writable($subpath)) {
103 return array(
104 'has_error' => true,
105 'why' => $write_str1 . '<br /><code>' . $subpath . '</code><br />' . $solution_str . '<br />' . $write_str2
106 );
107 }
108
109 }
110
111 return array('has_error' => false, 'why' => '');
112 }
113
114 function jirafeau_add_ending_slash($path) {
115 return $path . ((substr($path, -1) == '/') ? '' : '/');
116 }
117
118 if(!file_exists(JIRAFEAU_CFG)) {
119 // we try to create an empty one
120 if(!@touch(JIRAFEAU_CFG)) {
121 require(JIRAFEAU_ROOT . 'lib/template/header.php');
122 echo '<div class="error"><p>' . _('The local configuration file could not be created. Create a <code>lib/config.local.php</code> file and give the write right to the web server (preferred solution), or give the write right to the web server on the <code>lib</code> directory.') . '</p></div>';
123 require(JIRAFEAU_ROOT . 'lib/template/footer.php');
124 exit;
125 }
126 }
127
128 if (!is_writable(JIRAFEAU_CFG) && !@chmod(JIRAFEAU_CFG, '0666')) {
129 require(JIRAFEAU_ROOT . 'lib/template/header.php');
130 echo '<div class="error"><p>' . _('The local configuration is not writable by the web server. Give the write right to the web server on the <code>lib/config.local.php</code> file.') . '</p></div>';
131 require(JIRAFEAU_ROOT . 'lib/template/footer.php');
132 exit;
133 }
134
135 if(isset($_POST['step']) && isset($_POST['next'])) {
136 switch($_POST['step']) {
137 case 1:
138 $cfg['lang'] = $_POST['lang'];
139 jirafeau_export_cfg($cfg);
140 break;
141
142 case 2:
143 $cfg['web_root'] = jirafeau_add_ending_slash($_POST['web_root']);
144 $cfg['var_root'] = jirafeau_add_ending_slash($_POST['var_root']);
145 jirafeau_export_cfg($cfg);
146 break;
147
148 default:
149 // nothing to do
150 }
151
152 }
153
154 require(JIRAFEAU_ROOT . 'lib/settings.php');
155 require(JIRAFEAU_ROOT . 'lib/template/header.php');
156
157 $current = 1;
158 if(isset($_POST['next'])) {
159 $current = $_POST['step'] + 1;
160 } else if(isset($_POST['previous'])) {
161 $current = $_POST['step'] - 1;
162 } else if(isset($_POST['retry'])) {
163 $current = $_POST['step'];
164 }
165
166 switch($current) {
167 case 3:
168 ?>
169 <h2><?php printf(_('Installation of Jirafeau - step %d out of %d'), 3, 3); ?></h2>
170 <div id="install">
171 <form action="<?php echo basename(__FILE__); ?>" method="post">
172 <input type="hidden" name="jirafeau" value="<?php echo JIRAFEAU_VERSION; ?>" />
173 <input type="hidden" name="step" value="3" />
174 <fieldset>
175 <legend><?php echo _('Finalisation'); ?></legend>
176 <table>
177 <tr>
178 <td class="info" colspan="2"><?php echo _('Jirafeau is setting the website according to the configuration you provided.'); ?></td>
179 </tr>
180 <tr>
181 <td class="nav previous"><input type="submit" name="previous" value="<?php echo _('Previous step'); ?>" /></td>
182 <td></td>
183 </tr>
184 </table>
185 </fieldset>
186 </form>
187 </div>
188
189 <?php
190 $err = jirafeau_check_var_dir($cfg['var_root']);
191 if($err['has_error']) {
192 echo '<div class="error"><p>' . $err['why'] . '<br />' . NL;
193 ?>
194 <form action="<?php echo basename(__FILE__); ?>" method="post">
195 <input type="hidden" name="jirafeau" value="<?php echo JIRAFEAU_VERSION; ?>" />
196 <input type="hidden" name="step" value="3" />
197 <input type="submit" name="retry" value="<?php echo _('Retry this step'); ?>" />
198 </form>
199 <?php
200 echo '</p></div>';
201 } else {
202 echo '<div class="message"><p>' . _('Your website is now fully operational:') . '<br /><a href="' . $cfg['web_root'] . '">' . $cfg['web_root'] . '</a></p></div>';
203 }
204 break;
205
206 case 2:
207 ?>
208 <h2><?php printf(_('Installation of Jirafeau - step %d out of %d'), 2, 3); ?></h2>
209 <div id="install">
210 <form action="<?php echo basename(__FILE__); ?>" method="post">
211 <input type="hidden" name="jirafeau" value="<?php echo JIRAFEAU_VERSION; ?>" />
212 <input type="hidden" name="step" value="2" />
213 <fieldset>
214 <legend><?php echo _('Information'); ?></legend>
215 <table>
216 <tr>
217 <td class="info" colspan="2"><?php echo _('The base address of Jirafeau is the first part of the URL, until (and including) the last slash. For example: "http://www.example.com/". Do not forget the ending slash!'); ?></td>
218 </tr>
219 <tr>
220 <td class="label"><label for="input_web_root"><?php echo _('Base address:'); ?></label></td>
221 <td class="field"><input type="text" name="web_root" id="input_web_root" value="<?php
222 echo (empty($cfg['web_root']) ?
223 'http://' . $_SERVER['HTTP_HOST'] . str_replace(basename(__FILE__), '', $_SERVER['REQUEST_URI']) :
224 $cfg['web_root']);
225 ?>" size="40" /></td>
226 </tr>
227 <tr>
228 <td class="info" colspan="2"><?php echo _('The data directory is where your files and information about your files will be stored. You should put it outside your web site, or at least restrict the access of this directory. Do not forget the ending slash!'); ?></td>
229 </tr>
230 <tr>
231 <td class="label"><label for="input_var_root"><?php echo _('Data directory:'); ?></label></td>
232 <td class="field"><input type="text" name="var_root" id="input_var_root" value="<?php
233 if(empty($cfg['var_root'])) {
234 $alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
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>
246 <tr>
247 <td colspan="2">
248 <input type="submit" class="navright" name="next" value="<?php echo _('Next step'); ?>" />
249 <input type="submit" class="navleft" name="previous" value="<?php echo _('Previous step'); ?>" />
250 </td>
251 </tr>
252 </table>
253 </fieldset>
254 </form>
255 </div>
256 <?php
257 break;
258
259 case 1:
260 default:
261 $languages = array(
262 '' => 'English',
263 'fr_FR.UTF-8' => 'Français'
264 );
265 ?>
266 <h2><?php printf(_('Installation of Jirafeau - step %d out of %d'), 1, 3); ?></h2>
267 <div id="install">
268 <form action="<?php echo basename(__FILE__); ?>" method="post">
269 <input type="hidden" name="jirafeau" value="<?php echo JIRAFEAU_VERSION; ?>" />
270 <input type="hidden" name="step" value="1" />
271 <fieldset>
272 <legend><?php echo _('Language'); ?></legend>
273 <table>
274 <tr>
275 <td class="info" colspan="2"><?php echo _('The installation of Jirafeau is internationalised, so you can have it in your own language if the translation is available.'); ?></td>
276 </tr>
277 <tr>
278 <td class="label"><label for="select_lang"><?php echo _('Choose your language:'); ?></label></td>
279 <td class="field">
280 <select name="lang" id="select_lang">
281 <?php
282 foreach($languages as $key => $item) {
283 echo '<option value="' . $key . '"' . ($key == $cfg['lang'] ? ' selected="selected"' : '') . '>' . $item . '</option>' . NL;
284 }
285 ?>
286 </select>
287 </td>
288 </tr>
289 <tr class="nav">
290 <td></td>
291 <td class="nav next"><input type="submit" name="next" value="<?php echo _('Next step'); ?>" /></td>
292 </tr>
293 </table>
294 </fieldset>
295 </form>
296 </div>
297 <?php
298 break;
299 }
300
301
302 require(JIRAFEAU_ROOT . 'lib/template/footer.php');
303
304
305 /**
306
307 step 3:
308 var dir
309 verify if they are present and if the are writable
310 create them
311
312 remove install.php
313
314 */
315
316 ?>

patrick-canterino.de