国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.7KB

  1. <?php
  2. if(!defined('DEDEINC'))
  3. {
  4. exit("Request Error!");
  5. }
  6. /**
  7. * SQL标签
  8. *
  9. * @version $Id: sql.lib.php 2 10:00 2010-11-11 tianya $
  10. * @package DedeCMS.Taglib
  11. * @copyright Copyright (c) 2007 - 2020, DesDev, Inc.
  12. * @license http://help.dedecms.com/usersguide/license.html
  13. * @link http://www.dedecms.com
  14. */
  15. /*>>dede>>
  16. <name>SQL标签</name>
  17. <type>全局标记</type>
  18. <for>V55,V56,V57</for>
  19. <description>用于获取MySQL数据库内容的标签</description>
  20. <demo>
  21. {dede:sql sql='' db='default'}
  22. [field:title/]
  23. {/dede}
  24. </demo>
  25. <attributes>
  26. <iterm>sql:需要查询的sql语句</iterm>
  27. <iterm>appname: 默认为default,即当前dedecms的数据库,如果需要自定义,可以在data/tag/sql.inc.php中扩展,具体扩展方法查看配置文件头部说明</iterm>
  28. </attributes>
  29. >>dede>>*/
  30. function lib_sql(&$ctag,&$refObj)
  31. {
  32. global $dsql,$sqlCt,$cfg_soft_lang;
  33. $attlist="sql|appname";
  34. FillAttsDefault($ctag->CAttribute->Items,$attlist);
  35. extract($ctag->CAttribute->Items, EXTR_SKIP);
  36. //传递环境参数
  37. preg_match_all("/~([A-Za-z0-9]+)~/s", $sql, $conditions);
  38. $appname = empty($appname)? 'default' : $appname;
  39. if(is_array($conditions))
  40. {
  41. foreach ($conditions[1] as $key => $value)
  42. {
  43. if(isset($refObj->Fields[$value]))
  44. {
  45. $sql = str_replace($conditions[0][$key], "'".addslashes($refObj->Fields[$value])."'", $sql);
  46. }
  47. }
  48. }
  49. $revalue = '';
  50. $Innertext = trim($ctag->GetInnerText());
  51. if($sql=='' || $Innertext=='') return '';
  52. if(empty($sqlCt)) $sqlCt = 0;
  53. $ctp = new DedeTagParse();
  54. $ctp->SetNameSpace('field','[',']');
  55. $ctp->LoadSource($Innertext);
  56. $thisrs = 'sq'.$sqlCt;
  57. $GLOBALS['autoindex'] = 0;
  58. // 引入配置文件
  59. if ($appname != 'default')
  60. {
  61. require_once(DEDEDATA.'/tag/sql.inc.php');
  62. global $sqltag;
  63. $config = $sqltag[$appname];
  64. if (!isset($config['dbname'])) return '';
  65. // 链接数据库
  66. $linkid = @mysql_connect($config['dbhost'], $config['dbuser'], $config['dbpwd']);
  67. if(!$linkid) return '';
  68. @mysql_select_db($config['dbname']);
  69. $mysqlver = explode('.',$dsql->GetVersion());
  70. $mysqlver = $mysqlver[0].'.'.$mysqlver[1];
  71. // 设定数据库编码及长连接
  72. if($mysqlver > 4.0)
  73. {
  74. @mysql_query("SET NAMES '".$config['dblanguage']."', character_set_client=binary, sql_mode='', interactive_timeout=3600 ;", $linkid);
  75. }
  76. $prefix="#@__";
  77. $sql = str_replace($prefix, $config['dbprefix'], $sql);
  78. // 校验SQL字符串并获取数组返回
  79. $sql = CheckSql($sql);
  80. $rs = @mysql_query($sql, $linkid);
  81. while($row = mysql_fetch_array($rs,MYSQL_ASSOC))
  82. {
  83. $sqlCt++;
  84. $GLOBALS['autoindex']++;
  85. // 根据程序判断编码类型,并进行转码,这里主要就是gbk和utf-8
  86. if (substr($cfg_soft_lang, 0, 2) != substr($config['dblanguage'], 0, 2))
  87. {
  88. $row = AutoCharset($row, $config['dblanguage'], $cfg_soft_lang);
  89. }
  90. foreach($ctp->CTags as $tagid=>$ctag)
  91. {
  92. if($ctag->GetName()=='array')
  93. {
  94. $ctp->Assign($tagid, $row);
  95. }
  96. else
  97. {
  98. if( !empty($row[$ctag->GetName()]))
  99. {
  100. $ctp->Assign($tagid, $row[$ctag->GetName()]);
  101. } else {
  102. $ctp->Assign($tagid, "");
  103. }
  104. }
  105. }
  106. $revalue .= $ctp->GetResult();
  107. }
  108. @mysql_free_result($rs);
  109. } else {
  110. $dsql->Execute($thisrs, $sql);
  111. while($row = $dsql->GetArray($thisrs))
  112. {
  113. $sqlCt++;
  114. $GLOBALS['autoindex']++;
  115. foreach($ctp->CTags as $tagid=>$ctag)
  116. {
  117. if($ctag->GetName()=='array')
  118. {
  119. $ctp->Assign($tagid,$row);
  120. }
  121. else
  122. {
  123. if( !empty($row[$ctag->GetName()]))
  124. {
  125. $ctp->Assign($tagid,$row[$ctag->GetName()]);
  126. } else {
  127. $ctp->Assign($tagid,"");
  128. }
  129. }
  130. }
  131. $revalue .= $ctp->GetResult();
  132. }
  133. }
  134. return $revalue;
  135. }