PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // | Modify: yhustc <yhustc@gmail.com>
  11. // +----------------------------------------------------------------------
  12. // $Id$
  13. /**
  14. +----------------------------------------------------------
  15. * ????????
  16. +----------------------------------------------------------
  17. * @return void
  18. +----------------------------------------------------------
  19. */
  20. function empty_dir($directory)
  21. {
  22. $handle = opendir($directory);
  23. while (($file = readdir($handle)) !== false)
  24. {
  25. if ($file != "." && $file != "..")
  26. {
  27. closedir($handle);
  28. return false;
  29. }
  30. }
  31. closedir($handle);
  32. return true;
  33. }
  34. /**
  35. +----------------------------------------------------------
  36. * ????
  37. +----------------------------------------------------------
  38. * @param string $path ????
  39. * @param string $app ?????
  40. +----------------------------------------------------------
  41. * @return Array
  42. +----------------------------------------------------------
  43. */
  44. function get_plugins($path=PLUGIN_PATH,$app=APP_NAME,$ext='.php')
  45. {
  46. static $plugins = array ();
  47. if(isset($plugins[$app])) {
  48. return $plugins[$app];
  49. }
  50. // ???????? ?????
  51. if(empty_dir($path)) {
  52. return array();
  53. }
  54. $path = realpath($path);
  55. // ???? ????????
  56. /*
  57. $dir = glob ( $path . '/*' );
  58. if($dir) {
  59. foreach($dir as $val) {
  60. if(is_dir($val)){
  61. $subdir = glob($val.'/*'.$ext);
  62. if($subdir) {
  63. foreach($subdir as $file)
  64. $plugin_files[] = $file;
  65. }
  66. }else{
  67. if (strrchr($val, '.') == $ext)
  68. $plugin_files[] = $val;
  69. }
  70. }
  71. */
  72. $dir = dir($path);
  73. if($dir) {
  74. $plugin_files = array();
  75. while (false !== ($file = $dir->read())) {
  76. if($file == "." || $file == "..") continue;
  77. if(is_dir($path.'/'.$file)){
  78. $subdir = dir($path.'/'.$file);
  79. if ($subdir) {
  80. while (($subfile = $subdir->read()) !== false) {
  81. if($subfile == "." || $subfile == "..") continue;
  82. if (preg_match('/\.php$/', $subfile))
  83. $plugin_files[] = "$file/$subfile";
  84. }
  85. $subdir->close();
  86. }
  87. }else{
  88. $plugin_files[] = $file;
  89. }
  90. }
  91. $dir->close();
  92. //???????
  93. if(count($plugin_files)>1) {
  94. sort($plugin_files);
  95. }
  96. $plugins[$app] = array();
  97. foreach ($plugin_files as $plugin_file) {
  98. if ( !is_readable("$path/$plugin_file")) continue;
  99. //?????????
  100. $plugin_data = get_plugin_info("$path/$plugin_file");
  101. if (empty ($plugin_data['name'])) {
  102. continue;
  103. }
  104. $plugins[$app][] = $plugin_data;
  105. }
  106. return $plugins[$app];
  107. }else {
  108. return array();
  109. }
  110. }
  111. /**
  112. +----------------------------------------------------------
  113. * ??????
  114. +----------------------------------------------------------
  115. * @param string $plugin_file ?????
  116. +----------------------------------------------------------
  117. * @return Array
  118. +----------------------------------------------------------
  119. */
  120. function get_plugin_info($plugin_file) {
  121. $plugin_data = file_get_contents($plugin_file);
  122. preg_match("/Plugin Name:(.*)/i", $plugin_data, $plugin_name);
  123. if(empty($plugin_name)) {
  124. return false;
  125. }
  126. preg_match("/Plugin URI:(.*)/i", $plugin_data, $plugin_uri);
  127. preg_match("/Description:(.*)/i", $plugin_data, $description);
  128. preg_match("/Author:(.*)/i", $plugin_data, $author_name);
  129. preg_match("/Author URI:(.*)/i", $plugin_data, $author_uri);
  130. if (preg_match("/Version:(.*)/i", $plugin_data, $version))
  131. $version = trim($version[1]);
  132. else
  133. $version = '';
  134. if(!empty($author_name)) {
  135. if(!empty($author_uri)) {
  136. $author_name = '<a href="'.trim($author_uri[1]).'" target="_blank">'.$author_name[1].'</a>';
  137. }else {
  138. $author_name = $author_name[1];
  139. }
  140. }else {
  141. $author_name = '';
  142. }
  143. 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);
  144. }
  145. /**
  146. +----------------------------------------------------------
  147. * ??????????
  148. +----------------------------------------------------------
  149. * @param string $tag ????????
  150. * @param string $compiler ?????
  151. +----------------------------------------------------------
  152. * @return boolean
  153. +----------------------------------------------------------
  154. */
  155. function add_compiler($tag,$compiler)
  156. {
  157. $GLOBALS['template_compiler'][strtoupper($tag)] = $compiler ;
  158. return ;
  159. }
  160. /**
  161. +----------------------------------------------------------
  162. * ????????
  163. +----------------------------------------------------------
  164. * @param string $tag ????????
  165. +----------------------------------------------------------
  166. * @return boolean
  167. +----------------------------------------------------------
  168. */
  169. function use_compiler($tag)
  170. {
  171. $args = array_slice(func_get_args(), 1);
  172. if(is_callable($GLOBALS['template_compiler'][strtoupper($tag)])) {
  173. call_user_func_array($GLOBALS['template_compiler'][strtoupper($tag)],$args);
  174. }else{
  175. throw_exception(L('_TEMPLATE_ERROR_').'?'.C('TMPL_ENGINE_TYPE'));
  176. }
  177. return ;
  178. }
  179. /**
  180. +----------------------------------------------------------
  181. * ???????
  182. +----------------------------------------------------------
  183. * @param string $tag ?????
  184. * @param string $function ?????
  185. * @param integer $priority ?????
  186. * @param integer $args ??
  187. +----------------------------------------------------------
  188. * @return boolean
  189. +----------------------------------------------------------
  190. */
  191. function add_filter($tag,$function,$priority = 10,$args = 1)
  192. {
  193. static $_filter = array();
  194. if ( isset($_filter[APP_NAME.'_'.$tag]["$priority"]) ) {
  195. foreach($_filter[APP_NAME.'_'.$tag]["$priority"] as $filter) {
  196. if ( $filter['function'] == $function ) {
  197. return true;
  198. }
  199. }
  200. }
  201. $_filter[APP_NAME.'_'.$tag]["$priority"][] = array('function'=> $function,'args'=> $args);
  202. $_SESSION['_filters'] = $_filter;
  203. return true;
  204. }
  205. /**
  206. +----------------------------------------------------------
  207. * ??????????
  208. +----------------------------------------------------------
  209. * @param string $tag ?????
  210. * @param string $function ?????
  211. * @param integer $priority ?????
  212. +----------------------------------------------------------
  213. * @return boolean
  214. +----------------------------------------------------------
  215. */
  216. function remove_filter($tag, $function_to_remove, $priority = 10) {
  217. $_filter = $_SESSION['_filters'];
  218. if ( isset($_filter[APP_NAME.'_'.$tag]["$priority"]) ) {
  219. $new_function_list = array();
  220. foreach($_filter[APP_NAME.'_'.$tag]["$priority"] as $filter) {
  221. if ( $filter['function'] != $function_to_remove ) {
  222. $new_function_list[] = $filter;
  223. }
  224. }
  225. $_filter[APP_NAME.'_'.$tag]["$priority"] = $new_function_list;
  226. }
  227. $_SESSION['_filters'] = $_filter;
  228. return true;
  229. }
  230. /**
  231. +----------------------------------------------------------
  232. * ?????
  233. +----------------------------------------------------------
  234. * @param string $tag ?????
  235. * @param string $string ??
  236. +----------------------------------------------------------
  237. * @return boolean
  238. +----------------------------------------------------------
  239. */
  240. function apply_filter($tag,$string='')
  241. {
  242. if (!isset($_SESSION['_filters']) || !isset($_SESSION['_filters'][APP_NAME.'_'.$tag]) ) {
  243. return $string;
  244. }
  245. $_filter = $_SESSION['_filters'][APP_NAME.'_'.$tag];
  246. ksort($_filter);
  247. $args = array_slice(func_get_args(), 2);
  248. foreach ($_filter as $priority => $functions) {
  249. if ( !is_null($functions) ) {
  250. foreach($functions as $function) {
  251. if(is_callable($function['function'])) {
  252. $args = array_merge(array($string), $args);
  253. $string = call_user_func_array($function['function'],$args);
  254. }
  255. }
  256. }
  257. }
  258. return $string;
  259. }
  260. /**
  261. +----------------------------------------------------------
  262. * ????????
  263. +----------------------------------------------------------
  264. * @param string $tag ??????
  265. * @param string $function ???????
  266. * @param integer $priority ?????
  267. * @param integer $args ????
  268. +----------------------------------------------------------
  269. * @return boolean
  270. +----------------------------------------------------------
  271. */
  272. function add_action($tag,$function,$priority = 10,$args = '')
  273. {
  274. static $_action = array();
  275. if(empty($priority)) //??args??,??????priority,????
  276. $priority = 10;
  277. if ( isset($_action[APP_NAME.'_'.$tag]["$priority"]) ) {
  278. foreach($_action[APP_NAME.'_'.$tag]["$priority"] as $action) {
  279. if ( $action['function'] == $function ) {
  280. return true;
  281. }
  282. }
  283. }
  284. $argArray = array_slice(func_get_args(), 3);
  285. $_action[APP_NAME.'_'.$tag]["$priority"][] = array('function'=> $function,'args'=>$argArray);
  286. $_SESSION['_actions'] = $_action;
  287. return true;
  288. }
  289. /**
  290. +----------------------------------------------------------
  291. * ???????????
  292. +----------------------------------------------------------
  293. * @param string $tag ??????
  294. * @param string $function ???????
  295. * @param integer $priority ?????
  296. +----------------------------------------------------------
  297. * @return boolean
  298. +----------------------------------------------------------
  299. */
  300. function remove_action($tag, $function_to_remove, $priority = 10) {
  301. $_action = $_SESSION['_actions'];
  302. if ( isset($_action[APP_NAME.'_'.$tag]["$priority"]) ) {
  303. $new_function_list = array();
  304. foreach($_action[APP_NAME.'_'.$tag]["$priority"] as $action) {
  305. if ( $action['function'] != $function_to_remove ) {
  306. $new_function_list[] = $action;
  307. }
  308. }
  309. $_action[APP_NAME.'_'.$tag]["$priority"] = $new_function_list;
  310. }
  311. $_SESSION['_actions'] = $_action;
  312. return true;
  313. }
  314. /**
  315. +----------------------------------------------------------
  316. * ??????
  317. +----------------------------------------------------------
  318. * @param string $tag ??????
  319. * @param string $string ??
  320. +----------------------------------------------------------
  321. * @return boolean
  322. +----------------------------------------------------------
  323. */
  324. function apply_action($tag)
  325. {
  326. if (!isset($_SESSION['_actions']) || !isset($_SESSION['_actions'][APP_NAME.'_'.$tag]) ) {
  327. return;
  328. }
  329. $_action = $_SESSION['_actions'][APP_NAME.'_'.$tag];
  330. ksort($_action);
  331. foreach ($_action as $priority => $functions) {
  332. if ( !is_null($functions) ) {
  333. foreach($functions as $function) {
  334. if(is_callable($function['function'])) {
  335. call_user_func_array($function['function'],$function['args']);
  336. }
  337. }
  338. }
  339. }
  340. }
  341. ?>