/pc/userfile.php
https://github.com/yyms/lilacweb · PHP · 309 lines · 264 code · 30 blank · 15 comment · 59 complexity · dca086f378743f69906efd8ba5b7804a MD5 · raw file
- <?php
- require('pcfuncs.php');
- if (!$pcconfig["USERFILES"]) //²»Ö§³Ö¸öÈË¿Õ¼ä
- header('Location: /');
- if (!defined('_USER_FILE_ROOT_'))
- header('Location: /');
- function bbs_userfile_getfiledir($userid) {
- return _USER_FILE_ROOT_ . '/'. strtoupper($userid[0]) .'/'. $userid;
- }
- function bbs_userfile_getfilepath($userid, $filepath) {
- return bbs_userfile_getfiledir($userid) . '/' . $filepath;
- }
-
- function bbs_userfile_getrootfid($uid) {
- $query = 'SELECT fid FROM userfiles WHERE uid = '.intval($uid).' AND type = 1 AND pid = 0 LIMIT 1;';
- $result = mysql_query($query);
- if ($rows = mysql_fetch_array($result))
- {
- mysql_free_result($result);
- return $rows[fid];
- }
- if (!UserFile::add_file('',1,'','',0,'',0,$uid))
- return false;
- return bbs_userfile_getrootfid($uid);
- }
- $file_access = array('¹«¿ª','ºÃÓÑ','˽ÓÐ');
- class UserFile {
- var $fid;
- var $uid;
- var $pid;
- var $filename;
- var $filepath;
- var $filesize;
- var $filetype;
- var $type;
- var $hostname;
- var $filetime;
- var $access;
- var $remark;
- var $err;
- function UserFile ($fid, $uid=0) {
- $fid = intval($fid);
- $uid = intval($uid);
- if ($uid)
- $query = 'SELECT * FROM userfiles WHERE fid ='.$fid.' AND uid = '.$uid.' LIMIT 1;';
- else
- $query = 'SELECT * FROM userfiles WHERE fid ='.$fid.' LIMIT 1;';
- $result = mysql_query($query);
- if (!($rows = mysql_fetch_array($result))) {
- $this->err = 'Îļþ²»´æÔÚ';
- return false;
- }
- $this->fid = $rows[fid];
- $this->uid = $rows[uid];
- $this->pid = $rows[pid];
- $this->type = $rows[type];
- $this->hostname = $rows[hostname];
- $this->filetime = $rows[filetime];
- $this->access = $rows[access];
- $this->remark = $rows[remark];
- $this->filename = $rows[filename];
- $this->filepath = $rows[filepath];
- $this->filesize = ($this->type==1)?'-':$rows[filesize];
- $this->filetype = ($this->type==1)?'-':$rows[filetype];
- mysql_free_result($result);
-
- return true;
- }
- function uf_edit($new_filename,$new_access,$new_remark) {
- return $this->edit_file($this->pid,$new_filename,$new_access,$new_remark);
- }
- function uf_mv($target_fid) {
- if (!($target_file = new UserFile($target_fid , $this->uid))) {
- $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
- return false;
- }
- if (!$target_file->fid) {
- $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
- return false;
- }
- if ($target_file->type != 1) {
- $this->err = 'ÄúÖ»Äܽ«Õâ¸öÎļþÒƶ¯µ½ÁíÒ»¸öÎļþ¼ÐÖÐ';
- return false;
- }
- return $this->edit_file($target_file->fid,$this->filename,$this->access,$this->remark);
- }
- function uf_readdir(&$sub_files) {
- if ($this->type != 1) {
- $this->err = 'Õâ²»ÊÇÒ»¸öĿ¼';
- return false;
- }
- $sub_files = $this->check_dir();
- return true;
- }
- function uf_mkdir($filename, $access, $remark) {
- return $this->add_file($filename,1,'','',$access,$remark,0);
- }
- function uf_add($filename,$tmp_filepath,$filetype,$access,$remark) {
- return $this->add_file($filename,0,$tmp_filepath,$filetype,$access,$remark,1);
- }
- function uf_cp($target_fid) {
- if ($target_fid == $this->pid) {
- $this->err = 'ÄúÖ»Äܽ«Õâ¸öÎļþ¸´ÖƵ½ÁíÒ»¸öÎļþ¼ÐÖÐ';
- return false;
- }
- if ($this->type == 1) {
- $this->err = '½öÓÐÎļþ²ÅÄܸ´ÖÆ';
- return false;
- }
- if (!($target_file = new UserFile($target_fid , $this->uid))) {
- $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
- return false;
- }
- if (!$target_file->fid) {
- $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
- return false;
- }
- if ($target_file->type != 1) {
- $this->err = 'ÄúÖ»Äܽ«Õâ¸öÎļþ¸´ÖƵ½ÁíÒ»¸öÎļþ¼ÐÖÐ';
- return false;
- }
- $ret = $target_file->add_file($this->filename,0,$this->filepath,$this->filetype,$this->access,$this->remark,0);
- $this->err = $target_file->err;
- return $ret;
- }
- function uf_rm() {
- if ($this->type == 1) {
- if ($this->check_dir()) {
- $this->err = 'ÇëÏÈɾ³ý¸ÃĿ¼ÏµÄËùÓÐÎļþ';
- return false;
- }
- }
- else
- @unlink($this->filepath);
- $query = 'DELETE FROM userfiles WHERE fid = '.$this->fid.' LIMIT 1;';
- mysql_query($query);
- return true;
- }
- function gen_filepath() {
- global $currentuser;
- $filepath = 'F';
- $filepath .= '.'.$currentuser['index'];
- $filepath .= '.'.time();
- $filepath .= '.'.rand(1000,9999);
- return $filepath;
- }
- /**
- * add_file(string filename, int type , string tmp_filepath , string filetype , int access , string remark, int after_del, int auto_add_uid)
- * Ìí¼ÓÎļþ/Ŀ¼
- * filename: Îļþ/Ŀ¼Ãû
- * type : 0:Îļþ / 1: Ŀ¼
- * tmp_filepath: ÁÙʱÎļþ
- * filetype: ÎļþÀàÐÍ
- * access: ȨÏÞ 0:¹«¿ª/1:ºÃÓÑ/2:˽ÓÐ
- ¡Á remark: ±¸×¢
- * upload_file: ÊÇ·ñΪÉÏ´«Îļþ 0:²»/1:ÊÇ
- * auto_add_uid : ÊÇ·ñΪ×Ô¶¯Ìí¼Ó 0:²»/1:ÊÇ
- */
- function add_file($filename ,$type ,$tmp_filepath ,$filetype ,$access ,$remark, $upload_file, $auto_add_uid=0) {
- global $currentuser;
-
- $type = ($type==1)?1:0;
- $filename = trim(ltrim($filename));
- if ($auto_add_uid==0) {
- if (!$filename) {
- $this->err = ($type==1)?'ÇëÊäÈëĿ¼Ãû':'ÇëÊäÈëÎļþÃû';
- return false;
- }
- }
- if ($type == 0) {
- if ($upload_file) { //ÉÏ´«Îļþ
- if (!is_uploaded_file($tmp_filepath)) {
- $this->err = 'ÎļþÉÏ´«´íÎó';
- return false;
- }
- }
- else {
- if (!file_exists($tmp_filepath)) {
- $this->err = 'Ô´Îļþ '.$tmp_filepath.' ²»´æÔÚ';
- return false;
- }
- }
- $filedir = bbs_userfile_getfiledir($currentuser['userid']);
- if (!file_exists($filedir)) {
- if (!mkdir($filedir)) {
- $this->err = '´´½¨ÎļþĿ¼ʧ°Ü';
- return false;
- }
- }
- $filepath = bbs_userfile_getfilepath($currentuser['userid'], $this->gen_filepath());
- if ($upload_file) {
- if (!move_uploaded_file($tmp_filepath,$filepath)) {
- $this->err = 'Òƶ¯Îļþʧ°Ü';
- return false;
- }
- }
- else {
- if (!copy($tmp_filepath, $filepath)) {
- $this->err = '¿½±´Îļþʧ°Ü';
- return false;
- }
- }
-
- $filetype = trim(ltrim($filetype));
- $filesize = filesize($filepath);
- }
- else {
- $filepath = $filetype = '';
- $filesize = 0;
- }
-
- if ($access != 1 && $access != 0)
- $access = 2;
-
- $remark = trim($remark);
-
- $auto_add_uid = intval($auto_add_uid);
- if ($auto_add_uid!=0)
- {
- $uid = $auto_add_uid;
- $pid = 0;
- }
- else
- {
- $uid = $this->uid;
- $pid = $this->fid;
- }
-
- $query = 'INSERT INTO `userfiles` ( `fid` , `uid` , `pid` , `type` , `filename` , `filepath` , `hostname` , `filetime` , `filesize` , `filetype` , `access` , `remark` ) '.
- 'VALUES ( \'\', \''.$uid.'\', \''.$pid.'\', \''.$type.'\', \''.addslashes($filename).'\', \''.addslashes($filepath).'\', \''.addslashes($_SERVER['REMOTE_ADDR']).'\', NOW( ) , \''.intval($filesize).'\', \''.addslashes($filetype).'\', \''.$access.'\', \''.addslashes($remark).'\' );';
- if (!mysql_query($query)) {
- if ($type == 0 )
- @unlink($filepath);
- $this->err = 'Ìí¼ÓÎļþÐÅϢʧ°Ü';
- return false;
- }
- if ($auto_add_uid==0)
- $this->err = '';
- return true;
- }
- /**
- * edit_file(int new_pid , string new_filename , int new_access , string new_remark)
- */
- function edit_file($new_pid,$new_filename,$new_access,$new_remark) {
- if ($this->pid != $new_pid) {
- if (!($p_file = new UserFile($new_pid , $this->uid))) {
- $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
- return false;
- }
- if (!$p_file->fid) {
- $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
- return false;
- }
- if ($p_file->type != 1) {
- $this->err = 'Ä¿±êÎļþ¼Ð²»´æÔÚ';
- return false;
- }
- }
- $new_filename = trim(ltrim($new_filename));
- if (!$new_filename) {
- $this->err = 'ÐÂÎļþÃû²»ÄÜΪ¿Õ';
- return false;
- }
- if ($new_access != 0 && $new_access != 1)
- $new_access = 2;
-
- $query = 'UPDATE userfiles SET '.
- ' pid = '.$new_pid.' ,'.
- ' filename = \''.addslashes($new_filename).'\' ,'.
- ' access = '.$new_access.' ,'.
- ' remark = \''.addslashes($new_remark).'\' '.
- 'WHERE fid = '.$this->fid.' LIMIT 1';
- mysql_query($query);
- return true;
- }
- function check_dir() {
- $query = 'SELECT `fid` FROM userfiles WHERE pid = '.$this->fid.' ORDER BY type DESC , filename ASC;';
- $result = mysql_query($query);
- $sub_files = array();
- while($rows = mysql_fetch_array($result))
- $sub_files[] = $rows[fid];
- if (mysql_num_rows($result))
- return $sub_files;
- else
- return false;
- }
-
- }
- ?>