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

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

Display missing extension before major upgrade

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