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

800 lines
29KB

  1. <?php
  2. if (!defined('DEDEINC')) exit ('dedebiz');
  3. /**
  4. * 系统存放函数
  5. *
  6. * @version $id:common.func.php 4 16:39 2010年7月6日 tianya $
  7. * @package DedeBIZ.Libraries
  8. * @copyright Copyright (c) 2022 DedeBIZ.COM
  9. * @license GNU GPL v2 (https://www.dedebiz.com/license)
  10. * @link https://www.dedebiz.com
  11. */
  12. require_once DEDEINC."/archive/partview.class.php";
  13. if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
  14. if (!function_exists('mysql_connect') and function_exists('mysqli_connect')) {
  15. function mysql_connect($server, $username, $password)
  16. {
  17. return mysqli_connect($server, $username, $password);
  18. }
  19. }
  20. if (!function_exists('mysql_query') and function_exists('mysqli_query')) {
  21. function mysql_query($query, $link)
  22. {
  23. return mysqli_query($link, $query);
  24. }
  25. }
  26. if (!function_exists('mysql_select_db') and function_exists('mysqli_select_db')) {
  27. function mysql_select_db($database_name, $link)
  28. {
  29. return mysqli_select_db($link, $database_name);
  30. }
  31. }
  32. if (!function_exists('mysql_fetch_array') and function_exists('mysqli_fetch_array')) {
  33. function mysql_fetch_array($result)
  34. {
  35. return mysqli_fetch_array($result);
  36. }
  37. }
  38. if (!function_exists('mysql_close') and function_exists('mysqli_close')) {
  39. function mysql_close($link)
  40. {
  41. if ($link) {
  42. return @mysqli_close($link);
  43. } else {
  44. return false;
  45. }
  46. }
  47. }
  48. if (!function_exists('mysql_error') and function_exists('mysqli_connect_error')) {
  49. function mysql_error($link)
  50. {
  51. if (mysqli_connect_errno()) {
  52. return mysqli_connect_error();
  53. }
  54. if ($link) {
  55. return @mysqli_error($link);
  56. } else {
  57. return false;
  58. }
  59. }
  60. }
  61. if (!function_exists('mysql_free_result') and function_exists('mysqli_free_result')) {
  62. function mysql_free_result($result)
  63. {
  64. return mysqli_free_result($result);
  65. }
  66. }
  67. if (!function_exists('split')) {
  68. function split($pattern, $string)
  69. {
  70. return explode($pattern, $string);
  71. }
  72. }
  73. }
  74. //一个支持在PHP Cli Server打印的方法
  75. function var_dump_cli($val,...$values)
  76. {
  77. ob_start();
  78. var_dump($val,$values);
  79. error_log(ob_get_clean(), 4);
  80. }
  81. function get_mime_type($filename)
  82. {
  83. if (!function_exists('finfo_open')) {
  84. return 'unknow/octet-stream';
  85. }
  86. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  87. $mimeType = finfo_file($finfo, $filename);
  88. if (preg_match('#\.(php|pl|cgi|asp|aspx|jsp|php5|php4|php3|shtm|shtml|htm)$#i', trim($filename))) {
  89. return 'forbid/octet-stream';
  90. }
  91. finfo_close($finfo);
  92. return $mimeType;
  93. }
  94. function is_all_numeric(array $array)
  95. {
  96. foreach ($array as $item) {
  97. if (!is_numeric($item)) return false;
  98. }
  99. return true;
  100. }
  101. function make_hash()
  102. {
  103. $rand = dede_random_bytes(16);
  104. $_SESSION['token'] = ($rand === FALSE) ? md5(uniqid(mt_rand(), TRUE)) : bin2hex($rand);
  105. return $_SESSION['token'];
  106. }
  107. function dede_random_bytes($length)
  108. {
  109. if (empty($length) or !ctype_digit((string) $length)) {
  110. return FALSE;
  111. }
  112. if (function_exists('openssl_random_pseudo_bytes')) {
  113. return openssl_random_pseudo_bytes($length);
  114. }
  115. if (function_exists('random_bytes')) {
  116. try {
  117. return random_bytes((int) $length);
  118. } catch (Exception $e) {
  119. return FALSE;
  120. }
  121. }
  122. if (is_readable('/dev/urandom') && ($fp = fopen('/dev/urandom', 'rb')) !== FALSE) {
  123. version_compare(PHP_VERSION, '5.4.0', '>=') && stream_set_chunk_size($fp, $length);
  124. $output = fread($fp, $length);
  125. fclose($fp);
  126. if ($output !== FALSE) {
  127. return $output;
  128. }
  129. }
  130. return FALSE;
  131. }
  132. //SQL语句过滤程序,由80sec提供,这里作了适当的修改
  133. if (!function_exists('CheckSql')) {
  134. function CheckSql($db_string, $querytype = 'select')
  135. {
  136. global $cfg_cookie_encode;
  137. $clean = '';
  138. $error = '';
  139. $old_pos = 0;
  140. $pos = -1;
  141. $enkey = substr(md5(substr($cfg_cookie_encode.'dedebiz', 0, 5)), 0, 10);
  142. $log_file = DEDEDATA.'/checksql_'.$enkey.'_safe.txt';
  143. $userIP = GetIP();
  144. $getUrl = GetCurUrl();
  145. //如果是普通查询语句,直接过滤一些特殊语法
  146. if ($querytype == 'select') {
  147. $notallow1 = "[^0-9a-z@\._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@\.-]{1,}";
  148. if (preg_match("/".$notallow1."/i", $db_string)) {
  149. fputs(fopen($log_file, 'a+'), "$userIP||$getUrl||$db_string||SelectBreak\r\n");
  150. exit("<span>Safe Alert: Request Error step 1 !</span>");
  151. }
  152. }
  153. //完整的SQL检查
  154. while (TRUE) {
  155. $pos = strpos($db_string, '\'', $pos + 1);
  156. if ($pos === FALSE) {
  157. break;
  158. }
  159. $clean .= substr($db_string, $old_pos, $pos - $old_pos);
  160. while (TRUE) {
  161. $pos1 = strpos($db_string, '\'', $pos + 1);
  162. $pos2 = strpos($db_string, '\\', $pos + 1);
  163. if ($pos1 === FALSE) {
  164. break;
  165. } elseif ($pos2 == FALSE || $pos2 > $pos1) {
  166. $pos = $pos1;
  167. break;
  168. }
  169. $pos = $pos2 + 1;
  170. }
  171. $clean .= '$s$';
  172. $old_pos = $pos + 1;
  173. }
  174. $clean .= substr($db_string, $old_pos);
  175. $clean = trim(strtolower(preg_replace(array('~\s+~s'), array(' '), $clean)));
  176. if (
  177. strpos($clean, '@') !== FALSE or strpos($clean, 'char(') !== FALSE or strpos($clean, '"') !== FALSE
  178. or strpos($clean, '$s$$s$') !== FALSE
  179. ) {
  180. $fail = TRUE;
  181. if (preg_match("#^create table#i", $clean)) $fail = FALSE;
  182. $error = "unusual character";
  183. }
  184. //老版本数据库不支持union,程序不使用union,但黑客使用它,所以检查它
  185. if (strpos($clean, 'union') !== FALSE && preg_match('~(^|[^a-z])union($|[^[a-z])~s', $clean) != 0) {
  186. $fail = TRUE;
  187. $error = "union detect";
  188. }
  189. //发布版本的程序比较少包括--,#这样的注释,但黑客经常使用它们
  190. elseif (strpos($clean, '/*') > 2 || strpos($clean, '--') !== FALSE || strpos($clean, '#') !== FALSE) {
  191. $fail = TRUE;
  192. $error = "comment detect";
  193. }
  194. //这些函数不会被使用,但是黑客会用它来操作文件,down掉数据库
  195. elseif (strpos($clean, 'sleep') !== FALSE && preg_match('~(^|[^a-z])sleep($|[^[a-z])~s', $clean) != 0) {
  196. $fail = TRUE;
  197. $error = "slown down detect";
  198. } elseif (strpos($clean, 'benchmark') !== FALSE && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) {
  199. $fail = TRUE;
  200. $error = "slown down detect";
  201. } elseif (strpos($clean, 'load_file') !== FALSE && preg_match('~(^|[^a-z])load_file($|[^[a-z])~s', $clean) != 0) {
  202. $fail = TRUE;
  203. $error = "file fun detect";
  204. } elseif (strpos($clean, 'into outfile') !== FALSE && preg_match('~(^|[^a-z])into\s+outfile($|[^[a-z])~s', $clean) != 0) {
  205. $fail = TRUE;
  206. $error = "file fun detect";
  207. }
  208. //老版本数据库不支持子查询,该功能也用得少,但黑客可以使用它来查询数据库敏感信息
  209. elseif (preg_match('~\([^)]*?select~s', $clean) != 0) {
  210. $fail = TRUE;
  211. $error = "sub select detect";
  212. }
  213. if (!empty($fail)) {
  214. fputs(fopen($log_file, 'a+'), "$userIP||$getUrl||$db_string||$error\r\n");
  215. exit("<span>Safe Alert: Request Error step 2!</span>");
  216. } else {
  217. return $db_string;
  218. }
  219. }
  220. }
  221. /**
  222. * 载入助手,系统默认载入助手示例
  223. * <code>
  224. * if (!function_exists('HelloDede'))
  225. * {
  226. * function HelloDede()
  227. * {
  228. * echo "Hello! Dede";
  229. * }
  230. * }
  231. * </code>
  232. * 开发中使用这个助手的时候直接使用函数helper('test');初始化它,然后在文件中就可以直接使用:HelloDede();调用
  233. *
  234. * @access public
  235. * @param mixed $helpers 助手名称,可以是数组,可以是单个字符串
  236. * @return void
  237. */
  238. $_helpers = array();
  239. function helper($helpers)
  240. {
  241. //如果是数组,则进行递归操作
  242. if (is_array($helpers)) {
  243. foreach ($helpers as $dede) {
  244. helper($dede);
  245. }
  246. return;
  247. }
  248. if (isset($_helpers[$helpers])) {
  249. return;
  250. }
  251. if (file_exists(DEDEINC.'/helpers/'.$helpers.'.helper.php')) {
  252. include_once(DEDEINC.'/helpers/'.$helpers.'.helper.php');
  253. $_helpers[$helpers] = TRUE;
  254. }
  255. //无法载入助手
  256. if (!isset($_helpers[$helpers])) {
  257. exit('Unable to load the requested file: helpers/'.$helpers.'.helper.php');
  258. }
  259. }
  260. function dede_htmlspecialchars($str)
  261. {
  262. global $cfg_soft_lang;
  263. if (version_compare(PHP_VERSION, '5.4.0', '<')) return htmlspecialchars($str);
  264. if ($cfg_soft_lang == 'gb2312') return htmlspecialchars($str, ENT_COMPAT, 'ISO-8859-1');
  265. else return htmlspecialchars($str);
  266. }
  267. /**
  268. * 载入助手,这里会员载入用helps载入多个助手
  269. *
  270. * @access public
  271. * @param string
  272. * @return void
  273. */
  274. function helpers($helpers)
  275. {
  276. helper($helpers);
  277. }
  278. //兼容php4的file_put_contents
  279. if (!function_exists('file_put_contents')) {
  280. function file_put_contents($n, $d)
  281. {
  282. $f = @fopen($n, "w");
  283. if (!$f) {
  284. return FALSE;
  285. } else {
  286. fwrite($f, $d);
  287. fclose($f);
  288. return TRUE;
  289. }
  290. }
  291. }
  292. /**
  293. * 短消息函数,可以在某个动作处理后友好的系统提示
  294. *
  295. * @param string $msg 消息系统提示
  296. * @param string $gourl 跳转地址
  297. * @param int $onlymsg 仅显示信息
  298. * @param int $limittime 限制时间
  299. * @param string $btnmsg 按钮提示
  300. * @param string $target 跳转类型
  301. * @return void
  302. */
  303. function ShowMsg($msg, $gourl, $onlymsg = 0, $limittime = 0)
  304. {
  305. if (defined('DEDE_DIALOG_UPLOAD') && !isset($GLOBALS['noeditor'])) {
  306. echo json_encode(array(
  307. "uploaded"=>0,
  308. "error"=>array(
  309. "message" => $msg,
  310. ),
  311. ));
  312. return;
  313. }
  314. if (isset($GLOBALS['format']) && strtolower($GLOBALS['format'])==='json') {
  315. echo json_encode(array(
  316. "code"=>0,
  317. "msg"=>$msg,
  318. "gourl"=>$gourl,
  319. ));
  320. return;
  321. }
  322. if (empty($GLOBALS['cfg_plus_dir'])) $GLOBALS['cfg_plus_dir'] = '..';
  323. $htmlhead = "<!DOCTYPE html><html><head><meta charset='utf-8'><meta http-equiv='X-UA-Compatible' content='IE=Edge,chrome=1'><meta name='viewport' content='width=device-width,initial-scale=1'><title>系统提示</title><link rel='stylesheet' href='/static/web/css/bootstrap.min.css'><link rel='stylesheet' href='/static/web/css/admin.css'></head><base target='_self'><body>";
  324. $htmlfoot = "</body></html>";
  325. $litime = ($limittime == 0 ? 1000 : $limittime);
  326. $func = '';
  327. if ($gourl == '-1') {
  328. if ($limittime == 0) $litime = 3000;
  329. $gourl = "javascript:history.go(-1);";
  330. }
  331. if ($gourl == '' || $onlymsg == 1) {
  332. $msg = "<script>alert(\"".str_replace("\"", "“", $msg)."\");</script>";
  333. } else {
  334. //当网址为:close::objname时,关闭父框架的id=objname元素
  335. if (preg_match('/close::/', $gourl)) {
  336. $tgobj = trim(preg_replace('/close::/', '', $gourl));
  337. $gourl = 'javascript:;';
  338. $func .= "<script>window.parent.document.getElementById('{$tgobj}').style.display='none';</script>";
  339. }
  340. $func .= "<script>var pgo=0;function JumpUrl(){if (pgo==0) {location='$gourl'; pgo=1;}}</script>";
  341. $rmsg = $func;
  342. $rmsg .= "<div class='tips'><div class='tips-box shadow-sm'><div class='tips-head'><p>系统提示</p></div>";
  343. $rmsg .= "<div class='tips-body'>";
  344. $rmsg .= "".str_replace("\"", "“", $msg)."";
  345. $rmsg .= "";
  346. if ($onlymsg == 0) {
  347. if ($gourl != 'javascript:;' && $gourl != '') {
  348. $rmsg .= "<div class='text-center mt-3'><a href='{$gourl}' class='btn btn-success btn-sm'>点击反应</a></div>";
  349. $rmsg .= "<script>setTimeout('JumpUrl()', $litime);</script>";
  350. } else {
  351. $rmsg .= "</div>";
  352. }
  353. } else {
  354. $rmsg .= "</div></div>";
  355. }
  356. $msg = $htmlhead.$rmsg.$htmlfoot;
  357. }
  358. echo $msg;
  359. }
  360. /**
  361. * 表中是否存在某个字段
  362. *
  363. * @param mixed $tablename 表名称
  364. * @param mixed $field 字段名
  365. * @return bool
  366. */
  367. function TableHasField($tablename, $field)
  368. {
  369. global $dsql,$cfg_dbtype;
  370. if ($cfg_dbtype === 'mysql') {
  371. $dsql->GetTableFields($tablename,"tfd");
  372. while ($r = $dsql->GetFieldObject("tfd")) {
  373. if ($r->name === $field) {
  374. return true;
  375. }
  376. }
  377. return false;
  378. } elseif ($cfg_dbtype === 'sqlite') {
  379. $k = $dsql->GetTableFields($tablename);
  380. while ($r = $dsql->GetFieldObject($k)) {
  381. if ($r->name === $field) {
  382. return true;
  383. }
  384. }
  385. return false;
  386. } else {
  387. return false;
  388. }
  389. }
  390. function GetSimpleServerSoftware()
  391. {
  392. if (preg_match("#^php#i",$_SERVER["SERVER_SOFTWARE"])) {
  393. return 'PHP Server';
  394. } else if (preg_match("#^apache#i",$_SERVER["SERVER_SOFTWARE"])){
  395. return 'Apache';
  396. } else if (preg_match("#^nginx#i",$_SERVER["SERVER_SOFTWARE"])){
  397. return 'Nginx';
  398. } else if (preg_match("#^microsoft-iis#i",$_SERVER["SERVER_SOFTWARE"])){
  399. return 'IIS';
  400. } else if (preg_match("#^caddy#i",$_SERVER["SERVER_SOFTWARE"])){
  401. return 'Caddy';
  402. } else {
  403. return 'Other';
  404. }
  405. }
  406. /**
  407. * 获取验证码的session值
  408. *
  409. * @return string
  410. */
  411. function GetCkVdValue()
  412. {
  413. @session_id($_COOKIE['PHPSESSID']);
  414. @session_start();
  415. return isset($_SESSION['securimage_code_value']) ? $_SESSION['securimage_code_value'] : '';
  416. }
  417. /**
  418. * PHP某些版本有Bug,不能在同一作用域中同时读session并改注销它,因此调用后需执行本函数
  419. *
  420. * @return void
  421. */
  422. function ResetVdValue()
  423. {
  424. @session_start();
  425. $_SESSION['securimage_code_value'] = '';
  426. }
  427. function IndexSub($idx, $num)
  428. {
  429. return intval($idx) - intval($num) == 0 ? '0 ' : intval($idx) - intval($num);
  430. }
  431. /**
  432. * HideEmail隐藏邮箱
  433. *
  434. * @param mixed $email
  435. * @return string
  436. */
  437. function HideEmail($email)
  438. {
  439. if (empty($email)) return "暂无";
  440. $em = explode("@",$email);
  441. $name = implode('@', array_slice($em, 0, count($em)-1));
  442. $len = floor(strlen($name)/2);
  443. return substr($name,0, $len).str_repeat('*', $len)."@".end($em);
  444. }
  445. //用来返回index的active
  446. function IndexActive($idx)
  447. {
  448. if ($idx == 1) {
  449. return ' active';
  450. } else {
  451. return '';
  452. }
  453. }
  454. //是否是HTTPS
  455. function IsSSL()
  456. {
  457. if (@$_SERVER['HTTPS'] && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
  458. return true;
  459. } elseif ('https' == @$_SERVER['REQUEST_SCHEME']) {
  460. return true;
  461. } elseif ('443' == $_SERVER['SERVER_PORT']) {
  462. return true;
  463. } elseif ('https' == @$_SERVER['HTTP_X_FORWARDED_PROTO']) {
  464. return true;
  465. }
  466. return false;
  467. }
  468. //获取对应版本号的更新SQL
  469. function GetUpdateSQL()
  470. {
  471. global $cfg_dbprefix, $cfg_dbtype, $cfg_db_language;
  472. $result = array();
  473. $query = '';
  474. $sql4tmp = "ENGINE=MyISAM DEFAULT CHARSET=".$cfg_db_language;
  475. $fp = fopen(DEDEDATA.'/admin/update.txt','r');
  476. $sqls = array();
  477. $current_ver = '';
  478. while(!feof($fp))
  479. {
  480. $line = rtrim(fgets($fp,1024));
  481. if (preg_match("/\-\- ([\d\.]+)/",$line,$matches)) {
  482. if (count($sqls) > 0) {
  483. $result[$current_ver] = $sqls;
  484. }
  485. $sqls = array();
  486. $current_ver = $matches[1];
  487. }
  488. if (preg_match("#;$#", $line)) {
  489. $query .= $line."\n";
  490. $query = str_replace('#@__',$cfg_dbprefix,$query);
  491. if ($cfg_dbtype == 'sqlite') {
  492. $query = preg_replace('/character set (.*?) /i','',$query);
  493. $query = preg_replace('/unsigned/i','',$query);
  494. $query = str_replace('TYPE=MyISAM','',$query);
  495. $query = preg_replace ('/TINYINT\(([\d]+)\)/i','INTEGER',$query);
  496. $query = preg_replace ('/mediumint\(([\d]+)\)/i','INTEGER',$query);
  497. $query = preg_replace ('/smallint\(([\d]+)\)/i','INTEGER',$query);
  498. $query = preg_replace('/int\(([\d]+)\)/i','INTEGER',$query);
  499. $query = preg_replace('/auto_increment/i','PRIMARY KEY AUTOINCREMENT',$query);
  500. $query = preg_replace('/,([\t\s ]+)KEY(.*?)MyISAM;/','',$query);
  501. $query = preg_replace('/,([\t\s ]+)KEY(.*?);/',');',$query);
  502. $query = preg_replace('/,([\t\s ]+)UNIQUE KEY(.*?);/',');',$query);
  503. $query = preg_replace('/set\(([^\)]*?)\)/','varchar',$query);
  504. $query = preg_replace('/enum\(([^\)]*?)\)/','varchar',$query);
  505. if (preg_match("/PRIMARY KEY AUTOINCREMENT/",$query)) {
  506. $query = preg_replace('/,([\t\s ]+)PRIMARY KEY([\t\s ]+)\(`([0-9a-zA-Z]+)`\)/i','',$query);
  507. }
  508. $sqls[] = $query;
  509. } else {
  510. if (preg_match('#CREATE#i', $query)) {
  511. $sqls[] = preg_replace("#TYPE=MyISAM#i",$sql4tmp,$query);
  512. } else {
  513. $sqls[] = $query;
  514. }
  515. }
  516. $query='';
  517. } else if (!preg_match("#^(\/\/|--)#", $line)) {
  518. $query .= $line;
  519. }
  520. }
  521. if (count($sqls) > 0) {
  522. $result[$current_ver] = $sqls;
  523. }
  524. fclose($fp);
  525. return $result;
  526. }
  527. /**
  528. * GetMimeTypeOrExtension
  529. *
  530. * @param mixed $str 字符串
  531. * @param mixed $t 类型,0获取mime type,1获取扩展名
  532. * @return string
  533. */
  534. function GetMimeTypeOrExtension($str, $t = 0) {
  535. $mime_types = array(
  536. 'aac' => 'audio/aac',
  537. 'abw' => 'application/x-abiword',
  538. 'arc' => 'application/x-freearc',
  539. 'avi' => 'video/x-msvideo',
  540. 'azw' => 'application/vnd.amazon.ebook',
  541. 'bin' => 'application/octet-stream',
  542. 'bmp' => 'image/bmp',
  543. 'bz' => 'application/x-bzip',
  544. 'bz2' => 'application/x-bzip2',
  545. 'csh' => 'application/x-csh',
  546. 'css' => 'text/css',
  547. 'csv' => 'text/csv',
  548. 'doc' => 'application/msword',
  549. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  550. 'eot' => 'application/vnd.ms-fontobject',
  551. 'epub' => 'application/epub+zip',
  552. 'gif' => 'image/gif',
  553. 'htm' => 'text/html',
  554. 'html' => 'text/html',
  555. 'ico' => 'image/vnd.microsoft.icon',
  556. 'ics' => 'text/calendar',
  557. 'jar' => 'application/java-archive',
  558. 'jpeg' => 'image/jpeg',
  559. 'jpg' => 'image/jpeg',
  560. 'js' => 'text/javascript',
  561. 'json' => 'application/json',
  562. 'jsonld' => 'application/ld+json',
  563. 'mid' => 'audio/midi',
  564. 'midi' => 'audio/midi',
  565. 'mjs' => 'text/javascript',
  566. 'mp3' => 'audio/mpeg',
  567. 'mp4' => 'video/mp4',
  568. 'mpeg' => 'video/mpeg',
  569. 'mpkg' => 'application/vnd.apple.installer+xml',
  570. 'odp' => 'application/vnd.oasis.opendocument.presentation',
  571. 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  572. 'odt' => 'application/vnd.oasis.opendocument.text',
  573. 'oga' => 'audio/ogg',
  574. 'ogv' => 'video/ogg',
  575. 'ogx' => 'application/ogg',
  576. 'otf' => 'font/otf',
  577. 'png' => 'image/png',
  578. 'pdf' => 'application/pdf',
  579. 'ppt' => 'application/vnd.ms-powerpoint',
  580. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  581. 'rar' => 'application/x-rar-compressed',
  582. 'rtf' => 'application/rtf',
  583. 'sh' => 'application/x-sh',
  584. 'svg' => 'image/svg+xml',
  585. 'swf' => 'application/x-shockwave-flash',
  586. 'tar' => 'application/x-tar',
  587. 'tif' => 'image/tiff',
  588. 'tiff' => 'image/tiff',
  589. 'ttf' => 'font/ttf',
  590. 'txt' => 'text/plain',
  591. 'vsd' => 'application/vnd.visio',
  592. 'wav' => 'audio/wav',
  593. 'weba' => 'audio/webm',
  594. 'webm' => 'video/webm',
  595. 'webp' => 'image/webp',
  596. 'woff' => 'font/woff',
  597. 'woff2' => 'font/woff2',
  598. 'xhtml' => 'application/xhtml+xml',
  599. 'xls' => 'application/vnd.ms-excel',
  600. 'xlsx' => 'application/vnd.ms-excel',
  601. 'xml' => 'application/xml',
  602. 'xul' => 'application/vnd.mozilla.xul+xml',
  603. 'zip' => 'application/zip',
  604. '3gp' => 'video/3gpp',
  605. '3g2' => 'video/3gpp2',
  606. '7z' => 'application/x-7z-compressed',
  607. 'wmv' => 'video/x-ms-asf',
  608. 'wma' => 'audio/x-ms-wma',
  609. 'mov' => 'video/quicktime',
  610. 'rm' => 'application/vnd.rn-realmedia',
  611. 'mpg' => 'video/mpeg',
  612. 'mpga' => 'audio/mpeg',
  613. );
  614. if ($t===0) {
  615. return isset($mime_types[$str])? $mime_types[$str] : 'application/octet-stream';
  616. } else {
  617. foreach ($mime_types as $key => $value) {
  618. if ($value == $str) return $key;
  619. }
  620. return "dedebiz";
  621. }
  622. }
  623. //用于实际请求接口并返回处理结果
  624. function DedeSearchDo($action, $parms=array()) {
  625. if ($action === 'update') {
  626. DedeSearchDo('delete', $parms);
  627. return DedeSearchDo('add', $parms);
  628. }
  629. //生成完整请求URL
  630. $url = DedeSearchAPIURL($action, $parms);
  631. //初始化cURL
  632. $ch = curl_init();
  633. curl_setopt($ch, CURLOPT_URL, $url); //设置请求URL
  634. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回结果而不是直接输出
  635. curl_setopt($ch, CURLOPT_TIMEOUT, 10); //设置超时时间(秒)
  636. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); //设置连接超时(秒)
  637. curl_setopt($ch, CURLOPT_USERAGENT, 'DedeSearchAPI/1.0'); //设置User-Agent
  638. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https连接
  639. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //支持https连接
  640. //执行请求
  641. $response = curl_exec($ch);
  642. //获取HTTP状态码和错误信息
  643. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  644. $curlError = curl_error($ch);
  645. //关闭cURL资源
  646. curl_close($ch);
  647. //处理HTTP错误
  648. if ($response === false || $httpCode !== 200) {
  649. return array(
  650. 'code' => -1,
  651. 'message' => !empty($curlError) ? $curlError : "HTTP Error: $httpCode",
  652. 'data' => null,
  653. );
  654. }
  655. //解析返回的JSON数据
  656. $result = json_decode($response, true);
  657. if (json_last_error() !== JSON_ERROR_NONE) {
  658. return array(
  659. 'code' => -2,
  660. 'message' => 'Invalid JSON response',
  661. 'data' => null,
  662. );
  663. }
  664. //检查返回的业务逻辑中的code
  665. if (!isset($result['code']) || $result['code'] !== 0) {
  666. return array(
  667. 'code' => isset($result['code'])? $result['code'] : -3,
  668. 'message' => isset($result['message'])? $result['message'] : 'Unknown error',
  669. 'data' => null,
  670. );
  671. }
  672. //返回成功结果
  673. return array(
  674. 'code' => 0,
  675. 'message' => 'Success',
  676. 'data' => isset($result['data'])? $result['data'] : null,
  677. );
  678. }
  679. //获取接口地址
  680. function DedeSearchAPIURL($action, $parms=array())
  681. {
  682. $baseUrl = DEDEBIZSEARCHHOST."/api/$action"; //替换为实际的API地址
  683. //添加公共参数
  684. $timestamp = time(); //当前时间戳
  685. $parms['timestamp'] = $timestamp;
  686. $parms['pageSize'] = isset($parms['pageSize'])? $parms['pageSize']:10;
  687. $parms['page'] = isset($parms['page'])? $parms['page']:1;
  688. $parms['q'] = isset($parms['q'])? $parms['q']:"";
  689. if ($action == "delete" || $action == "add") {
  690. $parms['pageSize'] = 0;
  691. $parms['page'] = 0;
  692. $parms['q'] = isset($parms['id'])? $parms['id']:"";
  693. }
  694. //生成签名字符串
  695. $signBaseString = "key=" . DEDEBIZSEARCHKEY . "&q=".$parms['q']. "&pageSize=".$parms['pageSize']. "&page=".$parms['page']. "&timestamp=".$parms['timestamp'];
  696. $parms['sign'] = md5($signBaseString); //使用MD5生成签名
  697. if ($action == "delete" || $action == "add") {
  698. unset($parms['q']);
  699. unset($parms['pageSize']);
  700. unset($parms['page']);
  701. }
  702. //拼接完整URL
  703. $finalQueryString = http_build_query($parms);
  704. $finalUrl = $baseUrl . '?' . $finalQueryString;
  705. return $finalUrl;
  706. }
  707. function ConvertMysqlToSqlite($mysqlQuery) {
  708. //移除CHARACTER SET和COLLATE
  709. $query = preg_replace('/character set \S+/i', '', $mysqlQuery);
  710. $query = preg_replace('/collate \S+/i', '', $query);
  711. //移除unsigned关键字
  712. $query = str_replace('unsigned', '', $query);
  713. //替换MySQL的整数类型为SQLite的INTEGER
  714. $query = preg_replace('/\b(TINY|SMALL|MEDIUM|BIG)?INT\(\d+\)/i', 'INTEGER', $query);
  715. //替换AUTO_INCREMENT为PRIMARY KEY AUTOINCREMENT (仅适用于INTEGER类型)
  716. $query = preg_replace('/\bINTEGER\s+NOT NULL\s+PRIMARY KEY AUTOINCREMENT/i', 'INTEGER PRIMARY KEY AUTOINCREMENT', $query);
  717. $query = preg_replace('/\bAUTO_INCREMENT\b/i', '', $query);
  718. //移除MySQL特有的ENGINE、CHARSET、COLLATE、TYPE选项
  719. $query = preg_replace('/ENGINE=\S+/i', '', $query);
  720. $query = preg_replace('/DEFAULT CHARSET=\S+/i', '', $query);
  721. $query = preg_replace('/COLLATE=\S+/i', '', $query);
  722. $query = preg_replace('/TYPE=MyISAM;/i', '', $query);
  723. //移除COMMENT语法(SQLite不支持)
  724. $query = preg_replace('/COMMENT\s+\'[^\']*\'/i', '', $query);
  725. //移除KEY和UNIQUE KEY定义(SQLite会自动管理索引),同时处理USING BTREE
  726. $query = preg_replace('/,?\s*KEY\s+\S+\s*\([^)]*\)\s*(USING BTREE)?/i', '', $query);
  727. $query = preg_replace('/,?\s*UNIQUE KEY\s+\S+\s*\([^)]*\)\s*(USING BTREE)?/i', '', $query);
  728. //移除不完整的UNIQUE定义
  729. $query = preg_replace('/,?\s*UNIQUE\s*(?!\()/', '', $query);
  730. //替换ENUM和SET为TEXT
  731. $query = preg_replace('/\b(ENUM|SET)\([^)]*\)/i', 'TEXT', $query);
  732. //替换MEDIUMTEXT为TEXT
  733. $query = preg_replace('/\bMEDIUMTEXT\b/i', 'TEXT', $query);
  734. //处理DEFAULT CURRENT_TIMESTAMP
  735. $query = preg_replace('/DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP/i', 'DEFAULT CURRENT_TIMESTAMP', $query);
  736. //处理DEFAULT值
  737. $query = preg_replace('/DEFAULT\s+\'([^\']+)\'/i', 'DEFAULT "$1"', $query);
  738. //处理PRIMARY KEY只能用于INTEGER
  739. if (preg_match('/PRIMARY KEY \(`(\w+)`\)/', $query, $matches)) {
  740. $primaryKeyColumn = $matches[1];
  741. $query = preg_replace('/,?\s*PRIMARY KEY\s*\(`' . $primaryKeyColumn . '`\)/i', '', $query);
  742. $query = preg_replace('/(`' . $primaryKeyColumn . '`\s+INTEGER)/i', '$1 PRIMARY KEY', $query);
  743. }
  744. //处理CONCAT替换为SQLite兼容形式
  745. if (preg_match('/CONCAT\(([^)]*?)\)/i', $query, $matches)) {
  746. $query = preg_replace('/CONCAT\(([^)]*?)\)/i', str_replace(",", "||", $matches[1]), $query);
  747. $query = str_replace("'||'", "','", $query);
  748. }
  749. //修正FIND_IN_SET替换
  750. $query = preg_replace("/FIND_IN_SET\('([\w]+)', arc.flag\)>0/i", "(',' || arc.flag || ',') LIKE '%,\\1,%'", $query);
  751. $query = preg_replace("/FIND_IN_SET\('([\w]+)', arc.flag\)<1/i", "(',' || arc.flag || ',') NOT LIKE '%,\\1,%'", $query);
  752. //修正FIND_IN_SET替换(允许列名包含点号)
  753. $query = preg_replace_callback(
  754. "/FIND_IN_SET\s*\(\s*'([^']+)'\s*,\s*([a-zA-Z0-9_`\.]+)\s*\)/i",
  755. function ($matches) {
  756. //返回SQLite兼容的LIKE语法
  757. return "(',' || " . $matches[2] . " || ',' LIKE '%," . $matches[1] . ",%')";
  758. },
  759. $query
  760. );
  761. //替换FIELD函数为CASE表达式
  762. $query = preg_replace_callback(
  763. '/\bFIELD\s*\(\s*([^,]+)\s*,\s*((?:\'[^\']+\'|`[^`]+`|[^),]+(?:,\s*)?)+)\s*\)/i',
  764. function ($matches) {
  765. $field = trim($matches[1]);
  766. $values = trim($matches[2]);
  767. //更精确分割值列表(支持带引号、反引号及无空格分隔的数值)
  768. preg_match_all('/\'[^\']+\'|`[^`]+`|\d+|\w+/', $values, $valueParts);
  769. $cases = [];
  770. $position = 1;
  771. foreach ($valueParts[0] as $value) {
  772. $cases[] = "WHEN $field = $value THEN $position";
  773. $position++;
  774. }
  775. return "(CASE " . implode(' ', $cases) . " ELSE 0 END)";
  776. },
  777. $query
  778. );
  779. //新增的转换逻辑
  780. $query = preg_replace("/SHOW fields FROM `([\w]+)`/i", "PRAGMA table_info('\\1') ", $query);
  781. $query = preg_replace("/SHOW CREATE TABLE `([\w]+)`/i", "SELECT 0,sql FROM sqlite_master WHERE name='\\1'; ", $query);
  782. $query = preg_replace("/Show Tables/i", "SELECT name FROM sqlite_master WHERE type = \"table\"", $query);
  783. $query = str_replace("\'", "\"", $query);
  784. $query = str_replace('\t\n', "", $query);
  785. $query = str_ireplace('rand', 'RANDOM', $query);
  786. return trim($query);
  787. }
  788. function ThemeInclude($path)
  789. {
  790. global $cfg_basedir, $cfg_templets_dir, $cfg_df_style;
  791. $tmpfile = $cfg_basedir.$cfg_templets_dir.'/'.$cfg_df_style.'/'.$path;
  792. $dtp = new PartView();
  793. $dtp->SetTemplet($tmpfile);
  794. $dtp->Display();
  795. }
  796. //自定义函数接口
  797. if (file_exists(DEDEINC.'/extend.func.php')) {
  798. require_once(DEDEINC.'/extend.func.php');
  799. }
  800. ?>