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

109 lines
4.5KB

  1. <?php
  2. if (!defined('DEDEINC')) exit ('dedebiz');
  3. if (isset($_FILES['GLOBALS'])) exit ('Request not allow!');
  4. /**
  5. * 文件上传安全校验方法
  6. *
  7. * @version $id:uploadsafe.inc.php 15:59 2020年8月19日 tianya $
  8. * @package DedeBIZ.Libraries
  9. * @copyright Copyright (c) 2022 DedeBIZ.COM
  10. * @license GNU GPL v2 (https://www.dedebiz.com/license)
  11. * @link https://www.dedebiz.com
  12. */
  13. //为了防止会员通过注入,这里强制限定的某些文件类型禁止上传
  14. $cfg_not_allowall = "php|pl|cgi|asp|aspx|jsp|php3|shtm|shtml|htm";
  15. $keyarr = array('name', 'type', 'tmp_name', 'size');
  16. if (
  17. ($GLOBALS['cfg_html_editor'] == 'ckeditor' ||
  18. $GLOBALS['cfg_html_editor'] == 'ckeditor4') && isset($_FILES['upload'])
  19. ) {
  20. $_FILES['imgfile'] = $_FILES['upload'];
  21. $CKUpload = TRUE;
  22. unset($_FILES['upload']);
  23. }
  24. foreach ($_FILES as $_key => $_value) {
  25. foreach ($keyarr as $k) {
  26. if (!isset($_FILES[$_key][$k])) {
  27. exit('dedebiz');
  28. }
  29. }
  30. if (preg_match('#^(cfg_|GLOBALS)#', $_key)) {
  31. echo DedeAlert('危险的请求参数', ALERT_DANGER);
  32. exit;
  33. }
  34. $$_key = $_FILES[$_key]['tmp_name'];
  35. ${$_key.'_name'} = $_FILES[$_key]['name'];
  36. ${$_key.'_type'} = $_FILES[$_key]['type'] = preg_replace('#[^0-9a-z\./]#i', '', $_FILES[$_key]['type']);
  37. ${$_key.'_size'} = $_FILES[$_key]['size'] = preg_replace('#[^0-9]#', '', $_FILES[$_key]['size']);
  38. if (is_array(${$_key.'_name'}) && count(${$_key.'_name'}) > 0) {
  39. foreach (${$_key.'_name'} as $key => $value) {
  40. $value = trim($value);
  41. if (!empty($value) && (preg_match("#\.(".$cfg_not_allowall.")$#i", $value) || !preg_match("#\.#", $value) || preg_match('#\.[\x00-\x1F\x7F]*$#', trim($value)))) {
  42. if (!defined('DEDEADMIN')) {
  43. echo DedeAlert('禁止上传当前格式的文件', ALERT_DANGER);
  44. exit;
  45. }
  46. }
  47. }
  48. } else {
  49. $fname = trim(${$_key.'_name'});
  50. if (!empty($fname) && (preg_match("#\.(".$cfg_not_allowall.")$#i", $fname) || !preg_match("#\.#", $fname) || preg_match('#\.[\x00-\x1F\x7F]*$#', trim($value)))) {
  51. if (!defined('DEDEADMIN')) {
  52. echo DedeAlert('禁止上传当前格式的文件', ALERT_DANGER);
  53. exit;
  54. }
  55. }
  56. }
  57. if (empty(${$_key.'_size'})) {
  58. ${$_key.'_size'} = @filesize($$_key);
  59. }
  60. $imgtypes = array("image/pjpeg", "image/jpeg", "image/gif", "image/png", "image/xpng", "image/wbmp", "image/webp");
  61. if (is_array(${$_key.'_type'}) && count(${$_key.'_type'}) > 0) {
  62. foreach (${$_key.'_type'} as $key => $value) {
  63. if (in_array(strtolower(trim($value)), $imtypes)) {
  64. $image_dd = @getimagesize($$_key);
  65. if ($image_dd == false) {
  66. continue;
  67. }
  68. if (!is_array($image_dd)) {
  69. echo DedeAlert('禁止上传当前格式的文件', ALERT_DANGER);
  70. exit;
  71. }
  72. }
  73. $imgtypes = array("image/pjpeg", "image/jpeg", "image/gif", "image/png", "image/xpng", "image/wbmp", "image/webp");
  74. if (in_array(strtolower(trim($value)), $imtypes)) {
  75. $image_dd = @getimagesize($$_key);
  76. if ($image_dd == false) {
  77. continue;
  78. }
  79. if (!is_array($image_dd)) {
  80. echo DedeAlert('禁止上传当前格式的文件', ALERT_DANGER);
  81. exit;
  82. }
  83. }
  84. }
  85. } else {
  86. if (in_array(strtolower(trim(${$_key.'_type'})), $imtypes)) {
  87. $image_dd = @getimagesize($$_key);
  88. if ($image_dd == false) {
  89. continue;
  90. }
  91. if (!is_array($image_dd)) {
  92. echo DedeAlert('禁止上传当前格式的文件', ALERT_DANGER);
  93. exit;
  94. }
  95. }
  96. $imgtypes = array("image/pjpeg", "image/jpeg", "image/gif", "image/png", "image/xpng", "image/wbmp", "image/webp");
  97. if (in_array(strtolower(trim(${$_key.'_type'})), $imtypes)) {
  98. $image_dd = @getimagesize($$_key);
  99. if ($image_dd == false) {
  100. continue;
  101. }
  102. if (!is_array($image_dd)) {
  103. echo DedeAlert('禁止上传当前格式的文件', ALERT_DANGER);
  104. exit;
  105. }
  106. }
  107. }
  108. }
  109. ?>