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

103 lines
4.1KB

  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. $cfg_not_allowall = "php|pl|cgi|asp|aspx|jsp|php3|shtm|shtml";
  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. exit('Request var not allow for uploadsafe!');
  32. }
  33. $$_key = $_FILES[$_key]['tmp_name'];
  34. ${$_key.'_name'} = $_FILES[$_key]['name'];
  35. ${$_key.'_type'} = $_FILES[$_key]['type'] = preg_replace('#[^0-9a-z\./]#i', '', $_FILES[$_key]['type']);
  36. ${$_key.'_size'} = $_FILES[$_key]['size'] = preg_replace('#[^0-9]#', '', $_FILES[$_key]['size']);
  37. if (is_array(${$_key.'_name'}) && count(${$_key.'_name'}) > 0) {
  38. foreach (${$_key.'_name'} as $key => $value) {
  39. if (!empty($value) && (preg_match("#\.(".$cfg_not_allowall.")$#i", $value) || !preg_match("#\.#", $value))) {
  40. if (!defined('DEDEADMIN')) {
  41. exit('Not Admin Upload filetype not allow !');
  42. }
  43. }
  44. }
  45. } else {
  46. if (!empty(${$_key.'_name'}) && (preg_match("#\.(".$cfg_not_allowall.")$#i", ${$_key.'_name'}) || !preg_match("#\.#", ${$_key.'_name'}))) {
  47. if (!defined('DEDEADMIN')) {
  48. exit('Not Admin Upload filetype not allow !');
  49. }
  50. }
  51. }
  52. if (empty(${$_key.'_size'})) {
  53. ${$_key.'_size'} = @filesize($$_key);
  54. }
  55. $imtypes = array("image/pjpeg", "image/jpeg", "image/gif", "image/png", "image/xpng", "image/wbmp", "image/bmp");
  56. if (is_array(${$_key.'_type'}) && count(${$_key.'_type'}) > 0) {
  57. foreach (${$_key.'_type'} as $key => $value) {
  58. if (in_array(strtolower(trim($value)), $imtypes)) {
  59. $image_dd = @getimagesize($$_key);
  60. if ($image_dd == false) {
  61. continue;
  62. }
  63. if (!is_array($image_dd)) {
  64. exit('Upload filetype not allow !');
  65. }
  66. }
  67. $imtypes = array(
  68. "image/pjpeg", "image/jpeg", "image/gif", "image/png", "image/xpng", "image/wbmp", "image/bmp"
  69. );
  70. if (in_array(strtolower(trim($value)), $imtypes)) {
  71. $image_dd = @getimagesize($$_key);
  72. if ($image_dd == false) {
  73. continue;
  74. }
  75. if (!is_array($image_dd)) {
  76. exit('Upload filetype not allow !');
  77. }
  78. }
  79. }
  80. } else {
  81. if (in_array(strtolower(trim(${$_key.'_type'})), $imtypes)) {
  82. $image_dd = @getimagesize($$_key);
  83. if ($image_dd == false) {
  84. continue;
  85. }
  86. if (!is_array($image_dd)) {
  87. exit('Upload filetype not allow !');
  88. }
  89. }
  90. $imtypes = array(
  91. "image/pjpeg", "image/jpeg", "image/gif", "image/png", "image/xpng", "image/wbmp", "image/bmp"
  92. );
  93. if (in_array(strtolower(trim(${$_key.'_type'})), $imtypes)) {
  94. $image_dd = @getimagesize($$_key);
  95. if ($image_dd == false) {
  96. continue;
  97. }
  98. if (!is_array($image_dd)) {
  99. exit('Upload filetype not allow !');
  100. }
  101. }
  102. }
  103. }