Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions Plugins/Test.php

This file was deleted.

60 changes: 20 additions & 40 deletions featherbb/Controller/Admin/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

namespace FeatherBB\Controller\Admin;

use FeatherBB\Core\Lister;
use FeatherBB\Core\Error;
use FeatherBB\Core\Url;
use FeatherBB\Core\Utils;
use FeatherBB\Core\Url;
use FeatherBB\Core\Plugin as PluginManager;

class Plugins
{
Expand All @@ -22,72 +24,50 @@ public function __construct()
$this->config = $this->feather->config;
$this->user = $this->feather->user;
$this->request = $this->feather->request;
load_textdomain('featherbb', $this->feather->forum_env['FEATHER_ROOT'].'featherbb/lang/'.$this->user->language.'/admin/plugins.mo');
}

public function index()
{
// Update permissions
// if ($this->feather->request->isPost()) {
// $this->model->update_permissions();
// }

// AdminUtils::generateAdminMenu('plugins');
$this->feather->template->addAsset('js', 'style/imports/common.js', array('type' => 'text/javascript'));

$pluginsList = \FeatherBB\Core\Lister::getValidPlugins();
// var_dump($pluginsList);
$plugins = Lister::getPlugins();
// var_dump($plugins);
$activePlugins = $this->feather->cache->isCached('active_plugins') ? $this->feather->cache->retrieve('active_plugins') : array();
// var_dump($activePlugins);
// $this->feather->cache->delete('active_plugins');

$this->feather->template->setPageInfo(array(
'admin_console' => true,
'active_page' => 'admin',
'pluginsList' => $pluginsList,
'plugins' => $plugins,
'activePlugins' => $activePlugins,
'title' => array(Utils::escape($this->config['o_board_title']), __('Admin'), 'Plugins'),
'title' => array(Utils::escape($this->config['o_board_title']), __('Admin'), __('Extension')),
)
)->addTemplate('admin/plugins.php')->display();
}

public function activate($pluginName = null)
public function activate($plugin = null)
{
// The plugin to load should be supplied via GET
// $pluginName = $this->request->get('plugin') ? $this->request->get('plugin') : null;
if (!$pluginName) {
throw new Error(__('Bad request'), 400);
}
if (!$plugin) throw new Error(__('Bad request'), 400);

// Check if plugin follows PSR-4 conventions and extends base forum plugin
if (!class_exists($pluginName) || !property_exists($pluginName, 'isValidFBPlugin')) {
throw new Error(sprintf(__('No plugin message'), Utils::escape($pluginName)), 400);
}
$manager = new PluginManager();
$manager->activate($plugin);

$plugin = new $pluginName;
try {
$plugin->activate($pluginName);
} catch (\Exception $e) {
Url::redirect($this->feather->urlFor('adminPlugins'), $e->getMessage());
}
// Plugin has been activated, confirm and redirect
Url::redirect($this->feather->urlFor('adminPlugins'), 'Plugin "'.$pluginName::$name.'" activated!');
Url::redirect($this->feather->urlFor('adminPlugins'), 'Plugin activated!');
}

public function deactivate()
public function deactivate($plugin = null)
{
// The plugin to load should be supplied via GET
$class = $this->request->get('plugin') ? $this->request->get('plugin') : null;
if (!$class) {
throw new Error(__('Bad request'), 400);
}
if (!$plugin) throw new Error(__('Bad request'), 400);

$manager = new PluginManager();
$manager->deactivate($plugin);

$plugin = new $class;
try {
$plugin->deactivate($class);
} catch (\Exception $e) {
Url::redirect($this->feather->urlFor('adminPlugins'), $this->feather->utils->escape($e->getMessage()));
}
// Plugin has been activated, confirm and redirect
Url::redirect($this->feather->urlFor('adminPlugins'), array('warning', 'Plugin "'.$class::$name.'" deactivated!'));
Url::redirect($this->feather->urlFor('adminPlugins'), array('warning', 'Plugin deactivated!'));
}

public function display()
Expand Down
49 changes: 8 additions & 41 deletions featherbb/Core/Lister.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,21 @@

class Lister
{
/**
* Get all php files in plugin folder.
*/
public static function getPluginFiles($folder = '')
{
$plugin_files = array();
$feather = \Slim\Slim::getInstance();

$plugins_dir = new \RecursiveDirectoryIterator($feather->forum_env['FEATHER_ROOT'].'plugins/'.$folder);
$iterator = new \RecursiveIteratorIterator($plugins_dir);
$iterator->setMaxDepth(1);
$php_files = new \RegexIterator($iterator, '/.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);

foreach ($php_files as $file) {
$plugin_files[] = $file[0];
}

return $plugin_files;
}

/**
* Get all valid plugin files.
*/
public static function getValidPlugins($folder = '')
public static function getPlugins()
{
$valid_plugins = array();

$plugin_files = self::getPluginFiles($folder);

$plugins = array();
$feather = \Slim\Slim::getInstance();
$feather_root = $feather->forum_env['FEATHER_ROOT'];

foreach ($plugin_files as $key => $file_path) {
// Remove forum base path
$relative_path = DIRECTORY_SEPARATOR.preg_replace("/" . preg_quote($feather_root, "/") . "/", '', $file_path);
preg_match('/^(.+)\.php$/i', $relative_path, $class_name);
$parts = explode(DIRECTORY_SEPARATOR, $class_name[1]);
$parts[1] = ucfirst($parts[1]); // Replace \plugins to \Core\Plugins for convention
$name_space = join($parts, "\\");
// Check if plugin follows PSR-4 conventions and extends base forum plugin
if (class_exists($name_space) && property_exists($name_space, 'isValidFBPlugin')) {
$class = new $name_space;
$valid_plugins[end($parts)] = $name_space;
}
}
foreach (glob($feather->forum_env['FEATHER_ROOT'].'plugins/*/featherbb.json') as $plugin_file)
{
$plugins[] = json_decode(file_get_contents($plugin_file));
}

ksort($valid_plugins);
return $valid_plugins;
natcasesort($plugins);
return $plugins;
}

/**
Expand Down
Loading