source: extensions/autoupdate/autoupdate.php @ 3898

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

Avoid timeout errors during file download.

File size: 3.7 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          autoupdate_deltree($pathfile);
16        }
17        else
18        {
19          @unlink($pathfile);
20        }
21      }
22    }
23    closedir($fh);
24    return @rmdir($path);
25  }
26}
27
28function check_version_for_autoupdate($version=PHPWG_VERSION)
29{
30  global $conf, $header_notes;
31
32  if (preg_match('/(\d+\.\d+)\.(\d+)/', $version, $matches) and @fetchRemote(PHPWG_URL.'/download/latest_version', $result))
33  {
34    $lines = @explode("\r\n", $result);
35    $new_version = trim($lines[1]);
36    $new_branch = preg_replace('/(\d+\.\d+)\.\d+/', '$1', $new_version);
37    $actual_branch = $matches[1];
38    $update_to =  $actual_branch . '.' . ($matches[2]+1);
39
40    if (version_compare($version, $new_version) < 0
41      and $actual_branch == $new_branch)
42    {
43      $path = $conf['local_data_dir'].'/autoupdate/';
44      $code = $version.'_to_'.$update_to;
45      $filename = $path.$version.'_to_'.$update_to.'.zip';
46      mkgetdir($path);
47
48      if (!file_exists($filename) or !filesize($filename))
49      {
50        $zip = @fopen($filename, 'w+');
51        @fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.str_replace(array('.', '_'), '', $code), $zip);
52        @fclose($zip);
53      }
54
55      if (file_exists($filename) and filesize($filename))
56      {
57        if (isset($_GET['autoupdate']) and preg_match('/\d+\.\d+\.\d+_to_'.preg_quote($version).'/', $_GET['autoupdate']))
58        {
59          redirect('admin.php?autoupdate='.$code);
60        }
61        else
62        {
63          array_push($header_notes,
64            '<p>'.l10n('A new version of Piwigo is available.').'<br>',
65            '<a href="admin.php?autoupdate='.$code.'" onClick="return confirm(\''.l10n('autoupdate_alert').'\');">'.l10n('Click here to upgrade automatically').'</a></p>'
66          );
67        }
68      }
69    }
70  }
71}
72
73load_language('plugin.lang', dirname(__FILE__).'/');
74
75if (isset($_GET['autoupdate']))
76{
77  if ($_GET['autoupdate'] == 'success')
78  {
79    array_push($page['infos'], sprintf(l10n('autoupdate_success'), PHPWG_VERSION));
80  }
81  elseif ($file = $conf['local_data_dir'].'/autoupdate/'.$_GET['autoupdate'].'.zip' and file_exists($file))
82  {
83    include_once(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php');
84    $zip = new PclZip($file);
85    if ($result = $zip->extract(PCLZIP_OPT_PATH, PHPWG_ROOT_PATH, PCLZIP_OPT_REMOVE_PATH, $_GET['autoupdate'], PCLZIP_OPT_REPLACE_NEWER))
86    {
87      if (file_exists(PHPWG_ROOT_PATH.'obsolete.list')
88        and $old_files = file(PHPWG_ROOT_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES)
89        and !empty($old_files))
90      {
91        array_push($old_files, 'obsolete.list');
92        foreach($old_files as $old_file)
93        {
94          $path = PHPWG_ROOT_PATH.$old_file;
95          if (is_file($path))
96          {
97            @unlink($path);
98          }
99          elseif (is_dir($path))
100          {
101            autoupdate_deltree($path);
102          }
103        }
104      }
105      if (preg_match('/\d+\.\d+\.\d+_to_(\d+\.\d+\.\d+)/', $_GET['autoupdate'], $matches))
106      {
107        check_version_for_autoupdate($matches[1]);
108      }
109      autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
110      invalidate_user_cache(true);
111      $template->delete_compiled_templates();
112      redirect('admin.php?autoupdate=success');
113    }
114    else
115    {
116      autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
117      array_push($page['errors'], l10n('autoupdate_fail'));
118    }
119  }
120  else
121  {
122    array_push($page['errors'], l10n('autoupdate_fail'));
123  }
124}
125else
126{
127  check_version_for_autoupdate();
128}
129
130?>
Note: See TracBrowser for help on using the repository browser.