source: extensions/autoupdate/trunk/include/autoupdate.class.php @ 10037

Last change on this file since 10037 was 10037, checked in by patdenice, 13 years ago

Don't deactivate autoupdate when update itself.
Check merged extensions.

File size: 6.6 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5class autoupdate
6{
7  var $types = array();
8  var $plugins;
9  var $themes;
10  var $languages;
11  var $missing = array();
12  var $default_plugins = array();
13  var $default_themes = array();
14  var $default_languages = array();
15  var $merged_extensions = array();
16  var $merged_extension_url = 'http://piwigo.org/download/merged_extensions.txt';
17
18  function autoupdate()
19  {
20    $this->types = array('plugins', 'themes', 'languages');
21    $this->default_themes = array('clear', 'dark', 'Sylvia');
22    $this->default_plugins = array('admin_multi_view', 'c13y_upgrade', 'language_switch', 'LocalFilesEditor');
23
24    foreach ($this->types as $type)
25    {
26      include_once(PHPWG_ROOT_PATH.'admin/include/'.$type.'.class.php');
27      $this->$type = new $type();
28    }
29  }
30
31  function check_piwigo_upgrade()
32  {
33    $_SESSION['need_update'] = null;
34
35    if (preg_match('/(\d+\.\d+)\.(\d+)/', PHPWG_VERSION, $matches)
36      and @fetchRemote(PHPWG_URL.'/download/all_versions.php', $result))
37    {
38      $all_versions = @explode("\n", $result);
39      $new_version = trim($all_versions[0]);
40      $_SESSION['need_update'] = version_compare(PHPWG_VERSION, $new_version, '<');
41    }
42  }
43
44  function get_server_extensions($version=PHPWG_VERSION)
45  {
46    global $user;
47
48    $get_data = array(
49      'format' => 'php',
50    );
51
52    // Retrieve PEM versions
53    $versions_to_check = array();
54    $url = PEM_URL . '/api/get_version_list.php';
55    if (fetchRemote($url, $result, $get_data) and $pem_versions = @unserialize($result))
56    {
57      if (!preg_match('/^\d+\.\d+\.\d+/', $version))
58      {
59        $version = $pem_versions[0]['name'];
60      }
61      $branch = substr($version, 0, strrpos($version, '.'));
62      foreach ($pem_versions as $pem_version)
63      {
64        if (strpos($pem_version['name'], $branch) === 0)
65        {
66          $versions_to_check[] = $pem_version['id'];
67        }
68      }
69    }
70    if (empty($versions_to_check))
71    {
72      return false;
73    }
74
75    // Extensions to check
76    $ext_to_check = array();
77    foreach ($this->types as $type)
78    {
79      $fs = 'fs_'.$type;
80      foreach ($this->$type->$fs as $ext)
81      {
82        if (isset($ext['extension']))
83        {
84          $ext_to_check[$ext['extension']] = $type;
85        }
86      }
87    }
88
89    // Retrieve PEM plugins infos
90    $url = PEM_URL . '/api/get_revision_list.php';
91    $get_data = array_merge($get_data, array(
92      'last_revision_only' => 'true',
93      'version' => implode(',', $versions_to_check),
94      'lang' => substr($user['language'], 0, 2),
95      'get_nb_downloads' => 'true',
96      )
97    );
98
99    if (!empty($ext_to_check))
100    {
101      $get_data['extension_include'] = implode(',', array_keys($ext_to_check));
102    }
103
104    if (fetchRemote($url, $result, $get_data))
105    {
106      $pem_exts = @unserialize($result);
107      if (!is_array($pem_exts))
108      {
109        return false;
110      }
111      foreach ($pem_exts as $ext)
112      {
113        if (isset($ext_to_check[$ext['extension_id']]))
114        {
115          $server = 'server_'.$ext_to_check[$ext['extension_id']];
116          $this->$ext_to_check[$ext['extension_id']]->$server += array($ext['extension_id'] => $ext);
117          unset($ext_to_check[$ext['extension_id']]);
118        }
119      }
120      $this->check_missing_extensions($ext_to_check);
121      return true;
122    }
123    return false;
124  }
125
126  // Check all extensions upgrades
127  function check_extensions()
128  {
129    global $conf;
130
131    if (!$this->get_server_extensions())
132    {
133      autoupdate_error();
134    }
135
136    $_SESSION['extensions_need_update'] = array();
137
138    foreach ($this->types as $type)
139    {
140      $fs = 'fs_'.$type;
141      $server = 'server_'.$type;
142      $server_ext = $this->$type->$server;
143      $fs_ext = $this->$type->$fs;
144
145      $ignore_list = array();
146      $need_upgrade = array();
147
148      foreach($fs_ext as $ext_id => $fs_ext)
149      {
150        if (isset($fs_ext['extension']) and isset($server_ext[$fs_ext['extension']]))
151        {
152          $ext_info = $server_ext[$fs_ext['extension']];
153
154          if (!$this->version_compare($fs_ext['version'], $ext_info['revision_name'], $type))
155          {
156            if (in_array($ext_id, $conf['AU_ignore'][$type]))
157            {
158              array_push($ignore_list, $ext_id);
159            }
160            else
161            {
162              $_SESSION['extensions_need_update'][$type][$ext_id] = $ext_info['revision_name'];
163            }
164          }
165        }
166      }
167      $conf['AU_ignore'][$type] = $ignore_list;
168    }
169    conf_update_param('autoupdate_ignore_list', pwg_db_real_escape_string(serialize($conf['AU_ignore'])));
170  }
171
172  // Check if extension have been upgraded since last check
173  function check_updated_extensions()
174  {
175    foreach ($this->types as $type)
176    {
177      if (!empty($_SESSION['extensions_need_update'][$type]))
178      {
179        $fs = 'fs_'.$type;
180        foreach($this->$type->$fs as $ext_id => $fs_ext)
181        {
182          if (isset($_SESSION['extensions_need_update'][$type][$ext_id])
183            and $this->version_compare($fs_ext['version'], $_SESSION['extensions_need_update'][$type][$ext_id], $type))
184          {
185            // Extension have been upgraded
186            $this->check_extensions();
187            break;
188          }
189        }
190      }
191    }
192  }
193
194  function check_missing_extensions($missing)
195  {
196    foreach ($missing as $id => $type)
197    {
198      $fs = 'fs_'.$type;
199      $default = 'default_'.$type;
200      foreach ($this->$type->$fs as $ext_id => $ext)
201      {
202        if (isset($ext['extension']) and $id == $ext['extension']
203          and !in_array($ext_id, $this->$default)
204          and !in_array($ext['extension'], $this->merged_extensions))
205        {
206          $this->missing[$type][] = $ext;
207          break;
208        }
209      }
210    }
211  }
212
213  function get_merged_extensions($version)
214  {
215    if (fetchRemote($this->merged_extension_url, $result))
216    {
217      $rows = explode("\n", $result);
218      foreach ($rows as $row)
219      {
220        if (preg_match('/^(\d+\.\d+): *(.*)$/', $row, $match))
221        {
222          if (version_compare($version, $match[1], '>='))
223          {
224            $extensions = explode(',', trim($match[2]));
225            $this->merged_extensions = array_merge($this->merged_extensions, $extensions);
226          }
227        }
228      }
229    }
230  }
231
232  function version_compare($a, $b, $type)
233  {
234    $version_compare = rtrim($type, 's').'_version_compare';
235
236    return $this->$type->$version_compare($a, $b);
237  }
238}
239
240?>
Note: See TracBrowser for help on using the repository browser.