mirror of
https://github.com/Piwigo/Piwigo.git
synced 2025-04-27 19:59:56 +03:00
Use class for plugins management
git-svn-id: http://piwigo.org/svn/trunk@2263 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
parent
0c83b734fb
commit
6ecb1f3507
10 changed files with 676 additions and 702 deletions
|
@ -24,153 +24,6 @@
|
|||
// | USA. |
|
||||
// +-----------------------------------------------------------------------+
|
||||
|
||||
/* Returns an array of plugins defined in the plugin directory
|
||||
*/
|
||||
function get_fs_plugins()
|
||||
{
|
||||
$plugins = array();
|
||||
|
||||
$dir = opendir(PHPWG_PLUGINS_PATH);
|
||||
while ($file = readdir($dir))
|
||||
{
|
||||
if ($file!='.' and $file!='..')
|
||||
{
|
||||
$path = PHPWG_PLUGINS_PATH.$file;
|
||||
if (is_dir($path) and !is_link($path)
|
||||
and preg_match('/^[a-zA-Z0-9-_]+$/', $file )
|
||||
and file_exists($path.'/main.inc.php')
|
||||
)
|
||||
{
|
||||
$plugin = array(
|
||||
'name'=>$file,
|
||||
'version'=>'0',
|
||||
'uri'=>'',
|
||||
'description'=>'',
|
||||
'author'=>'',
|
||||
);
|
||||
$plg_data = implode( '', file($path.'/main.inc.php') );
|
||||
|
||||
if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
|
||||
{
|
||||
$plugin['name'] = trim( $val[1] );
|
||||
}
|
||||
if (preg_match("|Version: (.*)|", $plg_data, $val))
|
||||
{
|
||||
$plugin['version'] = trim($val[1]);
|
||||
}
|
||||
if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) )
|
||||
{
|
||||
$plugin['uri'] = trim($val[1]);
|
||||
}
|
||||
if ( preg_match("|Description: (.*)|", $plg_data, $val) )
|
||||
{
|
||||
$plugin['description'] = trim($val[1]);
|
||||
}
|
||||
if ( preg_match("|Author: (.*)|", $plg_data, $val) )
|
||||
{
|
||||
$plugin['author'] = trim($val[1]);
|
||||
}
|
||||
if ( preg_match("|Author URI: (.*)|", $plg_data, $val) )
|
||||
{
|
||||
$plugin['author uri'] = trim($val[1]);
|
||||
}
|
||||
// IMPORTANT SECURITY !
|
||||
$plugin = array_map('htmlspecialchars', $plugin);
|
||||
$plugins[$file] = $plugin;
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir($dir);
|
||||
return $plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Activates a plugin. It will be loaded only on the next page ...
|
||||
* @param string $plugin_id the plugin to activate
|
||||
* @param array $errors errors to be returned
|
||||
*/
|
||||
function activate_plugin($plugin_id, &$errors)
|
||||
{
|
||||
// the plugin_id must exist in the DB as inactive
|
||||
$db_plugins = get_db_plugins('', $plugin_id);
|
||||
if (!empty($db_plugins))
|
||||
{
|
||||
$crt_db_plugin = $db_plugins[0];
|
||||
if ($crt_db_plugin['state'] != 'inactive')
|
||||
{
|
||||
array_push($errors, 'CANNOT ACTIVATE - INVALID CURRENT STATE ' . $crt_db_plugin['state']);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
array_push($errors, 'CANNOT ACTIVATE - NOT INSTALLED');
|
||||
return false;
|
||||
}
|
||||
|
||||
// now try to activate it
|
||||
$file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php';
|
||||
if (file_exists($file_to_include))
|
||||
{
|
||||
include_once($file_to_include);
|
||||
if (function_exists('plugin_activate'))
|
||||
{
|
||||
plugin_activate($plugin_id, $crt_db_plugin['version'], $errors);
|
||||
}
|
||||
}
|
||||
if (empty($errors))
|
||||
{
|
||||
$query = '
|
||||
UPDATE ' . PLUGINS_TABLE . ' SET state="active" WHERE id="' . $plugin_id . '"';
|
||||
pwg_query($query);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deactivates a plugin. It will be unloaded only on the next page ...
|
||||
* @param string $plugin_id the plugin to activate
|
||||
* @param array $errors errors to be returned
|
||||
*/
|
||||
function deactivate_plugin($plugin_id, &$errors)
|
||||
{
|
||||
// the plugin_id must exist in the DB as inactive
|
||||
$db_plugins = get_db_plugins('', $plugin_id);
|
||||
if (!empty($db_plugins))
|
||||
{
|
||||
$crt_db_plugin = $db_plugins[0];
|
||||
if ($crt_db_plugin['state'] != 'active')
|
||||
{
|
||||
array_push($errors, 'CANNOT DEACTIVATE - INVALID CURRENT STATE ' . $crt_db_plugin['state']);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
array_push($errors, 'CANNOT DEACTIVATE - NOT INSTALLED');
|
||||
return false;
|
||||
}
|
||||
|
||||
// now try to deactivate it
|
||||
$query = '
|
||||
UPDATE ' . PLUGINS_TABLE . ' SET state="inactive" WHERE id="' . $plugin_id . '"';
|
||||
pwg_query($query);
|
||||
|
||||
$file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php';
|
||||
if (file_exists($file_to_include))
|
||||
{
|
||||
include_once($file_to_include);
|
||||
if (function_exists('plugin_deactivate'))
|
||||
{
|
||||
plugin_deactivate($plugin_id);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves an url for a plugin page.
|
||||
* @param string file - php script full name
|
||||
|
@ -193,203 +46,4 @@ function get_admin_plugin_menu_link($file)
|
|||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort plugins by status
|
||||
*/
|
||||
function sort_plugins_by_state($plugins, $db_plugins_by_id)
|
||||
{
|
||||
$active_plugins = array();
|
||||
$inactive_plugins = array();
|
||||
$not_installed = array();
|
||||
|
||||
foreach($plugins as $plugin_id => $plugin)
|
||||
{
|
||||
if (isset($db_plugins_by_id[$plugin_id]))
|
||||
{
|
||||
$db_plugins_by_id[$plugin_id]['state'] == 'active' ?
|
||||
$active_plugins[$plugin_id] = $plugin : $inactive_plugins[$plugin_id] = $plugin;
|
||||
}
|
||||
else
|
||||
{
|
||||
$not_installed[$plugin_id] = $plugin;
|
||||
}
|
||||
}
|
||||
return $active_plugins + $inactive_plugins + $not_installed;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve PEM server datas
|
||||
* @param bool (true for retrieve new extensions)
|
||||
*/
|
||||
function check_server_plugins(& $fs_plugins, $newext=false)
|
||||
{
|
||||
foreach($fs_plugins as $plugin_id => $fs_plugin)
|
||||
{
|
||||
if (!empty($fs_plugin['uri']) and strpos($fs_plugin['uri'] , 'extension_view.php?eid='))
|
||||
{
|
||||
list( , $extension) = explode('extension_view.php?eid=', $fs_plugin['uri']);
|
||||
if (!is_numeric($extension)) continue;
|
||||
$plugins_to_check[] = $extension;
|
||||
$fs_plugins[$plugin_id]['extension'] = $extension;
|
||||
}
|
||||
}
|
||||
|
||||
$url = PEM_URL . '/uptodate.php?version=' . rawurlencode(PHPWG_VERSION) . '&extensions=' . implode(',', $plugins_to_check);
|
||||
$url .= $newext ? '&newext=Plugin' : '';
|
||||
|
||||
if (!empty($plugins_to_check) and $source = @file_get_contents($url))
|
||||
{
|
||||
return @unserialize($source);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract plugin files from archive
|
||||
* @param string - install or upgrade
|
||||
* @param string - archive URL
|
||||
* @param string - destination path
|
||||
*/
|
||||
function extract_plugin_files($action, $source, $dest)
|
||||
{
|
||||
if ($archive = tempnam( PHPWG_PLUGINS_PATH, 'zip'))
|
||||
{
|
||||
if (@copy(PEM_URL . str_replace(' ', '%20', $source), $archive))
|
||||
{
|
||||
$zip = new PclZip($archive);
|
||||
if ($list = $zip->listContent())
|
||||
{
|
||||
foreach ($list as $file)
|
||||
{
|
||||
// we search main.inc.php in archive
|
||||
if (basename($file['filename']) == 'main.inc.php'
|
||||
and (!isset($main_filepath)
|
||||
or strlen($file['filename']) < strlen($main_filepath)))
|
||||
{
|
||||
$main_filepath = $file['filename'];
|
||||
}
|
||||
}
|
||||
if (isset($main_filepath))
|
||||
{
|
||||
$root = dirname($main_filepath); // main.inc.php path in archive
|
||||
if ($action == 'upgrade')
|
||||
{
|
||||
$extract_path = PHPWG_PLUGINS_PATH.$dest;
|
||||
}
|
||||
else
|
||||
{
|
||||
$extract_path = PHPWG_PLUGINS_PATH
|
||||
. ($root == '.' ? 'extension_' . $dest : basename($root));
|
||||
}
|
||||
if($result = $zip->extract(PCLZIP_OPT_PATH, $extract_path,
|
||||
PCLZIP_OPT_REMOVE_PATH, $root,
|
||||
PCLZIP_OPT_REPLACE_NEWER))
|
||||
{
|
||||
foreach ($result as $file)
|
||||
{
|
||||
if ($file['stored_filename'] == $main_filepath)
|
||||
{
|
||||
$status = $file['status'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else $status = 'extract_error';
|
||||
}
|
||||
else $status = 'archive_error';
|
||||
}
|
||||
else $status = 'archive_error';
|
||||
}
|
||||
else $status = 'dl_archive_error';
|
||||
}
|
||||
else $status = 'temp_path_error';
|
||||
|
||||
@unlink($archive);
|
||||
return $status;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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))
|
||||
{
|
||||
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
|
||||
*/
|
||||
function extension_name_compare($a, $b)
|
||||
{
|
||||
return strcmp(strtolower($a['ext_name']), strtolower($b['ext_name']));
|
||||
}
|
||||
|
||||
function extension_author_compare($a, $b)
|
||||
{
|
||||
$r = strcmp(strtolower($a['author']), strtolower($b['author']));
|
||||
if ($r == 0) return extension_name_compare($a, $b);
|
||||
else return $r;
|
||||
}
|
||||
|
||||
function plugin_author_compare($a, $b)
|
||||
{
|
||||
return strcasecmp( $a['author'], $b['author']);
|
||||
}
|
||||
|
||||
function plugin_version_compare($a, $b)
|
||||
{
|
||||
return version_compare( $a['version'], $b['version']);
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue