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

199 lines
9.5KB

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