Skip to content

Commit 9a9fa3f

Browse files
committed
Begin plugins management
1 parent f8d01f8 commit 9a9fa3f

File tree

14 files changed

+544
-62
lines changed

14 files changed

+544
-62
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ include/config.php
44
nbproject/
55
lang/French
66
.htaccess
7+
vendor
8+
composer.lock

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "featherbb/featherbb",
3+
"type": "project",
4+
"license": "GNU General Public Licence",
5+
"description": "A multi-framework Composer library installer",
6+
"keywords": ["forum", "lightweight", "featherbb"],
7+
"homepage": "http://featherbb.github.com/installers/",
8+
"authors": [
9+
{"name": "adaur"},
10+
{"name": "capkokoon"},
11+
{"name": "beaver"}
12+
],
13+
"autoload": {
14+
"classmap": ["plugins", "include/classes"]
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "4.1.*"
18+
}
19+
}

controller/admin/plugins.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct()
2020
$this->request = $this->feather->request;
2121
$this->header = new \controller\header();
2222
$this->footer = new \controller\footer();
23-
$this->model = new \model\admin\plugins();
23+
// $this->model = new \model\admin\plugins();
2424
require FEATHER_ROOT . 'include/common_admin.php';
2525
}
2626

@@ -29,6 +29,66 @@ public function __autoload($class_name)
2929
require FEATHER_ROOT . $class_name . '.php';
3030
}
3131

32+
public function index()
33+
{
34+
if (!$this->user->is_admmod) {
35+
message(__('No permission'), '403');
36+
}
37+
38+
// Update permissions
39+
// if ($this->feather->request->isPost()) {
40+
// $this->model->update_permissions();
41+
// }
42+
43+
$page_title = array(feather_escape($this->config['o_board_title']), __('Admin'), __('Permissions'));
44+
45+
$this->header->setTitle($page_title)->setActivePage('admin')->enableAdminConsole()->display();
46+
47+
generate_admin_menu('plugins');
48+
49+
$pluginsList = \FeatherBB\Plugin::getPluginsList();
50+
51+
$this->feather->render('admin/plugins.php', array(
52+
'pluginsList' => $pluginsList
53+
)
54+
);
55+
56+
$this->footer->display();
57+
}
58+
59+
public function activate()
60+
{
61+
if (!$this->user->is_admmod) {
62+
message(__('No permission'), '403');
63+
}
64+
65+
// Update permissions
66+
// if ($this->feather->request->isPost()) {
67+
// $this->model->update_permissions();
68+
// }
69+
70+
// The plugin to load should be supplied via GET
71+
$plugin = $this->request->get('plugin') ? $this->request->get('plugin') : null;
72+
if (!$plugin) {
73+
message(__('Bad request'), '404');
74+
}
75+
76+
// Require all valide filenames...
77+
\FeatherBB\Plugin::getPluginsList();
78+
// And make sure the plugin actually extends base Plugin class
79+
if (!property_exists('\plugin\\'.$plugin, 'isFeatherPlugin')) {
80+
message(sprintf(__('No plugin message'), $plugin));
81+
}
82+
83+
try {
84+
\FeatherBB\Plugin::activate($plugin);
85+
redirect(get_link('admin/plugins/'), "Plugin $plugin activated");
86+
} catch (\Exception $e) {
87+
redirect(get_link('admin/plugins/'), $e->getMessage());
88+
}
89+
90+
}
91+
3292
public function display()
3393
{
3494
if (!$this->user->is_admmod) {

include/classes/core.class.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(array $data)
4646
require $this->forum_env['FEATHER_ROOT'].'include/classes/pomo/MO.php';
4747
require $this->forum_env['FEATHER_ROOT'].'include/l10n.php';
4848
require $this->forum_env['FEATHER_ROOT'].'include/classes/database.class.php';
49-
require $this->forum_env['FEATHER_ROOT'].'plugins/test/plugintest.php';
49+
// require $this->forum_env['FEATHER_ROOT'].'plugins/test/plugintest.php';
5050

5151
// Force POSIX locale (to prevent functions such as strtolower() from messing up UTF-8 strings)
5252
setlocale(LC_CTYPE, 'C');
@@ -244,7 +244,8 @@ public function call()
244244
$this->app->config = $this->forum_settings; // Legacy
245245
extract($this->forum_settings); // Legacy
246246

247-
new \plugin\plugintest();
247+
// new \plugin\plugintest();
248+
\FeatherBB\Plugin::runActivePlugins();
248249

249250
// Define time formats
250251
$forum_time_formats = array($this->forum_settings['o_time_format'], 'H:i:s', 'H:i', 'g:i:s a', 'g:i a');

include/classes/plugin.class.php

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<?php
2+
3+
/**
4+
* Copyright (C) 2015 FeatherBB
5+
* based on code by (C) 2008-2012 FluxBB
6+
* and Rickard Andersson (C) 2002-2008 PunBB
7+
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher.
8+
*/
9+
10+
namespace FeatherBB;
11+
12+
use DB;
13+
14+
class Plugin
15+
{
16+
17+
/**
18+
* @var object
19+
*/
20+
protected $feather;
21+
22+
/**
23+
* @var object
24+
*/
25+
protected $hooks;
26+
27+
/**
28+
* Ensure daughter plugin classes (users ones) are extending
29+
* this default class
30+
*
31+
* @var boolval
32+
*/
33+
protected $isFeatherPlugin;
34+
35+
/**
36+
* @var string
37+
*/
38+
protected $name;
39+
40+
/**
41+
* @var string
42+
*/
43+
protected $version;
44+
45+
/**
46+
* @var string
47+
*/
48+
protected $author;
49+
50+
/**
51+
* @var string
52+
*/
53+
protected $description;
54+
55+
/**
56+
* @var string
57+
*/
58+
protected $updateUrl;
59+
60+
/**
61+
* Structure of database table to create.
62+
*
63+
* @var array
64+
*/
65+
protected $databaseScheme = array();
66+
67+
/**
68+
* Constructor
69+
*/
70+
public function __construct()
71+
{
72+
$this->feather = \Slim\Slim::getInstance();
73+
$this->user = $this->feather->user;
74+
$this->hook = $this->feather->hooks;
75+
}
76+
77+
/**
78+
* Get a list of all available plugins.
79+
*/
80+
public static function getPluginsList()
81+
{
82+
$plugins = array();
83+
84+
// Get all files declaring a new plugin
85+
$directory = new \RecursiveDirectoryIterator('plugins');
86+
$iterator = new \RecursiveIteratorIterator($directory);
87+
$pluginClasses = new \RegexIterator($iterator, '/^.+\/(\\w+)\.plugin\.php$/i', \RecursiveRegexIterator::GET_MATCH);
88+
89+
// Require these files to access their properties and display them in view
90+
foreach ($pluginClasses as $pluginClass) {
91+
require $pluginClass[0];
92+
$className = "\plugin\\".$pluginClass[1];
93+
$plugins[$pluginClass[1]] = new $className();
94+
}
95+
96+
return $plugins;
97+
}
98+
99+
/**
100+
* Execute hooks of activated plugins
101+
*/
102+
public static function runActivePlugins()
103+
{
104+
// Get all files declaring a new plugin
105+
$directory = new \RecursiveDirectoryIterator('plugins');
106+
$iterator = new \RecursiveIteratorIterator($directory);
107+
$pluginClasses = new \RegexIterator($iterator, '/^.+\/(\\w+)\.plugin\.php$/i', \RecursiveRegexIterator::GET_MATCH);
108+
109+
$feather = \Slim\Slim::getInstance();
110+
$activePlugins = $feather->cache->retrieve('active_plugins') ? $feather->cache->retrieve('active_plugins') : array();
111+
112+
// Require these files to access their properties and display them in view
113+
foreach ($pluginClasses as $pluginClass) {
114+
require $pluginClass[0];
115+
$className = "\plugin\\".$pluginClass[1];
116+
$plugin = new $className();
117+
$plugin->runHooks();
118+
}
119+
120+
return true;
121+
}
122+
123+
/**
124+
* Activate a plugin based on class name
125+
*/
126+
public static function activate($pluginName)
127+
{
128+
// Instantiate the plugin
129+
$className = "\plugin\\".$pluginName;
130+
$plugin = new $className;
131+
132+
// TODO: Allow creation of new tables in database
133+
// \FeatherBB\Core::init_db($data);;
134+
// $feather = \Slim\Slim::getInstance();
135+
// // Handle db prefix
136+
// $db_prefix = $feather->forum_settings['db_prefix']
137+
// // Create tables
138+
// foreach ($this->getDatabaseScheme() as $table => $sql) {
139+
// if (!$this->createTable($db_prefix.$table, $sql)) {
140+
// // Error handling
141+
// $this->errors[] = 'A problem was encountered while creating table '.$table;
142+
// }
143+
// }
144+
// // Populate tables with default values
145+
// foreach ($this->loadMockData() as $group_name => $group_data) {
146+
// $this->model->addData('groups', $group_data);
147+
// }
148+
149+
$feather = \Slim\Slim::getInstance();
150+
$activePlugins = $feather->cache->isCached('active_plugins') ? $feather->cache->retrieve('active_plugins') : array();
151+
152+
// $feather->cache->delete('active_plugins');
153+
// Check if plugin isn't already activated
154+
if (!array_key_exists($pluginName, $activePlugins)) {
155+
$activePlugins[$pluginName] = true;
156+
$feather->cache->store('active_plugins', $activePlugins);
157+
return true;
158+
}
159+
throw new \Exception("Plugin $pluginName already activated", 1);
160+
}
161+
162+
protected function runHooks()
163+
{
164+
// Daughter classes may configure hooks in this function
165+
}
166+
167+
/**
168+
* Getters
169+
*/
170+
171+
public function getName()
172+
{
173+
return $this->name;
174+
}
175+
176+
public function getVersion()
177+
{
178+
return $this->version;
179+
}
180+
181+
public function getAuthor()
182+
{
183+
return $this->author;
184+
}
185+
186+
public function getDescription()
187+
{
188+
return $this->description;
189+
}
190+
191+
protected function getDatabaseScheme()
192+
{
193+
return $this->databaseScheme;
194+
}
195+
196+
197+
protected function createTable($table_name, $sql)
198+
{
199+
$db = DB::get_db();
200+
$req = preg_replace('/%t%/', '`'.$table_name.'`', $sql);
201+
return $db->exec($req);
202+
}
203+
204+
// protected function addData($table_name, array $data)
205+
// {
206+
// return (bool) DB::for_table($table_name)
207+
// ->create()
208+
// ->set($data)
209+
// ->save();
210+
// }
211+
212+
213+
}

include/routes.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@
143143
});
144144

145145
// Admin plugins
146-
$feather->map('/loader(/)', '\controller\admin\plugins:display')->via('GET', 'POST');
146+
$feather->group('/plugins', function() use ($feather) {
147+
$feather->map('/(/)', '\controller\admin\plugins:index')->via('GET', 'POST');
148+
$feather->map('/activate(/)', '\controller\admin\plugins:activate')->via('GET');
149+
// $feather->map('/loader(/)', '\controller\admin\plugins:display')->via('GET', 'POST');
150+
});
147151

148152
// Admin maintenance
149153
$feather->map('/maintenance(/)', '\controller\admin\maintenance:display')->via('GET', 'POST');

index.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
require 'Slim/Slim.php';
1818
\Slim\Slim::registerAutoloader();
1919

20+
// Load dependencies
21+
require 'vendor/autoload.php';
22+
2023
// Load FeatherBB
21-
require 'include/classes/autoload.class.php';
22-
\FeatherBB\Loader::registerAutoloader();
24+
// require 'include/classes/autoload.class.php';
25+
// \FeatherBB\Loader::registerAutoloader();
2326

2427
// Instantiate Slim and add CSRF
2528
$feather = new \Slim\Slim();

0 commit comments

Comments
 (0)