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

Last change on this file since 7430 was 7430, checked in by plg, 14 years ago

add ru_RU in language list

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