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

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

Minor optimization

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