source: trunk/admin/include/plugins.class.php @ 16177

Last change on this file since 16177 was 16177, checked in by mistic100, 12 years ago

incompatible plugins check doesn't work since 2.4, because version number format has changed

  • Property svn:eol-style set to LF
File size: 19.2 KB
RevLine 
[2263]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[12922]5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
[2297]6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
[2263]23
24class plugins
25{
26  var $fs_plugins = array();
27  var $db_plugins_by_id = array();
[2701]28  var $server_plugins = array();
[10098]29  var $default_plugins = array('LocalFilesEditor', 'language_switch', 'c13y_upgrade', 'admin_multi_view');
[2263]30
[2264]31  /**
32   * Initialize $fs_plugins and $db_plugins_by_id
33  */
34  function plugins()
[2263]35  {
[2264]36    $this->get_fs_plugins();
[2263]37
38    foreach (get_db_plugins() as $db_plugin)
39    {
40      $this->db_plugins_by_id[$db_plugin['id']] = $db_plugin;
41    }
42  }
43
44 /**
45   * Perform requested actions
46  *  @param string - action
47  * @param string - plugin id
[2264]48  * @param array - errors
[2263]49  */
[2272]50  function perform_action($action, $plugin_id)
[2263]51  {
52    if (isset($this->db_plugins_by_id[$plugin_id]))
53    {
54      $crt_db_plugin = $this->db_plugins_by_id[$plugin_id];
55    }
56    $file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php';
57
[2272]58    $errors = array();
59
[2263]60    switch ($action)
61    {
62      case 'install':
[10293]63        if (!empty($crt_db_plugin) or !isset($this->fs_plugins[$plugin_id]))
[2263]64        {
65          break;
66        }
67        if (file_exists($file_to_include))
68        {
69          include_once($file_to_include);
70          if (function_exists('plugin_install'))
71          {
72            plugin_install($plugin_id, $this->fs_plugins[$plugin_id]['version'], $errors);
73          }
74        }
75        if (empty($errors))
76        {
77          $query = '
[4385]78INSERT INTO ' . PLUGINS_TABLE . ' (id,version) VALUES (\''
79. $plugin_id . '\',\'' . $this->fs_plugins[$plugin_id]['version'] . '\'
[2263]80)';
81          pwg_query($query);
82        }
83        break;
84
85      case 'activate':
86        if (!isset($crt_db_plugin))
87        {
[10293]88          $errors = $this->perform_action('install', $plugin_id);
[12517]89          list($crt_db_plugin) = get_db_plugins(null, $plugin_id);
[11389]90          load_conf_from_db();
[2263]91        }
[10293]92        elseif ($crt_db_plugin['state'] == 'active')
[2263]93        {
94          break;
95        }
[10293]96        if (empty($errors) and file_exists($file_to_include))
[2263]97        {
98          include_once($file_to_include);
99          if (function_exists('plugin_activate'))
100          {
101            plugin_activate($plugin_id, $crt_db_plugin['version'], $errors);
102          }
103        }
104        if (empty($errors))
105        {
106          $query = '
[3205]107UPDATE ' . PLUGINS_TABLE . '
[4385]108SET state=\'active\', version=\''.$this->fs_plugins[$plugin_id]['version'].'\'
109WHERE id=\'' . $plugin_id . '\'';
[2263]110          pwg_query($query);
111        }
112        break;
113
114      case 'deactivate':
[10293]115        if (!isset($crt_db_plugin) or $crt_db_plugin['state'] != 'active')
[2263]116        {
[10293]117          break;
[2263]118        }
119        $query = '
[4385]120UPDATE ' . PLUGINS_TABLE . ' SET state=\'inactive\' WHERE id=\'' . $plugin_id . '\'';
[2263]121        pwg_query($query);
122        if (file_exists($file_to_include))
123        {
124          include_once($file_to_include);
125          if (function_exists('plugin_deactivate'))
126          {
127            plugin_deactivate($plugin_id);
128          }
129        }
130        break;
131
132      case 'uninstall':
133        if (!isset($crt_db_plugin))
134        {
[10293]135          break;
[2263]136        }
[10293]137        if ($crt_db_plugin['state'] == 'active')
138        {
139          $this->perform_action('deactivate', $plugin_id);
140        }
[2263]141        $query = '
[4385]142DELETE FROM ' . PLUGINS_TABLE . ' WHERE id=\'' . $plugin_id . '\'';
[2263]143        pwg_query($query);
144        if (file_exists($file_to_include))
145        {
146          include_once($file_to_include);
147          if (function_exists('plugin_uninstall'))
148          {
149            plugin_uninstall($plugin_id);
150          }
151        }
152        break;
153
[10293]154      case 'restore':
155        $this->perform_action('uninstall', $plugin_id);
156        unset($this->db_plugins_by_id[$plugin_id]);
157        $errors = $this->perform_action('activate', $plugin_id);
158        break;
159
[2263]160      case 'delete':
161        if (!empty($crt_db_plugin))
162        {
[10293]163          $this->perform_action('uninstall', $plugin_id);
[2263]164        }
165        if (!isset($this->fs_plugins[$plugin_id]))
166        {
167          break;
168        }
169        if (!$this->deltree(PHPWG_PLUGINS_PATH . $plugin_id))
170        {
171          $this->send_to_trash(PHPWG_PLUGINS_PATH . $plugin_id);
172        }
173        break;
174    }
175    return $errors;
176  }
177
178  /**
[2264]179  *  Get plugins defined in the plugin directory
[2263]180  */ 
181  function get_fs_plugins()
182  {
183    $dir = opendir(PHPWG_PLUGINS_PATH);
184    while ($file = readdir($dir))
185    {
186      if ($file!='.' and $file!='..')
187      {
188        $path = PHPWG_PLUGINS_PATH.$file;
189        if (is_dir($path) and !is_link($path)
190            and preg_match('/^[a-zA-Z0-9-_]+$/', $file )
191            and file_exists($path.'/main.inc.php')
192            )
193        {
194          $plugin = array(
195              'name'=>$file,
196              'version'=>'0',
197              'uri'=>'',
198              'description'=>'',
199              'author'=>'',
200            );
201          $plg_data = implode( '', file($path.'/main.inc.php') );
202
203          if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
204          {
205            $plugin['name'] = trim( $val[1] );
206          }
207          if (preg_match("|Version: (.*)|", $plg_data, $val))
208          {
209            $plugin['version'] = trim($val[1]);
210          }
211          if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) )
212          {
213            $plugin['uri'] = trim($val[1]);
214          }
[3716]215          if ($desc = load_language('description.txt', $path.'/', array('return' => true)))
[2263]216          {
[3716]217            $plugin['description'] = trim($desc);
218          }
219          elseif ( preg_match("|Description: (.*)|", $plg_data, $val) )
220          {
[2263]221            $plugin['description'] = trim($val[1]);
222          }
223          if ( preg_match("|Author: (.*)|", $plg_data, $val) )
224          {
225            $plugin['author'] = trim($val[1]);
226          }
227          if ( preg_match("|Author URI: (.*)|", $plg_data, $val) )
228          {
229            $plugin['author uri'] = trim($val[1]);
230          }
231          if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid='))
232          {
233            list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']);
234            if (is_numeric($extension)) $plugin['extension'] = $extension;
235          }
236          // IMPORTANT SECURITY !
237          $plugin = array_map('htmlspecialchars', $plugin);
238          $this->fs_plugins[$file] = $plugin;
239        }
240      }
241    }
242    closedir($dir);
243  }
244
245  /**
[2264]246   * Sort fs_plugins
[2263]247   */
[2264]248  function sort_fs_plugins($order='name')
[2263]249  {
[2264]250    switch ($order)
[2263]251    {
[2264]252      case 'name':
253        uasort($this->fs_plugins, 'name_compare');
254        break;
[2263]255      case 'status':
256        $this->sort_plugins_by_state();
257        break;
258      case 'author':
259        uasort($this->fs_plugins, array($this, 'plugin_author_compare'));
260        break;
261      case 'id':
262        uksort($this->fs_plugins, 'strcasecmp');
263        break;
264    }
265  }
266
[10098]267  // Retrieve PEM versions
268  function get_versions_to_check($version=PHPWG_VERSION)
[2263]269  {
[2647]270    $versions_to_check = array();
[10098]271    $url = PEM_URL . '/api/get_version_list.php?category=12&format=php';
272    if (fetchRemote($url, $result) and $pem_versions = @unserialize($result))
[2647]273    {
[11185]274      if (!preg_match('/^\d+\.\d+\.\d+$/', $version))
[2647]275      {
276        $version = $pem_versions[0]['name'];
277      }
[16177]278     
279      if (substr_count($version, '.') > 1)
280      {
281        $branch = substr($version, 0, strrpos($version, '.'));
282      }
283      else
284      {
285        $branch = $version;
286      }
287     
[2647]288      foreach ($pem_versions as $pem_version)
289      {
290        if (strpos($pem_version['name'], $branch) === 0)
291        {
292          $versions_to_check[] = $pem_version['id'];
293        }
294      }
295    }
[10098]296    return $versions_to_check;
297  }
298
299  /**
300   * Retrieve PEM server datas to $server_plugins
301   */
302  function get_server_plugins($new=false)
303  {
304    global $user;
305
306    $versions_to_check = $this->get_versions_to_check();
[2647]307    if (empty($versions_to_check))
308    {
309      return false;
310    }
311
312    // Plugins to check
313    $plugins_to_check = array();
[2264]314    foreach($this->fs_plugins as $fs_plugin)
[2263]315    {
316      if (isset($fs_plugin['extension']))
317      {
318        $plugins_to_check[] = $fs_plugin['extension'];
319      }
320    }
321
[2647]322    // Retrieve PEM plugins infos
[8083]323    $url = PEM_URL . '/api/get_revision_list.php';
[10098]324    $get_data = array(
325      'category_id' => 12,
326      'format' => 'php',
[8083]327      'last_revision_only' => 'true',
328      'version' => implode(',', $versions_to_check),
329      'lang' => substr($user['language'], 0, 2),
330      'get_nb_downloads' => 'true',
331    );
[2701]332
[2647]333    if (!empty($plugins_to_check))
[2263]334    {
[8083]335      if ($new)
336      {
337        $get_data['extension_exclude'] = implode(',', $plugins_to_check);
338      }
339      else
340      {
341        $get_data['extension_include'] = implode(',', $plugins_to_check);
342      }
[2263]343    }
[8083]344    if (fetchRemote($url, $result, $get_data))
[2647]345    {
[2880]346      $pem_plugins = @unserialize($result);
[2701]347      if (!is_array($pem_plugins))
348      {
349        return false;
350      }
[2647]351      foreach ($pem_plugins as $plugin)
352      {
353        $this->server_plugins[$plugin['extension_id']] = $plugin;
354      }
355      return true;
356    }
[2880]357    return false;
[2263]358  }
[10098]359
[10128]360  function get_incompatible_plugins($actualize=false)
[10098]361  {
[10128]362    if (isset($_SESSION['incompatible_plugins']) and !$actualize
363      and $_SESSION['incompatible_plugins']['~~expire~~'] > time())
[10098]364    {
365      return $_SESSION['incompatible_plugins'];
366    }
367
[10128]368    $_SESSION['incompatible_plugins'] = array('~~expire~~' => time() + 300);
[10098]369
370    $versions_to_check = $this->get_versions_to_check();
371    if (empty($versions_to_check))
372    {
373      return false;
374    }
375
376    // Plugins to check
377    $plugins_to_check = array();
378    foreach($this->fs_plugins as $fs_plugin)
379    {
380      if (isset($fs_plugin['extension']))
381      {
382        $plugins_to_check[] = $fs_plugin['extension'];
383      }
384    }
385
386    // Retrieve PEM plugins infos
387    $url = PEM_URL . '/api/get_revision_list.php';
388    $get_data = array(
389      'category_id' => 12,
390      'format' => 'php',
391      'version' => implode(',', $versions_to_check),
392      'extension_include' => implode(',', $plugins_to_check),
393    );
394
395    if (fetchRemote($url, $result, $get_data))
396    {
397      $pem_plugins = @unserialize($result);
398      if (!is_array($pem_plugins))
399      {
400        return false;
401      }
402
403      $server_plugins = array();
404      foreach ($pem_plugins as $plugin)
405      {
406        if (!isset($server_plugins[$plugin['extension_id']]))
407        {
408          $server_plugins[$plugin['extension_id']] = array();
409        }
410        array_push($server_plugins[$plugin['extension_id']], $plugin['revision_name']);
411      }
412
413      foreach ($this->fs_plugins as $plugin_id => $fs_plugin)
414      {
415        if (isset($fs_plugin['extension'])
416          and !in_array($plugin_id, $this->default_plugins)
417          and $fs_plugin['version'] != 'auto'
418          and (!isset($server_plugins[$fs_plugin['extension']]) or !in_array($fs_plugin['version'], $server_plugins[$fs_plugin['extension']])))
419        {
420          $_SESSION['incompatible_plugins'][$plugin_id] = $fs_plugin['version'];
421        }
422      }
423      return $_SESSION['incompatible_plugins'];
424    }
425    return false;
426  }
[2264]427 
428  /**
429   * Sort $server_plugins
430   */
431  function sort_server_plugins($order='date')
[2263]432  {
[2264]433    switch ($order)
[2263]434    {
[2264]435      case 'date':
436        krsort($this->server_plugins);
437        break;
[2272]438      case 'revision':
439        usort($this->server_plugins, array($this, 'extension_revision_compare'));
440        break;
[2264]441      case 'name':
442        uasort($this->server_plugins, array($this, 'extension_name_compare'));
443        break;
444      case 'author':
445        uasort($this->server_plugins, array($this, 'extension_author_compare'));
446        break;
[3143]447      case 'downloads':
448        usort($this->server_plugins, array($this, 'extension_downloads_compare'));
449        break;
[2263]450    }
451  }
452
453  /**
454   * Extract plugin files from archive
455   * @param string - install or upgrade
456   *  @param string - archive URL
[2647]457    * @param string - plugin id or extension id
[2263]458   */
[5153]459  function extract_plugin_files($action, $revision, $dest)
[2263]460  {
[5153]461    if ($archive = tempnam( PHPWG_PLUGINS_PATH, 'zip'))
[2263]462    {
[8083]463      $url = PEM_URL . '/download.php';
464      $get_data = array(
465        'rid' => $revision,
466        'origin' => 'piwigo_'.$action,
467      );
[2880]468
[8083]469      if ($handle = @fopen($archive, 'wb') and fetchRemote($url, $handle, $get_data))
[2263]470      {
[2880]471        fclose($handle);
[2264]472        include(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php');
[2263]473        $zip = new PclZip($archive);
474        if ($list = $zip->listContent())
475        {
476          foreach ($list as $file)
477          {
478            // we search main.inc.php in archive
[5153]479            if (basename($file['filename']) == 'main.inc.php'
[2263]480              and (!isset($main_filepath)
481              or strlen($file['filename']) < strlen($main_filepath)))
482            {
483              $main_filepath = $file['filename'];
484            }
485          }
486          if (isset($main_filepath))
487          {
488            $root = dirname($main_filepath); // main.inc.php path in archive
489            if ($action == 'upgrade')
490            {
[5153]491              $extract_path = PHPWG_PLUGINS_PATH . $dest;
[2263]492            }
493            else
494            {
[5153]495              $extract_path = PHPWG_PLUGINS_PATH
[2263]496                  . ($root == '.' ? 'extension_' . $dest : basename($root));
497            }
498            if($result = $zip->extract(PCLZIP_OPT_PATH, $extract_path,
499                                       PCLZIP_OPT_REMOVE_PATH, $root,
500                                       PCLZIP_OPT_REPLACE_NEWER))
501            {
502              foreach ($result as $file)
503              {
504                if ($file['stored_filename'] == $main_filepath)
505                {
506                  $status = $file['status'];
507                  break;
508                }
509              }
[4034]510              if (file_exists($extract_path.'/obsolete.list')
511                and $old_files = file($extract_path.'/obsolete.list', FILE_IGNORE_NEW_LINES)
512                and !empty($old_files))
513              {
514                array_push($old_files, 'obsolete.list');
515                foreach($old_files as $old_file)
516                {
517                  $path = $extract_path.'/'.$old_file;
518                  if (is_file($path))
519                  {
520                    @unlink($path);
521                  }
522                  elseif (is_dir($path))
523                  {
524                    if (!$this->deltree($path))
525                    {
526                      $this->send_to_trash($path);
527                    }
528                  }
529                }
530              }
[2263]531            }
532            else $status = 'extract_error';
533          }
534          else $status = 'archive_error';
535        }
536        else $status = 'archive_error';
537      }
538      else $status = 'dl_archive_error';
539    }
540    else $status = 'temp_path_error';
541
542    @unlink($archive);
543    return $status;
544  }
[10098]545
546  function get_merged_extensions($version=PHPWG_VERSION)
547  {
[11047]548    $file = PHPWG_ROOT_PATH.'install/obsolete_extensions.list';
549    $merged_extensions = array();
[10128]550
[11047]551    if (file_exists($file) and $obsolete_ext = file($file, FILE_IGNORE_NEW_LINES) and !empty($obsolete_ext))
[10128]552    {
[11047]553      foreach ($obsolete_ext as $ext)
[10098]554      {
[11047]555        if (preg_match('/^(\d+) ?: ?(.*?)$/', $ext, $matches))
[10098]556        {
[11047]557          $merged_extensions[$matches[1]] = $matches[2];
[10098]558        }
559      }
560    }
[11047]561    return $merged_extensions;
[10098]562  }
[2263]563 
564  /**
565   * delete $path directory
566   * @param string - path
567   */
568  function deltree($path)
569  {
570    if (is_dir($path))
571    {
572      $fh = opendir($path);
573      while ($file = readdir($fh))
574      {
575        if ($file != '.' and $file != '..')
576        {
577          $pathfile = $path . '/' . $file;
578          if (is_dir($pathfile))
579          {
580            $this->deltree($pathfile);
581          }
582          else
583          {
584            @unlink($pathfile);
585          }
586        }
587      }
588      closedir($fh);
589      return @rmdir($path);
590    }
591  }
592
593  /**
594   * send $path to trash directory
[2264]595   * @param string - path
[2263]596   */
597  function send_to_trash($path)
598  {
599    $trash_path = PHPWG_PLUGINS_PATH . 'trash';
600    if (!is_dir($trash_path))
601    {
602      @mkdir($trash_path);
603      $file = @fopen($trash_path . '/.htaccess', 'w');
604      @fwrite($file, 'deny from all');
605      @fclose($file);
606    }
607    while ($r = $trash_path . '/' . md5(uniqid(rand(), true)))
608    {
609      if (!is_dir($r))
610      {
611        @rename($path, $r);
612        break;
613      }
614    }
615  }
616
617  /**
618   * Sort functions
619   */
620  function plugin_version_compare($a, $b)
621  {
[6725]622    if (strtolower($a) == 'auto') return false;
623
[2272]624    $pattern = array('/([a-z])/ei', '/\.+/', '/\.\Z|\A\./');
625    $replacement = array( "'.'.intval('\\1', 36).'.'", '.', '');
626
[2647]627    $array = preg_replace($pattern, $replacement, array($a, $b));
[2272]628
629    return version_compare($array[0], $array[1], '>=');
[2263]630  }
631
[2272]632  function extension_revision_compare($a, $b)
633  {
[2647]634    if ($a['revision_date'] < $b['revision_date']) return 1;
[2272]635    else return -1;
636  }
637
[2263]638  function extension_name_compare($a, $b)
639  {
[2647]640    return strcmp(strtolower($a['extension_name']), strtolower($b['extension_name']));
[2263]641  }
642
643  function extension_author_compare($a, $b)
644  {
[2647]645    $r = strcasecmp($a['author_name'], $b['author_name']);
[2263]646    if ($r == 0) return $this->extension_name_compare($a, $b);
647    else return $r;
648  }
649
650  function plugin_author_compare($a, $b)
651  {
652    $r = strcasecmp($a['author'], $b['author']);
653    if ($r == 0) return name_compare($a, $b);
654    else return $r;
655  }
656
[3143]657  function extension_downloads_compare($a, $b)
658  {
659    if ($a['extension_nb_downloads'] < $b['extension_nb_downloads']) return 1;
660    else return -1;
661  }
662
[2263]663  function sort_plugins_by_state()
664  {
665    uasort($this->fs_plugins, 'name_compare');
666
667    $active_plugins = array();
668    $inactive_plugins = array();
669    $not_installed = array();
670
671    foreach($this->fs_plugins as $plugin_id => $plugin)
672    {
673      if (isset($this->db_plugins_by_id[$plugin_id]))
674      {
675        $this->db_plugins_by_id[$plugin_id]['state'] == 'active' ?
676          $active_plugins[$plugin_id] = $plugin : $inactive_plugins[$plugin_id] = $plugin;
677      }
678      else
679      {
680        $not_installed[$plugin_id] = $plugin;
681      }
682    }
683    $this->fs_plugins = $active_plugins + $inactive_plugins + $not_installed;
684  }
685}
686?>
Note: See TracBrowser for help on using the repository browser.