source: extensions/autoupdate/autoupdate.php @ 3575

Last change on this file since 3575 was 3575, checked in by patdenice, 15 years ago

Add Piwigo AutoUpgrade plugin.
Version 2.0.a

File size: 2.6 KB
Line 
1<?php
2
3function autoupdate_deltree($path)
4{
5  if (is_dir($path))
6  {
7    $fh = opendir($path);
8    while ($file = readdir($fh))
9    {
10      if ($file != '.' and $file != '..')
11      {
12        $pathfile = $path . '/' . $file;
13        if (is_dir($pathfile))
14        {
15          deltree($pathfile);
16        }
17        else
18        {
19          @unlink($pathfile);
20        }
21      }
22    }
23    closedir($fh);
24    return @rmdir($path);
25  }
26}
27
28load_language('plugin.lang', dirname(__FILE__).'/');
29
30if (isset($_GET['autoupdate']))
31{
32  if ($_GET['autoupdate'] == 'success')
33  {
34    array_push($page['infos'], sprintf(l10n('autoupdate_success'), PHPWG_VERSION));
35  }
36  elseif ($file = $conf['local_data_dir'].'/autoupdate/'.$_GET['autoupdate'].'.zip' and file_exists($file))
37  {
38    include_once(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php');
39    $zip = new PclZip($file);
40    if ($result = $zip->extract(PCLZIP_OPT_PATH, PHPWG_ROOT_PATH, PCLZIP_OPT_REMOVE_PATH, $_GET['autoupdate'], PCLZIP_OPT_REPLACE_NEWER))
41    {
42      autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
43      redirect('admin.php?autoupdate=success');
44    }
45    else
46    {
47      autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
48      array_push($page['errors'], l10n('autoupdate_fail'));
49    }
50  }
51  else
52  {
53    array_push($page['errors'], l10n('autoupdate_fail'));
54  }
55}
56elseif (preg_match('/\d+\.\d+\.\d+/', PHPWG_VERSION) and fetchRemote(PHPWG_URL.'/download/latest_version', $result))
57{
58  $lines = @explode("\r\n", $result);
59  $new_version = trim($lines[1]);
60  $new_branch = substr($new_version, 0, 3);
61  $actual_branch = substr(PHPWG_VERSION, 0, 3);
62  $actual_version = $actual_branch . '.x';
63
64  if (version_compare(PHPWG_VERSION, $new_version) < 0
65    and $actual_branch == $new_branch)
66  {
67    $path = $conf['local_data_dir'].'/autoupdate/';
68    $filename = $path.$actual_version.'_to_'.$new_version.'.zip';
69    mkgetdir($path);
70
71    if (!file_exists($filename) or !filesize($filename))
72    {
73      $zip = @fopen($filename, 'w+');
74      @fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.str_replace('.', '', $actual_version).'to'.str_replace('.', '', $new_version), $zip);
75      @fclose($zip);
76    }
77
78    if (file_exists($filename) and filesize($filename))
79    {
80      array_push($header_notes,
81        l10n('A new version of Piwigo is available.').'<br>',
82        '<a href="admin.php?autoupdate='.$actual_version.'_to_'.$new_version.'" onClick="return confirm(\''.l10n('autoupdate_alert').'\');">'.l10n('Click here to upgrade automatically').'</a>'
83      );
84    }
85  }
86}
87?>
Note: See TracBrowser for help on using the repository browser.