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

Last change on this file since 8381 was 8381, checked in by ddtddt, 13 years ago

[extensions] - netinstall - add lv_LV (Latvian) thanks to Aivars Baldone

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