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

Last change on this file since 3131 was 3131, checked in by plg, 15 years ago

New: final links on piwigo.org for netinstall

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