|
| 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 | +} |
0 commit comments