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

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

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