source: extensions/autoupdate/class.inc.php @ 4711

Last change on this file since 4711 was 4711, checked in by patdenice, 14 years ago

[Plugin][Piwigo Auto Upgrade]
If gallery and plugins are up to date, display note on admin homepage only one time for each session.
Allow to disable notification for plugins you don't want to update.

File size: 5.9 KB
Line 
1<?php
2
3class autoupdate
4{
5  function deltree($path)
6  {
7    if (is_dir($path))
8    {
9      $fh = opendir($path);
10      while ($file = readdir($fh))
11      {
12        if ($file != '.' and $file != '..')
13        {
14          $pathfile = $path . '/' . $file;
15          if (is_dir($pathfile))
16          {
17            $this->deltree($pathfile);
18          }
19          else
20          {
21            @unlink($pathfile);
22          }
23        }
24      }
25      closedir($fh);
26      return @rmdir($path);
27    }
28  }
29
30  function check_version($version=PHPWG_VERSION)
31  {
32    global $conf;
33
34    if (!isset($_SESSION['need_update']))
35    {
36      $_SESSION['need_update'] = null;
37
38      if (preg_match('/(\d+\.\d+)\.(\d+)/', $version, $matches)
39        and @fetchRemote(PHPWG_URL.'/download/latest_version', $result))
40      {
41        $lines = @explode("\r\n", $result);
42        $new_version = trim($lines[1]);
43        $new_branch = preg_replace('/(\d+\.\d+)\.\d+/', '$1', $new_version);
44        $actual_branch = $matches[1];
45        $update_to =  $actual_branch . '.' . ($matches[2]+1);
46
47        if (version_compare($version, $new_version) < 0 and $actual_branch == $new_branch)
48        {
49          $_SESSION['need_update'] = $new_version;
50        }
51        else
52        {
53          $_SESSION['need_update'] = false;
54        }
55      }
56    }
57
58    if ($_SESSION['need_update'])
59    {
60      preg_match('/(\d+\.\d+)\.(\d+)/', $version, $matches);
61      $update_to =  $matches[1] . '.' . ($matches[2]+1);
62      $path = $conf['local_data_dir'].'/autoupdate/';
63      $code = $version.'_to_'.$update_to;
64      $filename = $path.$version.'_to_'.$update_to.'.zip';
65      mkgetdir($path);
66
67      if (!file_exists($filename) or !filesize($filename))
68      {
69        $zip = @fopen($filename, 'w+');
70        @fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.str_replace(array('.', '_'), '', $code), $zip);
71        @fclose($zip);
72      }
73
74      if (file_exists($filename) and filesize($filename))
75      {
76        if (isset($_GET['autoupdate']) and preg_match('/\d+\.\d+\.\d+_to_'.preg_quote($version).'/', $_GET['autoupdate']))
77        {
78          redirect('admin.php?autoupdate='.$code);
79        }
80        else
81        {
82          return rtrim(l10n('A new version of Piwigo is available.'), '.').' ('.$_SESSION['need_update'].')<br>'
83            . '<a href="admin.php?autoupdate='.$code.'" onClick="return confirm_autoupdate();">'.l10n('Click here to upgrade automatically').'</a>';
84        }
85      }
86      else
87      {
88        @unlink($filename);
89        return rtrim(l10n('A new version of Piwigo is available.'), '.').' ('.$_SESSION['need_update'].')<br>'
90          . l10n('Piwigo cannot retrieve upgrade file from server').'<br>'
91          . '<a href="'.PHPWG_URL.'/releases/'.$_SESSION['need_update'].'" onclick="window.open(this.href, \'\'); return false;">'
92          . sprintf(l10n('Click here to see Piwigo %s release notes'), $_SESSION['need_update']).'</a>';
93      }
94    }
95    else
96    {
97      // Gallery is up to date
98      // Check plugins upgrade
99      include_once(PHPWG_ROOT_PATH.'admin/include/plugins.class.php');
100      $plugins = new plugins();
101      $plugin_list = array();
102      $ignore_list = unserialize($conf['autoupdate_ignore_list']);
103
104      if (!isset($_SESSION['plugins_need_update']))
105      {
106        $_SESSION['plugins_need_update'] = null;
107        $this->check_plugins_upgrade($plugins, $ignore_list);
108      }
109      elseif (!empty($_SESSION['plugins_need_update']))
110      {
111        // Check if plugins have been upgraded since last check
112        foreach($plugins->fs_plugins as $plugin_id => $fs_plugin)
113        {
114          if (isset($_SESSION['plugins_need_update'][$plugin_id])
115            and $plugins->plugin_version_compare($fs_plugin['version'], $_SESSION['plugins_need_update'][$plugin_id]))
116          {
117            // Plugin have been updated
118            $this->check_plugins_upgrade($plugins, $ignore_list);
119          }
120        }
121      }
122
123      if (!empty($_SESSION['plugins_need_update']))
124      {
125        $plugin_list = $_SESSION['plugins_need_update'];
126
127        // Check ignore list
128        foreach ($plugin_list as $plugin_id => $version)
129        {
130          if (in_array($plugins->fs_plugins[$plugin_id]['name'], $ignore_list))
131          {
132            unset($plugin_list[$plugin_id]);
133          }
134        }
135
136        if (!empty($plugin_list))
137        {
138          return l10n('Some upgrades are available for you plugins').'<br>'
139            . '<a href="admin.php?page=plugins_update">'.l10n('Click here see upgrade plugins page').'</a>';
140        }
141      }
142    }
143
144    if ($_SESSION['need_update'] === false and $plugin_list === array())
145    {
146      return l10n('Gallery and plugins are up to date');
147    }
148    else
149    {
150      return l10n('Unable to check upgrades...')
151        . '<br><a href="admin.php?action=check_autoupdate">'
152        . l10n('Click here to check upgrades now') . '</a>';
153    }
154  }
155
156  function check_plugins_upgrade($plugins, $ignore_list)
157  {
158    if ($plugins->get_server_plugins())
159    {
160      $new_ignore_list = array();
161      $_SESSION['plugins_need_update'] = array();
162
163      foreach($plugins->fs_plugins as $plugin_id => $fs_plugin)
164      {
165        if (isset($fs_plugin['extension']) and isset($plugins->server_plugins[$fs_plugin['extension']]))
166        {
167          $plugin_info = $plugins->server_plugins[$fs_plugin['extension']];
168
169          if (!$plugins->plugin_version_compare($fs_plugin['version'], $plugin_info['revision_name']))
170          {
171            // Plugin need upgrade
172            $_SESSION['plugins_need_update'][$plugin_id] = $plugin_info['revision_name'];
173
174            if (in_array($fs_plugin['name'], $ignore_list))
175            {
176              array_push($new_ignore_list, $fs_plugin['name']);
177            }
178          }
179        }
180      }
181
182      // Update ignore list in database
183      $query = '
184UPDATE '.CONFIG_TABLE.'
185SET value = "'.addslashes(serialize($new_ignore_list)).'"
186WHERE param = "autoupdate_ignore_list"
187;';
188      pwg_query($query);
189    }
190  }
191}
192?>
Note: See TracBrowser for help on using the repository browser.