source: extensions/netinstall/trunk/piwigo-netinstall.php @ 21219

Last change on this file since 21219 was 21219, checked in by ddtddt, 11 years ago

[netinstall] add pt_BT thanks to flaviove

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