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

patrick-canterino.de