/trunk/Tpblog/ThinkPHP/Common/plugin.php
http://tpblog.googlecode.com/ · PHP · 357 lines · 199 code · 16 blank · 142 comment · 50 complexity · 9e78a137610a039e7fd84ecf7666cdcf MD5 · raw file
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP
- // +----------------------------------------------------------------------
- // | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // | Modify: yhustc <yhustc@gmail.com>
- // +----------------------------------------------------------------------
- // $Id$
-
- /**
- +----------------------------------------------------------
- * ????????
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- function empty_dir($directory)
- {
- $handle = opendir($directory);
- while (($file = readdir($handle)) !== false)
- {
- if ($file != "." && $file != "..")
- {
- closedir($handle);
- return false;
- }
- }
- closedir($handle);
- return true;
- }
-
- /**
- +----------------------------------------------------------
- * ????
- +----------------------------------------------------------
- * @param string $path ????
- * @param string $app ?????
- +----------------------------------------------------------
- * @return Array
- +----------------------------------------------------------
- */
- function get_plugins($path=PLUGIN_PATH,$app=APP_NAME,$ext='.php')
- {
- static $plugins = array ();
- if(isset($plugins[$app])) {
- return $plugins[$app];
- }
- // ???????? ?????
- if(empty_dir($path)) {
- return array();
- }
- $path = realpath($path);
-
- // ???? ????????
- /*
- $dir = glob ( $path . '/*' );
- if($dir) {
- foreach($dir as $val) {
- if(is_dir($val)){
- $subdir = glob($val.'/*'.$ext);
- if($subdir) {
- foreach($subdir as $file)
- $plugin_files[] = $file;
- }
- }else{
- if (strrchr($val, '.') == $ext)
- $plugin_files[] = $val;
- }
- }
- */
-
- $dir = dir($path);
- if($dir) {
- $plugin_files = array();
- while (false !== ($file = $dir->read())) {
- if($file == "." || $file == "..") continue;
- if(is_dir($path.'/'.$file)){
- $subdir = dir($path.'/'.$file);
- if ($subdir) {
- while (($subfile = $subdir->read()) !== false) {
- if($subfile == "." || $subfile == "..") continue;
- if (preg_match('/\.php$/', $subfile))
- $plugin_files[] = "$file/$subfile";
- }
- $subdir->close();
- }
- }else{
- $plugin_files[] = $file;
- }
- }
- $dir->close();
-
- //???????
- if(count($plugin_files)>1) {
- sort($plugin_files);
- }
- $plugins[$app] = array();
- foreach ($plugin_files as $plugin_file) {
- if ( !is_readable("$path/$plugin_file")) continue;
- //?????????
- $plugin_data = get_plugin_info("$path/$plugin_file");
- if (empty ($plugin_data['name'])) {
- continue;
- }
- $plugins[$app][] = $plugin_data;
- }
- return $plugins[$app];
- }else {
- return array();
- }
- }
-
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @param string $plugin_file ?????
- +----------------------------------------------------------
- * @return Array
- +----------------------------------------------------------
- */
- function get_plugin_info($plugin_file) {
-
- $plugin_data = file_get_contents($plugin_file);
- preg_match("/Plugin Name:(.*)/i", $plugin_data, $plugin_name);
- if(empty($plugin_name)) {
- return false;
- }
- preg_match("/Plugin URI:(.*)/i", $plugin_data, $plugin_uri);
- preg_match("/Description:(.*)/i", $plugin_data, $description);
- preg_match("/Author:(.*)/i", $plugin_data, $author_name);
- preg_match("/Author URI:(.*)/i", $plugin_data, $author_uri);
- if (preg_match("/Version:(.*)/i", $plugin_data, $version))
- $version = trim($version[1]);
- else
- $version = '';
- if(!empty($author_name)) {
- if(!empty($author_uri)) {
- $author_name = '<a href="'.trim($author_uri[1]).'" target="_blank">'.$author_name[1].'</a>';
- }else {
- $author_name = $author_name[1];
- }
- }else {
- $author_name = '';
- }
- return array ('file'=>$plugin_file,'name' => trim($plugin_name[1]), 'uri' => trim($plugin_uri[1]), 'description' => trim($description[1]), 'author' => trim($author_name), 'version' => $version);
- }
-
- /**
- +----------------------------------------------------------
- * ??????????
- +----------------------------------------------------------
- * @param string $tag ????????
- * @param string $compiler ?????
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- function add_compiler($tag,$compiler)
- {
- $GLOBALS['template_compiler'][strtoupper($tag)] = $compiler ;
- return ;
- }
-
- /**
- +----------------------------------------------------------
- * ????????
- +----------------------------------------------------------
- * @param string $tag ????????
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- function use_compiler($tag)
- {
- $args = array_slice(func_get_args(), 1);
- if(is_callable($GLOBALS['template_compiler'][strtoupper($tag)])) {
- call_user_func_array($GLOBALS['template_compiler'][strtoupper($tag)],$args);
- }else{
- throw_exception(L('_TEMPLATE_ERROR_').'?'.C('TMPL_ENGINE_TYPE'));
- }
- return ;
- }
-
- /**
- +----------------------------------------------------------
- * ???????
- +----------------------------------------------------------
- * @param string $tag ?????
- * @param string $function ?????
- * @param integer $priority ?????
- * @param integer $args ??
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- function add_filter($tag,$function,$priority = 10,$args = 1)
- {
- static $_filter = array();
- if ( isset($_filter[APP_NAME.'_'.$tag]["$priority"]) ) {
- foreach($_filter[APP_NAME.'_'.$tag]["$priority"] as $filter) {
- if ( $filter['function'] == $function ) {
- return true;
- }
- }
- }
- $_filter[APP_NAME.'_'.$tag]["$priority"][] = array('function'=> $function,'args'=> $args);
- $_SESSION['_filters'] = $_filter;
- return true;
- }
-
- /**
- +----------------------------------------------------------
- * ??????????
- +----------------------------------------------------------
- * @param string $tag ?????
- * @param string $function ?????
- * @param integer $priority ?????
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- function remove_filter($tag, $function_to_remove, $priority = 10) {
- $_filter = $_SESSION['_filters'];
- if ( isset($_filter[APP_NAME.'_'.$tag]["$priority"]) ) {
- $new_function_list = array();
- foreach($_filter[APP_NAME.'_'.$tag]["$priority"] as $filter) {
- if ( $filter['function'] != $function_to_remove ) {
- $new_function_list[] = $filter;
- }
- }
- $_filter[APP_NAME.'_'.$tag]["$priority"] = $new_function_list;
- }
- $_SESSION['_filters'] = $_filter;
- return true;
- }
-
- /**
- +----------------------------------------------------------
- * ?????
- +----------------------------------------------------------
- * @param string $tag ?????
- * @param string $string ??
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- function apply_filter($tag,$string='')
- {
- if (!isset($_SESSION['_filters']) || !isset($_SESSION['_filters'][APP_NAME.'_'.$tag]) ) {
- return $string;
- }
- $_filter = $_SESSION['_filters'][APP_NAME.'_'.$tag];
- ksort($_filter);
- $args = array_slice(func_get_args(), 2);
- foreach ($_filter as $priority => $functions) {
- if ( !is_null($functions) ) {
- foreach($functions as $function) {
- if(is_callable($function['function'])) {
- $args = array_merge(array($string), $args);
- $string = call_user_func_array($function['function'],$args);
- }
- }
- }
- }
- return $string;
- }
-
- /**
- +----------------------------------------------------------
- * ????????
- +----------------------------------------------------------
- * @param string $tag ??????
- * @param string $function ???????
- * @param integer $priority ?????
- * @param integer $args ????
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- function add_action($tag,$function,$priority = 10,$args = '')
- {
- static $_action = array();
- if(empty($priority)) //??args??,??????priority,????
- $priority = 10;
-
- if ( isset($_action[APP_NAME.'_'.$tag]["$priority"]) ) {
- foreach($_action[APP_NAME.'_'.$tag]["$priority"] as $action) {
- if ( $action['function'] == $function ) {
- return true;
- }
- }
- }
- $argArray = array_slice(func_get_args(), 3);
- $_action[APP_NAME.'_'.$tag]["$priority"][] = array('function'=> $function,'args'=>$argArray);
- $_SESSION['_actions'] = $_action;
- return true;
- }
-
- /**
- +----------------------------------------------------------
- * ???????????
- +----------------------------------------------------------
- * @param string $tag ??????
- * @param string $function ???????
- * @param integer $priority ?????
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- function remove_action($tag, $function_to_remove, $priority = 10) {
- $_action = $_SESSION['_actions'];
- if ( isset($_action[APP_NAME.'_'.$tag]["$priority"]) ) {
- $new_function_list = array();
- foreach($_action[APP_NAME.'_'.$tag]["$priority"] as $action) {
- if ( $action['function'] != $function_to_remove ) {
- $new_function_list[] = $action;
- }
- }
- $_action[APP_NAME.'_'.$tag]["$priority"] = $new_function_list;
- }
- $_SESSION['_actions'] = $_action;
- return true;
- }
-
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @param string $tag ??????
- * @param string $string ??
- +----------------------------------------------------------
- * @return boolean
- +----------------------------------------------------------
- */
- function apply_action($tag)
- {
- if (!isset($_SESSION['_actions']) || !isset($_SESSION['_actions'][APP_NAME.'_'.$tag]) ) {
- return;
- }
- $_action = $_SESSION['_actions'][APP_NAME.'_'.$tag];
- ksort($_action);
- foreach ($_action as $priority => $functions) {
- if ( !is_null($functions) ) {
- foreach($functions as $function) {
- if(is_callable($function['function'])) {
- call_user_func_array($function['function'],$function['args']);
- }
- }
- }
- }
- }
- ?>