Blog.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace addons\blog\taglib;
  3. use fast\Random;
  4. use think\Cache;
  5. use think\template\TagLib;
  6. class Blog extends TagLib
  7. {
  8. /**
  9. * 定义标签列表
  10. */
  11. protected $tags = [
  12. // 标签定义: attr 属性列表 close 是否闭合(0 或者1 默认1) alias 标签别名 level 嵌套层次
  13. 'config' => ['attr' => 'name', 'close' => 0],
  14. 'execute' => ['attr' => 'sql', 'close' => 0],
  15. 'block' => ['attr' => 'id,name', 'close' => 0],
  16. 'query' => ['attr' => 'id,empty,key,mod,sql,cache', 'close' => 1],
  17. 'arclist' => ['attr' => 'id,row,limit,empty,key,mod,cache,orderby,orderway,imgwidth,imgheight,condition,model,type,field,flag', 'close' => 1],
  18. 'blocklist' => ['attr' => 'id,row,limit,empty,key,mod,cache,orderby,orderway,imgwidth,imgheight,condition,name', 'close' => 1],
  19. ];
  20. public function tagExecute($tag, $content)
  21. {
  22. $sql = isset($tag['sql']) ? $tag['sql'] : '';
  23. $sql = addslashes($sql);
  24. $parse = '<?php ';
  25. $parse .= '\think\Db::execute(\'' . $sql . '\');';
  26. $parse .= ' ?>';
  27. return $parse;
  28. }
  29. public function tagQuery($tag, $content)
  30. {
  31. $id = isset($tag['id']) ? $tag['id'] : 'item';
  32. $empty = isset($tag['empty']) ? $tag['empty'] : '';
  33. $key = !empty($tag['key']) ? $tag['key'] : 'i';
  34. $mod = isset($tag['mod']) ? $tag['mod'] : '2';
  35. $params = [];
  36. foreach ($tag as $k => & $v) {
  37. if (in_array($k, ['condition'])) {
  38. $v = $this->autoBuildVar($v);
  39. }
  40. $v = '"' . $v . '"';
  41. $params[] = '"' . $k . '"=>' . $v;
  42. }
  43. $var = Random::alnum(10);
  44. $parse = '<?php ';
  45. $parse .= '$__' . $var . '__ = \addons\blog\model\Post::getQueryList([' . implode(',', $params) . ']);';
  46. $parse .= ' ?>';
  47. $parse .= '{volist name="$__' . $var . '__" id="' . $id . '" empty="' . $empty . '" key="' . $key . '" mod="' . $mod . '"}';
  48. $parse .= $content;
  49. $parse .= '{/volist}';
  50. $parse .= '{php}$__LASTLIST__=$__' . $var . '__;{/php}';
  51. return $parse;
  52. }
  53. public function tagArclist($tag, $content)
  54. {
  55. $id = $tag['id'];
  56. $empty = isset($tag['empty']) ? $tag['empty'] : '';
  57. $key = !empty($tag['key']) ? $tag['key'] : 'i';
  58. $mod = isset($tag['mod']) ? $tag['mod'] : '2';
  59. $params = [];
  60. foreach ($tag as $k => & $v) {
  61. if (in_array($k, ['category', 'condition', 'keywords'])) {
  62. $v = $this->autoBuildVar($v);
  63. }
  64. if (in_array($k, ['category', 'keywords'])) {
  65. $v = preg_match("/^\d+[0-9\,]+\d+$/i", $v) ? '"' . $v . '"' : $v;
  66. } else {
  67. $v = '"' . $v . '"';
  68. }
  69. $params[] = '"' . $k . '"=>' . $v;
  70. }
  71. $var = Random::alnum(10);
  72. $parse = '<?php ';
  73. $parse .= '$__' . $var . '__ = \addons\blog\model\Post::getPostList([' . implode(',', $params) . ']);';
  74. $parse .= ' ?>';
  75. $parse .= '{volist name="$__' . $var . '__" id="' . $id . '" empty="' . $empty . '" key="' . $key . '" mod="' . $mod . '"}';
  76. $parse .= $content;
  77. $parse .= '{/volist}';
  78. $parse .= '{php}$__LASTLIST__=$__' . $var . '__;{/php}';
  79. return $parse;
  80. }
  81. public function tagConfig($tag)
  82. {
  83. $name = $tag['name'];
  84. $parse = '<?php ';
  85. $parse .= 'echo \think\Config::get("' . $name . '");';
  86. $parse .= ' ?>';
  87. return $parse;
  88. }
  89. public function tagBlock($tag)
  90. {
  91. return \addons\blog\model\Block::getBlockContent($tag);
  92. }
  93. public function tagBlocklist($tag, $content)
  94. {
  95. $id = $tag['id'];
  96. $empty = isset($tag['empty']) ? $tag['empty'] : '';
  97. $key = !empty($tag['key']) ? $tag['key'] : 'i';
  98. $mod = isset($tag['mod']) ? $tag['mod'] : '2';
  99. $params = [];
  100. foreach ($tag as $k => & $v) {
  101. if (in_array($k, ['condition'])) {
  102. $v = $this->autoBuildVar($v);
  103. }
  104. $v = '"' . $v . '"';
  105. $params[] = '"' . $k . '"=>' . $v;
  106. }
  107. $var = Random::alnum(10);
  108. $parse = '<?php ';
  109. $parse .= '$__' . $var . '__ = \addons\blog\model\Block::getBlockList([' . implode(',', $params) . ']);';
  110. $parse .= ' ?>';
  111. $parse .= '{volist name="$__' . $var . '__" id="' . $id . '" empty="' . $empty . '" key="' . $key . '" mod="' . $mod . '"}';
  112. $parse .= $content;
  113. $parse .= '{/volist}';
  114. $parse .= '{php}$__LASTLIST__=$__' . $var . '__;{/php}';
  115. return $parse;
  116. }
  117. }