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

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

[netinstall] add sk_SK thanks to dodo

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