国内流行的内容管理系统(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.

134 lines
4.1KB

  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) 2020, DedeBIZ.COM
  12. * @license https://www.dedebiz.com/license
  13. * @link https://www.dedebiz.com
  14. */
  15. function lib_sql(&$ctag,&$refObj)
  16. {
  17. global $dsql,$sqlCt,$cfg_soft_lang;
  18. $attlist="sql|appname";
  19. FillAttsDefault($ctag->CAttribute->Items,$attlist);
  20. extract($ctag->CAttribute->Items, EXTR_SKIP);
  21. //传递环境参数
  22. preg_match_all("/~([A-Za-z0-9]+)~/s", $sql, $conditions);
  23. $appname = empty($appname)? 'default' : $appname;
  24. if(is_array($conditions))
  25. {
  26. foreach ($conditions[1] as $key => $value)
  27. {
  28. if(isset($refObj->Fields[$value]))
  29. {
  30. $sql = str_replace($conditions[0][$key], "'".addslashes($refObj->Fields[$value])."'", $sql);
  31. }
  32. }
  33. }
  34. $revalue = '';
  35. $Innertext = trim($ctag->GetInnerText());
  36. if($sql=='' || $Innertext=='') return '';
  37. if(empty($sqlCt)) $sqlCt = 0;
  38. $ctp = new DedeTagParse();
  39. $ctp->SetNameSpace('field','[',']');
  40. $ctp->LoadSource($Innertext);
  41. $thisrs = 'sq'.$sqlCt;
  42. $GLOBALS['autoindex'] = 0;
  43. // 引入配置文件
  44. if ($appname != 'default')
  45. {
  46. require_once(DEDEDATA.'/tag/sql.inc.php');
  47. global $sqltag;
  48. $config = $sqltag[$appname];
  49. if (!isset($config['dbname'])) return '';
  50. // 链接数据库
  51. $linkid = @mysql_connect($config['dbhost'], $config['dbuser'], $config['dbpwd']);
  52. if(!$linkid) return '';
  53. @mysql_select_db($config['dbname']);
  54. $mysqlver = explode('.',$dsql->GetVersion());
  55. $mysqlver = $mysqlver[0].'.'.$mysqlver[1];
  56. // 设定数据库编码及长连接
  57. if($mysqlver > 4.0)
  58. {
  59. @mysql_query("SET NAMES '".$config['dblanguage']."', character_set_client=binary, sql_mode='', interactive_timeout=3600 ;", $linkid);
  60. }
  61. $prefix="#@__";
  62. $sql = str_replace($prefix, $config['dbprefix'], $sql);
  63. // 校验SQL字符串并获取数组返回
  64. $sql = CheckSql($sql);
  65. $rs = @mysql_query($sql, $linkid);
  66. while($row = mysql_fetch_array($rs,MYSQL_ASSOC))
  67. {
  68. $sqlCt++;
  69. $GLOBALS['autoindex']++;
  70. // 根据程序判断编码类型,并进行转码,这里主要就是gbk和utf-8
  71. if (substr($cfg_soft_lang, 0, 2) != substr($config['dblanguage'], 0, 2))
  72. {
  73. $row = AutoCharset($row, $config['dblanguage'], $cfg_soft_lang);
  74. }
  75. foreach($ctp->CTags as $tagid=>$ctag)
  76. {
  77. if($ctag->GetName()=='array')
  78. {
  79. $ctp->Assign($tagid, $row);
  80. }
  81. else
  82. {
  83. if( !empty($row[$ctag->GetName()]))
  84. {
  85. $ctp->Assign($tagid, $row[$ctag->GetName()]);
  86. } else {
  87. $ctp->Assign($tagid, "");
  88. }
  89. }
  90. }
  91. $revalue .= $ctp->GetResult();
  92. }
  93. @mysql_free_result($rs);
  94. } else {
  95. $dsql->Execute($thisrs, $sql);
  96. while($row = $dsql->GetArray($thisrs))
  97. {
  98. $sqlCt++;
  99. $GLOBALS['autoindex']++;
  100. foreach($ctp->CTags as $tagid=>$ctag)
  101. {
  102. if($ctag->GetName()=='array')
  103. {
  104. $ctp->Assign($tagid,$row);
  105. }
  106. else
  107. {
  108. if( !empty($row[$ctag->GetName()]))
  109. {
  110. $ctp->Assign($tagid,$row[$ctag->GetName()]);
  111. } else {
  112. $ctp->Assign($tagid,"");
  113. }
  114. }
  115. }
  116. $revalue .= $ctp->GetResult();
  117. }
  118. }
  119. return $revalue;
  120. }