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

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

Display missing extension before major upgrade

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