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

116 lines
4.2KB

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