]> git.p6c8.net - jirafeau.git/blob - install.php
Add langage support (french) and automatic langage selection
[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 define ('JIRAFEAU_ROOT', dirname (__FILE__) . '/');
20 define ('NL', "\n");
21 define ('QUOTE', "'");
22
23 define ('JIRAFEAU_CFG', JIRAFEAU_ROOT.'lib/config.local.php');
24 define ('JIRAFEAU_VAR_RAND_LENGTH', 15);
25
26 require (JIRAFEAU_ROOT . 'lib/lang.php');
27 require (JIRAFEAU_ROOT . 'lib/config.php');
28
29 function
30 jirafeau_quoted ($str)
31 {
32 return QUOTE . str_replace (QUOTE, "\'", $str) . QUOTE;
33 }
34
35 function
36 jirafeau_export_cfg ($cfg)
37 {
38 $handle = fopen (JIRAFEAU_CFG, 'w');
39 fwrite ($handle, '<?php' . NL);
40 fwrite ($handle,
41 '/* ' .
42 _ ('This file was generated by the install process. ' .
43 'You can edit it. Please see config.php to understand the ' .
44 'configuration items.') . ' */' . NL);
45 foreach ($cfg as $key => $item)
46 {
47 fwrite ($handle, '$cfg[' . jirafeau_quoted ($key) . '] = ');
48 if (is_bool ($item))
49 fwrite ($handle, ($item ? 'true' : 'false'));
50 else if (is_string ($item))
51 fwrite ($handle, jirafeau_quoted ($item));
52 else
53 fwrite ($handle, 'null');
54 fwrite ($handle, ';'.NL);
55 }
56 /* No newline at the end of the file to be able to send headers. */
57 fwrite ($handle, '?>');
58 fclose ($handle);
59 }
60
61 function
62 jirafeau_mkdir ($path)
63 {
64 if (!file_exists ($path) && !@mkdir ($path, 0755))
65 return false;
66 return true;
67 }
68
69 /**
70 * Returns true whether the path is writable or we manage to make it
71 * so, which essentially is the same thing.
72 * @param $path is the file or directory to be tested.
73 * @return true if $path is writable.
74 */
75 function
76 jirafeau_is_writable ($path)
77 {
78 /* "@" gets rid of error messages. */
79 return is_writable ($path) || @chmod ($path, 0777);
80 }
81
82 function
83 jirafeau_check_var_dir ($path)
84 {
85 $mkdir_str1 = _('The following directory could not be created:');
86 $mkdir_str2 = _('You should create this directory by hand.');
87 $write_str1 = _('The following directory is not writable:');
88 $write_str2 = _('You should give the write right to the web server on ' .
89 'this directory.');
90 $solution_str = _('Here is a solution:');
91
92 if (!jirafeau_mkdir ($path) || !jirafeau_is_writable ($path))
93 return array ('has_error' => true,
94 'why' => $mkdir_str1 . '<br /><code>' .
95 $path . '</code><br />' . $solution_str .
96 '<br />' . $mkdir_str2);
97
98 foreach (array ('files', 'links') as $subdir)
99 {
100 $subpath = $path.$subdir;
101
102 if (!jirafeau_mkdir ($subpath) || !jirafeau_is_writable ($subpath))
103 return array ('has_error' => true,
104 'why' => $mkdir_str1 . '<br /><code>' .
105 $subpath . '</code><br />' . $solution_str .
106 '<br />' . $mkdir_str2);
107 }
108
109 return array ('has_error' => false, 'why' => '');
110 }
111
112 function
113 jirafeau_add_ending_slash ($path)
114 {
115 return $path . ((substr ($path, -1) == '/') ? '' : '/');
116 }
117
118 if (!file_exists (JIRAFEAU_CFG))
119 {
120 /* We try to create an empty one. */
121 if (!@touch (JIRAFEAU_CFG))
122 {
123 require (JIRAFEAU_ROOT . 'lib/template/header.php');
124 echo '<div class="error"><p>' .
125 _('The local configuration file could not be created. Create a ' .
126 '<code>lib/config.local.php</code> file and give the write ' .
127 'right to the web server (preferred solution), or give the ' .
128 'write right to the web server on the <code>lib</code> ' .
129 'directory.') .
130 '</p></div>';
131 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
132 exit;
133 }
134 }
135
136 if (!is_writable (JIRAFEAU_CFG) && !@chmod (JIRAFEAU_CFG, '0666'))
137 {
138 require (JIRAFEAU_ROOT . 'lib/template/header.php');
139 echo '<div class="error"><p>' .
140 _('The local configuration is not writable by the web server. ' .
141 'Give the write right to the web server on the ' .
142 '<code>lib/config.local.php</code> file.') .
143 '</p></div>';
144 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
145 exit;
146 }
147
148 if (isset ($_POST['step']) && isset ($_POST['next']))
149 {
150 switch ($_POST['step'])
151 {
152 case 1:
153 $cfg['lang'] = $_POST['lang'];
154 jirafeau_export_cfg ($cfg);
155 break;
156
157 case 2:
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 default: break;
164 }
165
166 }
167
168 require (JIRAFEAU_ROOT . 'lib/settings.php');
169 require (JIRAFEAU_ROOT . 'lib/template/header.php');
170
171 $current = 1;
172 if (isset ($_POST['next']))
173 $current = $_POST['step'] + 1;
174 else if (isset ($_POST['previous']))
175 $current = $_POST['step'] - 1;
176 else if (isset ($_POST['retry']))
177 $current = $_POST['step'];
178
179 switch ($current)
180 {
181 case 3:
182 ?><h2><?php printf (_('Installation of Jirafeau - step %d out of %d'),
183 3, 3);
184 ?></h2> <div id = "install"> <form action =
185 "<?php echo basename(__FILE__); ?>" method = "post"> <input type =
186 "hidden" name = "jirafeau" value =
187 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
188 "step" value =
189 "3" /><fieldset> <legend><?php echo _('Finalisation');
190 ?></legend> <table> <tr> <td class = "info" colspan =
191 "2"><?php echo
192 _
193 ('Jirafeau is setting the website according to the configuration you provided.');
194 ?></td> </tr> <tr> <td class = "nav previous"><input type =
195 "submit" name = "previous" value =
196 "
197 <?php
198 echo _('Previous step');
199 ?>" /></td> <td></td> </tr>
200 </table> </fieldset> </form> </div>
201 <?php
202 $err = jirafeau_check_var_dir ($cfg['var_root']);
203 if ($err['has_error'])
204 {
205 echo '<div class="error"><p>'.$err['why'].'<br />'.NL;
206 ?><form action = "<?php echo basename(__FILE__); ?>" method =
207 "post"> <input type = "hidden" name = "jirafeau" value =
208 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
209 "step" value = "3" /><input type = "submit" name =
210 "retry" value =
211 "<?php echo _('Retry this step'); ?>" /></form>
212 <?php echo '</p></div>';
213 }
214 else
215 {
216 echo '<div class="message"><p>' .
217 _('Your website is now fully operational:') .
218 '<br /><a href="' . $cfg['web_root'] . '">' .
219 $cfg['web_root'].'</a></p></div>';
220 }
221 break;
222
223 case 2:
224 ?><h2><?php printf (_('Installation of Jirafeau - step %d out of %d'),
225 2, 3);
226 ?></h2> <div id = "install"> <form action =
227 "<?php echo basename(__FILE__); ?>" method = "post"> <input type =
228 "hidden" name = "jirafeau" value =
229 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
230 "step" value =
231 "2" /><fieldset> <legend><?php echo _('Information');
232 ?></legend> <table> <tr> <td class = "info" colspan =
233 "2"><?php echo
234 _
235 ('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!');
236 ?></td> </tr> <tr> <td class = "label"><label for = "input_web_root"
237 ><?php echo _('Base address:');
238 ?></label></td>
239 <td class = "field"><input type = "text" name = "web_root" id = "input_web_root" value = "<?php
240 echo (empty($cfg['web_root']) ?
241 'http://' . $_SERVER['HTTP_HOST'] . str_replace(basename(__FILE__), '', $_SERVER['REQUEST_URI']) :
242 $cfg['web_root']);
243 ?>" size = "40" /></td>
244 </tr> <tr> <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!');
245 ?></td> </tr> <tr> <td class = "label"><label for = "input_var_root"
246 ><?php echo _('Data directory:');
247 ?></label></td>
248 <td class = "field"><input type = "text" name = "var_root" id = "input_var_root" value = "<?php
249 if(empty($cfg['var_root'])) {
250 $alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
251 $len_alphanum = strlen($alphanum);
252 $var = 'var-';
253 for($i = 0; $i <JIRAFEAU_VAR_RAND_LENGTH; $i++) {
254 $var .= substr($alphanum, mt_rand(0, $len_alphanum - 1), 1);
255 }
256 echo JIRAFEAU_ROOT . $var . '/';
257 } else {
258 echo $cfg['var_root'];
259 }
260 ?>" size = "40" /></td>
261 </tr> <tr> <td colspan = "2"> <input type = "submit" class = "navright" name = "next" value = "<?php echo _('Next step'); ?>" /><input type = "submit" class = "navleft" name = "previous" value = "<?php echo _('Previous step'); ?>" /></td> </tr> </table> </fieldset> </form> </div> <?php break;
262
263 case 1:
264 default:
265 $languages = array ('' => 'English', 'fr_FR.UTF-8' => 'Français');
266 ?><h2><?php printf (_('Installation of Jirafeau - step %d out of %d'),
267 1, 3);
268 ?></h2> <div id = "install"> <form action =
269 "<?php echo basename(__FILE__); ?>" method = "post"> <input type =
270 "hidden" name = "jirafeau" value =
271 "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
272 "step" value = "1" /><fieldset> <legend><?php echo _('Language');
273 ?></legend> <table> <tr> <td class = "info" colspan =
274 "2"><?php echo
275 _
276 ('The installation of Jirafeau is internationalised, so you can have it in your own language if the translation is available.');
277 ?></td> </tr> <tr> <td class = "label"><label for = "select_lang"
278 ><?php echo _('Choose your language:');
279 ?></label></td>
280 <td class = "field">
281 <select name = "lang" id = "select_lang">
282 <?php foreach ($languages as $key => $item)
283 {
284 echo '<option value="'.$key.'"'.($key ==
285 $cfg['lang'] ? ' selected="selected"'
286 : '').'>'.$item.'</option>'.NL;
287 }
288 ?></select>
289 </td>
290 </tr>
291 <tr class = "nav">
292 <td></td>
293 <td class = "nav next"><input type = "submit" name = "next" value =
294 "<?php echo _('Next step'); ?>" /></td> </tr> </table>
295 </fieldset> </form> </div> <?php break;
296 }
297
298
299 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
300 ?>

patrick-canterino.de