aboutsummaryrefslogtreecommitdiffstats
path: root/admin/include/plugins.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'admin/include/plugins.class.php')
-rw-r--r--admin/include/plugins.class.php247
1 files changed, 129 insertions, 118 deletions
diff --git a/admin/include/plugins.class.php b/admin/include/plugins.class.php
index 3c0cb6ffa..0ddc85b50 100644
--- a/admin/include/plugins.class.php
+++ b/admin/include/plugins.class.php
@@ -2,7 +2,7 @@
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery |
// +-----------------------------------------------------------------------+
-// | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org |
+// | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
@@ -21,6 +21,43 @@
// | USA. |
// +-----------------------------------------------------------------------+
+/**
+ * class DummyPlugin_maintain
+ * used when a plugin uses the old procedural declaration of maintenance methods
+ */
+class DummyPlugin_maintain extends PluginMaintain
+{
+ function install($plugin_version, &$errors=array())
+ {
+ if (is_callable('plugin_install'))
+ {
+ return plugin_install($this->plugin_id, $plugin_version, $errors);
+ }
+ }
+ function activate($plugin_version, &$errors=array())
+ {
+ if (is_callable('plugin_activate'))
+ {
+ return plugin_activate($this->plugin_id, $plugin_version, $errors);
+ }
+ }
+ function deactivate()
+ {
+ if (is_callable('plugin_deactivate'))
+ {
+ return plugin_deactivate($this->plugin_id);
+ }
+ }
+ function uninstall()
+ {
+ if (is_callable('plugin_uninstall'))
+ {
+ return plugin_uninstall($this->plugin_id);
+ }
+ }
+}
+
+
class plugins
{
var $fs_plugins = array();
@@ -30,7 +67,7 @@ class plugins
/**
* Initialize $fs_plugins and $db_plugins_by_id
- */
+ */
function plugins()
{
$this->get_fs_plugins();
@@ -41,19 +78,51 @@ class plugins
}
}
- /**
+ /**
+ * Returns the maintain class of a plugin
+ * or build a new class with the procedural methods
+ * @param string $plugin_id
+ */
+ private static function build_maintain_class($plugin_id)
+ {
+ $file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php';
+ $classname = $plugin_id.'_maintain';
+
+ if (file_exists($file_to_include))
+ {
+ include_once($file_to_include);
+
+ if (class_exists($classname))
+ {
+ $plugin_maintain = new $classname($plugin_id);
+ }
+ else
+ {
+ $plugin_maintain = new DummyPlugin_maintain($plugin_id);
+ }
+ }
+ else
+ {
+ $plugin_maintain = new DummyPlugin_maintain($plugin_id);
+ }
+
+ return $plugin_maintain;
+ }
+
+ /**
* Perform requested actions
- * @param string - action
- * @param string - plugin id
- * @param array - errors
- */
+ * @param string - action
+ * @param string - plugin id
+ * @param array - errors
+ */
function perform_action($action, $plugin_id)
{
if (isset($this->db_plugins_by_id[$plugin_id]))
{
$crt_db_plugin = $this->db_plugins_by_id[$plugin_id];
}
- $file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php';
+
+ $plugin_maintain = self::build_maintain_class($plugin_id);
$errors = array();
@@ -64,20 +133,15 @@ class plugins
{
break;
}
- if (file_exists($file_to_include))
- {
- include_once($file_to_include);
- if (function_exists('plugin_install'))
- {
- plugin_install($plugin_id, $this->fs_plugins[$plugin_id]['version'], $errors);
- }
- }
+
+ $plugin_maintain->install($this->fs_plugins[$plugin_id]['version'], $errors);
+
if (empty($errors))
{
$query = '
-INSERT INTO ' . PLUGINS_TABLE . ' (id,version) VALUES (\''
-. $plugin_id . '\',\'' . $this->fs_plugins[$plugin_id]['version'] . '\'
-)';
+INSERT INTO '. PLUGINS_TABLE .' (id,version)
+ VALUES (\''. $plugin_id .'\', \''. $this->fs_plugins[$plugin_id]['version'] .'\')
+;';
pwg_query($query);
}
break;
@@ -93,20 +157,20 @@ INSERT INTO ' . PLUGINS_TABLE . ' (id,version) VALUES (\''
{
break;
}
- if (empty($errors) and file_exists($file_to_include))
+
+ if (empty($errors))
{
- include_once($file_to_include);
- if (function_exists('plugin_activate'))
- {
- plugin_activate($plugin_id, $crt_db_plugin['version'], $errors);
- }
+ $plugin_maintain->activate($crt_db_plugin['version'], $errors);
}
+
if (empty($errors))
{
$query = '
-UPDATE ' . PLUGINS_TABLE . '
-SET state=\'active\', version=\''.$this->fs_plugins[$plugin_id]['version'].'\'
-WHERE id=\'' . $plugin_id . '\'';
+UPDATE '. PLUGINS_TABLE .'
+ SET state=\'active\',
+ version=\''. $this->fs_plugins[$plugin_id]['version'] .'\'
+ WHERE id=\''. $plugin_id .'\'
+;';
pwg_query($query);
}
break;
@@ -116,17 +180,15 @@ WHERE id=\'' . $plugin_id . '\'';
{
break;
}
+
$query = '
-UPDATE ' . PLUGINS_TABLE . ' SET state=\'inactive\' WHERE id=\'' . $plugin_id . '\'';
+UPDATE '. PLUGINS_TABLE .'
+ SET state=\'inactive\'
+ WHERE id=\''. $plugin_id .'\'
+;';
pwg_query($query);
- if (file_exists($file_to_include))
- {
- include_once($file_to_include);
- if (function_exists('plugin_deactivate'))
- {
- plugin_deactivate($plugin_id);
- }
- }
+
+ $plugin_maintain->deactivate();
break;
case 'uninstall':
@@ -138,17 +200,14 @@ UPDATE ' . PLUGINS_TABLE . ' SET state=\'inactive\' WHERE id=\'' . $plugin_id .
{
$this->perform_action('deactivate', $plugin_id);
}
+
$query = '
-DELETE FROM ' . PLUGINS_TABLE . ' WHERE id=\'' . $plugin_id . '\'';
+DELETE FROM '. PLUGINS_TABLE .'
+ WHERE id=\''. $plugin_id .'\'
+;';
pwg_query($query);
- if (file_exists($file_to_include))
- {
- include_once($file_to_include);
- if (function_exists('plugin_uninstall'))
- {
- plugin_uninstall($plugin_id);
- }
- }
+
+ $plugin_maintain->uninstall();
break;
case 'restore':
@@ -166,18 +225,17 @@ DELETE FROM ' . PLUGINS_TABLE . ' WHERE id=\'' . $plugin_id . '\'';
{
break;
}
- if (!$this->deltree(PHPWG_PLUGINS_PATH . $plugin_id))
- {
- $this->send_to_trash(PHPWG_PLUGINS_PATH . $plugin_id);
- }
+
+ deltree(PHPWG_PLUGINS_PATH . $plugin_id, PHPWG_PLUGINS_PATH . 'trash');
break;
}
+
return $errors;
}
/**
- * Get plugins defined in the plugin directory
- */
+ * Get plugins defined in the plugin directory
+ */
function get_fs_plugins()
{
$dir = opendir(PHPWG_PLUGINS_PATH);
@@ -267,8 +325,10 @@ DELETE FROM ' . PLUGINS_TABLE . ' WHERE id=\'' . $plugin_id . '\'';
// Retrieve PEM versions
function get_versions_to_check($version=PHPWG_VERSION)
{
+ global $conf;
+
$versions_to_check = array();
- $url = PEM_URL . '/api/get_version_list.php?category=12&format=php';
+ $url = PEM_URL . '/api/get_version_list.php?category_id='. $conf['pem_plugins_category'] .'&format=php';
if (fetchRemote($url, $result) and $pem_versions = @unserialize($result))
{
if (!preg_match('/^\d+\.\d+\.\d+$/', $version))
@@ -400,7 +460,7 @@ DELETE FROM ' . PLUGINS_TABLE . ' WHERE id=\'' . $plugin_id . '\'';
{
$server_plugins[$plugin['extension_id']] = array();
}
- array_push($server_plugins[$plugin['extension_id']], $plugin['revision_name']);
+ $server_plugins[$plugin['extension_id']][] = $plugin['revision_name'];
}
foreach ($this->fs_plugins as $plugin_id => $fs_plugin)
@@ -504,7 +564,7 @@ DELETE FROM ' . PLUGINS_TABLE . ' WHERE id=\'' . $plugin_id . '\'';
and $old_files = file($extract_path.'/obsolete.list', FILE_IGNORE_NEW_LINES)
and !empty($old_files))
{
- array_push($old_files, 'obsolete.list');
+ $old_files[] = 'obsolete.list';
foreach($old_files as $old_file)
{
$path = $extract_path.'/'.$old_file;
@@ -514,10 +574,7 @@ DELETE FROM ' . PLUGINS_TABLE . ' WHERE id=\'' . $plugin_id . '\'';
}
elseif (is_dir($path))
{
- if (!$this->deltree($path))
- {
- $this->send_to_trash($path);
- }
+ deltree($path, PHPWG_PLUGINS_PATH . 'trash');
}
}
}
@@ -553,59 +610,6 @@ DELETE FROM ' . PLUGINS_TABLE . ' WHERE id=\'' . $plugin_id . '\'';
}
return $merged_extensions;
}
-
- /**
- * delete $path directory
- * @param string - path
- */
- function deltree($path)
- {
- if (is_dir($path))
- {
- $fh = opendir($path);
- while ($file = readdir($fh))
- {
- if ($file != '.' and $file != '..')
- {
- $pathfile = $path . '/' . $file;
- if (is_dir($pathfile))
- {
- $this->deltree($pathfile);
- }
- else
- {
- @unlink($pathfile);
- }
- }
- }
- closedir($fh);
- return @rmdir($path);
- }
- }
-
- /**
- * send $path to trash directory
- * @param string - path
- */
- function send_to_trash($path)
- {
- $trash_path = PHPWG_PLUGINS_PATH . 'trash';
- if (!is_dir($trash_path))
- {
- @mkdir($trash_path);
- $file = @fopen($trash_path . '/.htaccess', 'w');
- @fwrite($file, 'deny from all');
- @fclose($file);
- }
- while ($r = $trash_path . '/' . md5(uniqid(rand(), true)))
- {
- if (!is_dir($r))
- {
- @rename($path, $r);
- break;
- }
- }
- }
/**
* Sort functions
@@ -613,12 +617,19 @@ DELETE FROM ' . PLUGINS_TABLE . ' WHERE id=\'' . $plugin_id . '\'';
function plugin_version_compare($a, $b)
{
if (strtolower($a) == 'auto') return false;
-
- $pattern = array('/([a-z])/ei', '/\.+/', '/\.\Z|\A\./');
- $replacement = array( "'.'.intval('\\1', 36).'.'", '.', '');
-
- $array = preg_replace($pattern, $replacement, array($a, $b));
-
+
+ $array = preg_replace(
+ array('/\.+/', '/\.\Z|\A\./'),
+ array('.', ''),
+ array($a, $b)
+ );
+
+ $array = preg_replace_callback(
+ '/([a-z])/i',
+ create_function('$m', 'return intval($m[1], 36);'),
+ $array
+ );
+
return version_compare($array[0], $array[1], '>=');
}