/pc/userfile.php

https://github.com/yyms/lilacweb · PHP · 309 lines · 264 code · 30 blank · 15 comment · 59 complexity · dca086f378743f69906efd8ba5b7804a MD5 · raw file

  1. <?php
  2. require('pcfuncs.php');
  3. if (!$pcconfig["USERFILES"]) //²»Ö§³Ö¸öÈË¿Õ¼ä
  4. header('Location: /');
  5. if (!defined('_USER_FILE_ROOT_'))
  6. header('Location: /');
  7. function bbs_userfile_getfiledir($userid) {
  8. return _USER_FILE_ROOT_ . '/'. strtoupper($userid[0]) .'/'. $userid;
  9. }
  10. function bbs_userfile_getfilepath($userid, $filepath) {
  11. return bbs_userfile_getfiledir($userid) . '/' . $filepath;
  12. }
  13. function bbs_userfile_getrootfid($uid) {
  14. $query = 'SELECT fid FROM userfiles WHERE uid = '.intval($uid).' AND type = 1 AND pid = 0 LIMIT 1;';
  15. $result = mysql_query($query);
  16. if ($rows = mysql_fetch_array($result))
  17. {
  18. mysql_free_result($result);
  19. return $rows[fid];
  20. }
  21. if (!UserFile::add_file('',1,'','',0,'',0,$uid))
  22. return false;
  23. return bbs_userfile_getrootfid($uid);
  24. }
  25. $file_access = array('¹«¿ª','ºÃÓÑ','˽ÓÐ');
  26. class UserFile {
  27. var $fid;
  28. var $uid;
  29. var $pid;
  30. var $filename;
  31. var $filepath;
  32. var $filesize;
  33. var $filetype;
  34. var $type;
  35. var $hostname;
  36. var $filetime;
  37. var $access;
  38. var $remark;
  39. var $err;
  40. function UserFile ($fid, $uid=0) {
  41. $fid = intval($fid);
  42. $uid = intval($uid);
  43. if ($uid)
  44. $query = 'SELECT * FROM userfiles WHERE fid ='.$fid.' AND uid = '.$uid.' LIMIT 1;';
  45. else
  46. $query = 'SELECT * FROM userfiles WHERE fid ='.$fid.' LIMIT 1;';
  47. $result = mysql_query($query);
  48. if (!($rows = mysql_fetch_array($result))) {
  49. $this->err = 'Îļþ²»´æÔÚ';
  50. return false;
  51. }
  52. $this->fid = $rows[fid];
  53. $this->uid = $rows[uid];
  54. $this->pid = $rows[pid];
  55. $this->type = $rows[type];
  56. $this->hostname = $rows[hostname];
  57. $this->filetime = $rows[filetime];
  58. $this->access = $rows[access];
  59. $this->remark = $rows[remark];
  60. $this->filename = $rows[filename];
  61. $this->filepath = $rows[filepath];
  62. $this->filesize = ($this->type==1)?'-':$rows[filesize];
  63. $this->filetype = ($this->type==1)?'-':$rows[filetype];
  64. mysql_free_result($result);
  65. return true;
  66. }
  67. function uf_edit($new_filename,$new_access,$new_remark) {
  68. return $this->edit_file($this->pid,$new_filename,$new_access,$new_remark);
  69. }
  70. function uf_mv($target_fid) {
  71. if (!($target_file = new UserFile($target_fid , $this->uid))) {
  72. $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
  73. return false;
  74. }
  75. if (!$target_file->fid) {
  76. $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
  77. return false;
  78. }
  79. if ($target_file->type != 1) {
  80. $this->err = 'ÄúÖ»Äܽ«Õâ¸öÎļþÒƶ¯µ½ÁíÒ»¸öÎļþ¼ÐÖÐ';
  81. return false;
  82. }
  83. return $this->edit_file($target_file->fid,$this->filename,$this->access,$this->remark);
  84. }
  85. function uf_readdir(&$sub_files) {
  86. if ($this->type != 1) {
  87. $this->err = 'Õâ²»ÊÇÒ»¸öĿ¼';
  88. return false;
  89. }
  90. $sub_files = $this->check_dir();
  91. return true;
  92. }
  93. function uf_mkdir($filename, $access, $remark) {
  94. return $this->add_file($filename,1,'','',$access,$remark,0);
  95. }
  96. function uf_add($filename,$tmp_filepath,$filetype,$access,$remark) {
  97. return $this->add_file($filename,0,$tmp_filepath,$filetype,$access,$remark,1);
  98. }
  99. function uf_cp($target_fid) {
  100. if ($target_fid == $this->pid) {
  101. $this->err = 'ÄúÖ»Äܽ«Õâ¸öÎļþ¸´ÖƵ½ÁíÒ»¸öÎļþ¼ÐÖÐ';
  102. return false;
  103. }
  104. if ($this->type == 1) {
  105. $this->err = '½öÓÐÎļþ²ÅÄܸ´ÖÆ';
  106. return false;
  107. }
  108. if (!($target_file = new UserFile($target_fid , $this->uid))) {
  109. $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
  110. return false;
  111. }
  112. if (!$target_file->fid) {
  113. $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
  114. return false;
  115. }
  116. if ($target_file->type != 1) {
  117. $this->err = 'ÄúÖ»Äܽ«Õâ¸öÎļþ¸´ÖƵ½ÁíÒ»¸öÎļþ¼ÐÖÐ';
  118. return false;
  119. }
  120. $ret = $target_file->add_file($this->filename,0,$this->filepath,$this->filetype,$this->access,$this->remark,0);
  121. $this->err = $target_file->err;
  122. return $ret;
  123. }
  124. function uf_rm() {
  125. if ($this->type == 1) {
  126. if ($this->check_dir()) {
  127. $this->err = 'ÇëÏÈɾ³ý¸ÃĿ¼ÏµÄËùÓÐÎļþ';
  128. return false;
  129. }
  130. }
  131. else
  132. @unlink($this->filepath);
  133. $query = 'DELETE FROM userfiles WHERE fid = '.$this->fid.' LIMIT 1;';
  134. mysql_query($query);
  135. return true;
  136. }
  137. function gen_filepath() {
  138. global $currentuser;
  139. $filepath = 'F';
  140. $filepath .= '.'.$currentuser['index'];
  141. $filepath .= '.'.time();
  142. $filepath .= '.'.rand(1000,9999);
  143. return $filepath;
  144. }
  145. /**
  146. * add_file(string filename, int type , string tmp_filepath , string filetype , int access , string remark, int after_del, int auto_add_uid)
  147. * Ìí¼ÓÎļþ/Ŀ¼
  148. * filename: Îļþ/Ŀ¼Ãû
  149. * type : 0:Îļþ / 1: Ŀ¼
  150. * tmp_filepath: ÁÙʱÎļþ
  151. * filetype: ÎļþÀàÐÍ
  152. * access: ȨÏÞ 0:¹«¿ª/1:ºÃÓÑ/2:˽ÓÐ
  153. ¡Á remark: ±¸×¢
  154. * upload_file: ÊÇ·ñΪÉÏ´«Îļþ 0:²»/1:ÊÇ
  155. * auto_add_uid : ÊÇ·ñΪ×Ô¶¯Ìí¼Ó 0:²»/1:ÊÇ
  156. */
  157. function add_file($filename ,$type ,$tmp_filepath ,$filetype ,$access ,$remark, $upload_file, $auto_add_uid=0) {
  158. global $currentuser;
  159. $type = ($type==1)?1:0;
  160. $filename = trim(ltrim($filename));
  161. if ($auto_add_uid==0) {
  162. if (!$filename) {
  163. $this->err = ($type==1)?'ÇëÊäÈëĿ¼Ãû':'ÇëÊäÈëÎļþÃû';
  164. return false;
  165. }
  166. }
  167. if ($type == 0) {
  168. if ($upload_file) { //ÉÏ´«Îļþ
  169. if (!is_uploaded_file($tmp_filepath)) {
  170. $this->err = 'ÎļþÉÏ´«´íÎó';
  171. return false;
  172. }
  173. }
  174. else {
  175. if (!file_exists($tmp_filepath)) {
  176. $this->err = 'Ô´Îļþ '.$tmp_filepath.' ²»´æÔÚ';
  177. return false;
  178. }
  179. }
  180. $filedir = bbs_userfile_getfiledir($currentuser['userid']);
  181. if (!file_exists($filedir)) {
  182. if (!mkdir($filedir)) {
  183. $this->err = '´´½¨ÎļþĿ¼ʧ°Ü';
  184. return false;
  185. }
  186. }
  187. $filepath = bbs_userfile_getfilepath($currentuser['userid'], $this->gen_filepath());
  188. if ($upload_file) {
  189. if (!move_uploaded_file($tmp_filepath,$filepath)) {
  190. $this->err = 'Òƶ¯Îļþʧ°Ü';
  191. return false;
  192. }
  193. }
  194. else {
  195. if (!copy($tmp_filepath, $filepath)) {
  196. $this->err = '¿½±´Îļþʧ°Ü';
  197. return false;
  198. }
  199. }
  200. $filetype = trim(ltrim($filetype));
  201. $filesize = filesize($filepath);
  202. }
  203. else {
  204. $filepath = $filetype = '';
  205. $filesize = 0;
  206. }
  207. if ($access != 1 && $access != 0)
  208. $access = 2;
  209. $remark = trim($remark);
  210. $auto_add_uid = intval($auto_add_uid);
  211. if ($auto_add_uid!=0)
  212. {
  213. $uid = $auto_add_uid;
  214. $pid = 0;
  215. }
  216. else
  217. {
  218. $uid = $this->uid;
  219. $pid = $this->fid;
  220. }
  221. $query = 'INSERT INTO `userfiles` ( `fid` , `uid` , `pid` , `type` , `filename` , `filepath` , `hostname` , `filetime` , `filesize` , `filetype` , `access` , `remark` ) '.
  222. 'VALUES ( \'\', \''.$uid.'\', \''.$pid.'\', \''.$type.'\', \''.addslashes($filename).'\', \''.addslashes($filepath).'\', \''.addslashes($_SERVER['REMOTE_ADDR']).'\', NOW( ) , \''.intval($filesize).'\', \''.addslashes($filetype).'\', \''.$access.'\', \''.addslashes($remark).'\' );';
  223. if (!mysql_query($query)) {
  224. if ($type == 0 )
  225. @unlink($filepath);
  226. $this->err = 'Ìí¼ÓÎļþÐÅϢʧ°Ü';
  227. return false;
  228. }
  229. if ($auto_add_uid==0)
  230. $this->err = '';
  231. return true;
  232. }
  233. /**
  234. * edit_file(int new_pid , string new_filename , int new_access , string new_remark)
  235. */
  236. function edit_file($new_pid,$new_filename,$new_access,$new_remark) {
  237. if ($this->pid != $new_pid) {
  238. if (!($p_file = new UserFile($new_pid , $this->uid))) {
  239. $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
  240. return false;
  241. }
  242. if (!$p_file->fid) {
  243. $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
  244. return false;
  245. }
  246. if ($p_file->type != 1) {
  247. $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
  248. return false;
  249. }
  250. }
  251. $new_filename = trim(ltrim($new_filename));
  252. if (!$new_filename) {
  253. $this->err = 'ÐÂÎļþÃû²»ÄÜΪ¿Õ';
  254. return false;
  255. }
  256. if ($new_access != 0 && $new_access != 1)
  257. $new_access = 2;
  258. $query = 'UPDATE userfiles SET '.
  259. ' pid = '.$new_pid.' ,'.
  260. ' filename = \''.addslashes($new_filename).'\' ,'.
  261. ' access = '.$new_access.' ,'.
  262. ' remark = \''.addslashes($new_remark).'\' '.
  263. 'WHERE fid = '.$this->fid.' LIMIT 1';
  264. mysql_query($query);
  265. return true;
  266. }
  267. function check_dir() {
  268. $query = 'SELECT `fid` FROM userfiles WHERE pid = '.$this->fid.' ORDER BY type DESC , filename ASC;';
  269. $result = mysql_query($query);
  270. $sub_files = array();
  271. while($rows = mysql_fetch_array($result))
  272. $sub_files[] = $rows[fid];
  273. if (mysql_num_rows($result))
  274. return $sub_files;
  275. else
  276. return false;
  277. }
  278. }
  279. ?>