From 6ecb1f350753ac6c258677bb244d6003b3d359d3 Mon Sep 17 00:00:00 2001 From: patdenice Date: Fri, 7 Mar 2008 10:42:51 +0000 Subject: Use class for plugins management git-svn-id: http://piwigo.org/svn/trunk@2263 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin.php | 2 +- admin/include/functions_plugins.inc.php | 346 ------------------ admin/include/plugins.class.php | 598 ++++++++++++++++++++++++++++++++ admin/plugins.php | 66 ---- admin/plugins_list.php | 188 ++-------- admin/plugins_new.php | 82 ++--- admin/plugins_update.php | 88 ++--- template/yoga/admin/plugins_list.tpl | 2 +- template/yoga/admin/plugins_new.tpl | 2 +- template/yoga/admin/plugins_update.tpl | 2 +- 10 files changed, 675 insertions(+), 701 deletions(-) create mode 100644 admin/include/plugins.class.php delete mode 100644 admin/plugins.php diff --git a/admin.php b/admin.php index e593f6066..e6a7337ee 100644 --- a/admin.php +++ b/admin.php @@ -114,7 +114,7 @@ usort($plugin_menu_links, 'UC_name_compare'); array_unshift($plugin_menu_links, array( 'NAME' => l10n('admin'), - 'URL' => $link_start.'plugins' + 'URL' => $link_start.'plugins_list' ) ); diff --git a/admin/include/functions_plugins.inc.php b/admin/include/functions_plugins.inc.php index 3e79988fd..7bed61904 100644 --- a/admin/include/functions_plugins.inc.php +++ b/admin/include/functions_plugins.inc.php @@ -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']); -} - ?> \ No newline at end of file diff --git a/admin/include/plugins.class.php b/admin/include/plugins.class.php new file mode 100644 index 000000000..490abf762 --- /dev/null +++ b/admin/include/plugins.class.php @@ -0,0 +1,598 @@ +page = $page; + $this->order = $order; + + $this->my_base_url = get_root_url().'admin.php?page='.$this->page; + if (!empty($this->order)) + { + $this->my_base_url .= '&order=' . $this->order; + } + $this->html_base_url = htmlentities($this->my_base_url); + + $this->get_fs_plugins(); + foreach (get_db_plugins() as $db_plugin) + { + $this->db_plugins_by_id[$db_plugin['id']] = $db_plugin; + } + } + + /** + * Assign tabsheet + */ + function tabsheet() + { + include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php'); + $link = PHPWG_ROOT_PATH.'admin.php?page='; + + $tabsheet = new tabsheet(); + $tabsheet->add('plugins_list', l10n('plugins_tab_list'), $link.'plugins_list'); + $tabsheet->add('plugins_update', l10n('plugins_tab_update'), $link.'plugins_update'); + $tabsheet->add('plugins_new', l10n('plugins_tab_new'), $link.'plugins_new'); + $tabsheet->select($this->page); + $tabsheet->assign(); + } + + /** + * Set order + * @param array - order options + */ + function set_order_options($options) + { + global $template; + + $link = get_root_url().'admin.php?page='.$this->page.'&order='; + foreach($options as $key => $value) + { + $tpl_options[$link . $key] = $value; + } + $template->assign('order_options', $tpl_options); + $template->assign('order_selected', $link . $this->order); + } + + /** + * Perform requested actions + * @param string - action + * @param string - plugin id + * @param string - errors + */ + function perform_action($action, $plugin_id, $errors=array()) + { + 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'; + + switch ($action) + { + case 'install': + if (!empty($crt_db_plugin)) + { + array_push($errors, 'CANNOT INSTALL - ALREADY INSTALLED'); + break; + } + if (!isset($this->fs_plugins[$plugin_id])) + { + array_push($errors, 'CANNOT INSTALL - NO SUCH PLUGIN'); + 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); + } + } + if (empty($errors)) + { + $query = ' +INSERT INTO ' . PLUGINS_TABLE . ' (id,version) VALUES ("' +. $plugin_id . '","' . $this->fs_plugins[$plugin_id]['version'] . '" +)'; + pwg_query($query); + } + break; + + case 'activate': + if (!isset($crt_db_plugin)) + { + array_push($errors, 'CANNOT ACTIVATE - NOT INSTALLED'); + break; + } + if ($crt_db_plugin['state'] != 'inactive') + { + array_push($errors, 'invalid current state ' . $crt_db_plugin['state']); + break; + } + 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); + } + break; + + case 'deactivate': + if (!isset($crt_db_plugin)) + { + die ('CANNOT DEACTIVATE - NOT INSTALLED'); + } + if ($crt_db_plugin['state'] != 'active') + { + die('invalid current state ' . $crt_db_plugin['state']); + } + $query = ' +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); + } + } + break; + + case 'uninstall': + if (!isset($crt_db_plugin)) + { + die ('CANNOT UNINSTALL - NOT INSTALLED'); + } + $query = ' +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); + } + } + break; + + case 'delete': + if (!empty($crt_db_plugin)) + { + array_push($errors, 'CANNOT DELETE - PLUGIN IS INSTALLED'); + break; + } + if (!isset($this->fs_plugins[$plugin_id])) + { + array_push($errors, 'CANNOT DELETE - NO SUCH PLUGIN'); + break; + } + if (!$this->deltree(PHPWG_PLUGINS_PATH . $plugin_id)) + { + $this->send_to_trash(PHPWG_PLUGINS_PATH . $plugin_id); + } + break; + } + return $errors; + } + + /** + * Returns an array of plugins defined in the plugin directory + */ + function get_fs_plugins() + { + $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]); + } + if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid=')) + { + list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']); + if (is_numeric($extension)) $plugin['extension'] = $extension; + } + // IMPORTANT SECURITY ! + $plugin = array_map('htmlspecialchars', $plugin); + $this->fs_plugins[$file] = $plugin; + } + } + } + closedir($dir); + } + + /** + * sort fs_plugins + */ + function sort_fs_plugins() + { + switch ($this->order) + { + case 'status': + $this->sort_plugins_by_state(); + break; + case 'author': + uasort($this->fs_plugins, array($this, 'plugin_author_compare')); + break; + case 'id': + uksort($this->fs_plugins, 'strcasecmp'); + break; + default: //sort by plugin name + uasort($this->fs_plugins, 'name_compare'); + } + } + + /** + * Retrieve PEM server datas + */ + function check_server_plugins() + { + foreach($this->fs_plugins as $plugin_id => $fs_plugin) + { + if (isset($fs_plugin['extension'])) + { + $plugins_to_check[] = $fs_plugin['extension']; + } + } + $url = PEM_URL . '/uptodate.php?version=' . rawurlencode(PHPWG_VERSION) . '&extensions=' . implode(',', $plugins_to_check); + $url .= $this->page == 'plugins_new' ? '&newext=Plugin' : ''; + + if (!empty($plugins_to_check) and $source = @file_get_contents($url)) + { + $this->server_plugins = @unserialize($source); + switch ($this->order) + { + case 'name': + uasort($this->server_plugins, array($this, 'extension_name_compare')); + break; + case 'author': + uasort($this->server_plugins, array($this, 'extension_author_compare')); + break; + default: // sort by id desc + krsort($this->server_plugins); + } + } + else + { + $this->server_plugins = false; + } + } + + /** + * Upgrade plugin + * @param string - archive URL + * @param string - plugin id + */ + function upgrade($source, $plugin_id) + { + if (isset($this->db_plugins_by_id[$plugin_id]) + and $this->db_plugins_by_id[$plugin_id]['state'] == 'active') + { + $this->perform_action('deactivate', $plugin_id); + + redirect( + $this->my_base_url + . '&upgrade=' . $source + . '&plugin=' . $plugin_id + . '&reactivate=true'); + } + + include(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php'); + $upgrade_status = $this->extract_plugin_files('upgrade', $source, $plugin_id); + + if (isset($_GET['reactivate'])) + { + $this->perform_action('activate', $plugin_id); + } + redirect($this->my_base_url.'&plugin='.$plugin_id.'&upgradestatus='.$upgrade_status); + } + + + /** + * Install plugin + * @param string - archive URL + * @param string - extension id + */ + function install($source, $extension) + { + include(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php'); + $install_status = $this->extract_plugin_files('install', $source, $extension); + + redirect($this->my_base_url.'&installstatus='.$install_status); + } + + + /** + * 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; + } + + /** + * get install or upgrade result + */ + function get_result($result, $plugin='') + { + global $page; + + switch ($result) + { + case 'ok': + if ($this->page == 'plugins_update') + { + array_push($page['infos'], + sprintf( + l10n('plugins_upgrade_ok'), + $this->fs_plugins[$plugin]['name'])); + } + else + { + array_push($page['infos'], + l10n('plugins_install_ok'), + l10n('plugins_install_need_activate')); + } + break; + + case 'temp_path_error': + array_push($page['errors'], l10n('plugins_temp_path_error')); + break; + + case 'dl_archive_error': + array_push($page['errors'], l10n('plugins_dl_archive_error')); + break; + + case 'archive_error': + array_push($page['errors'], l10n('plugins_archive_error')); + break; + + default: + array_push($page['errors'], + sprintf(l10n('plugins_extract_error'), $result), + l10n('plugins_check_chmod')); + } + } + + /** + * 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 + */ + function plugin_version_compare($a, $b) + { + $r = version_compare($a['version'], $b['version']); + if ($r == 0) return strcasecmp($a['version'], $b['version']); + else return $r; + } + + function extension_name_compare($a, $b) + { + return strcmp(strtolower($a['ext_name']), strtolower($b['ext_name'])); + } + + function extension_author_compare($a, $b) + { + $r = strcasecmp($a['author'], $b['author']); + if ($r == 0) return $this->extension_name_compare($a, $b); + else return $r; + } + + function plugin_author_compare($a, $b) + { + $r = strcasecmp($a['author'], $b['author']); + if ($r == 0) return name_compare($a, $b); + else return $r; + } + + function sort_plugins_by_state() + { + uasort($this->fs_plugins, 'name_compare'); + + $active_plugins = array(); + $inactive_plugins = array(); + $not_installed = array(); + + foreach($this->fs_plugins as $plugin_id => $plugin) + { + if (isset($this->db_plugins_by_id[$plugin_id])) + { + $this->db_plugins_by_id[$plugin_id]['state'] == 'active' ? + $active_plugins[$plugin_id] = $plugin : $inactive_plugins[$plugin_id] = $plugin; + } + else + { + $not_installed[$plugin_id] = $plugin; + } + } + $this->fs_plugins = $active_plugins + $inactive_plugins + $not_installed; + } +} +?> \ No newline at end of file diff --git a/admin/plugins.php b/admin/plugins.php deleted file mode 100644 index 2206be228..000000000 --- a/admin/plugins.php +++ /dev/null @@ -1,66 +0,0 @@ -add('list', l10n('plugins_tab_list'), $tab_link.'list'); -$tabsheet->add('update', l10n('plugins_tab_update'), $tab_link.'update'); -$tabsheet->add('new', l10n('plugins_tab_new'), $tab_link.'new'); -// TabSheet selection -$tabsheet->select($page['section']); -// Assign tabsheet to template -$tabsheet->assign(); - - -// +-----------------------------------------------------------------------+ -// | start template output | -// +-----------------------------------------------------------------------+ - -include(PHPWG_ROOT_PATH.'admin/plugins_'.$page['section'].'.php'); -?> \ No newline at end of file diff --git a/admin/plugins_list.php b/admin/plugins_list.php index 5c333968f..90a8ef722 100644 --- a/admin/plugins_list.php +++ b/admin/plugins_list.php @@ -1,7 +1,7 @@ set_filenames(array('plugins' => 'admin/plugins_list.tpl')); +$order = isset($_GET['order']) ? $_GET['order'] : 'name'; +$plugins = new plugins($page['page'], $order); -$fs_plugins = get_fs_plugins(); -$db_plugins = get_db_plugins(); -$db_plugins_by_id = array(); -foreach ($db_plugins as $db_plugin) +//--------------------------------------------------perform requested actions +if (isset($_GET['action']) and isset($_GET['plugin']) and !is_adviser()) { - $db_plugins_by_id[$db_plugin['id']] = $db_plugin; + $page['errors'] = + $plugins->perform_action($_GET['action'], $_GET['plugin'], $page['errors']); + + if (empty($page['errors'])) redirect($plugins->my_base_url); } - // +-----------------------------------------------------------------------+ // | start template output | // +-----------------------------------------------------------------------+ - -$template->set_filenames(array('plugins' => 'admin/plugins_list.tpl')); - -$base_url = get_root_url().'admin.php'; - -//----------------------------------------------------------------sort options -$selected_order = isset($_GET['order']) ? $_GET['order'] : 'name'; - -$url = $base_url.get_query_string_diff( array('action', 'plugin', 'order')); - -$template->assign('order', - array( - $url.'&order=name' => l10n('Name'), - $url.'&order=status' => l10n('Status'), - $url.'&order=author' => l10n('Author'), - $url.'&order=id' => 'Id', - ) - ); - -$template->assign('selected', $url.'&order='.$selected_order); - -switch ($selected_order) -{ - case 'name': - uasort($fs_plugins, 'name_compare'); - break; - case 'id': - uksort($fs_plugins, 'strcasecmp'); - break; - case 'author': - uasort($fs_plugins, 'plugin_author_compare'); - break; - case 'status': - $fs_plugins = sort_plugins_by_state($fs_plugins, $db_plugins_by_id); - break; -} - - -//--------------------------------------------------------------display plugins - -$url = $base_url.get_query_string_diff( array('action', 'plugin') ); - -foreach($fs_plugins as $plugin_id => $fs_plugin) +$plugins->tabsheet(); +$plugins->sort_fs_plugins(); +$plugins->set_order_options(array( + 'name' => l10n('Name'), + 'status' => l10n('Status'), + 'author' => l10n('Author'), + 'id' => 'Id')); + +foreach($plugins->fs_plugins as $plugin_id => $fs_plugin) { $display_name = $fs_plugin['name']; if (!empty($fs_plugin['uri'])) @@ -214,11 +84,11 @@ foreach($fs_plugins as $plugin_id => $fs_plugin) 'VERSION' => $fs_plugin['version'], 'DESCRIPTION' => $desc); - $action_url = $url.'&plugin='.$plugin_id; + $action_url = $plugins->html_base_url . '&plugin=' . $plugin_id; - if (isset($db_plugins_by_id[$plugin_id])) - { - switch ($db_plugins_by_id[$plugin_id]['state']) + if (isset($plugins->db_plugins_by_id[$plugin_id])) + { + switch ($plugins->db_plugins_by_id[$plugin_id]['state']) { case 'active': $tpl_plugin['actions'][] = @@ -245,24 +115,24 @@ foreach($fs_plugins as $plugin_id => $fs_plugin) 'CONFIRM' => l10n('Are you sure?')); $tpl_plugin['actions'][] = array('U_ACTION' => $action_url . '&action=delete', - 'L_ACTION' => l10n('plugins_delete'), - 'CONFIRM' => l10n('plugins_confirm_delete')); + 'L_ACTION' => l10n('plugins_delete'), + 'CONFIRM' => l10n('plugins_confirm_delete')); } $template->append('plugins', $tpl_plugin); } $missing_plugin_ids = array_diff( - array_keys($db_plugins_by_id), array_keys($fs_plugins) + array_keys($plugins->db_plugins_by_id), array_keys($plugins->fs_plugins) ); foreach($missing_plugin_ids as $plugin_id) { - $action_url = $url.'&plugin='.$plugin_id; + $action_url = $plugins->html_base_url.'&plugin='.$plugin_id; $template->append( 'plugins', array( 'NAME' => $plugin_id, - 'VERSION' => $db_plugins_by_id[$plugin_id]['version'], + 'VERSION' => $plugins->db_plugins_by_id[$plugin_id]['version'], 'DESCRIPTION' => "ERROR: THIS PLUGIN IS MISSING BUT IT IS INSTALLED! UNINSTALL IT NOW !", 'actions' => array ( array ( 'U_ACTION' => $action_url . '&action=uninstall', diff --git a/admin/plugins_new.php b/admin/plugins_new.php index 8720228f6..61e03893c 100644 --- a/admin/plugins_new.php +++ b/admin/plugins_new.php @@ -1,7 +1,7 @@ set_filenames(array('plugins' => 'admin/plugins_new.tpl')); +$order = isset($_GET['order']) ? $_GET['order'] : 'date'; + +$plugins = new plugins($page['page'], $order); //------------------------------------------------------automatic installation if (isset($_GET['install']) and isset($_GET['extension']) and !is_adviser()) { - include(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php'); - - $install_status = extract_plugin_files('install', - $_GET['install'], - $_GET['extension']); - - redirect($my_base_url . '&installstatus=' . $install_status); + $plugins->install($_GET['install'], $_GET['extension']); } - //--------------------------------------------------------------install result if (isset($_GET['installstatus'])) { - switch ($_GET['installstatus']) - { - case 'ok': - array_push($page['infos'], l10n('plugins_install_ok'), l10n('plugins_install_need_activate')); - break; - - case 'temp_path_error': - array_push($page['errors'], l10n('plugins_temp_path_error')); - break; - - case 'dl_archive_error': - array_push($page['errors'], l10n('plugins_dl_archive_error')); - break; - - case 'archive_error': - array_push($page['errors'], l10n('plugins_archive_error')); - break; - - default: - array_push($page['errors'], sprintf(l10n('plugins_extract_error'), $_GET['installstatus']), l10n('plugins_check_chmod')); - } + $plugins->get_result($_GET['installstatus']); } - -//----------------------------------------------------------------sort options -$order = isset($_GET['order']) ? $_GET['order'] : 'date'; - -$template->assign('order', - array($my_base_url.'&order=date' => l10n('Post date'), - $my_base_url.'&order=name' => l10n('Name'), - $my_base_url.'&order=author' => l10n('Author'))); - -$template->assign('selected', $my_base_url.'&order='.$order); - - // +-----------------------------------------------------------------------+ // | start template output | // +-----------------------------------------------------------------------+ -$plugins_infos = check_server_plugins($fs_plugins, true); -if ($plugins_infos !== false) +$plugins->tabsheet(); +$plugins->check_server_plugins(); +$plugins->set_order_options(array( + 'date' => l10n('Post date'), + 'name' => l10n('Name'), + 'author' => l10n('Author'))); + +if ($plugins->server_plugins !== false) { - if ($order == 'date') krsort($plugins_infos); - else uasort($plugins_infos, 'extension_'.$order.'_compare'); - - foreach($plugins_infos as $plugin) + foreach($plugins->server_plugins as $plugin) { $ext_desc = nl2br(htmlspecialchars(strip_tags( utf8_encode($plugin['ext_description'])))); @@ -107,15 +71,15 @@ if ($plugins_infos !== false) nl2br(htmlspecialchars(strip_tags( utf8_encode($plugin['description']))))); - $url_auto_install = $my_base_url - . '&extension=' . $plugin['id_extension'] - . '&install=%2Fupload%2Fextension-' . $plugin['id_extension'] - . '%2Frevision-' . $plugin['id_revision'] . '%2F' - . str_replace(' ', '%20',$plugin['url']); + $url_auto_install = $plugins->html_base_url + . '&extension=' . $plugin['id_extension'] + . '&install=%2Fupload%2Fextension-' . $plugin['id_extension'] + . '%2Frevision-' . $plugin['id_revision'] . '%2F' + . str_replace(' ', '%20',$plugin['url']); $url_download = PEM_URL .'/upload/extension-'.$plugin['id_extension'] - . '/revision-' . $plugin['id_revision'] - . '/' . $plugin['url']; + . '/revision-' . $plugin['id_revision'] + . '/' . str_replace(' ', '%20',$plugin['url']); $template->append('plugins', array('EXT_NAME' => $plugin['ext_name'], diff --git a/admin/plugins_update.php b/admin/plugins_update.php index 2799fa89b..0cf10e188 100644 --- a/admin/plugins_update.php +++ b/admin/plugins_update.php @@ -1,7 +1,7 @@ set_filenames(array('plugins' => 'admin/plugins_update.tpl')); +$plugins = new plugins($page['page']); //-----------------------------------------------------------automatic upgrade if (isset($_GET['upgrade']) and isset($_GET['plugin']) and !is_adviser()) { - include(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php'); - - $upgrade_status = extract_plugin_files('upgrade', - $_GET['upgrade'], - $_GET['plugin']); - - $my_base_url .= isset($_GET['reactivate']) ? '&action=activate' : ''; - - redirect($my_base_url.'&plugin='.$_GET['plugin'].'&upgradestatus='.$upgrade_status); + $plugins->upgrade($_GET['upgrade'], $_GET['plugin']); } - //--------------------------------------------------------------upgrade result if (isset($_GET['upgradestatus']) and isset($_GET['plugin'])) { - switch ($_GET['upgradestatus']) - { - case 'ok': - array_push($page['infos'], - sprintf(l10n('plugins_upgrade_ok'), - $fs_plugins[$_GET['plugin']]['name'])); - break; - - case 'temp_path_error': - array_push($page['errors'], l10n('plugins_temp_path_error')); - break; - - case 'dl_archive_error': - array_push($page['errors'], l10n('plugins_dl_archive_error')); - break; - - case 'archive_error': - array_push($page['errors'], l10n('plugins_archive_error')); - break; - - default: - array_push($page['errors'], - sprintf(l10n('plugins_extract_error'), - $_GET['upgradestatus'])); - } + $plugins->get_result($_GET['upgradestatus'], $_GET['plugin']); } - // +-----------------------------------------------------------------------+ // | start template output | // +-----------------------------------------------------------------------+ -$plugins_infos = check_server_plugins($fs_plugins); +$plugins->tabsheet(); +$plugins->check_server_plugins(); -if ($plugins_infos !== false) +if ($plugins->server_plugins !== false) { - foreach($fs_plugins as $plugin_id => $fs_plugin) + foreach($plugins->fs_plugins as $plugin_id => $fs_plugin) { if (isset($fs_plugin['extension']) - and isset($plugins_infos[$fs_plugin['extension']])) + and isset($plugins->server_plugins[$fs_plugin['extension']])) { - $plugin_info = $plugins_infos[$fs_plugin['extension']]; + $plugin_info = $plugins->server_plugins[$fs_plugin['extension']]; $ext_desc = nl2br(htmlspecialchars(strip_tags( utf8_encode($plugin_info['ext_description'])))); $ver_desc = sprintf(l10n('plugins_description'), - $plugin_info['version'], - date('Y-m-d', $plugin_info['date']), - nl2br(htmlspecialchars(strip_tags( - utf8_encode($plugin_info['description']))))); + $plugin_info['version'], + date('Y-m-d', $plugin_info['date']), + nl2br(htmlspecialchars(strip_tags( + utf8_encode($plugin_info['description']))))); - if ($plugin_info['version'] == $fs_plugin['version']) + if ($plugins->plugin_version_compare($fs_plugin, $plugin_info) >= 0) { // Plugin is up to date $template->append('plugins_uptodate', array('URL' => $fs_plugin['uri'], 'NAME' => $fs_plugin['name'], 'EXT_DESC' => $ext_desc, - 'VERSION' => $fs_plugin['version'], - 'VER_DESC' => $ver_desc)); + 'VERSION' => $fs_plugin['version'])); } else { // Plugin need upgrade - $url_auto_update = $my_base_url + $url_auto_update = $plugins->html_base_url . '&plugin=' . $plugin_id - . ( - (isset($db_plugins_by_id[$plugin_id]) - and $db_plugins_by_id[$plugin_id]['state'] == 'active') ? - '&action=deactivate' : '' - ) . '&upgrade=%2Fupload%2Fextension-' . $fs_plugin['extension'] . '%2Frevision-' . $plugin_info['id_revision'] - . '%2F' . $plugin_info['url']; + . '%2F' . str_replace(' ', '%20',$plugin_info['url']); $url_download = PEM_URL.'/upload/extension-'. $fs_plugin['extension'] - . '/revision-' . $plugin_info['id_revision'] - . '/' . $plugin_info['url']; + . '/revision-' . $plugin_info['id_revision'] + . '/' . str_replace(' ', '%20',$plugin_info['url']); $template->append('plugins_not_uptodate', array('EXT_NAME' => $fs_plugin['name'], diff --git a/template/yoga/admin/plugins_list.tpl b/template/yoga/admin/plugins_list.tpl index 89b57d448..767a0924e 100644 --- a/template/yoga/admin/plugins_list.tpl +++ b/template/yoga/admin/plugins_list.tpl @@ -5,7 +5,7 @@ {'Sort order'|@translate} : {if isset($plugins)} diff --git a/template/yoga/admin/plugins_new.tpl b/template/yoga/admin/plugins_new.tpl index bf0081703..90167e2e9 100644 --- a/template/yoga/admin/plugins_new.tpl +++ b/template/yoga/admin/plugins_new.tpl @@ -5,7 +5,7 @@ {'Sort order'|@translate} : {if isset($plugins)} diff --git a/template/yoga/admin/plugins_update.tpl b/template/yoga/admin/plugins_update.tpl index ca1ca541e..9abc32be1 100644 --- a/template/yoga/admin/plugins_update.tpl +++ b/template/yoga/admin/plugins_update.tpl @@ -44,7 +44,7 @@ {$plugin.NAME} {$plugin.EXT_DESC} - {$plugin.VERSION}{$plugin.VER_DESC} + {$plugin.VERSION} {/foreach} -- cgit v1.2.3