138 lines
5.0 KiB
PHP
138 lines
5.0 KiB
PHP
<?php
|
||
class G6ftp
|
||
{
|
||
public $db;
|
||
private $paths;//array of westdc path
|
||
private $userpath;
|
||
private $westdcid=74;//从数据库中读出,newwestdc
|
||
private $westdcgroup=1;//从数据库中读出
|
||
public $pwd;
|
||
public $time;
|
||
function __construct()
|
||
{
|
||
}
|
||
private function getwestdcpath()
|
||
{
|
||
$sql=$this->db->quoteInto("select s.configvalue from g6ftpusersettings s where s.userid=? and s.configname='AccessList'",$this->westdcid);
|
||
$result=$this->db->fetchRow($sql);
|
||
$this->paths=explode("\n",$result['configvalue']);
|
||
}
|
||
private function getuserpath($paths)
|
||
{
|
||
$p[0]=$this->paths[0];
|
||
foreach($paths as $path)
|
||
{
|
||
foreach($this->paths as $p1)
|
||
{
|
||
if (strpos($p1, $path)!==false)
|
||
{
|
||
$p[]=$p1;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return implode("\n",$p);
|
||
}
|
||
private function getuserpathindb($userid)
|
||
{
|
||
$sql=$this->db->quoteInto("select s.configvalue from g6ftpusersettings s where s.userid=? and s.configname='AccessList'",$userid);
|
||
$result=$this->db->fetchRow($sql);
|
||
$this->userpath=explode("\n",$result['configvalue']);
|
||
}
|
||
private function getuserpath1($paths)
|
||
{
|
||
//数组判断
|
||
if (!is_array($this->userpath)) $this->userpath[]=$this->userpath;
|
||
//根据WESTDC帐号查找相似虚拟目录
|
||
foreach($paths as $path)
|
||
{
|
||
foreach($this->paths as $p1)
|
||
{
|
||
if (strpos($p1, $path)!==false)
|
||
{
|
||
$p[]=$p1;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
//对比已有帐号进行查找
|
||
foreach($this->userpath as $path)
|
||
{
|
||
foreach($p as $i=>$p1)
|
||
{
|
||
if (@strpos($p1, $path)!==false)
|
||
{
|
||
unset($p[$i]);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return implode("\n",$p);
|
||
}
|
||
function createuser($user)
|
||
{
|
||
//只插入到users_settings表中
|
||
$sql=$this->db->quoteInto("select * from g6ftpusers where name=?",$user->username);
|
||
$u=$this->db->fetchRow($sql);
|
||
if (empty($u))
|
||
{
|
||
$sql=$this->db->quoteInto("insert into g6ftpusers (domainid,name) values(1,?)",$user->username);
|
||
$this->db->query($sql);
|
||
$sql=$this->db->quoteInto("select id from g6ftpusers where domainid=1 and name=?",$user->username);
|
||
$a=$this->db->fetchRow($sql);
|
||
$userid = $a['id'];
|
||
/* 这部分功能已经放在数据库中实现
|
||
$sql="insert into g6ftpusersettings (userid,configname,configvalue) (select ".$userid." as userid,configname,configvalue from g6ftpusersettings where userid=".$this->westdcid.")";
|
||
$this->db->query($sql);
|
||
*/
|
||
} else $userid=$u['id'];//g6ftp中的用户ID,非系统的用户ID
|
||
$this->pwd=$user->password;//初始化
|
||
$this->time=$user->time;
|
||
//判断用户密码是否失效,或用户一次下载数据已经达到上限
|
||
$sql=$this->db->quoteInto("select * from ftpuser where userid=?",$user->id);
|
||
$u=$this->db->fetchRow($sql);
|
||
if (empty($u)) {
|
||
$sql=$this->db->quoteInto("update g6ftpusersettings set configvalue=? where configname='Password' and userid=".$userid,strtoupper('MD5:'.md5($user->password)));
|
||
$this->db->query($sql);
|
||
$this->getwestdcpath();
|
||
$sql=$this->db->quoteInto("update g6ftpusersettings set configvalue=? where configname='AccessList' and userid=".$userid,$this->getuserpath($user->path));
|
||
$this->db->query($sql);
|
||
//插入ftpuser信息
|
||
$sql="insert into ftpuser (userid,pwd,ts_created,ts_invalid,datacount) values(?,?,now(),?,1)";
|
||
$this->db->query($sql,array($user->id,$user->password,$user->time));
|
||
return true;
|
||
} elseif (strtotime($u['ts_invalid'])<=time() or $u['datacount']<1) {
|
||
//更新用户密码、数据地址和数据计数
|
||
//数据地址覆盖原来的信息
|
||
$sql=$this->db->quoteInto("update g6ftpusersettings set configvalue=? where configname='Password' and userid=".$userid,strtoupper('MD5:'.md5($user->password)));
|
||
$this->db->query($sql);
|
||
$this->getwestdcpath();
|
||
$sql=$this->db->quoteInto("update g6ftpusersettings set configvalue=? where configname='AccessList' and userid=".$userid,$this->getuserpath($user->path));
|
||
$this->db->query($sql);
|
||
$sql="update ftpuser set pwd=?,ts_created=now(),ts_invalid=?,datacount=1 where userid=?";
|
||
$this->db->query($sql,array($user->password,$user->time,$user->id));
|
||
return true;
|
||
} elseif (strtotime($u['ts_invalid'])>time() && $u['datacount']<$user->maxdata) {
|
||
//更新数据地址和数据计数
|
||
//数据地址要追加在原来的后面
|
||
//需要判断数据是否已经在下载进程中
|
||
$this->getwestdcpath();
|
||
$this->getuserpathindb($userid);
|
||
//var_dump($this->userpath);die();
|
||
if ($this->getuserpath1($user->path)) {
|
||
$sql=$this->db->quoteInto("update g6ftpusersettings set configvalue=configvalue||'\n'||? where configname='AccessList' and userid=".$userid,$this->getuserpath1($user->path));
|
||
$this->db->query($sql);
|
||
$sql="update ftpuser set ts_invalid=?,datacount=? where userid=?";
|
||
$this->db->query($sql,array($user->time,$user->datacount+1,$user->id));
|
||
} else {
|
||
$sql="select pwd,ts_invalid from ftpuser where userid=?";
|
||
$u=$this->db->fetchRow($sql,array($user->id));
|
||
$this->pwd=$u['pwd'];
|
||
$this->time=$u['ts_invalid'];
|
||
}
|
||
return true;
|
||
} else //同时下载数据数超过限制
|
||
return false;
|
||
}
|
||
}
|
||
?>
|