1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Piwigo. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2008 Olivier Meunier and contributors |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | // +-----------------------------------------------------------------------+ |
---|
14 | // | Configuration | |
---|
15 | // +-----------------------------------------------------------------------+ |
---|
16 | |
---|
17 | define('DC_LOADER_SERVICE','http://piwigo.org/download/netinstall/'); |
---|
18 | define('DC_LOADER_ARCHIVE','http://piwigo.org/download/dlcounter.php?code=latest'); |
---|
19 | |
---|
20 | $available_languages = array( |
---|
21 | 'en_UK' => 'English [UK]', |
---|
22 | 'fr_FR' => 'Français [FR]', |
---|
23 | 'es_ES' => 'Español [ES]', |
---|
24 | 'it_IT' => 'Italiano [IT]', |
---|
25 | 'de_DE' => 'Deutch [DE]', |
---|
26 | 'pl_PL' => 'Polski [PL]', |
---|
27 | 'zh_CN' => '简体中文 [CN]', |
---|
28 | 'cs_CZ' => 'Česky [CZ]', |
---|
29 | 'hu_HU' => 'Magyar [HU]', |
---|
30 | ); |
---|
31 | |
---|
32 | // +-----------------------------------------------------------------------+ |
---|
33 | |
---|
34 | error_reporting(E_ALL & ~E_NOTICE); |
---|
35 | |
---|
36 | getLanguage(); |
---|
37 | |
---|
38 | $step = !empty($_REQUEST['step']) ? (integer)$_REQUEST['step'] : 1; |
---|
39 | $got_php5 = version_compare(PHP_VERSION, '5', '>='); |
---|
40 | if (!$got_php5 && $step != 2) |
---|
41 | { |
---|
42 | $step = 1; |
---|
43 | } |
---|
44 | |
---|
45 | function l10n($str) |
---|
46 | { |
---|
47 | global $lang; |
---|
48 | |
---|
49 | return isset($lang[$str]) ? $lang[$str] : $str; |
---|
50 | } |
---|
51 | |
---|
52 | function fetchRemote($src,&$dest,$step=0) |
---|
53 | { |
---|
54 | if ($step > 3) |
---|
55 | { |
---|
56 | return false; |
---|
57 | } |
---|
58 | |
---|
59 | // Try curl to read remote file |
---|
60 | if (function_exists('curl_init')) |
---|
61 | { |
---|
62 | $ch = @curl_init(); |
---|
63 | @curl_setopt($ch, CURLOPT_URL, $src); |
---|
64 | @curl_setopt($ch, CURLOPT_HEADER, 0); |
---|
65 | @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
---|
66 | @curl_setopt($ch, CURLOPT_USERAGENT, 'Piwigo Net Install'); |
---|
67 | $content = @curl_exec($ch); |
---|
68 | @curl_close($ch); |
---|
69 | if ($content !== false) |
---|
70 | { |
---|
71 | write_dest($content, $dest); |
---|
72 | return true; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | // Try file_get_contents to read remote file |
---|
77 | if ((boolean)ini_get('allow_url_fopen')) |
---|
78 | { |
---|
79 | $content = @file_get_contents($src); |
---|
80 | if ($content !== false) |
---|
81 | { |
---|
82 | write_dest($content, $dest); |
---|
83 | return true; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | // Try fsockopen to read remote file |
---|
88 | $src = parse_url($src); |
---|
89 | $host = $src['host']; |
---|
90 | $path = $src['path']; |
---|
91 | |
---|
92 | if (($s = @fsockopen($host,80,$errno,$errstr,5)) === false) |
---|
93 | { |
---|
94 | return false; |
---|
95 | } |
---|
96 | |
---|
97 | fwrite($s, |
---|
98 | 'GET '.$path." HTTP/1.0\r\n" |
---|
99 | .'Host: '.$host."\r\n" |
---|
100 | ."User-Agent: Piwigo Net Install\r\n" |
---|
101 | ."Accept: text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*\r\n" |
---|
102 | ."\r\n" |
---|
103 | ); |
---|
104 | |
---|
105 | $i = 0; |
---|
106 | $in_content = false; |
---|
107 | while (!feof($s)) |
---|
108 | { |
---|
109 | $line = fgets($s,4096); |
---|
110 | |
---|
111 | if (rtrim($line,"\r\n") == '' && !$in_content) |
---|
112 | { |
---|
113 | $in_content = true; |
---|
114 | $i++; |
---|
115 | continue; |
---|
116 | } |
---|
117 | if ($i == 0) |
---|
118 | { |
---|
119 | if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/',rtrim($line,"\r\n"), $m)) |
---|
120 | { |
---|
121 | fclose($s); |
---|
122 | return false; |
---|
123 | } |
---|
124 | $status = (integer) $m[2]; |
---|
125 | if ($status < 200 || $status >= 400) |
---|
126 | { |
---|
127 | fclose($s); |
---|
128 | return false; |
---|
129 | } |
---|
130 | } |
---|
131 | if (!$in_content) |
---|
132 | { |
---|
133 | if (preg_match('/Location:\s+?(.+)$/',rtrim($line,"\r\n"),$m)) |
---|
134 | { |
---|
135 | fclose($s); |
---|
136 | return fetchRemote(trim($m[1]),$dest,$step+1); |
---|
137 | } |
---|
138 | $i++; |
---|
139 | continue; |
---|
140 | } |
---|
141 | write_dest($line, $dest); |
---|
142 | $i++; |
---|
143 | } |
---|
144 | fclose($s); |
---|
145 | return true; |
---|
146 | } |
---|
147 | |
---|
148 | function write_dest($str, &$dest) |
---|
149 | { |
---|
150 | if (is_resource($dest)) |
---|
151 | { |
---|
152 | fwrite($dest, $str); |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | $dest .= $str; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | function getLanguage() |
---|
161 | { |
---|
162 | global $lang, $available_languages; |
---|
163 | |
---|
164 | if (isset($_GET['language']) and isset($available_languages[$_GET['language']])) |
---|
165 | { |
---|
166 | $language = $_GET['language']; |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | $language = 'en_UK'; |
---|
171 | // Try to get browser language |
---|
172 | foreach ($available_languages as $language_code => $language_name) |
---|
173 | { |
---|
174 | if (substr($language_code,0,2) == @substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2)) |
---|
175 | { |
---|
176 | $language = $language_code; |
---|
177 | break; |
---|
178 | } |
---|
179 | } |
---|
180 | } |
---|
181 | // Retrieve traductions |
---|
182 | $lang = array(); |
---|
183 | if (fetchRemote(DC_LOADER_SERVICE.'language/'.$language.'/loader.lang.txt', $code)) |
---|
184 | { |
---|
185 | @eval($code); |
---|
186 | define('DC_LOADER_LANG', $language); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | function getLocation() |
---|
191 | { |
---|
192 | $server_name = explode(':',$_SERVER['HTTP_HOST']); |
---|
193 | $server_name = $server_name[0]; |
---|
194 | if ($_SERVER['SERVER_PORT'] == '443') |
---|
195 | { |
---|
196 | $scheme = 'https'; |
---|
197 | $port = ''; |
---|
198 | } |
---|
199 | elseif (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') |
---|
200 | { |
---|
201 | $scheme = 'https'; |
---|
202 | $port = ($_SERVER['SERVER_PORT'] != '443') ? ':'.$_SERVER['SERVER_PORT'] : ''; |
---|
203 | } |
---|
204 | else |
---|
205 | { |
---|
206 | $scheme = 'http'; |
---|
207 | $port = ($_SERVER['SERVER_PORT'] != '80') ? ':'.$_SERVER['SERVER_PORT'] : ''; |
---|
208 | } |
---|
209 | $loc = preg_replace('#/$#','',str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']))); |
---|
210 | |
---|
211 | return $scheme.'://'.$server_name.$port.$loc.'/'; |
---|
212 | } |
---|
213 | |
---|
214 | function openPage() |
---|
215 | { |
---|
216 | header('Content-Type: text/html; charset=UTF-8'); |
---|
217 | echo |
---|
218 | '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" '. |
---|
219 | ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n". |
---|
220 | '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.substr(DC_LOADER_LANG,0,2).'" lang="'.substr(DC_LOADER_LANG,0,2).'">'."\n". |
---|
221 | "<head>\n". |
---|
222 | ' <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'."\n". |
---|
223 | ' <title>'.l10n('Piwigo NetInstall').'</title>'."\n". |
---|
224 | ' <meta name="ROBOTS" content="NOARCHIVE,NOINDEX,NOFOLLOW" />'."\n". |
---|
225 | ' <link rel="stylesheet" type="text/css" media="screen" href="'.DC_LOADER_SERVICE.'loader.css" />'."\n". |
---|
226 | '</head>'."\n". |
---|
227 | '<body>'."\n". |
---|
228 | '<div id="headbranch"></div>'."\n". |
---|
229 | '<div id="theHeader"></div>'."\n". |
---|
230 | '<div id="content">'."\n". |
---|
231 | '<h2>'.l10n('Piwigo NetInstall').'</h2>'."\n"; |
---|
232 | } |
---|
233 | |
---|
234 | function closePage() |
---|
235 | { |
---|
236 | echo |
---|
237 | '</div>'."\n". |
---|
238 | '<div id="footer">'."\n". |
---|
239 | l10n('This loader was initialy coded for <a href="http://www.dotclear.net" title="Dotclear">Dotclear</a>. Thanks!')."\n". |
---|
240 | '</div>'."\n". |
---|
241 | '</body>'."\n". |
---|
242 | '</html>'; |
---|
243 | } |
---|
244 | |
---|
245 | function initPHP5() |
---|
246 | { |
---|
247 | $htaccess = dirname(__FILE__).'/.htaccess'; |
---|
248 | if (file_exists($htaccess)) { |
---|
249 | if (!is_readable($htaccess) || !is_writable($htaccess)) |
---|
250 | { |
---|
251 | return false; |
---|
252 | } |
---|
253 | } |
---|
254 | $rawdatas = ''; |
---|
255 | if (!fetchRemote(DC_LOADER_SERVICE.'hosting.txt',$rawdatas)) |
---|
256 | { |
---|
257 | return false; |
---|
258 | } |
---|
259 | $rawdatas = explode("\n",$rawdatas); |
---|
260 | if (!($my_hostname = @gethostbyaddr($_SERVER['SERVER_ADDR']))) |
---|
261 | { |
---|
262 | return false; |
---|
263 | } |
---|
264 | $found = false; |
---|
265 | foreach ($rawdatas as $line) { |
---|
266 | list($name,$hostname,$rule) = explode('|',trim($line)); |
---|
267 | if (preg_match('!'.preg_quote($hostname).'$!',$my_hostname)) |
---|
268 | { |
---|
269 | $found = $rule; |
---|
270 | break; |
---|
271 | } |
---|
272 | } |
---|
273 | if ($found) { |
---|
274 | if (false !== ($fh = @fopen($htaccess,"ab"))) |
---|
275 | { |
---|
276 | fwrite($fh,"\n".$found); |
---|
277 | fclose($fh); |
---|
278 | return true; |
---|
279 | } |
---|
280 | } |
---|
281 | return false; |
---|
282 | } |
---|
283 | |
---|
284 | function cleanFiles() |
---|
285 | { |
---|
286 | @unlink(dirname(__FILE__).'/pwg_files.php'); |
---|
287 | @unlink(dirname(__FILE__).'/pwg_unzip.php'); |
---|
288 | @unlink(dirname(__FILE__).'/piwigo-install.zip'); |
---|
289 | } |
---|
290 | |
---|
291 | function grabFiles() |
---|
292 | { |
---|
293 | $failed = true; |
---|
294 | $lib_files = @fopen(dirname(__FILE__).'/pwg_files.php','wb'); |
---|
295 | $lib_unzip = @fopen(dirname(__FILE__).'/pwg_unzip.php','wb'); |
---|
296 | $dc_zip = @fopen(dirname(__FILE__).'/piwigo-install.zip','wb'); |
---|
297 | |
---|
298 | if (!$lib_files || !$lib_unzip || !$dc_zip) |
---|
299 | { |
---|
300 | return false; |
---|
301 | } |
---|
302 | |
---|
303 | if (fetchRemote(DC_LOADER_SERVICE.'lib.files.txt',$lib_files)) |
---|
304 | { |
---|
305 | if (fetchRemote(DC_LOADER_SERVICE.'class.unzip.txt',$lib_unzip)) |
---|
306 | { |
---|
307 | if (fetchRemote(DC_LOADER_ARCHIVE.'',$dc_zip)) |
---|
308 | { |
---|
309 | $failed = false; |
---|
310 | } |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | fclose($lib_files); |
---|
315 | fclose($lib_unzip); |
---|
316 | fclose($dc_zip); |
---|
317 | |
---|
318 | if ($failed) |
---|
319 | { |
---|
320 | cleanFiles(); |
---|
321 | return false; |
---|
322 | } |
---|
323 | return true; |
---|
324 | } |
---|
325 | |
---|
326 | function writeMessage($level,$title,$lines) |
---|
327 | { |
---|
328 | if (empty($lines)) |
---|
329 | { |
---|
330 | return; |
---|
331 | } |
---|
332 | |
---|
333 | echo |
---|
334 | '<div class="msg '.$level.'">'."\n". |
---|
335 | '<h3>'.$title.'</h3>'."\n". |
---|
336 | '<p>'."\n"; |
---|
337 | foreach ($lines as $line) |
---|
338 | { |
---|
339 | echo $line.'<br />'."\n"; |
---|
340 | } |
---|
341 | echo '</p></div>'."\n"; |
---|
342 | } |
---|
343 | |
---|
344 | function nextAction($label,$step,$more='') |
---|
345 | { |
---|
346 | echo |
---|
347 | '<form action="'.$_SERVER['SCRIPT_NAME'].'?language='.DC_LOADER_LANG.'" method="post">'."\n". |
---|
348 | $more."\n". |
---|
349 | '<p class="button"><input type="hidden" name="step" value="'.$step.'" />'."\n". |
---|
350 | '<input type="hidden" name="lang" value="'.DC_LOADER_LANG.'" />'."\n". |
---|
351 | '<input type="submit" name="submit" value="'.$label.'"/>'."\n". |
---|
352 | '</p></form>'."\n"; |
---|
353 | } |
---|
354 | |
---|
355 | |
---|
356 | if (!defined('DC_LOADER_LANG')) |
---|
357 | { |
---|
358 | // No traduction for this part because can't fetch! |
---|
359 | openPage(); |
---|
360 | echo |
---|
361 | '<h2>Piwigo NetInstall</h2>'."\n"; |
---|
362 | writeMessage('warning','Damnit!', array( |
---|
363 | 'Due to restrictions in your PHP configuration, NetInstall cannot get its job done.', |
---|
364 | 'Please see Piwigo documentation to perform a normal installation.', |
---|
365 | 'Really sorry for the inconvenience.' |
---|
366 | )); |
---|
367 | closePage(); |
---|
368 | exit; |
---|
369 | } |
---|
370 | |
---|
371 | switch ($step) |
---|
372 | { |
---|
373 | case 1 : |
---|
374 | { |
---|
375 | openPage(); |
---|
376 | echo '<h3>'.l10n('Welcome to NetInstall!').'</h3>'."\n"; |
---|
377 | |
---|
378 | // Show available languages |
---|
379 | asort($available_languages); |
---|
380 | echo |
---|
381 | '<p class="language">'.l10n('Language').' '."\n". |
---|
382 | '<select name="language" onchange="document.location = \''.basename(__FILE__).'?language=\'+this.options[this.selectedIndex].value;">'."\n"; |
---|
383 | foreach ($available_languages as $language_code => $language_name) |
---|
384 | { |
---|
385 | echo '<option label="'.$language_name.'" value="'.$language_code.'" '.($language_code == DC_LOADER_LANG ? 'selected="selected"' : '').'>'.$language_name.'</option>'."\n"; |
---|
386 | } |
---|
387 | echo '</select>'."\n".'</p>'."\n"; |
---|
388 | |
---|
389 | echo |
---|
390 | '<p>'.l10n('This tool is meant to retrieve the latest Piwigo archive and unzip it in your webspace.').'<br />'."\n". |
---|
391 | l10n('Right after then, you will be redirect to the Piwigo Setup Wizard.').'</p>'."\n"; |
---|
392 | |
---|
393 | if (!is_writable(dirname(__FILE__))) |
---|
394 | { |
---|
395 | writeMessage('warning',l10n('Write access is needed'), array( |
---|
396 | l10n('It looks like NetInstall wont be able to write in the current directory, and this is required to follow on.'), |
---|
397 | l10n('Please try to change the permissions to allow write access, then reload this page by hitting the Refresh button.') |
---|
398 | )); |
---|
399 | nextAction(l10n('Refresh'),1); |
---|
400 | } |
---|
401 | elseif (!$got_php5) |
---|
402 | { |
---|
403 | writeMessage('notice',l10n('PHP 5 is required'), array( |
---|
404 | sprintf(l10n('It appears your webhost is currently running PHP %s.'), PHP_VERSION), |
---|
405 | l10n('NetInstall may try to switch your configuration to PHP 5 by creating or modifying a .htaccess file.'), |
---|
406 | l10n('Note you can change your configuration by yourself and restart NetInstall after that.') |
---|
407 | )); |
---|
408 | nextAction(l10n('Try to configure PHP 5'),2); |
---|
409 | } |
---|
410 | else |
---|
411 | { |
---|
412 | nextAction(l10n('Retrieve and unzip Piwigo'),3, |
---|
413 | '<p class="destination"><label for="destination">'.l10n('Destination:').'</label> '. |
---|
414 | getLocation(). |
---|
415 | ' <input type="text" id="destination" name="destination" '. |
---|
416 | 'value="piwigo" size="15" maxlength="100" /></p>' |
---|
417 | ); |
---|
418 | } |
---|
419 | closePage(); |
---|
420 | break; |
---|
421 | } |
---|
422 | |
---|
423 | case 2 : |
---|
424 | { |
---|
425 | if (!empty($_POST['submit']) && !$got_php5) |
---|
426 | { |
---|
427 | $got_php5 = initPHP5(); |
---|
428 | } |
---|
429 | if ($got_php5) |
---|
430 | { |
---|
431 | header('Location: '.$_SERVER['SCRIPT_NAME'].'?step=1&language='.DC_LOADER_LANG); |
---|
432 | } |
---|
433 | else |
---|
434 | { |
---|
435 | openPage(); |
---|
436 | writeMessage('warning',l10n('Sorry!'),array( |
---|
437 | l10n('NetInstall was not able to configure PHP 5.'), |
---|
438 | l10n("You may referer to your hosting provider's support and see how you could switch to PHP 5 by yourself."), |
---|
439 | l10n('Hope to see you back soon.') |
---|
440 | )); |
---|
441 | closePage(); |
---|
442 | } |
---|
443 | break; |
---|
444 | } |
---|
445 | |
---|
446 | case 3 : |
---|
447 | { |
---|
448 | $msg = array(l10n('What are you doing here that way ?!')); |
---|
449 | $text = ''; |
---|
450 | if (!empty($_POST['submit']) && isset($_POST['destination'])) |
---|
451 | { |
---|
452 | $msg = array(); |
---|
453 | $dest = preg_replace('/[^A-Za-z0-9_\/-]/','',$_POST['destination']); |
---|
454 | $dest = preg_replace('#/+#','/',$dest); |
---|
455 | |
---|
456 | if (file_exists(dirname(__FILE__).'/./'.$dest.'/include/mysql.inc.php') || file_exists(dirname(__FILE__).'/./'.$dest.'/include/default_config.inc.php')) |
---|
457 | { |
---|
458 | $msg[] = l10n('It seems like a previous Piwigo installation is still sitting in that space.'); |
---|
459 | $msg[] = l10n('You need to rename or remove it before we can go further...'); |
---|
460 | } |
---|
461 | elseif (grabFiles()) |
---|
462 | { |
---|
463 | $lib_files = dirname(__FILE__).'/pwg_files.php'; |
---|
464 | $lib_unzip = dirname(__FILE__).'/pwg_unzip.php'; |
---|
465 | $dc_zip = dirname(__FILE__).'/piwigo-install.zip'; |
---|
466 | if (!file_exists($lib_files) || !file_exists($lib_unzip) || !file_exists($dc_zip)) |
---|
467 | { |
---|
468 | $msg[] = l10n('Needed files are not present.'); |
---|
469 | } |
---|
470 | |
---|
471 | require $lib_files; |
---|
472 | require $lib_unzip; |
---|
473 | $uz = new fileUnzip($dc_zip); |
---|
474 | $files = $uz->getList(); |
---|
475 | if (!is_array($files) or count($files) == 0) |
---|
476 | { |
---|
477 | $msg[] = l10n('Invalid zip file.'); |
---|
478 | } |
---|
479 | else |
---|
480 | { |
---|
481 | foreach ($files as $k => $v) |
---|
482 | { |
---|
483 | if ($v['is_dir']) |
---|
484 | { |
---|
485 | continue; |
---|
486 | } |
---|
487 | if (preg_match('#^[^/]*/_data#', $k)) |
---|
488 | { |
---|
489 | continue; |
---|
490 | } |
---|
491 | $t = preg_replace('#^[^/]*/#','./'.$dest.'/',$k); |
---|
492 | $uz->unzip($k,$t); |
---|
493 | } |
---|
494 | } |
---|
495 | $uz->close; |
---|
496 | unset($uz); |
---|
497 | |
---|
498 | if (!is_dir(dirname(__FILE__).'/'.$dest)) |
---|
499 | { |
---|
500 | $msg[] = l10n('It seems that the zip file was not extracted.'); |
---|
501 | } |
---|
502 | else |
---|
503 | { |
---|
504 | # Remove files and self-destruction |
---|
505 | cleanFiles(); |
---|
506 | unlink(__FILE__); |
---|
507 | |
---|
508 | $redir = preg_replace('#/+#','/',str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])).'/'.$dest.'/install.php'); |
---|
509 | |
---|
510 | $text = '<h3>'.l10n('Congratulations!').'</h3>' |
---|
511 | .'<p>'.l10n('Everything went fine. You are now ready to start the installation procedure.').'</p>' |
---|
512 | .'<form action="'.$redir.'" method="get"><p class="button">' |
---|
513 | . '<input type="hidden" name="language" value="'.DC_LOADER_LANG.'">' |
---|
514 | . '<input type="submit" value="'.l10n('Install Piwigo now').'" />' |
---|
515 | . '</p></form>'; |
---|
516 | } |
---|
517 | } |
---|
518 | else |
---|
519 | { |
---|
520 | $msg[] = l10n('An error occurred while grabbing the necessary files to go on.'); |
---|
521 | } |
---|
522 | } |
---|
523 | openPage(); |
---|
524 | writeMessage('warning',l10n('Something went wrong...'),$msg); |
---|
525 | echo $text; |
---|
526 | closePage(); |
---|
527 | break; |
---|
528 | } |
---|
529 | } |
---|
530 | ?> |
---|