PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/source/class/discuz/discuz_upload.php

https://github.com/kuaileshike/upload
PHP | 223 lines | 178 code | 39 blank | 6 comment | 81 complexity | d69be204fe81c406f5412072871a1f6b MD5 | raw file
  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 29368 2012-04-09 04:05:31Z zhengqingpeng $
  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. }
  103. return $imageinfo;
  104. } else {
  105. return false;
  106. }
  107. }
  108. function is_upload_file($source) {
  109. return $source && ($source != 'none') && (is_uploaded_file($source) || is_uploaded_file(str_replace('\\\\', '\\', $source)));
  110. }
  111. function get_target_filename($type, $extid = 0, $forcename = '') {
  112. if($type == 'group' || ($type == 'common' && $forcename != '')) {
  113. $filename = $type.'_'.intval($extid).($forcename != '' ? "_$forcename" : '');
  114. } else {
  115. $filename = date('His').strtolower(random(16));
  116. }
  117. return $filename;
  118. }
  119. function get_target_extension($ext) {
  120. static $safeext = array('attach', 'jpg', 'jpeg', 'gif', 'png', 'swf', 'bmp', 'txt', 'zip', 'rar', 'mp3');
  121. return strtolower(!in_array(strtolower($ext), $safeext) ? 'attach' : $ext);
  122. }
  123. function get_target_dir($type, $extid = '', $check_exists = true) {
  124. $subdir = $subdir1 = $subdir2 = '';
  125. if($type == 'album' || $type == 'forum' || $type == 'portal' || $type == 'category' || $type == 'profile') {
  126. $subdir1 = date('Ym');
  127. $subdir2 = date('d');
  128. $subdir = $subdir1.'/'.$subdir2.'/';
  129. } elseif($type == 'group' || $type == 'common') {
  130. $subdir = $subdir1 = substr(md5($extid), 0, 2).'/';
  131. }
  132. $check_exists && discuz_upload::check_dir_exists($type, $subdir1, $subdir2);
  133. return $subdir;
  134. }
  135. function check_dir_type($type) {
  136. return !in_array($type, array('forum', 'group', 'album', 'portal', 'common', 'temp', 'category', 'profile')) ? 'temp' : $type;
  137. }
  138. function check_dir_exists($type = '', $sub1 = '', $sub2 = '') {
  139. $type = discuz_upload::check_dir_type($type);
  140. $basedir = !getglobal('setting/attachdir') ? (DISCUZ_ROOT.'./data/attachment') : getglobal('setting/attachdir');
  141. $typedir = $type ? ($basedir.'/'.$type) : '';
  142. $subdir1 = $type && $sub1 !== '' ? ($typedir.'/'.$sub1) : '';
  143. $subdir2 = $sub1 && $sub2 !== '' ? ($subdir1.'/'.$sub2) : '';
  144. $res = $subdir2 ? is_dir($subdir2) : ($subdir1 ? is_dir($subdir1) : is_dir($typedir));
  145. if(!$res) {
  146. $res = $typedir && discuz_upload::make_dir($typedir);
  147. $res && $subdir1 && ($res = discuz_upload::make_dir($subdir1));
  148. $res && $subdir1 && $subdir2 && ($res = discuz_upload::make_dir($subdir2));
  149. }
  150. return $res;
  151. }
  152. function save_to_local($source, $target) {
  153. if(!discuz_upload::is_upload_file($source)) {
  154. $succeed = false;
  155. }elseif(@copy($source, $target)) {
  156. $succeed = true;
  157. }elseif(function_exists('move_uploaded_file') && @move_uploaded_file($source, $target)) {
  158. $succeed = true;
  159. }elseif (@is_readable($source) && (@$fp_s = fopen($source, 'rb')) && (@$fp_t = fopen($target, 'wb'))) {
  160. while (!feof($fp_s)) {
  161. $s = @fread($fp_s, 1024 * 512);
  162. @fwrite($fp_t, $s);
  163. }
  164. fclose($fp_s); fclose($fp_t);
  165. $succeed = true;
  166. }
  167. if($succeed) {
  168. $this->errorcode = 0;
  169. @chmod($target, 0644); @unlink($source);
  170. } else {
  171. $this->errorcode = 0;
  172. }
  173. return $succeed;
  174. }
  175. function make_dir($dir, $index = true) {
  176. $res = true;
  177. if(!is_dir($dir)) {
  178. $res = @mkdir($dir, 0777);
  179. $index && @touch($dir.'/index.html');
  180. }
  181. return $res;
  182. }
  183. }
  184. ?>