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

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

[netinstall] add el_GR thanks to Vasilis Fotopoulos

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