source: trunk/admin/include/updates.class.php @ 23821

Last change on this file since 23821 was 23821, checked in by mistic100, 11 years ago

bug:2898 make some updates methods static + factorize code from plugins, themes & languages

File size: 13.1 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5class updates
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 __construct($page='updates')
19  {
20    $this->types = array('plugins', 'themes', 'languages');
21
22    if (in_array($page, $this->types))
23    {
24      $this->types = array($page);
25    }
26    $this->default_themes = array('clear', 'dark', 'Sylvia', 'elegant');
27    $this->default_plugins = array('admin_multi_view', 'c13y_upgrade', 'language_switch', 'LocalFilesEditor');
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  static 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?rand='.md5(uniqid(rand(), true)), $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    $get_data = array(
54      'format' => 'php',
55    );
56
57    // Retrieve PEM versions
58    $versions_to_check = array();
59    $url = PEM_URL . '/api/get_version_list.php';
60    if (fetchRemote($url, $result, $get_data) and $pem_versions = @unserialize($result))
61    {
62      if (!preg_match('/^\d+\.\d+\.\d+$/', $version))
63      {
64        $version = $pem_versions[0]['name'];
65      }
66      $branch = get_branch_from_version($version);
67      foreach ($pem_versions as $pem_version)
68      {
69        if (strpos($pem_version['name'], $branch) === 0)
70        {
71          $versions_to_check[] = $pem_version['id'];
72        }
73      }
74    }
75    if (empty($versions_to_check))
76    {
77      return false;
78    }
79
80    // Extensions to check
81    $ext_to_check = array();
82    foreach ($this->types as $type)
83    {
84      $fs = 'fs_'.$type;
85      foreach ($this->$type->$fs as $ext)
86      {
87        if (isset($ext['extension']))
88        {
89          $ext_to_check[$ext['extension']] = $type;
90        }
91      }
92    }
93
94    // Retrieve PEM plugins infos
95    $url = PEM_URL . '/api/get_revision_list.php';
96    $get_data = array_merge($get_data, array(
97      'last_revision_only' => 'true',
98      'version' => implode(',', $versions_to_check),
99      'lang' => substr($user['language'], 0, 2),
100      'get_nb_downloads' => 'true',
101      )
102    );
103
104    $post_data = array();
105    if (!empty($ext_to_check))
106    {
107      $post_data['extension_include'] = implode(',', array_keys($ext_to_check));
108    }
109
110    if (fetchRemote($url, $result, $get_data, $post_data))
111    {
112      $pem_exts = @unserialize($result);
113      if (!is_array($pem_exts))
114      {
115        return false;
116      }
117      foreach ($pem_exts as $ext)
118      {
119        if (isset($ext_to_check[$ext['extension_id']]))
120        {
121          $server = 'server_'.$ext_to_check[$ext['extension_id']];
122          $this->$ext_to_check[$ext['extension_id']]->$server += array($ext['extension_id'] => $ext);
123          unset($ext_to_check[$ext['extension_id']]);
124        }
125      }
126      $this->check_missing_extensions($ext_to_check);
127      return true;
128    }
129    return false;
130  }
131
132  // Check all extensions upgrades
133  function check_extensions()
134  {
135    global $conf;
136
137    if (!$this->get_server_extensions())
138    {
139      return false;
140    }
141
142    $_SESSION['extensions_need_update'] = array();
143
144    foreach ($this->types as $type)
145    {
146      $fs = 'fs_'.$type;
147      $server = 'server_'.$type;
148      $server_ext = $this->$type->$server;
149      $fs_ext = $this->$type->$fs;
150
151      $ignore_list = array();
152      $need_upgrade = array();
153
154      foreach($fs_ext as $ext_id => $fs_ext)
155      {
156        if (isset($fs_ext['extension']) and isset($server_ext[$fs_ext['extension']]))
157        {
158          $ext_info = $server_ext[$fs_ext['extension']];
159
160          if (!$this->version_compare($fs_ext['version'], $ext_info['revision_name'], $type))
161          {
162            if (in_array($ext_id, $conf['updates_ignored'][$type]))
163            {
164              array_push($ignore_list, $ext_id);
165            }
166            else
167            {
168              $_SESSION['extensions_need_update'][$type][$ext_id] = $ext_info['revision_name'];
169            }
170          }
171        }
172      }
173      $conf['updates_ignored'][$type] = $ignore_list;
174    }
175    conf_update_param('updates_ignored', pwg_db_real_escape_string(serialize($conf['updates_ignored'])));
176  }
177
178  // Check if extension have been upgraded since last check
179  function check_updated_extensions()
180  {
181    foreach ($this->types as $type)
182    {
183      if (!empty($_SESSION['extensions_need_update'][$type]))
184      {
185        $fs = 'fs_'.$type;
186        foreach($this->$type->$fs as $ext_id => $fs_ext)
187        {
188          if (isset($_SESSION['extensions_need_update'][$type][$ext_id])
189            and $this->version_compare($fs_ext['version'], $_SESSION['extensions_need_update'][$type][$ext_id], $type))
190          {
191            // Extension have been upgraded
192            $this->check_extensions();
193            break;
194          }
195        }
196      }
197    }
198  }
199
200  function check_missing_extensions($missing)
201  {
202    foreach ($missing as $id => $type)
203    {
204      $fs = 'fs_'.$type;
205      $default = 'default_'.$type;
206      foreach ($this->$type->$fs as $ext_id => $ext)
207      {
208        if (isset($ext['extension']) and $id == $ext['extension']
209          and !in_array($ext_id, $this->$default)
210          and !in_array($ext['extension'], $this->merged_extensions))
211        {
212          $this->missing[$type][] = $ext;
213          break;
214        }
215      }
216    }
217  }
218
219  function get_merged_extensions($version)
220  {
221    if (fetchRemote($this->merged_extension_url, $result))
222    {
223      $rows = explode("\n", $result);
224      foreach ($rows as $row)
225      {
226        if (preg_match('/^(\d+\.\d+): *(.*)$/', $row, $match))
227        {
228          if (version_compare($version, $match[1], '>='))
229          {
230            $extensions = explode(',', trim($match[2]));
231            $this->merged_extensions = array_merge($this->merged_extensions, $extensions);
232          }
233        }
234      }
235    }
236  }
237
238  function version_compare($a, $b, $type)
239  {
240    $version_compare = rtrim($type, 's').'_version_compare';
241
242    return $this->$type->$version_compare($a, $b);
243  }
244
245  static function process_obsolete_list($file)
246  {
247    if (file_exists(PHPWG_ROOT_PATH.$file)
248      and $old_files = file(PHPWG_ROOT_PATH.$file, FILE_IGNORE_NEW_LINES)
249      and !empty($old_files))
250    {
251      array_push($old_files, $file);
252      foreach($old_files as $old_file)
253      {
254        $path = PHPWG_ROOT_PATH.$old_file;
255        if (is_file($path))
256        {
257          @unlink($path);
258        }
259        elseif (is_dir($path))
260        {
261          deltree($path, PHPWG_ROOT_PATH.'_trash');
262        }
263      }
264    }
265  }
266
267  static function dump_database($include_history=false)
268  {
269    global $page, $conf, $cfgBase;
270
271    if (version_compare(PHPWG_VERSION, '2.1', '<'))
272    {
273      $conf['db_base'] = $cfgBase;
274    }
275
276    include(PHPWG_ROOT_PATH.'admin/include/mysqldump.php');
277
278    $path = PHPWG_ROOT_PATH.$conf['data_location'].'update';
279
280    if (@mkgetdir($path)
281      and ($backupFile = tempnam($path, 'sql'))
282      and ($dumper = new MySQLDump($conf['db_base'],$backupFile,false,false)))
283    {
284      foreach (get_defined_constants() as $constant => $value)
285      {
286        if (preg_match('/_TABLE$/', $constant))
287        {
288          $dumper->getTableStructure($value);
289          if ($constant == 'HISTORY_TABLE' and !$include_history)
290          {
291            continue;
292          }
293          $dumper->getTableData($value);
294        }
295      }
296    }
297
298    if (@filesize($backupFile))
299    {
300      $http_headers = array(
301        'Content-Length: '.@filesize($backupFile),
302        'Content-Type: text/x-sql',
303        'Content-Disposition: attachment; filename="database.sql";',
304        'Content-Transfer-Encoding: binary',
305        );
306
307      foreach ($http_headers as $header) {
308        header($header);
309      }
310
311      @readfile($backupFile);
312      deltree(PHPWG_ROOT_PATH.$conf['data_location'].'update');
313      exit();
314    }
315    else
316    {
317      array_push($page['errors'], l10n('Unable to dump database.'));
318    }
319  }
320
321  static function upgrade_to($upgrade_to, &$step, $check_current_version=true)
322  {
323    global $page, $conf, $template;
324
325    if ($check_current_version and !version_compare($upgrade_to, PHPWG_VERSION, '>'))
326    {
327      redirect(get_root_url().'admin.php?page=plugin-'.basename(dirname(__FILE__)));
328    }
329
330    if ($step == 2)
331    {
332      preg_match('/(\d+\.\d+)\.(\d+)/', PHPWG_VERSION, $matches);
333      $code =  $matches[1].'.x_to_'.$upgrade_to;
334      $dl_code = str_replace(array('.', '_'), '', $code);
335      $remove_path = $code;
336      $obsolete_list = 'obsolete.list';
337    }
338    else
339    {
340      $code = $upgrade_to;
341      $dl_code = $code;
342      $remove_path = version_compare($code, '2.0.8', '>=') ? 'piwigo' : 'piwigo-'.$code;
343      $obsolete_list = PHPWG_ROOT_PATH.'install/obsolete.list';
344    }
345
346    if (empty($page['errors']))
347    {
348      $path = PHPWG_ROOT_PATH.$conf['data_location'].'update';
349      $filename = $path.'/'.$code.'.zip';
350      @mkgetdir($path);
351
352      $chunk_num = 0;
353      $end = false;
354      $zip = @fopen($filename, 'w');
355
356      while (!$end)
357      {
358        $chunk_num++;
359        if (@fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.$dl_code.'&chunk_num='.$chunk_num, $result)
360          and $input = @unserialize($result))
361        {
362          if (0 == $input['remaining'])
363          {
364            $end = true;
365          }
366          @fwrite($zip, base64_decode($input['data']));
367        }
368        else
369        {
370          $end = true;
371        }
372      }
373      @fclose($zip);
374
375      if (@filesize($filename))
376      {
377        $zip = new PclZip($filename);
378        if ($result = $zip->extract(PCLZIP_OPT_PATH, PHPWG_ROOT_PATH,
379                                    PCLZIP_OPT_REMOVE_PATH, $remove_path,
380                                    PCLZIP_OPT_SET_CHMOD, 0755,
381                                    PCLZIP_OPT_REPLACE_NEWER))
382        {
383          //Check if all files were extracted
384          $error = '';
385          foreach($result as $extract)
386          {
387            if (!in_array($extract['status'], array('ok', 'filtered', 'already_a_directory')))
388            {
389              // Try to change chmod and extract
390              if (@chmod(PHPWG_ROOT_PATH.$extract['filename'], 0777)
391                and ($res = $zip->extract(PCLZIP_OPT_BY_NAME, $remove_path.'/'.$extract['filename'],
392                                          PCLZIP_OPT_PATH, PHPWG_ROOT_PATH,
393                                          PCLZIP_OPT_REMOVE_PATH, $remove_path,
394                                          PCLZIP_OPT_SET_CHMOD, 0755,
395                                          PCLZIP_OPT_REPLACE_NEWER))
396                and isset($res[0]['status'])
397                and $res[0]['status'] == 'ok')
398              {
399                continue;
400              }
401              else
402              {
403                $error .= $extract['filename'].': '.$extract['status']."\n";
404              }
405            }
406          }
407
408          if (empty($error))
409          {
410            self::process_obsolete_list($obsolete_list);
411            deltree(PHPWG_ROOT_PATH.$conf['data_location'].'update');
412            invalidate_user_cache(true);
413            $template->delete_compiled_templates();
414            unset($_SESSION['need_update']);
415            if ($step == 2)
416            {
417              array_push($page['infos'], l10n('Update Complete'), $upgrade_to);
418              $step = -1;
419            }
420            else
421            {
422              redirect(PHPWG_ROOT_PATH.'upgrade.php?now=');
423            }
424          }
425          else
426          {
427            file_put_contents(PHPWG_ROOT_PATH.$conf['data_location'].'update/log_error.txt', $error);
428            array_push(
429              $page['errors'],
430              sprintf(
431                l10n('An error has occured during extract. Please check files permissions of your piwigo installation.<br><a href="%s">Click here to show log error</a>.'),
432                get_root_url().$conf['data_location'].'update/log_error.txt'
433              )
434            );
435          }
436        }
437        else
438        {
439          deltree(PHPWG_ROOT_PATH.$conf['data_location'].'update');
440          array_push($page['errors'], l10n('An error has occured during upgrade.'));
441        }
442      }
443      else
444      {
445        array_push($page['errors'], l10n('Piwigo cannot retrieve upgrade file from server'));
446      }
447    }
448  }
449}
450
451?>
Note: See TracBrowser for help on using the repository browser.