source: extensions/autoupdate/branches/2.0/include/autoupdate.class.php @ 10038

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

Check merged extensions.

File size: 6.7 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 $missing = array();
11  var $default_plugins = array();
12  var $default_themes = array();
13  var $merged_extensions = array();
14  var $merged_extension_url = 'http://piwigo.org/download/merged_extensions.txt';
15
16  function autoupdate()
17  {
18    if (version_compare(PHPWG_VERSION, '2.1', '>='))
19    {
20      $this->types = array('plugins', 'themes');
21      $this->default_themes = array('clear', 'dark', 'Sylvia');
22      $this->default_plugins = array('admin_multi_view', 'c13y_upgrade', 'language_switch', 'LocalFilesEditor');
23    }
24    else
25    {
26      $this->types = array('plugins');
27      $this->default_plugins = array('admin_multi_view', 'c13y_upgrade', 'language_switch', 'LocalFilesEditor', 'event_tracer', 'admin_advices', 'add_index');
28    }
29
30    foreach ($this->types as $type)
31    {
32      include_once(PHPWG_ROOT_PATH.'admin/include/'.$type.'.class.php');
33      $this->$type = new $type();
34    }
35  }
36
37  function check_piwigo_upgrade()
38  {
39    $_SESSION['need_update'] = null;
40
41    if (preg_match('/(\d+\.\d+)\.(\d+)/', PHPWG_VERSION, $matches)
42      and @fetchRemote(PHPWG_URL.'/download/all_versions.php', $result))
43    {
44      $all_versions = @explode("\n", $result);
45      $new_version = trim($all_versions[0]);
46      $_SESSION['need_update'] = version_compare(PHPWG_VERSION, $new_version, '<');
47    }
48  }
49
50  function get_server_extensions($version=PHPWG_VERSION)
51  {
52    global $user;
53
54    // Retrieve PEM versions
55    $versions_to_check = array();
56    $url = PEM_URL . '/api/get_version_list.php?format=php';
57    if (fetchRemote($url, $result) and $pem_versions = @unserialize($result))
58    {
59      if (!preg_match('/^\d+\.\d+\.\d+/', $version))
60      {
61        $version = $pem_versions[0]['name'];
62      }
63      $branch = substr($version, 0, strrpos($version, '.'));
64      foreach ($pem_versions as $pem_version)
65      {
66        if (strpos($pem_version['name'], $branch) === 0)
67        {
68          $versions_to_check[] = $pem_version['id'];
69        }
70      }
71    }
72    if (empty($versions_to_check))
73    {
74      return false;
75    }
76
77    // Extensions to check
78    $ext_to_check = array();
79    foreach ($this->types as $type)
80    {
81      $fs = 'fs_'.$type;
82      foreach ($this->$type->$fs as $ext)
83      {
84        if (isset($ext['extension']))
85        {
86          $ext_to_check[$ext['extension']] = $type;
87        }
88      }
89    }
90
91    // Retrieve PEM plugins infos
92    $url = PEM_URL . '/api/get_revision_list.php'
93      . '?format=php'
94      . '&last_revision_only=true'
95      . '&version='.implode(',', $versions_to_check)
96      . '&lang='.substr($user['language'], 0, 2)
97      . '&get_nb_downloads=true';
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.