source: extras/netinstall/trunk/piwigo-netinstall.php @ 3151

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

add pl language thanks to voyteckst

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