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

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

code simplification

File size: 5.5 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      $ignore_list = unserialize($conf['autoupdate_ignore_list']);
102
103      if (!isset($_SESSION['plugins_need_update']))
104      {
105        $_SESSION['plugins_need_update'] = null;
106        $this->check_plugins_upgrade($plugins, $ignore_list);
107      }
108      elseif (!empty($_SESSION['plugins_need_update']))
109      {
110        // Check if plugins have been upgraded since last check
111        foreach($plugins->fs_plugins as $plugin_id => $fs_plugin)
112        {
113          if (isset($_SESSION['plugins_need_update'][$plugin_id])
114            and $plugins->plugin_version_compare($fs_plugin['version'], $_SESSION['plugins_need_update'][$plugin_id]))
115          {
116            // Plugin have been updated
117            $this->check_plugins_upgrade($plugins, $ignore_list);
118          }
119        }
120      }
121
122      if (!empty($_SESSION['plugins_need_update']))
123      {
124        return l10n('Some upgrades are available for you plugins').'<br>'
125          . '<a href="admin.php?page=plugins_update">'.l10n('Click here see upgrade plugins page').'</a>';
126      }
127    }
128
129    if ($_SESSION['need_update'] === false and $_SESSION['plugins_need_update'] === array())
130    {
131      return l10n('Gallery and plugins are up to date');
132    }
133    else
134    {
135      return l10n('Unable to check upgrades...')
136        . '<br><a href="admin.php?action=check_autoupdate">'
137        . l10n('Click here to check upgrades now') . '</a>';
138    }
139  }
140
141  function check_plugins_upgrade($plugins, $ignore_list)
142  {
143    if ($plugins->get_server_plugins())
144    {
145      $new_ignore_list = array();
146      $_SESSION['plugins_need_update'] = array();
147
148      foreach($plugins->fs_plugins as $plugin_id => $fs_plugin)
149      {
150        if (isset($fs_plugin['extension']) and isset($plugins->server_plugins[$fs_plugin['extension']]))
151        {
152          $plugin_info = $plugins->server_plugins[$fs_plugin['extension']];
153
154          if (!$plugins->plugin_version_compare($fs_plugin['version'], $plugin_info['revision_name']))
155          {
156            if (in_array($fs_plugin['name'], $ignore_list))
157            {
158              array_push($new_ignore_list, $fs_plugin['name']);
159            }
160            else
161            {
162              $_SESSION['plugins_need_update'][$plugin_id] = $plugin_info['revision_name'];
163            }
164          }
165        }
166      }
167
168      // Update ignore list in database
169      $query = '
170UPDATE '.CONFIG_TABLE.'
171SET value = "'.addslashes(serialize($new_ignore_list)).'"
172WHERE param = "autoupdate_ignore_list"
173;';
174      pwg_query($query);
175    }
176  }
177}
178?>
Note: See TracBrowser for help on using the repository browser.