Files
Kleeja/includes/plugins.php

261 lines
6.8 KiB
PHP
Raw Permalink Normal View History

2018-01-09 02:09:07 +03:00
<?php
/**
*
* @package Kleeja
2020-04-11 22:45:48 +02:00
* @copyright (c) 2007 Kleeja.net
* @license http://www.kleeja.net/license
2018-01-09 02:09:07 +03:00
*
*/
2019-05-19 02:40:43 +03:00
2018-01-09 02:09:07 +03:00
//no for directly open
2019-05-03 23:52:08 +03:00
if (! defined('IN_COMMON'))
2018-01-09 02:09:07 +03:00
{
exit();
}
2019-05-03 23:52:08 +03:00
// We are in the plugin system, plugins files won't work outside here
2018-01-09 02:09:07 +03:00
define('IN_PLUGINS_SYSTEM', true);
/**
* Kleeja Plugins System
* @package plugins
*/
class Plugins
{
/**
* List of loaded plugins
*/
2019-05-03 23:52:08 +03:00
private $plugins = [];
2018-01-09 02:09:07 +03:00
/**
* All hooks from all plugins listed in this variable
*/
2019-05-03 23:52:08 +03:00
private $all_plugins_hooks = [];
private $installed_plugins = [];
private $installed_plugins_info = [];
2018-01-09 02:09:07 +03:00
2019-05-08 02:07:47 +03:00
private $plugin_path = PATH . 'plugins';
2018-01-09 02:09:07 +03:00
private static $instance;
/**
* Initiating the class
*/
public function __construct()
{
global $SQL, $dbprefix;
2019-05-03 23:52:08 +03:00
//if plugins system is turned off, then stop right now!
2018-01-09 02:09:07 +03:00
if (defined('STOP_PLUGINS'))
{
return;
}
2019-05-08 02:07:47 +03:00
if (defined('KLEEJA_PLUGINS_FOLDER'))
{
$this->plugin_path = PATH . KLEEJA_PLUGINS_FOLDER;
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
// Get installed plugins
$query = [
'SELECT' => 'plg_name, plg_ver',
'FROM' => "{$dbprefix}plugins",
'WHERE' => 'plg_disabled = 0'
];
2018-01-09 02:09:07 +03:00
$result = $SQL->build($query);
while ($row = $SQL->fetch($result))
{
$this->installed_plugins[$row['plg_name']] = $row['plg_ver'];
}
2019-05-30 07:32:17 +03:00
$SQL->freeresult($result);
2018-01-09 02:09:07 +03:00
$this->load_enabled_plugins();
}
/**
* Load the plugins from root/plugins folder
2019-05-04 00:36:05 +03:00
* @return void
2018-01-09 02:09:07 +03:00
*/
private function load_enabled_plugins()
{
$dh = opendir($this->plugin_path);
2019-05-08 02:07:47 +03:00
while ($dh !== false and false !== ($folder_name = readdir($dh)))
2018-01-09 02:09:07 +03:00
{
if (is_dir($this->plugin_path . '/' . $folder_name) && preg_match('/[a-z0-9_.]{3,}/', $folder_name))
{
2019-05-03 23:52:08 +03:00
if (! empty($this->installed_plugins[$folder_name]))
2018-01-09 02:09:07 +03:00
{
if ($this->fetch_plugin($folder_name))
{
array_push($this->plugins, $folder_name);
}
}
}
}
2019-05-03 23:52:08 +03:00
//sort the plugins from high to low priority
2018-01-09 02:09:07 +03:00
krsort($this->plugins);
}
/**
* Get the plugin information and other things
2019-05-03 23:52:08 +03:00
* @param string $plugin_name
2018-01-09 02:09:07 +03:00
* @return bool
*/
private function fetch_plugin($plugin_name)
{
2019-05-03 23:52:08 +03:00
//load the plugin
2018-01-09 02:09:07 +03:00
@include_once $this->plugin_path . '/' . $plugin_name . '/init.php';
if (empty($kleeja_plugin))
{
return false;
}
2019-05-03 23:52:08 +03:00
$priority = $kleeja_plugin[$plugin_name]['information']['plugin_priority'];
2018-01-09 02:09:07 +03:00
$this->installed_plugins_info[$plugin_name] = $kleeja_plugin[$plugin_name]['information'];
2019-05-03 23:52:08 +03:00
//bring the real priority of plugin and replace current one
2018-01-09 02:09:07 +03:00
$plugin_current_priority = array_search($plugin_name, $this->plugins);
unset($this->plugins[$plugin_current_priority]);
$this->plugins[$priority] = $plugin_name;
//update plugin if current loaded version is > than installed one
if ($this->installed_plugins[$plugin_name])
2019-05-03 23:52:08 +03:00
{
2018-01-09 02:09:07 +03:00
if (version_compare($this->installed_plugins[$plugin_name], $kleeja_plugin[$plugin_name]['information']['plugin_version'], '<'))
{
if (is_callable($kleeja_plugin[$plugin_name]['update']))
{
global $SQL, $dbprefix;
2019-05-03 23:52:08 +03:00
//update plugin
2018-01-09 02:09:07 +03:00
$kleeja_plugin[$plugin_name]['update']($this->installed_plugins[$plugin_name], $kleeja_plugin[$plugin_name]['information']['plugin_version']);
2019-05-03 23:52:08 +03:00
//update current plugin version
$update_query = [
2018-01-09 02:09:07 +03:00
'UPDATE' => "{$dbprefix}plugins",
2019-05-03 23:52:08 +03:00
'SET' => "plg_ver='" . $SQL->escape($kleeja_plugin[$plugin_name]['information']['plugin_version']) . "'",
'WHERE' => "plg_name='" . $SQL->escape($plugin_name) . "'"
];
2018-01-09 02:09:07 +03:00
$SQL->build($update_query);
}
}
2019-05-03 23:52:08 +03:00
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
//add plugin hooks to global hooks, depend on its priority
if (! empty($kleeja_plugin[$plugin_name]['functions']))
2018-01-09 02:09:07 +03:00
{
foreach ($kleeja_plugin[$plugin_name]['functions'] as $hook_name => $hook_value)
{
if (empty($this->all_plugins_hooks[$hook_name][$priority]))
{
2019-05-03 23:52:08 +03:00
$this->all_plugins_hooks[$hook_name][$priority] = [];
2018-01-09 02:09:07 +03:00
}
array_push($this->all_plugins_hooks[$hook_name][$priority], $hook_value);
krsort($this->all_plugins_hooks[$hook_name]);
}
}
return true;
}
/**
* get an installed plugin information
2019-05-18 01:47:17 +03:00
* @param string $plugin_name
2019-05-04 00:36:05 +03:00
* @return array
2018-01-09 02:09:07 +03:00
*/
public function installed_plugin_info($plugin_name)
{
2019-05-03 23:52:08 +03:00
if (! empty($this->installed_plugins_info[$plugin_name]))
2018-01-09 02:09:07 +03:00
{
return $this->installed_plugins_info[$plugin_name];
}
2019-05-04 00:36:05 +03:00
return [];
2018-01-09 02:09:07 +03:00
}
/**
* Bring all codes of this hook
* This function scattered all over kleeja files
2019-05-18 01:47:17 +03:00
* @param string $hook_name
* @param array $args
2019-05-04 00:36:05 +03:00
* @return array
2018-01-09 02:09:07 +03:00
*/
2019-05-03 23:52:08 +03:00
public function run($hook_name, $args = [])
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
$return_value = $to_be_returned = [];
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if (! empty($this->all_plugins_hooks[$hook_name]))
2018-01-09 02:09:07 +03:00
{
2019-05-04 00:36:05 +03:00
foreach ($this->all_plugins_hooks[$hook_name] as $_ => $functions)
2018-01-09 02:09:07 +03:00
{
foreach ($functions as $function)
{
if (is_callable($function))
{
$return_value = $function($args);
2019-05-03 23:52:08 +03:00
if (is_array($return_value))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
$args = array_merge($args, $return_value);
2018-01-09 02:09:07 +03:00
$to_be_returned = array_merge($to_be_returned, $return_value);
}
}
}
}
}
2019-05-04 00:36:05 +03:00
return sizeof($to_be_returned) ? $to_be_returned : [];
2018-01-09 02:09:07 +03:00
}
2019-05-04 00:36:05 +03:00
/**
* return current instance Plugins class
*
* @return Plugins
*/
2018-01-09 02:09:07 +03:00
public static function getInstance()
{
if (is_null(self::$instance))
{
self::$instance = new self();
}
return self::$instance;
}
/**
* return debug info about plugins system
* @return array
*/
2019-05-03 23:52:08 +03:00
public function getDebugInfo()
{
if (! defined('DEV_STAGE'))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
return [];
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
return [
2018-01-09 02:09:07 +03:00
'all_plugins_hooks' => $this->all_plugins_hooks,
'installed_plugins' => $this->installed_plugins,
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
}
}