function_upload.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: function_upload.php 29000 2012-03-22 03:52:01Z zhengqingpeng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. function getuploadconfig($uid=0, $fid=0, $limit=true) {
  12. global $_G;
  13. $notallow = $config = array();
  14. $config['limit'] = 0;
  15. $uid = !empty($uid) ? intval($uid) : $_G['uid'];
  16. $authkey = $_G['config']['security']['authkey'];
  17. $config['hash'] = md5(substr(md5($authkey), 8).$uid);
  18. $imageexts = array('jpg','jpeg','gif','png','bmp');
  19. $forumattachextensions = '';
  20. $fid = intval($fid);
  21. if($fid) {
  22. $forum = $fid != $_G['fid'] ? C::t('forum_forum')->fetch_info_by_fid($fid) : $_G['forum'];
  23. $levelinfo = C::t('forum_grouplevel')->fetch($forum['level']);
  24. if($forum['status'] == 3 && $forum['level'] && $postpolicy = $levelinfo['postpolicy']) {
  25. $postpolicy = dunserialize($postpolicy);
  26. $forumattachextensions = $postpolicy['attachextensions'];
  27. } else {
  28. $forumattachextensions = $forum['attachextensions'];
  29. }
  30. }
  31. $extendtype = '';
  32. loadcache('attachtype');
  33. $fid = isset($_G['cache']['attachtype'][$fid]) ? $fid : 0;
  34. $filter = array();
  35. foreach($_G['cache']['attachtype'][$fid] as $extension => $maxsize) {
  36. if($maxsize == 0) {
  37. $notallow[] = $extension;
  38. } else {
  39. $filter[] = "'$extension':$maxsize";
  40. }
  41. }
  42. if(!empty($filter)) {
  43. $config['filtertype'] = '{'.implode(',', $filter).'}';
  44. }
  45. $_G['group']['attachextensions'] = !$forumattachextensions ? $_G['group']['attachextensions'] : $forumattachextensions;
  46. $config['imageexts'] = array('ext' => '', 'depict' => 'Image File');
  47. $config['attachexts'] = array('ext' => '*.*', 'depict' => 'All Support Formats');
  48. if($_G['group']['attachextensions'] !== '') {
  49. $_G['group']['attachextensions'] = str_replace(' ', '', $_G['group']['attachextensions']);
  50. $exts = explode(',', $_G['group']['attachextensions']);
  51. $imagext = filterexts(array_intersect($imageexts, $exts), $notallow);
  52. $config['imageexts']['ext'] = !empty($imagext) ? '*.'.implode(';*.', $imagext) : '';
  53. $exts = filterexts($exts, $notallow);
  54. $config['attachexts']['ext'] = !empty($exts) ? '*.'.implode(';*.', $exts) : '';
  55. } else {
  56. $imageexts = filterexts($imageexts, $notallow);
  57. $config['imageexts']['ext'] = !empty($imageexts) ? '*.'.implode(';*.', $imageexts) : '';
  58. }
  59. $config['max'] = 0;
  60. if(!empty($_G['group']['maxattachsize'])) {
  61. $config['max'] = intval($_G['group']['maxattachsize']);
  62. } else {
  63. $config['max'] = @ini_get(upload_max_filesize);
  64. $unit = strtolower(substr($config['max'], -1, 1));
  65. $config['max'] = intval($config['max']);
  66. if($unit == 'k') {
  67. $config['max'] = $config['max']*1024;
  68. } elseif($unit == 'm') {
  69. $config['max'] = $config['max']*1024*1024;
  70. } elseif($unit == 'g') {
  71. $config['max'] = $config['max']*1024*1024*1024;
  72. }
  73. }
  74. $config['max'] = $config['max'] / 1024;
  75. if($limit) {
  76. if($_G['group']['maxattachnum']) {
  77. $todayattachs = getuserprofile('todayattachs');
  78. $config['maxattachnum'] = $_G['group']['maxattachnum'] - $todayattachs;
  79. $config['maxattachnum'] = $config['maxattachnum'] > 0 ? $config['maxattachnum'] : -1;
  80. $config['limit'] = $config['maxattachnum'] > 0 ? $config['maxattachnum'] : 0;
  81. }
  82. if($_G['group']['maxsizeperday']) {
  83. $todayattachsize = getuserprofile('todayattachsize');
  84. $config['maxsizeperday'] = $_G['group']['maxsizeperday'] - $todayattachsize;
  85. $config['maxsizeperday'] = $config['maxsizeperday'] > 0 ? $config['maxsizeperday'] : -1;
  86. }
  87. }
  88. return $config;
  89. }
  90. function filterexts($needle, $haystack) {
  91. foreach($needle as $key => $value) {
  92. if(in_array($value, $haystack)) {
  93. unset($needle[$key]);
  94. }
  95. }
  96. return $needle;
  97. }
  98. ?>