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

100 lines
3.8KB

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