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

198 lines
9.5KB

  1. <?php if (!defined('DEDEINC')) exit('dedebiz');
  2. /**
  3. * 上传处理小助手
  4. *
  5. * @version $Id: upload.helper.php 1 2010-07-05 11:43:09Z tianya $
  6. * @package DedeBIZ.Helpers
  7. * @copyright Copyright (c) 2021, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. /**
  12. * 管理员上传文件的通用函数
  13. *
  14. * @access public
  15. * @param string $uploadname 上传名称
  16. * @param string $ftype 文件类型
  17. * @param string $rnddd 后缀数字
  18. * @param bool $watermark 是否水印
  19. * @param string $filetype image、media、addon
  20. * $file_type='' 对于swfupload上传的文件, 因为没有filetype,所以需指定,并且有些特殊之处不同
  21. * @return int -1 没选定上传文件,0 文件类型不允许, -2 保存失败,其它:返回上传后的文件名
  22. */
  23. if (!function_exists('AdminUpload')) {
  24. function AdminUpload($uploadname, $ftype = 'image', $rnddd = 0, $watermark = TRUE, $filetype = '')
  25. {
  26. global $dsql, $cuserLogin, $cfg_addon_savetype, $cfg_dir_purview;
  27. global $cfg_basedir, $cfg_image_dir, $cfg_soft_dir, $cfg_other_medias;
  28. global $cfg_imgtype, $cfg_softtype, $cfg_mediatype;
  29. if ($watermark) include_once(DEDEINC.'/image.func.php');
  30. $file_tmp = isset($GLOBALS[$uploadname]) ? $GLOBALS[$uploadname] : '';
  31. if ($file_tmp == '' || !is_uploaded_file($file_tmp)) {
  32. return -1;
  33. }
  34. $file_tmp = $GLOBALS[$uploadname];
  35. $file_size = filesize($file_tmp);
  36. $file_type = $filetype == '' ? strtolower(trim($GLOBALS[$uploadname.'_type'])) : $filetype;
  37. $file_name = isset($GLOBALS[$uploadname.'_name']) ? $GLOBALS[$uploadname.'_name'] : '';
  38. $file_snames = explode('.', $file_name);
  39. $file_sname = strtolower(trim($file_snames[count($file_snames) - 1]));
  40. if ($ftype == 'image' || $ftype == 'imagelit') {
  41. $filetype = '1';
  42. $sparr = array('image/pjpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/xpng', 'image/wbmp');
  43. if (!in_array($file_type, $sparr)) return 0;
  44. if ($file_sname == '') {
  45. if ($file_type == 'image/gif') $file_sname = 'jpg';
  46. else if ($file_type == 'image/png' || $file_type == 'image/xpng') $file_sname = 'png';
  47. else if ($file_type == 'image/wbmp') $file_sname = 'bmp';
  48. else $file_sname = 'jpg';
  49. }
  50. $filedir = $cfg_image_dir.'/'.MyDate($cfg_addon_savetype, time());
  51. } else if ($ftype == 'media') {
  52. $filetype = '3';
  53. if (!preg_match('/'.$cfg_mediatype.'/', $file_sname)) return 0;
  54. $filedir = $cfg_other_medias.'/'.MyDate($cfg_addon_savetype, time());
  55. } else {
  56. $filetype = '4';
  57. $cfg_softtype .= '|'.$cfg_mediatype.'|'.$cfg_imgtype;
  58. $cfg_softtype = str_replace('||', '|', $cfg_softtype);
  59. if (!preg_match('/'.$cfg_softtype.'/', $file_sname)) return 0;
  60. $filedir = $cfg_soft_dir.'/'.MyDate($cfg_addon_savetype, time());
  61. }
  62. if (!is_dir(DEDEROOT.$filedir)) {
  63. MkdirAll($cfg_basedir.$filedir, $cfg_dir_purview);
  64. CloseFtp();
  65. }
  66. $filename = $cuserLogin->getUserID().'-'.dd2char(MyDate('ymdHis', time())).$rnddd;
  67. if ($ftype == 'imagelit') $filename .= '-L';
  68. if (file_exists($cfg_basedir.$filedir.'/'.$filename.'.'.$file_sname)) {
  69. for ($i = 50; $i <= 5000; $i++) {
  70. if (!file_exists($cfg_basedir.$filedir.'/'.$filename.'-'.$i.'.'.$file_sname)) {
  71. $filename = $filename.'-'.$i;
  72. break;
  73. }
  74. }
  75. }
  76. $fileurl = $filedir.'/'.$filename.'.'.$file_sname;
  77. $rs = move_uploaded_file($file_tmp, $cfg_basedir.$fileurl);
  78. if (!$rs) return -2;
  79. if ($ftype == 'image' && $watermark) {
  80. WaterImg($cfg_basedir.$fileurl, 'up');
  81. }
  82. //保存信息到数据库
  83. $title = $filename.'.'.$file_sname;
  84. $inquery = "INSERT INTO `#@__uploads`(title,url,mediatype,width,height,playtime,filesize,uptime,mid)
  85. VALUES ('$title','$fileurl','$filetype','0','0','0','".filesize($cfg_basedir.$fileurl)."','".time()."','".$cuserLogin->getUserID()."'); ";
  86. $dsql->ExecuteNoneQuery($inquery);
  87. $fid = $dsql->GetLastID();
  88. AddMyAddon($fid, $fileurl);
  89. return $fileurl;
  90. }
  91. }
  92. //前台会员通用上传函数
  93. //$upname 是文件上传框的表单名,而不是表单的变量
  94. //$handname 允许用户手工指定网址情况下的网址
  95. if (!function_exists('MemberUploads')) {
  96. function MemberUploads($upname, $handname, $userid = 0, $utype = 'image', $exname = '', $maxwidth = 0, $maxheight = 0, $water = false, $isadmin = false)
  97. {
  98. global $cfg_imgtype, $cfg_mb_addontype, $cfg_mediatype, $cfg_user_dir, $cfg_basedir, $cfg_dir_purview;
  99. //当为游客投稿的情况下,这个 id 为 0
  100. if (empty($userid)) $userid = 0;
  101. if (!is_dir($cfg_basedir.$cfg_user_dir."/$userid")) {
  102. MkdirAll($cfg_basedir.$cfg_user_dir."/$userid", $cfg_dir_purview);
  103. CloseFtp();
  104. }
  105. //有上传文件
  106. $allAllowType = str_replace('||', '|', $cfg_imgtype.'|'.$cfg_mediatype.'|'.$cfg_mb_addontype);
  107. if (!empty($GLOBALS[$upname]) && is_uploaded_file($GLOBALS[$upname])) {
  108. $nowtme = time();
  109. $GLOBALS[$upname.'_name'] = trim(preg_replace("#[ \r\n\t\*\%\\\/\?><\|\":]{1,}#", '', $GLOBALS[$upname.'_name']));
  110. //源文件类型检查
  111. if ($utype == 'image') {
  112. if (!preg_match("/\.(".$cfg_imgtype.")$/", $GLOBALS[$upname.'_name'])) {
  113. ShowMsg("您所上传的图片类型不在许可列表,请上传{$cfg_imgtype}类型!", '-1');
  114. exit();
  115. }
  116. $sparr = array("image/pjpeg", "image/jpeg", "image/gif", "image/png", "image/xpng", "image/wbmp");
  117. $imgfile_type = strtolower(trim($GLOBALS[$upname.'_type']));
  118. if (!in_array($imgfile_type, $sparr)) {
  119. ShowMsg('上传的图片格式错误,请使用JPEG、GIF、PNG、WBMP格式的其中一种!', '-1');
  120. exit();
  121. }
  122. } else if ($utype == 'flash' && !preg_match("/\.swf$/", $GLOBALS[$upname.'_name'])) {
  123. ShowMsg('上传的文件必须为flash文件!', '-1');
  124. exit();
  125. } else if ($utype == 'media' && !preg_match("/\.(".$cfg_mediatype.")$/", $GLOBALS[$upname.'_name'])) {
  126. ShowMsg('您所上传的文件类型必须为:'.$cfg_mediatype, '-1');
  127. exit();
  128. } else if (!preg_match("/\.(".$allAllowType.")$/", $GLOBALS[$upname.'_name'])) {
  129. ShowMsg("您所上传的文件类型不被允许!", '-1');
  130. exit();
  131. }
  132. //再次严格检测文件扩展名是否符合系统定义的类型
  133. $fs = explode('.', $GLOBALS[$upname.'_name']);
  134. $sname = $fs[count($fs) - 1];
  135. $alltypes = explode('|', $allAllowType);
  136. if (!in_array(strtolower($sname), $alltypes)) {
  137. ShowMsg('您所上传的文件类型不被允许!', '-1');
  138. exit();
  139. }
  140. //强制禁止的文件类型
  141. if (preg_match("/(asp|php|pl|cgi|shtm|js)$/", $sname)) {
  142. ShowMsg('您上传的文件为系统禁止的类型!', '-1');
  143. exit();
  144. }
  145. if ($exname == '') {
  146. $filename = $cfg_user_dir."/$userid/".dd2char($nowtme.'-'.mt_rand(1000, 9999)).'.'.$sname;
  147. } else {
  148. $filename = $cfg_user_dir."/{$userid}/{$exname}.".$sname;
  149. }
  150. move_uploaded_file($GLOBALS[$upname], $cfg_basedir.$filename) or die("上传文件到 {$filename} 失败!");
  151. @unlink($GLOBALS[$upname]);
  152. if (@filesize($cfg_basedir.$filename) > $GLOBALS['cfg_mb_upload_size'] * 1024) {
  153. @unlink($cfg_basedir.$filename);
  154. ShowMsg('您上传的文件超出系统大小限制!', '-1');
  155. exit();
  156. }
  157. //加水印或缩小图片
  158. if ($utype == 'image') {
  159. include_once(DEDEINC.'/image.func.php');
  160. if ($maxwidth > 0 || $maxheight > 0) {
  161. ImageResize($cfg_basedir.$filename, $maxwidth, $maxheight);
  162. } else if ($water) {
  163. WaterImg($cfg_basedir.$filename);
  164. }
  165. }
  166. return $filename;
  167. }
  168. //没有上传文件
  169. else {
  170. //强制禁止的文件类型
  171. if ($handname == '') {
  172. return $handname;
  173. } else if (preg_match("/\.(asp|php|pl|cgi|shtm|js)$/", $handname)) {
  174. exit('Not allow filename for not safe!');
  175. } else if (!preg_match("/\.(".$allAllowType.")$/", $handname)) {
  176. exit('Not allow filename for filetype!');
  177. }
  178. // 2011-4-10 修复会员中心修改相册时候错误(by:jason123j)
  179. else if (!preg_match('#^http:#', $handname) && !preg_match('#^'.$cfg_user_dir.'/'.$userid."#", $handname) && !$isadmin) {
  180. exit('Not allow filename for not userdir!');
  181. }
  182. return $handname;
  183. }
  184. }
  185. }