<?php
include(ROOT.'Config/config.inc.php');
include(ROOT.'api/config.inc.php');
include(ROOT.'api/client/client.php');
class Ucenter {
	/*
	@var $ErrorInfo string;
	*/
	public $ErrorInfo;
	/*
	用户同步插入
	@param $username string 
	@param $password string
	@param $email string
	return bool
	*/
	function add($username, $password, $email){
		$uid = uc_user_register($username, $password, $email);
		if($uid <= 0) {
			if($uid == -1) {
				$this->error= _('用户名不合法');
			} elseif($uid == -2) {
				$this->error =  _('包含要允许注册的词语');
			} elseif($uid == -3) {
				$this->error = _('用户名已经存在');
			} elseif($uid == -4) {
				$this->error = _('Email 格式有误');
			} elseif($uid == -5) {
				$this->error = _('Email 不允许注册');
			} elseif($uid == -6) {
				$this->error = _('该 Email 已经被注册');
			} else {
				$this->error = _('未定义');
			}
			return false;	//插入失败
		}else{
			return true;	//插入成功
		}
	}
	/*
	资料同步修改
	*/
	public function modify($username , $oldpass, $password, $email, $ignoreoldpw=1){
		$ucresult  = uc_user_edit($username, $oldpass, $password, $email, $ignoreoldpw);
		if($ucresult == -1) {
			$this->ErrorInfo = _('旧密码不正确');
			return false;
		} elseif($ucresult == -4) {
			$this->ErrorInfo = _('Email 格式有误');
			return false;
		} elseif($ucresult == -5) {
			$this->ErrorInfo = _('Email 不允许注册');
			return false;
		} elseif($ucresult == -6) {
			$this->ErrorInfo = _('该 Email 已经被注册');
			return false;
		}else{
			return true;
		}
	}
	/*
	用户删除
	@param $username string
	*/
	public function delete($username){
		return uc_user_delete($username);
	}
	/*
	验证用户名或者邮箱是否重复
	*/
	public function checkusername($action, $val){
		if ($action == 'username'){
			$uid = uc_user_checkname($val);
			if ($uid>0){
				return true;
			}else{
				return false;
			}
		}elseif ($action == 'email'){
			$uid = uc_user_checkemail($val);
			if ($uid>0){
				return true;
			}else{
				return false;
			}			
		}else{
			exit('undefined');
		}
	}
	/*
	用户同步登录
	*/
	public function synclogin($uid){
		return uc_user_synlogin($uid);
	}
	/*
	同步退出
	*/
	public function synlogout($uid){
		return uc_user_synlogout($uid);
	}
	public function login($username , $password){
		return uc_user_login($username, $password);
	}
}
?>