PageRenderTime 75ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/core/magic.class.php

https://github.com/BankKit/daishu
PHP | 804 lines | 742 code | 28 blank | 34 comment | 20 complexity | 2a4db0efa8df9443551497b27de40243 MD5 | raw file
  1. <?php
  2. /******************************
  3. * $File: magic.class.php
  4. * $Description: 模板引擎
  5. * $Author: QQ134716
  6. * $Time:2012-03-09
  7. * $Vesion:1.0
  8. * $Update:None
  9. * $UpdateDate:None
  10. ******************************/
  11. /**
  12. * 定义magic的目录
  13. */
  14. if (!defined('MAGIC_DIR')) {
  15. define('MAGIC_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  16. }
  17. class Magic
  18. {
  19. var $template_dir = "themes";//定义模板目录
  20. var $compile_dir = "data/compile";//定义模板缓存目录
  21. var $plugins_dir = "plugins/magic";//定义插件目录
  22. var $left_tag = "{";//左边的样式
  23. var $right_tag = "}";//右边的样式
  24. var $mysql_file = 'mysql.class.php';//mysql 文件
  25. var $mysql_open = false;//数据库文件是否打开
  26. var $magic_vars = array();//magic的定义变量
  27. var $file_perms = 0644;//文件的属性
  28. var $dir_perms = 0711;//文件的属性
  29. var $is_compile = true;//模板编译检查
  30. var $force_compile = false;//是否一直编译
  31. var $template_error = false;//模板编译错误
  32. /**
  33. * 构造函数
  34. */
  35. function Magic() {
  36. global $mysql;
  37. $this->mysql = $mysql;
  38. $this->assign('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME']: @$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
  39. }
  40. /**
  41. * 定义模板的值
  42. *
  43. * @param array|string $tpl_var the template variable name(s)
  44. * @param mixed $value the value to assign
  45. */
  46. function assign($tpl_var, $value = "") {
  47. if (is_array($tpl_var)){
  48. foreach ($tpl_var as $key => $value) {
  49. if ($key != '') {
  50. $this->magic_vars[$key] = $value;
  51. }
  52. }
  53. } else {
  54. if ($tpl_var != '')
  55. $this->magic_vars[$tpl_var] = $value;
  56. }
  57. }
  58. /**
  59. * 清除定义的变量
  60. *
  61. * @param string $tpl_var
  62. */
  63. function assign_clear($tpl_var) {
  64. if (is_array($tpl_var)) {
  65. foreach ($tpl_var as $curr_var){
  66. unset($this->magic_vars[$curr_var]);
  67. }
  68. }else{
  69. unset($this->magic_vars[$tpl_var]);
  70. }
  71. }
  72. /**
  73. * 清除所有的变量
  74. *
  75. */
  76. function assign_clear_all() {
  77. $this->_tpl_vars = array();
  78. }
  79. function magic_include($parse){
  80. $include_file = $parse['file'];
  81. $template_dir = empty($parse['vars']['template_dir'])?$this->template_dir:$parse['vars']['template_dir'];
  82. $this->magic_vars = array_merge($this->magic_vars, $parse['vars']);
  83. $this->compile_file($include_file,$template_dir);
  84. }
  85. /**编译的文件名路劲**/
  86. function compile_path($template_name){
  87. $new_compile_name = "%".urlencode($template_name).".php";
  88. if ($this->compile_dir != ""){
  89. $this->compile_path = $this->compile_dir."/".$new_compile_name;
  90. }else{
  91. $this->compile_path = $new_compile_name;
  92. }
  93. }
  94. /**模板的文件名路劲**/
  95. function template_path($template_name,$template_dir=""){
  96. if ($template_dir!=""){
  97. $this->template_path = $template_dir."/".$template_name;
  98. }elseif ($this->template_dir != "" ){
  99. $this->template_path = $this->template_dir."/".$template_name;
  100. }else{
  101. $this->template_path = $template_name;
  102. }
  103. }
  104. function template_read($filename) {
  105. if ( file_exists($filename) && is_readable($filename) && ($fd = @fopen($filename, 'rb')) ) {
  106. $contents = '';
  107. while (!feof($fd)) {
  108. $contents .= fread($fd, 8192);
  109. }
  110. fclose($fd);
  111. return $contents;
  112. } else {
  113. return false;
  114. }
  115. }
  116. //创建文件
  117. function template_write($compile_path,$contents) {
  118. $this->template_create_dir(dirname($compile_path));//先创建目录
  119. if (!($fd = @fopen($compile_path, 'wb'))) {
  120. $_tmp_file = $compile_path . DIRECTORY_SEPARATOR . uniqid('wrt');
  121. if (!($fd = @fopen($_tmp_file, 'wb'))) {
  122. $this->trigger_error("系统无法写入文件'$_tmp_file'");
  123. return false;
  124. }
  125. }
  126. fwrite($fd, $contents);
  127. fclose($fd);
  128. @chmod($template_path, $this->file_perms);
  129. return true;
  130. }
  131. /* 循环创建目录 */
  132. function template_create_dir($compile_path) {
  133. if (DIRECTORY_SEPARATOR!='/') {
  134. $compile_path = str_replace('\\','/', $compile_path);
  135. }
  136. if (is_dir($compile_path)){
  137. return true;
  138. }
  139. if (@ mkdir($compile_path, $this->dir_perms)){
  140. return true;
  141. }
  142. if (!$this->template_create_dir(dirname($compile_path))){
  143. return false;
  144. }
  145. return mkdir($compile_path, $this->dir_perms);
  146. }
  147. function check_compiled($template_path, $compile_path){
  148. if ($this->force_compile) {
  149. return false;
  150. }elseif (!$this->is_compile) {
  151. return true;//不需要检查编译
  152. }elseif (!file_exists($compile_path)){
  153. return false;
  154. }
  155. if (filemtime($template_path) <= filemtime($compile_path)) {
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. }
  161. function gethtml($template_name,$template_dir){
  162. $this->compile_path($template_name);//编译文件的完整路劲
  163. //return $this->compile_path;
  164. $this->template_path($template_name,$template_dir);//模板的完整路劲
  165. $_template_path = $this->template_path;
  166. $_template_dir = $this->template_dir ;
  167. $this->template_dir = $template_dir;
  168. $this->template_path = $template_dir."/".$template_name;
  169. /* 判断文件是否存在 */
  170. $_tpldir = $this->magic_vars["tpldir"];
  171. $this->magic_vars["tpldir"] = $this->magic_vars['sys_info']['con_webpath'].$template_dir;
  172. if (!file_exists($this->template_path)){
  173. $this->trigger_error("the template $this->template_path not exitst");
  174. }
  175. $_contents = $this->template_read($this->template_path);//读取模板的内容
  176. $contents = $this->compile_content($_contents);//内容编译
  177. /** 检查是否需要编译,是则自动编译并读取相关的文件 **/
  178. if (!$this->check_compiled($this->template_path,$this->compile_path)){
  179. $this->template_write($this->compile_path,$contents);
  180. }
  181. if(!$this->is_compile){
  182. $this->compile_path = tempnam ("/tmp", "FOO");
  183. $this->template_write($this->compile_path,$contents);
  184. }
  185. ob_start();
  186. include $this->compile_path ;
  187. $contents = ob_get_contents();
  188. ob_end_clean();
  189. $this->magic_vars["tpldir"] = $_tpldir;
  190. $this->template_path = $_template_path;
  191. $this->template_dir = $_template_dir;
  192. return $contents;
  193. }
  194. /**
  195. * 执行显示模板文件
  196. *
  197. * @param string $resource_name
  198. * @param string $cache_id
  199. * @param string $compile_id
  200. */
  201. function display($template_name,$template_dir=null,$type=null) {
  202. $this->compile_file($template_name,$template_dir,$type);
  203. }
  204. function compile_file($template_name,$template_dir="",$type=""){
  205. $this->template_path($template_name,$template_dir);//模板的完整路劲
  206. $this->compile_path($template_name);//编译文件的完整路劲
  207. /* 判断文件是否存在 */
  208. if (!file_exists($this->template_path)){
  209. $this->trigger_error("the template $this->template_path) not exitst");
  210. }
  211. $_contents = $this->template_read($this->template_path);//读取模板的内容
  212. $contents = $this->compile_content($_contents);//内容编译
  213. /** 检查是否需要编译,是则自动编译并读取相关的文件 **/
  214. if (!$this->check_compiled($this->template_path,$this->compile_path)){
  215. $this->template_write($this->compile_path,$contents);
  216. }
  217. if(!$this->is_compile){
  218. $this->compile_path = tempnam ("/tmp", "FOO");
  219. $this->template_write($this->compile_path,$contents);
  220. }
  221. ob_start();
  222. include $this->compile_path ;
  223. $contents = ob_get_contents();
  224. ob_end_clean();
  225. echo $contents;
  226. }
  227. function compile_content($content){
  228. if ($content == "") return "";
  229. $left_tag = preg_quote($this->left_tag, '~');
  230. $right_tag = preg_quote($this->right_tag, '~');
  231. $text_blocks = array();
  232. $compiled_tags = array();
  233. /* 用替换的思想替换掉literal */
  234. $ldq = preg_quote($this->left_tag, '~');
  235. $rdq = preg_quote($this->right_tag, '~');
  236. $search = "~({$ldq}\s*literal\s*{$rdq})(.*?)({$ldq}\s*/literal\s*{$rdq})|{$ldq}\s*php\s*{$rdq}(.*?){$ldq}\s*/php\s*{$rdq}~s";
  237. preg_match_all($search, $content, $__match);
  238. $magic_literal = array();
  239. foreach ($__match[2] as $key => $value){
  240. $_m = "magicliteral".$key;
  241. $$_m = $value;
  242. $content = str_replace($value,$_m,$content);
  243. }
  244. preg_match_all("~{$left_tag}\s*(.*?)\s*{$right_tag}~s", $content, $_match);
  245. $template_tags = $_match[1];
  246. /* 区分所有的模块 */
  247. $text_blocks = preg_split("~{$left_tag}.*?{$right_tag}~s", $content);
  248. /* 模块的集成 */
  249. for ($i = 0; $i < count($template_tags); $i++) {
  250. $compiled_tags[] = $this->compile_tag($template_tags[$i]);
  251. }
  252. /* 模块的集成 */
  253. $content = "";
  254. for ($i = 0; $i < count($text_blocks); $i++) {
  255. if (strpos($text_blocks[$i],"magicliteral")!==false){
  256. $__m = $text_blocks[$i];
  257. $content .= $$__m ;
  258. }else{
  259. $content .= $text_blocks[$i];
  260. }
  261. if (isset($compiled_tags[$i])){
  262. $content .= $compiled_tags[$i];
  263. }
  264. }
  265. return $content;
  266. }
  267. function compile_tag($template_tag){
  268. preg_match_all('~( (?>[^"\"\'=\s]+))+ | [=]~x', $template_tag, $match);
  269. $tag_str = $match[0];
  270. $tag_command = $tag_str[0];
  271. /* 变量 */
  272. if ($tag_command{0} == '$' ){
  273. return $this->compile_variable_tag($template_tag);
  274. }
  275. /* if判断语句 */
  276. elseif ($tag_command == 'if' || $tag_command == 'else' || $tag_command == 'elseif' || $tag_command == '/if'){
  277. return $this->compile_if_tag($tag_command,$template_tag);
  278. }
  279. /* foreach */
  280. elseif ($tag_command == 'foreach' || $tag_command == '/foreach'){
  281. $parse_var = $this->compile_parse_var($tag_str);
  282. return $this->compile_foreach_tag($tag_command,$parse_var);
  283. }
  284. /* literal */
  285. elseif ($tag_command == 'literal' || $tag_command == '/literal'){
  286. //return $this->compile_literal_tag($tag_command,$parse_var);
  287. }
  288. /* include */
  289. elseif ($tag_command == 'include'){
  290. $parse_var = $this->compile_parse_var($tag_str);
  291. return $this->compile_include_tag($tag_command,$parse_var);
  292. }
  293. /* editor */
  294. elseif ($tag_command == 'editor'){
  295. $parse_var = $this->compile_parse_var($tag_str);
  296. return $this->compile_editor_tag($parse_var);
  297. }
  298. //其他
  299. else{
  300. $parse_var = $this->compile_parse_var($tag_str);
  301. if ($output=$this->magic_function($tag_command, $parse_var)) {
  302. return $output;
  303. }elseif ($output=$this->magic_sqlfunc_tag($tag_command, $parse_var)) {
  304. return $output;
  305. }elseif ($output=$this->magic_sql_tag($tag_command, $template_tag)) {
  306. return $output;
  307. }elseif ($output=$this->magic_block($tag_command, $parse_var)) {
  308. return $output;
  309. } else {
  310. $this->trigger_error("unrecognized tag '$tag_command'", E_USER_ERROR, __FILE__, __LINE__);
  311. }
  312. }
  313. }
  314. function compile_variable_tag($template_tag){
  315. $_tpl_var = explode("|",$template_tag);
  316. $result = $this->compile_var_tag($_tpl_var[0]);
  317. $_output = "";
  318. $cline = "&*&%$##";//将\|转换为此码,最后再转义
  319. $aline = "&@@#$&&";//
  320. $_res = str_replace('\|',$cline,strstr($template_tag,"|"));
  321. preg_match_all('/(\|)(([a-zA-Z0-9_])+)(([:])?)/',$_res,$res);//标签
  322. $res = $res[0];
  323. foreach($res as $key){
  324. $_res = str_replace($key,$aline,$_res);//将标签替换
  325. }
  326. foreach (explode($aline,$_res) as $key ){
  327. $_rea[] = str_replace($cline,"|",$key);
  328. }
  329. for($i=0;$i<count($res);$i++){
  330. $__tpl_var = str_replace(array("|",":"),"",$res[$i]);
  331. $__var = $this->compile_var_tag(str_replace(array("\"","'"),"",$_rea[$i+1]));
  332. if ($__var=="") $__var = null;
  333. if ($__var{0}!="$"){
  334. $__var = "\"".$__var."\"";
  335. }else{
  336. $_output .= " if (!isset($__var)) $__var = '';\r\n";
  337. }
  338. $_output .= "\$_tmp = \$this->magic_modifier(\"".$__tpl_var."\",\$_tmp,".$__var.");";
  339. }
  340. $chars = preg_split('/([%|-|+|*|\/|\(|\)])/',$result, -1, PREG_SPLIT_DELIM_CAPTURE);//运算符
  341. if ($_output == ""){
  342. /*return "<? echo $result; ?>";*/
  343. $_result = "";
  344. foreach ($chars as $tag_str){
  345. if ($tag_str{0} == "$"){
  346. $_result .= "if (!isset($tag_str)) $tag_str = '';";
  347. }
  348. }
  349. return "<? $_result echo $result; ?>";
  350. }else{
  351. //$output = "<? if (!isset($result)) $result = '';\n";
  352. $output ="<?";
  353. foreach ($chars as $tag_str){
  354. if ($tag_str{0} == "$"){
  355. $output .= " if (!isset($tag_str)) $tag_str = '';";
  356. }
  357. }
  358. $output .= "\$_tmp = $result;";
  359. $output .= $_output."echo \$_tmp;unset(\$_tmp); ?>";
  360. return $output;
  361. }
  362. }
  363. //处理foreachif等的
  364. function compile_parse_var($tag_str){
  365. $parse_var = array();
  366. if (is_array($tag_str)){
  367. foreach($tag_str as $key => $value){
  368. if ($value == "=" ){
  369. $parse_var[$tag_str[$key-1]] = $this->compile_var_tag($tag_str[$key+1]);
  370. }
  371. }
  372. return $parse_var;
  373. }else{
  374. return $tag_str;
  375. }
  376. }
  377. function magic_modifier ($tag_str,$string,$parse_var){
  378. $_file = $this->plugins_dir."/modifier.".$tag_str.".php";
  379. if (file_exists($_file)){
  380. include_once $_file;
  381. $_func = "magic_modifier_".$tag_str;
  382. if (function_exists($_func)){
  383. return $_func($string,$parse_var,$this->magic_vars);
  384. }else{
  385. return $this->trigger_error("modifier: missing function '$_func' attribute");
  386. }
  387. }
  388. }
  389. function magic_function ($tag_str,$parse_var){
  390. $_file = $this->plugins_dir."/function.".$tag_str.".php";
  391. if (file_exists($_file)){
  392. include_once $_file;
  393. $_func = "magic_function_".$tag_str;
  394. if (function_exists($_func)){
  395. return $_func($parse_var);
  396. }else{
  397. return $this->trigger_error("function: missing function '$_func' attribute");
  398. }
  399. }
  400. }
  401. //直接获取的函数
  402. function magic_sqlfunc_tag ($tag_str,$parse){
  403. $_file = $this->plugins_dir."/sqlfunc.".$tag_str.".php";
  404. if (file_exists($_file)){
  405. $output = "";
  406. $parse_var = 'array'.'(';
  407. foreach ($parse as $key => $value){
  408. $_parse_var[] = '"'.$key.'"=>"'.$value.'"';
  409. }
  410. if (isset($_parse_var)){
  411. $parse_var .= join(",",$_parse_var);
  412. }
  413. $parse_var .= ")";
  414. $_tmp = "\$this->magic_vars['sqlfunc']";
  415. $output .= "<? $_tmp = \$this->magic_sqlfunc(\"".$tag_str."\",$parse_var);\n";
  416. $output .= "echo $_tmp;?>";
  417. return $output;
  418. }
  419. }
  420. //直接获取的函数
  421. function magic_sqlfunc ($tag_str,$parse_var){
  422. $_file = $this->plugins_dir."/sqlfunc.".$tag_str.".php";
  423. if (file_exists($_file)){
  424. include_once $_file;
  425. $_func = "magic_sqlfunc_".$tag_str;
  426. if (function_exists($_func)){
  427. return $_func($parse_var,$this->magic_vars,$this->mysql);
  428. }else{
  429. return $this->trigger_error("function: missing function '$_func' attribute");
  430. }
  431. }
  432. }
  433. function magic_sql_tag ($tag_str,$template_tag){
  434. $_tag_str = str_replace("/","",$tag_str);
  435. $_file = $this->plugins_dir."/sql.".$_tag_str.".php";
  436. preg_match_all('~( (?>[^"\"=\s]+))+ | [ \"=]~x', $template_tag, $match);
  437. $tag_str = $match[0];
  438. $tag_command = $tag_str[0];
  439. $_var = array();
  440. $m=0;
  441. $var = "";
  442. $q=1;
  443. $a = "b";
  444. $d =0;
  445. $n = 1;
  446. for ($i=1 ;$i<count($tag_str);$i++){
  447. if ( $i>$q && $tag_str[$i]!=" " && $a == "b"){//判断第一个的key
  448. $a = $tag_str[$i];
  449. $_var[$a] = "";
  450. }
  451. if($tag_str[$i]=="\"" && $m==0){
  452. $q = $i;
  453. $m=1;
  454. }
  455. if($tag_str[$i] == "=" ){
  456. $d=$i;
  457. }
  458. if ($m==1 && $i>$q && $i>$d && $tag_str[$i]!="\"" && $a != "b" ){
  459. $_var[$a] .= $this->compile_var_tag($tag_str[$i]);
  460. }
  461. if ($i>$q && $tag_str[$i]=="\"" && $m==1 ){
  462. $q = $i;
  463. $a = "b";
  464. $m=0;
  465. }
  466. }
  467. $parse_var = 'array'.'(';
  468. foreach ($_var as $key => $value){
  469. $_parse_var[] = '"'.$key.'"=>"'.$value.'"';
  470. }
  471. if (isset($_parse_var)){
  472. $parse_var .= join(",",$_parse_var);
  473. }
  474. $parse_var .= ")";
  475. $var_name = !isset($_var['var'])?"var":$_var['var'];
  476. $_default = !isset($_var['default'])?"":$_var['default'];
  477. if (file_exists($_file)){
  478. if ($tag_str[0]{0} != "/"){
  479. $output = "";
  480. $_tmp = "\$this->magic_vars['list']";
  481. $output .= "<? \$default = '$_default'; $_tmp = \$this->magic_sql(\"".$_tag_str."\",$parse_var,\$this->magic_vars,\$this->mysql);\n";
  482. $output .= " if (count($_tmp"."['result'])>0):foreach ($_tmp"."['result'] as \$this->magic_vars['key'] => \$this->magic_vars['$var_name']):\n;?>";
  483. }else{
  484. $output = "<? endforeach;else:echo \"\$default\";endif; ?>";
  485. }
  486. return $output;
  487. }
  488. }
  489. function magic_sql ($tag_str,$parse_var,$magic_vars,$mysql){
  490. $_file = $this->plugins_dir."/sql.".$tag_str.".php";
  491. include_once $_file;
  492. $_func = "magic_sql_".$tag_str;
  493. if (function_exists($_func)){
  494. return $_func($parse_var,$magic_vars,$mysql);
  495. }else{
  496. return $this->trigger_error("modifier: missing function '$_func' attribute");
  497. }
  498. }
  499. function magic_block ($tag_str,$parse_var){
  500. $_tag_str = str_replace("/","",$tag_str);
  501. $_file = $this->plugins_dir."/block.".$_tag_str.".php";
  502. if (file_exists($_file)){
  503. include_once $_file;
  504. $_func = "magic_block_".$_tag_str;
  505. if (function_exists($_func)){
  506. return $_func($tag_str,$parse_var,$this->magic_vars);
  507. }else{
  508. return $this->trigger_error("block: missing function '$_func' attribute");
  509. }
  510. }
  511. }
  512. function compile_literal_tag(){
  513. return "";
  514. }
  515. //变量处理
  516. function compile_var_tag($tag_str){
  517. $result = "";
  518. $chars = preg_split('/([%|-|+|*|\/|\(|\)])/',$tag_str, -1, PREG_SPLIT_DELIM_CAPTURE);//运算符
  519. foreach ($chars as $tag_str){
  520. if ($tag_str!="" && $tag_str{0} == '$' ){
  521. $tag_str = str_replace("$","",$tag_str);
  522. $_var = explode(".",$tag_str);
  523. if (count($_var)==1){
  524. if (isset($this->magic_vars['$tag_str'])) { $this->magic_vars['$tag_str'] = "";}
  525. $result .= "\$this->magic_vars['$tag_str']";
  526. }elseif ($_var[0] == "magic"){
  527. $__var = "";
  528. for($i=2;$i<count($_var);$i++){
  529. $__var .= "['$_var[$i]']";
  530. }
  531. if ($_var[1] == "request"){
  532. $result .= "\$_REQUEST".$__var;
  533. }
  534. if ($_var[1] == "server"){
  535. $result .= "\$_SERVER".$__var;
  536. }
  537. if ($_var[1] == "get"){
  538. $result .= "\$_GET".$__var;
  539. }
  540. if ($_var[1] == "post"){
  541. $result .= "\$_POST".$__var;
  542. }
  543. if ($_var[1] == "session"){
  544. $result .= "\$_SESSION".$__var;
  545. }
  546. }else{
  547. $result .= "\$this->magic_vars";
  548. foreach($_var as $key ){
  549. $result .= "['$key']";
  550. }
  551. }
  552. }else{
  553. $result .= $tag_str;
  554. }
  555. }
  556. return $result;
  557. }
  558. function compile_if_tag ($tag_command,$template_tag){
  559. if ($tag_command == "if" || $tag_command == "elseif"){
  560. $_preg_match = " |!==|*|-|+|\/|%|===|==|!=|<>|<<|>>|<=|>=|\&\&|\|\||>|<|\(|\)|\"\"|\'";
  561. preg_match_all('~( (?>[^"\''.$_preg_match.'\s]+))+ | ['.$_preg_match.']~x', $template_tag, $match);
  562. $tag_str = $match[0];
  563. array_shift($tag_str);
  564. $_result = "";
  565. $result = "";
  566. $_res = "";
  567. foreach ($tag_str as $key){
  568. if ($key{0} == "$"){
  569. $_result = $this->compile_var_tag($key);
  570. $result .= " $_result";
  571. $_res .= "if (!isset($_result)) $_result='';";
  572. }elseif ($key == "magic"){
  573. $result = "";
  574. }else{
  575. $result .= $key;
  576. }
  577. }
  578. $result = "<? $_res ;$tag_command ($result): ?>";
  579. return $result;
  580. }elseif ($tag_command == "else" ){
  581. return "<? else: ?>";
  582. }elseif ($tag_command == "/if" ){
  583. return "<? endif; ?>";
  584. }
  585. }
  586. function compile_include_tag($tag_command,$parse_var) {
  587. $arg_list = array();
  588. if (empty($parse_var['file'])) {
  589. return $this->trigger_error("include: missing 'file' attribute");
  590. }
  591. $_file = $parse_var['file'];
  592. $_arg = "";
  593. foreach ($parse_var as $key => $value) {
  594. if ($key != "file"){
  595. if ($value{0} != "$"){
  596. $arg_list[] = "'$key' => '$value'";
  597. }else{
  598. $arg_list[] = "'$key' => $value";
  599. $_arg .= "if (!isset($value)) $value=''; ";
  600. }
  601. }else{
  602. $include_file = $value;
  603. }
  604. }
  605. if ($_file{0} != "$"){
  606. return "<? \$this->magic_include(array('file' => \"" . $include_file . "\", 'vars' => array(".implode(',', (array)$arg_list).")));?>";
  607. }else{
  608. $output = "<? if (!isset($_file)) $_file='';$_arg; \$_from = $_file; ";
  609. $output .= " \$this->magic_include(array('file' => \$_from, 'vars' => array(".implode(',', (array)$arg_list)."))); unset(\$_from);?>";
  610. return $output;
  611. }
  612. }
  613. function compile_editor_tag($parse_var){
  614. $_name = $parse_var['name'];
  615. if (empty($_name)){
  616. return $this->trigger_error("editor: missing 'name' attribute");
  617. }
  618. $_code = empty($parse_var['code'])?"sinaeditor":$parse_var['code'];
  619. $_value = $parse_var['value'];
  620. if ($_value{0} != "$"){
  621. $_value = "\"$_value\"";
  622. }else{
  623. $_var = " if (!isset($_value)) $_value='';";
  624. }
  625. $result = $this->mysql->db_select("editor","code = '$_code'");
  626. return "<? $_var ; \$name = \"$_name\" ; \$value = $_value;".html_entity_decode($result['api'])." ?>";
  627. }
  628. /*
  629. function compile_loop_tag($tag_command,$parse_var){
  630. if ($tag_command == "loop" ) {
  631. $_table = "";
  632. if (isset($parse_var['tablename'])){
  633. $_table = $parse_var['tablename'];
  634. }
  635. if (empty($_table) && !empty($parse_var['table'])){
  636. $_table = $parse_var['table'];
  637. }else{
  638. return $this->trigger_error("loop: missing 'table' attribute");
  639. }
  640. $var_name = empty($parse_var['name'])?"var":$parse_var['name'];
  641. $where = empty($parse_var['where'])?"":"where ".$parse_var['where'];
  642. $mysql_result = $this->mysql->db_fetch_arrays("select * from {".$_table."} $where");
  643. $this->magic_vars["loop_".$var_name] = $mysql_result;
  644. $_magic_vars = "\$this->magic_vars['loop_$var_name']";
  645. $result = '<? ';
  646. $result .= " if(!isset($_magic_vars)) $_magic_vars = array(); \$_from = $_magic_vars; \n if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); } \n";
  647. if (isset($name)) {
  648. $foreach_props = "\$this->_foreach[$name]";
  649. $result .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
  650. $result .= "if ({$foreach_props}['total'] > 0):\n";
  651. $result .= " foreach (\$_from as $key_part\$this->magic_vars['$item']):\n";
  652. $result .= " {$foreach_props}['iteration']++;\n";
  653. } else {
  654. $result .= "if (count(\$_from)):\n";
  655. $result .= " foreach (\$_from as \$this->magic_vars['key'] => \$this->magic_vars['$var_name']):\n";
  656. }
  657. $result .= '?>';
  658. return $result;
  659. }else if ($tag_command == "/loop"){
  660. return "<? endforeach; endif; unset(\$_from); ?>";
  661. }
  662. }
  663. */
  664. function compile_foreach_tag($tag_command,$parse_var) {
  665. if ($tag_command == "foreach" ) {
  666. if (empty($parse_var['from'])) {
  667. return $this->trigger_error("foreach: missing 'from' attribute");
  668. }
  669. $from = $parse_var['from'];
  670. if (empty($parse_var['item'])) {
  671. return $this->trigger_error("foreach: missing 'item' attribute");
  672. }
  673. $item = $parse_var['item'];
  674. if (isset($parse_var['key'])) {
  675. $key = $parse_var['key'];
  676. if (!preg_match('~^\w+$~', $key)) {
  677. return $this->trigger_error("foreach: 'key' must to be a variable name (literal string)");
  678. }
  679. $key_part = "\$this->magic_vars['$key'] => ";
  680. } else {
  681. $key = null;
  682. $key_part ="\$this->magic_vars['key'] => ";
  683. }
  684. //var_dump(count($this->magic_vars['move_list']));
  685. //var_dump(count($from));
  686. $result = '<? ';
  687. $result .= " if(!isset($from) || $from=='') $from = array(); \$_from = $from; \n if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); } \n";
  688. if (isset($name)) {
  689. $foreach_props = "\$this->_foreach[$name]";
  690. $result .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
  691. $result .= "if ({$foreach_props}['total'] > 0):\n ";
  692. $result .= " foreach (\$_from as $key_part\$this->magic_vars['$item']):\n";
  693. $result .= " {$foreach_props}['iteration']++;\n";
  694. } else {
  695. $result .= "if (count(\$_from)>0):\n;";
  696. $result .= " foreach (\$_from as $key_part \$this->magic_vars['$item']):\n";
  697. }
  698. $result .= '?>';
  699. return $result;
  700. }else if ($tag_command == "/foreach"){
  701. return "<? endforeach; endif; unset(\$_from); ?>";
  702. }
  703. }
  704. /**
  705. * 触发器错误
  706. *
  707. * @param string $error_msg
  708. * @param integer $error_type
  709. */
  710. function trigger_error($error_msg, $error_type = E_USER_WARNING) {
  711. trigger_error("Magic error: $error_msg", $error_type);
  712. }
  713. }
  714. ?>