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

Last change on this file since 3422 was 3422, checked in by ddtddt, 15 years ago

add zh_CN language to netinstall
Thanks to Winson

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