source: extensions/autoupdate/autoupdate.php @ 3604

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

Invalidate user cache and purge templates after upgrade.

File size: 3.1 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      if (file_exists(PHPWG_ROOT_PATH.'obsolete.list')
43        and $old_files = file(PHPWG_ROOT_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES)
44        and !empty($old_files))
45      {
46        array_push($old_files, 'obsolete.list');
47        foreach($old_files as $old_file)
48        {
49          $path = PHPWG_ROOT_PATH.$old_file;
50          if (is_file($path))
51          {
52            @unlink($path);
53          }
54          elseif (is_dir($path))
55          {
56            autoupdate_deltree($path);
57          }
58        }
59      }
60      autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
61      invalidate_user_cache(true);
62      $template->delete_compiled_templates();
63      redirect('admin.php?autoupdate=success');
64    }
65    else
66    {
67      autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
68      array_push($page['errors'], l10n('autoupdate_fail'));
69    }
70  }
71  else
72  {
73    array_push($page['errors'], l10n('autoupdate_fail'));
74  }
75}
76elseif (preg_match('/\d+\.\d+\.\d+/', PHPWG_VERSION) and fetchRemote(PHPWG_URL.'/download/latest_version', $result))
77{
78  $lines = @explode("\r\n", $result);
79  $new_version = trim($lines[1]);
80  $new_branch = substr($new_version, 0, 3);
81  $actual_branch = substr(PHPWG_VERSION, 0, 3);
82  $actual_version = $actual_branch . '.x';
83
84  if (version_compare(PHPWG_VERSION, $new_version) < 0
85    and $actual_branch == $new_branch)
86  {
87    $path = $conf['local_data_dir'].'/autoupdate/';
88    $filename = $path.$actual_version.'_to_'.$new_version.'.zip';
89    mkgetdir($path);
90
91    if (!file_exists($filename) or !filesize($filename))
92    {
93      $zip = @fopen($filename, 'w+');
94      @fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.str_replace('.', '', $actual_version).'to'.str_replace('.', '', $new_version), $zip);
95      @fclose($zip);
96    }
97
98    if (file_exists($filename) and filesize($filename))
99    {
100      array_push($header_notes,
101        l10n('A new version of Piwigo is available.').'<br>',
102        '<a href="admin.php?autoupdate='.$actual_version.'_to_'.$new_version.'" onClick="return confirm(\''.l10n('autoupdate_alert').'\');">'.l10n('Click here to upgrade automatically').'</a>'
103      );
104    }
105  }
106}
107?>
Note: See TracBrowser for help on using the repository browser.