Login   Register  
PHP Classes
elePHPant
Icontem

File: bt-include/plugins.functions.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Michael Dale  >  Bluetrait  >  bt-include/plugins.functions.php  >  Download  
File: bt-include/plugins.functions.php
Role: Auxiliary script
Content type: text/plain
Description: Plugin Support File
Class: Bluetrait
A multi-user blog system
Author: By
Last change:
Date: 5 years ago
Size: 10,309 bytes
 

Contents

Class file image Download
<?php
/*
	Bluetrait 2.0 Plugin Support
	Michael Dale Copyright 2008
*/
	if (!defined('BT_ROOT')) exit;
	
	$bt_installed_plugins = bt_get_config('plugin_data');
	$bt_plugin_dir = BT_ROOT . BT_CONTENT . '/bt-plugins/';
	$bt_tasks = array();
	$bt_content_filters = array();

	function bt_load_plugins() {
		global $bt_installed_plugins, $bt_plugin_dir;
		if (is_array($bt_installed_plugins)) {
			foreach($bt_installed_plugins as $x) {
				$x = bt_sanitize_plugin_name($x);
				if (file_exists("$bt_plugin_dir$x.plugin.php")) {
					include("$bt_plugin_dir$x.plugin.php");
				}
				else {
					bt_disable_plugin($x);
				}
			}
		}		
	}
	
	function bt_plugin_loaded($plugin_name) {
		global $bt_installed_plugins;
		if (!empty($plugin_name) && is_array($bt_installed_plugins)) {
			if (in_array($plugin_name, $bt_installed_plugins)) {
				return true;
			}
			else {
				return false;
			}
		}
		else {
			return false;
		}
	}
	
	//enable a plugin
	function bt_enable_plugin($plugin_name) {
		global $bt_installed_plugins;
		$plugin_name = bt_sanitize_plugin_name($plugin_name);
		if (!empty($plugin_name)) {
			if (!in_array($plugin_name, $bt_installed_plugins)) {
				bt_run_section('enable_plugin_' . $plugin_name);
				$bt_installed_plugins[] = $plugin_name;
				trigger_error('Plugin enabled: "' . $plugin_name . '"', E_USER_NOTICE);
				bt_set_config('plugin_data',  $bt_installed_plugins);
			}
		}
	}
	
	//disable a plugin
	function bt_disable_plugin($plugin_name) {
		global $bt_installed_plugins;
		if (in_array($plugin_name, $bt_installed_plugins)) {
			bt_run_section('disable_plugin_' . $plugin_name);
			$key = array_search($plugin_name, $bt_installed_plugins);
			unset($bt_installed_plugins[$key]);
			$bt_installed_plugins = array_values($bt_installed_plugins);
			trigger_error('Plugin disabled: "' . $plugin_name . '"', E_USER_NOTICE);
			bt_set_config('plugin_data',  $bt_installed_plugins);
		}
	}
	
	function bt_disable_all() {
		$bt_installed_plugins;
		foreach ($bt_installed_plugins as $index => $value) {
			bt_run_section('disable_plugin_' . $value);
		}
		$installed_plugins = array();
		trigger_error("Plugins Disabled", E_USER_NOTICE);
		bt_set_config('plugin_data', $installed_plugins);
	}
	
	function bt_any_enabled() {
		global $bt_installed_plugins;
		if (empty($bt_installed_plugins)) {
			return false;
		}
		else {
			return true;
		}
	}
	
	function bt_add_task($plugin_name, $section, $function, $priority = 10, $arguments = 1) {
		global $bt_tasks;
	
		//need to fix this for objects
		if (is_object($function[0])) bt_die('Objects can not currently be used for tasks.');
		$bt_tasks[$section][$priority][$function] = array($plugin_name, $arguments);
	
		return true;

	}
	function bt_has_task($section) {
		global $bt_tasks;
		
		if (isset($bt_tasks[$section])) {
			return true;
		}
		else {
			return false;
		}
	}
	
	function bt_remove_task($section, $function, $priority = 10) {
		global $bt_tasks;
		
		unset($bt_tasks[$section][$priority][$function]);
		
		return true;
	}
	
	function bt_add_content_filter($plugin_name, $section, $function, $priority = 10, $arguments = 1) {
		global $bt_content_filters;
	
		//need to fix this for objects
		if (is_object($function[0])) bt_die('Objects can not currently be used for tasks.');
		$bt_content_filters[$section][$priority][$function] = array($plugin_name, $arguments);
	
		return;

	}
	
	function bt_has_content_filter($section) {
		global $bt_content_filters;
		
		if (isset($bt_content_filters[$section])) {
			return true;
		}
		else {
			return false;
		}
	}
	
	
	function bt_remove_content_filter($section, $function, $priority = 10) {
		global $bt_content_filters;
		
		unset($bt_content_filters[$section][$priority][$function]);
		
		return;
	}
	
	//pass by reference
	function bt_run_section_ref($section, &$args = '') {
		global $bt_tasks;
		//echo $section . '<br />';
		if (!isset($bt_tasks[$section])) return false;
		
		//sorts based on priority, 1 being the highest (first run)
		ksort($bt_tasks[$section], SORT_NUMERIC);
		
		//call_user_func_array requires an array
		$arguments = array(&$args);

		foreach($bt_tasks[$section] as $priority => $functions) {
				foreach($bt_tasks[$section][$priority] as $function => $task_details) {
					if (!function_exists($function)) {
						trigger_error('Plugin "'. bt_htmlentities($task_details[0]) .'" called undefined function "' . bt_htmlentities($function) . '" in section "' . bt_htmlentities($section) . '".', E_USER_ERROR);
						continue;
					}
					call_user_func_array($function, $arguments);
				}
		}
		
		return true;
	}
	
	function bt_run_section($section, $args = '') {
		return bt_run_section_ref($section, $args);
	}

	/*
	
	*/
	function bt_run_content_filter($section, $content = '') {
		global $bt_content_filters;
		//echo $section . '<br />';
		if (!isset($bt_content_filters[$section])) return $content;
		
		//sorts based on priority, 1 being the highest (first run)
		ksort($bt_content_filters[$section], SORT_NUMERIC);

		foreach($bt_content_filters[$section] as $priority => $functions) {
				foreach($bt_content_filters[$section][$priority] as $function => $task_details) {
					if (!function_exists($function)) {
						trigger_error('Plugin "'. bt_htmlentities($task_details[0]) .'" called undefined function "' . bt_htmlentities($function) . '" in section "' . bt_htmlentities($section) . '".', E_USER_ERROR);
						continue;
					}
					$content = call_user_func($function, $content);
				}
		}
		
		return $content;
	}
		
	function bt_get_all_plugins() {
		global $bt_plugin_dir, $bt_general_plugin_warning;

		$plugin_info = array();
		$plugin_info_temp = array();
		$bt_general_plugin_warning = FALSE;

		if (is_dir($bt_plugin_dir)) {
			$plugin_folders[] = '';
			$folder = opendir($bt_plugin_dir);
			while (false !== ($dir_array = readdir($folder))) {
				if ($dir_array != '.' && $dir_array != '..') {
					if(is_dir($bt_plugin_dir . $dir_array)) {
						if (bt_sanitize_plugin_name($dir_array) == $dir_array) {
							$plugin_folders[] = $dir_array . '/';
						}
					}
				}
			}
			closedir($folder);
		}
		foreach($plugin_folders as $plugin_folder) {
			$plugin_folder_full = $bt_plugin_dir . $plugin_folder;
			if (is_dir($plugin_folder_full)) {
				$folder = opendir($plugin_folder_full);
				while (false !== ($file_array = readdir($folder))) {
					if ($file_array != '.' && $file_array != '..') {
						if(filetype($plugin_folder_full . $file_array) == 'file') {
							if(preg_match("/^([a-z0-9_\/]+).plugin.php$/", $file_array, $matches) > 0){
								$plugin_info[$matches[1]] =
										array (
											'plugin_file_name' => $plugin_folder . $matches[1],
											'plugin_name' => $matches[1],
											'plugin_description' => 'Meta File Missing Or Corrupt',
											'plugin_update_checker_url' => '',
											'plugin_author' => '',
											'plugin_author_website' => '',
											'plugin_website' => '',
											'plugin_version' => ''
										);
								if (file_exists($plugin_folder_full . $matches[1] . '.meta.plugin.php')) {
									include_once($plugin_folder_full . $matches[1] . '.meta.plugin.php');
									if (class_exists($matches[1])) {
										$plugin_info_temp[$matches[1]] = new $matches[1];
										if (method_exists($plugin_info_temp[$matches[1]], 'meta_data')) {
											$plugin_info[$matches[1]] = $plugin_info_temp[$matches[1]]->meta_data();
											$plugin_info[$matches[1]]['plugin_file_name'] = $plugin_folder . $matches[1];
										}
										else {
										
										}
									}
									else {
										$bt_general_plugin_warning = TRUE;
										trigger_error('The meta file for plugin "' . bt_htmlentities($matches[1]) . '" does not contain the "' . bt_htmlentities($matches[1]) . '" class.', E_USER_WARNING);
									}
								}
								else {
									$bt_general_plugin_warning = TRUE;
									trigger_error('The meta file for plugin "' . bt_htmlentities($matches[1]) . '" cannot be found.<br />It should be in the plugins folder called "' . bt_htmlentities($matches[1] . '.meta.plugin.php') . '" but is not.', E_USER_WARNING);
								}
							}
						}
					}
				}
				closedir($folder);
			}
		}
		
		ksort($plugin_info);
		
		return $plugin_info;
	}
	
	function bt_sanitize_plugin_name($plugin_name) {
		$plugin_name = strtolower($plugin_name);
		$plugin_name = preg_replace('([^0-9a-z_\/])', '', $plugin_name);
		return $plugin_name;
	}
	
	//function from WordPress
	function bt_plugin_basename($file) {
		$file = str_replace('\\','/',$file); // sanitize for Win32 installs
		$file = preg_replace('|/+|','/', $file); // remove any duplicate slash
		$file = preg_replace('|^.*' . BT_CONTENT . '/bt-plugins' . '/|','',$file); // get relative path from plugins dir
		return $file;
	}
	
	function bt_plugin_base_url($file) {
		$file = bt_plugin_basename($file);
		$file = str_replace('.plugin.php','',$file);
		return $file;
	}
	
	//check if a plugin is out of date
	function bt_plugin_out_of_date($plugin_name, $plugin_version) {
		static $plugin_update_data_array;
		if (!isset($plugin_update_data_array)) {
			$plugin_update_data_array = bt_get_config('plugin_update_data');
		}
		$plugin_name = bt_sanitize_plugin_name($plugin_name);	
		if (isset($plugin_update_data_array[$plugin_name])) {
			try {
				$plugin_update_data = $plugin_update_data_array[$plugin_name];
				$current_version = explode('-', $plugin_version);
				$current_version = $current_version[0];
				$xml = new SimpleXMLElement($plugin_update_data);
					
				if (version_compare($current_version, $xml->version, '<')) {
					//new version
					$array['version'] = $xml->version;
					$array['release_notes'] = $xml->release_notes;
					$array['download'] = $xml->download;
					return $array;
				}
				else {
					return false;
				}
			}
			catch (Exception $e) {
				trigger_error('Unable to read update information for plugin "'.bt_htmlentities($plugin_name).'"', E_USER_ERROR);
			}
		}
		else {
			return false;
		}
	}
?>