One Hat Cyber Team
Your IP :
216.73.216.55
Server IP :
5.189.175.239
Server :
Linux panel.gemx-ai.com 5.14.0-570.19.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 4 04:00:24 EDT 2025 x86_64
Server Software :
LiteSpeed
PHP Version :
8.2.28
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
farmersapp
/
loans.farmersapp.store
/
includes
/
View File Name :
plugins.php
<?php // includes/plugins.php // Plugin system for extensibility require_once '../config/database.php'; class PluginSystem { private $db; private $plugins = []; private $hooks = []; public function __construct() { $this->db = new Database(); $this->loadPlugins(); } /** * Load all active plugins */ private function loadPlugins() { // Load from database $this->db->query("SELECT * FROM plugins WHERE is_active = 1 ORDER BY id"); $plugins = $this->db->resultSet(); foreach ($plugins as $plugin) { $pluginFile = PLUGIN_PATH . '/' . $plugin['plugin_path']; if (file_exists($pluginFile)) { require_once $pluginFile; // Extract plugin class name from file $className = $this->getPluginClassName($pluginFile); if (class_exists($className)) { $pluginInstance = new $className(); $this->plugins[$plugin['slug']] = [ 'instance' => $pluginInstance, 'info' => $plugin ]; // Initialize plugin if (method_exists($pluginInstance, 'init')) { $pluginInstance->init(); } // Load plugin hooks $this->loadPluginHooks($plugin['id']); } } } } /** * Get plugin class name from file */ private function getPluginClassName($file) { $content = file_get_contents($file); $tokens = token_get_all($content); $className = ''; foreach ($tokens as $index => $token) { if ($token[0] === T_CLASS) { $className = $tokens[$index + 2][1]; break; } } return $className; } /** * Load plugin hooks from database */ private function loadPluginHooks($pluginId) { $this->db->query("SELECT * FROM plugin_hooks WHERE plugin_id = :plugin_id AND is_active = 1 ORDER BY priority"); $this->db->bind(':plugin_id', $pluginId); $hooks = $this->db->resultSet(); foreach ($hooks as $hook) { $this->addHook($hook['hook_name'], $hook['callback'], $hook['priority']); } } /** * Add a hook */ public function addHook($hookName, $callback, $priority = 10) { if (!isset($this->hooks[$hookName])) { $this->hooks[$hookName] = []; } $this->hooks[$hookName][] = [ 'callback' => $callback, 'priority' => $priority ]; // Sort by priority usort($this->hooks[$hookName], function($a, $b) { return $a['priority'] - $b['priority']; }); } /** * Execute hooks */ public function doAction($hookName, $args = []) { if (!isset($this->hooks[$hookName])) { return; } foreach ($this->hooks[$hookName] as $hook) { if (is_callable($hook['callback'])) { call_user_func_array($hook['callback'], $args); } } } /** * Apply filters */ public function applyFilters($hookName, $value, $args = []) { if (!isset($this->hooks[$hookName])) { return $value; } foreach ($this->hooks[$hookName] as $hook) { if (is_callable($hook['callback'])) { $value = call_user_func_array($hook['callback'], array_merge([$value], $args)); } } return $value; } /** * Get all plugins */ public function getPlugins() { return $this->plugins; } /** * Get plugin by slug */ public function getPlugin($slug) { return $this->plugins[$slug] ?? null; } /** * Install plugin */ public function installPlugin($pluginFile) { // Check if plugin file exists if (!file_exists($pluginFile)) { return ['success' => false, 'message' => 'Plugin file not found']; } // Extract plugin info $pluginInfo = $this->extractPluginInfo($pluginFile); if (!$pluginInfo) { return ['success' => false, 'message' => 'Invalid plugin file']; } // Check if plugin already installed $this->db->query("SELECT id FROM plugins WHERE slug = :slug"); $this->db->bind(':slug', $pluginInfo['slug']); $existing = $this->db->single(); if ($existing) { return ['success' => false, 'message' => 'Plugin already installed']; } // Install plugin $this->db->query("INSERT INTO plugins (name, slug, version, author, description, plugin_path, installed_at) VALUES (:name, :slug, :version, :author, :description, :plugin_path, NOW())"); $this->db->bind(':name', $pluginInfo['name']); $this->db->bind(':slug', $pluginInfo['slug']); $this->db->bind(':version', $pluginInfo['version']); $this->db->bind(':author', $pluginInfo['author']); $this->db->bind(':description', $pluginInfo['description']); $this->db->bind(':plugin_path', $pluginInfo['plugin_path']); if ($this->db->execute()) { $pluginId = $this->db->lastInsertId(); // Register plugin hooks $this->registerPluginHooks($pluginId, $pluginInfo['hooks']); return ['success' => true, 'message' => 'Plugin installed successfully']; } return ['success' => false, 'message' => 'Failed to install plugin']; } /** * Extract plugin info from file */ private function extractPluginInfo($pluginFile) { $content = file_get_contents($pluginFile); // Look for plugin header if (!preg_match('/Plugin Name:\s*(.+)/i', $content, $name)) { return false; } preg_match('/Plugin Slug:\s*(.+)/i', $content, $slug); preg_match('/Version:\s*(.+)/i', $content, $version); preg_match('/Author:\s*(.+)/i', $content, $author); preg_match('/Description:\s*(.+)/i', $content, $description); // Get relative path $pluginPath = str_replace(PLUGIN_PATH . '/', '', $pluginFile); // Extract hooks from plugin file $hooks = $this->extractHooksFromFile($content); return [ 'name' => trim($name[1]), 'slug' => isset($slug[1]) ? trim($slug[1]) : sanitize_title(trim($name[1])), 'version' => isset($version[1]) ? trim($version[1]) : '1.0.0', 'author' => isset($author[1]) ? trim($author[1]) : 'Unknown', 'description' => isset($description[1]) ? trim($description[1]) : '', 'plugin_path' => $pluginPath, 'hooks' => $hooks ]; } /** * Extract hooks from plugin content */ private function extractHooksFromFile($content) { $hooks = []; // Look for add_action and add_filter calls preg_match_all('/add_action\s*\(\s*[\'"]([^\'"]+)[\'"]\s*,\s*[\'"]([^\'"]+)[\'"]/i', $content, $actions); preg_match_all('/add_filter\s*\(\s*[\'"]([^\'"]+)[\'"]\s*,\s*[\'"]([^\'"]+)[\'"]/i', $content, $filters); for ($i = 0; $i < count($actions[0]); $i++) { $hooks[] = [ 'hook_name' => $actions[1][$i], 'callback' => $actions[2][$i], 'type' => 'action' ]; } for ($i = 0; $i < count($filters[0]); $i++) { $hooks[] = [ 'hook_name' => $filters[1][$i], 'callback' => $filters[2][$i], 'type' => 'filter' ]; } return $hooks; } /** * Register plugin hooks in database */ private function registerPluginHooks($pluginId, $hooks) { foreach ($hooks as $hook) { $this->db->query("INSERT INTO plugin_hooks (plugin_id, hook_name, callback, type) VALUES (:plugin_id, :hook_name, :callback, :type)"); $this->db->bind(':plugin_id', $pluginId); $this->db->bind(':hook_name', $hook['hook_name']); $this->db->bind(':callback', $hook['callback']); $this->db->bind(':type', $hook['type']); $this->db->execute(); } } /** * Activate plugin */ public function activatePlugin($slug) { $this->db->query("UPDATE plugins SET is_active = 1, activated_at = NOW() WHERE slug = :slug"); $this->db->bind(':slug', $slug); if ($this->db->execute()) { // Reload plugins $this->loadPlugins(); return ['success' => true, 'message' => 'Plugin activated successfully']; } return ['success' => false, 'message' => 'Failed to activate plugin']; } /** * Deactivate plugin */ public function deactivatePlugin($slug) { $this->db->query("UPDATE plugins SET is_active = 0 WHERE slug = :slug"); $this->db->bind(':slug', $slug); if ($this->db->execute()) { // Remove from memory unset($this->plugins[$slug]); return ['success' => true, 'message' => 'Plugin deactivated successfully']; } return ['success' => false, 'message' => 'Failed to deactivate plugin']; } /** * Uninstall plugin */ public function uninstallPlugin($slug) { // Get plugin info $plugin = $this->getPlugin($slug); if (!$plugin) { return ['success' => false, 'message' => 'Plugin not found']; } // Delete plugin hooks $this->db->query("DELETE FROM plugin_hooks WHERE plugin_id = :plugin_id"); $this->db->bind(':plugin_id', $plugin['info']['id']); $this->db->execute(); // Delete plugin $this->db->query("DELETE FROM plugins WHERE slug = :slug"); $this->db->bind(':slug', $slug); if ($this->db->execute()) { // Remove from memory unset($this->plugins[$slug]); return ['success' => true, 'message' => 'Plugin uninstalled successfully']; } return ['success' => false, 'message' => 'Failed to uninstall plugin']; } } // Helper function to sanitize title function sanitize_title($title) { $title = strtolower($title); $title = preg_replace('/[^a-z0-9-]/', '-', $title); $title = preg_replace('/-+/', '-', $title); $title = trim($title, '-'); return $title; } // Create plugin system instance $pluginSystem = new PluginSystem(); /** * Add action hook */ function add_action($hookName, $callback, $priority = 10) { global $pluginSystem; $pluginSystem->addHook($hookName, $callback, $priority); } /** * Do action */ function do_action($hookName, ...$args) { global $pluginSystem; $pluginSystem->doAction($hookName, $args); } /** * Add filter hook */ function add_filter($hookName, $callback, $priority = 10) { global $pluginSystem; $pluginSystem->addHook($hookName, $callback, $priority); } /** * Apply filter */ function apply_filters($hookName, $value, ...$args) { global $pluginSystem; return $pluginSystem->applyFilters($hookName, $value, $args); } ?>