discuz_upload.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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: discuz_upload.php 34648 2014-06-18 02:53:07Z hypowang $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. Class discuz_upload{
  12. var $attach = array();
  13. var $type = '';
  14. var $extid = 0;
  15. var $errorcode = 0;
  16. var $forcename = '';
  17. public function __construct() {
  18. }
  19. function init($attach, $type = 'temp', $extid = 0, $forcename = '') {
  20. if(!is_array($attach) || empty($attach) || !$this->is_upload_file($attach['tmp_name']) || trim($attach['name']) == '' || $attach['size'] == 0) {
  21. $this->attach = array();
  22. $this->errorcode = -1;
  23. return false;
  24. } else {
  25. $this->type = $this->check_dir_type($type);
  26. $this->extid = intval($extid);
  27. $this->forcename = $forcename;
  28. $attach['size'] = intval($attach['size']);
  29. $attach['name'] = trim($attach['name']);
  30. $attach['thumb'] = '';
  31. $attach['ext'] = $this->fileext($attach['name']);
  32. $attach['name'] = dhtmlspecialchars($attach['name'], ENT_QUOTES);
  33. if(strlen($attach['name']) > 90) {
  34. $attach['name'] = cutstr($attach['name'], 80, '').'.'.$attach['ext'];
  35. }
  36. $attach['isimage'] = $this->is_image_ext($attach['ext']);
  37. $attach['extension'] = $this->get_target_extension($attach['ext']);
  38. $attach['attachdir'] = $this->get_target_dir($this->type, $extid);
  39. $attach['attachment'] = $attach['attachdir'].$this->get_target_filename($this->type, $this->extid, $this->forcename).'.'.$attach['extension'];
  40. $attach['target'] = getglobal('setting/attachdir').'./'.$this->type.'/'.$attach['attachment'];
  41. $this->attach = & $attach;
  42. $this->errorcode = 0;
  43. return true;
  44. }
  45. }
  46. function save($ignore = 0) {
  47. if($ignore) {
  48. if(!$this->save_to_local($this->attach['tmp_name'], $this->attach['target'])) {
  49. $this->errorcode = -103;
  50. return false;
  51. } else {
  52. $this->errorcode = 0;
  53. return true;
  54. }
  55. }
  56. if(empty($this->attach) || empty($this->attach['tmp_name']) || empty($this->attach['target'])) {
  57. $this->errorcode = -101;
  58. } elseif(in_array($this->type, array('group', 'album', 'category')) && !$this->attach['isimage']) {
  59. $this->errorcode = -102;
  60. } elseif(in_array($this->type, array('common')) && (!$this->attach['isimage'] && $this->attach['ext'] != 'ext')) {
  61. $this->errorcode = -102;
  62. } elseif(!$this->save_to_local($this->attach['tmp_name'], $this->attach['target'])) {
  63. $this->errorcode = -103;
  64. } elseif(($this->attach['isimage'] || $this->attach['ext'] == 'swf') && (!$this->attach['imageinfo'] = $this->get_image_info($this->attach['target'], true))) {
  65. $this->errorcode = -104;
  66. @unlink($this->attach['target']);
  67. } else {
  68. $this->errorcode = 0;
  69. return true;
  70. }
  71. return false;
  72. }
  73. function error() {
  74. return $this->errorcode;
  75. }
  76. function errormessage() {
  77. return lang('error', 'file_upload_error_'.$this->errorcode);
  78. }
  79. function fileext($filename) {
  80. return addslashes(strtolower(substr(strrchr($filename, '.'), 1, 10)));
  81. }
  82. function is_image_ext($ext) {
  83. static $imgext = array('jpg', 'jpeg', 'gif', 'png', 'bmp');
  84. return in_array($ext, $imgext) ? 1 : 0;
  85. }
  86. function get_image_info($target, $allowswf = false) {
  87. $ext = discuz_upload::fileext($target);
  88. $isimage = discuz_upload::is_image_ext($ext);
  89. if(!$isimage && ($ext != 'swf' || !$allowswf)) {
  90. return false;
  91. } elseif(!is_readable($target)) {
  92. return false;
  93. } elseif($imageinfo = @getimagesize($target)) {
  94. list($width, $height, $type) = !empty($imageinfo) ? $imageinfo : array('', '', '');
  95. $size = $width * $height;
  96. if($size > 16777216 || $size < 16 ) {
  97. return false;
  98. } elseif($ext == 'swf' && $type != 4 && $type != 13) {
  99. return false;
  100. } elseif($isimage && !in_array($type, array(1,2,3,6,13))) {
  101. return false;
  102. } elseif(!$allowswf && ($ext == 'swf' || $type == 4 || $type == 13)) {
  103. return false;
  104. }
  105. return $imageinfo;
  106. } else {
  107. return false;
  108. }
  109. }
  110. function is_upload_file($source) {
  111. return $source && ($source != 'none') && (is_uploaded_file($source) || is_uploaded_file(str_replace('\\\\', '\\', $source)));
  112. }
  113. function get_target_filename($type, $extid = 0, $forcename = '') {
  114. if($type == 'group' || ($type == 'common' && $forcename != '')) {
  115. $filename = $type.'_'.intval($extid).($forcename != '' ? "_$forcename" : '').random(8);
  116. } else {
  117. $filename = date('His').strtolower(random(16));
  118. }
  119. return $filename;
  120. }
  121. function get_target_extension($ext) {
  122. static $safeext = array('attach', 'jpg', 'jpeg', 'gif', 'png', 'swf', 'bmp', 'txt', 'zip', 'rar', 'mp3');
  123. return strtolower(!in_array(strtolower($ext), $safeext) ? 'attach' : $ext);
  124. }
  125. function get_target_dir($type, $extid = '', $check_exists = true) {
  126. $subdir = $subdir1 = $subdir2 = '';
  127. if($type == 'album' || $type == 'forum' || $type == 'portal' || $type == 'category' || $type == 'profile') {
  128. $subdir1 = date('Ym');
  129. $subdir2 = date('d');
  130. $subdir = $subdir1.'/'.$subdir2.'/';
  131. } elseif($type == 'group' || $type == 'common') {
  132. $subdir = $subdir1 = substr(md5($extid), 0, 2).'/';
  133. }
  134. $check_exists && discuz_upload::check_dir_exists($type, $subdir1, $subdir2);
  135. return $subdir;
  136. }
  137. function check_dir_type($type) {
  138. return !in_array($type, array('forum', 'group', 'album', 'portal', 'common', 'temp', 'category', 'profile')) ? 'temp' : $type;
  139. }
  140. function check_dir_exists($type = '', $sub1 = '', $sub2 = '') {
  141. $type = discuz_upload::check_dir_type($type);
  142. $basedir = !getglobal('setting/attachdir') ? (DISCUZ_ROOT.'./data/attachment') : getglobal('setting/attachdir');
  143. $typedir = $type ? ($basedir.'/'.$type) : '';
  144. $subdir1 = $type && $sub1 !== '' ? ($typedir.'/'.$sub1) : '';
  145. $subdir2 = $sub1 && $sub2 !== '' ? ($subdir1.'/'.$sub2) : '';
  146. $res = $subdir2 ? is_dir($subdir2) : ($subdir1 ? is_dir($subdir1) : is_dir($typedir));
  147. if(!$res) {
  148. $res = $typedir && discuz_upload::make_dir($typedir);
  149. $res && $subdir1 && ($res = discuz_upload::make_dir($subdir1));
  150. $res && $subdir1 && $subdir2 && ($res = discuz_upload::make_dir($subdir2));
  151. }
  152. return $res;
  153. }
  154. function save_to_local($source, $target) {
  155. if(!discuz_upload::is_upload_file($source)) {
  156. $succeed = false;
  157. }elseif(@copy($source, $target)) {
  158. $succeed = true;
  159. }elseif(function_exists('move_uploaded_file') && @move_uploaded_file($source, $target)) {
  160. $succeed = true;
  161. }elseif (@is_readable($source) && (@$fp_s = fopen($source, 'rb')) && (@$fp_t = fopen($target, 'wb'))) {
  162. while (!feof($fp_s)) {
  163. $s = @fread($fp_s, 1024 * 512);
  164. @fwrite($fp_t, $s);
  165. }
  166. fclose($fp_s); fclose($fp_t);
  167. $succeed = true;
  168. }
  169. if($succeed) {
  170. $this->errorcode = 0;
  171. @chmod($target, 0644); @unlink($source);
  172. } else {
  173. $this->errorcode = 0;
  174. }
  175. return $succeed;
  176. }
  177. function make_dir($dir, $index = true) {
  178. $res = true;
  179. if(!is_dir($dir)) {
  180. $res = @mkdir($dir, 0777);
  181. $index && @touch($dir.'/index.html');
  182. }
  183. return $res;
  184. }
  185. }
  186. ?>