westdc-zf1/application/models/Proftp.php

85 lines
3.0 KiB
PHP
Raw Normal View History

2010-11-06 09:41:02 +00:00
<?php
class Proftp
{
public $db;
public $pwd;
public $time;
function __construct()
{
}
// deal with online & offline user
// online user, $user->param is 'onlineappid=xx', xx is number
// offline user, $user->param is 'offlineid=xx', xx is number
2010-11-06 09:41:02 +00:00
function createuser($user)
{
$this->insert_user($user);
//处理FTP用户目录
if (!empty($user->host))
{
//vist $user->host web
$page=file_get_contents('http://'.$user->host.'/proftp.php?'.$user->param);
if (!empty($page) die($page);//there are errors in visit ftp page
}
return $this->update_pwd($user);
}
/*
* insert user into pureftp table
* param $user: username,
* return null
*/
private function insert_user($user)
2010-11-06 09:41:02 +00:00
{
//只插入到users表中
$sql=$this->db->quoteInto("select * from proftpusers where userid=?",$user->username);
$u=$this->db->fetchRow($sql);
if (empty($u))
{
2010-11-08 02:08:31 +00:00
//$sql="insert into proftpusers (userid,passwd,uid,gid,homedir) values(?,?,109,1002,?)";
//$this->db->query($sql,array($user->username,$user->password,'/home/ftp/'.$user->username));
$sql="insert into proftpusers (userid,passwd,uid,gid,homedir) values('".$user->username."','".$user->password."',109,1002,'".'/home/ftp/'.$user->username."')";
$this->db->query($sql);
}
}
/*
* update password in the table used by pureftp
* param $user: password,id,time,username
* return: bool
*/
private function update_pwd($user)
{
2010-11-06 09:41:02 +00:00
$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)) {
//插入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) {
2010-11-08 02:08:31 +00:00
//更新用户密码和数据计数
$sql="update proftpusers set passwd='".$user->password."' where userid='".$user->username."'";
//$this->db->query($sql,array($user->password,$user->username));
2010-11-06 09:41:02 +00:00
$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*/) {
2010-11-08 02:08:31 +00:00
//更新数据计数
//同步ftpuser和proftpusers用户密码
$sql="update ftpuser set ts_invalid=?,datacount=datacount+?,pwd=proftpusers.passwd
2011-12-22 13:34:06 +00:00
from proftpusers where ftpuser.userid=? and proftpusers.userid=?";
$this->db->query($sql,array($user->time,$user->datacount,$user->id,$user->username));
2010-11-06 09:41:02 +00:00
$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;
2010-11-06 09:41:02 +00:00
}
}
?>