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

114 lines
3.9KB

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