<?php
class UserAction extends CommonAction{
    public function index(){
		parent::islogin();
		parent::showSiteInfo('个人信息--控制中心');
		$user=M('user');
		$info=$user->field('id,nickname,sex,memo')->find(getUserID());
		$this->assign('info',$info);
        $this->display();
    }
	public function passwd()
	{
		parent::showSiteInfo('密码设置--控制中心');
		$this->display();
	}
	public function mail()
	{
		parent::islogin();
		parent::showSiteInfo('邮箱设置--控制中心');
		$user=M('user');
		$info=$user->field('id,mail')->find(getUserID());
		$this->assign('info',$info);
        $this->display();
	}
	public function icon()
	{
		parent::islogin();
		parent::showSiteInfo('个人形象设置--控制中心');
		$user=M('user');
		$info=$user->field('id,icon')->find(getUserID());
		$this->assign('info',$info);
		$this->display();
	}
	public function cutimg()
	{
		parent::islogin();
		parent::showSiteInfo('图片处理--控制中心');
		$result =$this->upload('temp');
		if (!is_array($result))
		{
			$this->redirect('icon');
		}
		else
		{
		   $this->assign('imgurl', '__ROOT__/' . C('ATTACHDIR') . '/temp/' . $result[0]['savename']);
		   $this->assign('imgname', $result[0]['savename']);
		   $this->display();
		}
	}
 public function upload($module = '', $path = '', $thumb = '', $width = '', $height = '')
 {
 	parent::islogin();
	$module = $module = ""?'temp':$module;
	switch ($module)
	{
	   case 'temp':$path = C(ATTACHDIR) . '/temp/' . $path;
			break;
	   default:$path = C(ATTACHDIR) . '/file/' . $path;
	}
	if (!is_dir($path)) @mk_dir($path);
	import("ORG.Net.UploadFile");
	$upload = new UploadFile();
	$upload->maxSize = C(ATTACHSIZE);
	$upload->allowExts = explode(',', strtolower(C(ATTACHEXT)));
	$upload->savePath = $path;
	$upload->saveRule = 'uniqid';
	empty($thumb)?$upload->thumb = C(ATTACH):$upload->thumb = $thumb;
	empty($width)?$upload->thumbMaxWidth = C(THUMBMAXWIDTH):$upload->thumbMaxWidth = $width;
	empty($height)?$upload->thumbMaxHeight = C(THUMBMAXHEIGHT):$upload->thumbMaxHeight = $height;
	if (!$upload->upload())
	{
	
		return $this->error($upload->getErrorMsg());
	}
	else
	{
		return $upload->getUploadFileInfo();
	}
 }
 public function setavatar()
 {
 	parent::islogin();
	if (!empty($_POST['cut_pos']))
	{
		import('Com.ImageResize');
	    $imgresize = new ImageResize(); 
	   // use the full path
	    $url = C('ATTACHDIR') . '/temp/' . trim($_POST['imgname']);
	    $imgresize->load($url);
	    //dump($url);
	    $posary = explode(',', $_POST['cut_pos']);
	    foreach($posary as $k => $v)
	    $posary[$k] = intval($v);
		if ($posary[2] > 0 && $posary[3] > 0) $imgresize->resize($posary[2], $posary[3]);
		//dump($posary); 
   		// create filename rule
	   $uico = time().rand(100,999).'.jpg';
	   //dump($uico);
	   $path = C('AVATAR'); 
	   // save 120*120 image
	   $imgresize->cut(120, 120, intval($posary[0]), intval($posary[1]));
	   $large = 'dadeng_' . $uico;
	   $imgresize->save($path . $large);
	   $imgresize->resize(60,60);
	   $large = 'dadeng_60_' . $uico;
	   $imgresize->save($path . $large);
	   $large = 'dadeng_30_' . $uico;
	   $imgresize->resize(30,30);
	   $imgresize->save($path . $large,true);
	   unlink($url);
	   //echo '<img src="'.__ROOT__.'/' . $path . $large . '" />'; 
	   if($uico)
	   {
	   		$user=M('user');
			$data=array();
			$data['id']=getUserID();
			$s=$user->field('id,icon')->where($data)->find();
			//删除以前的个人形象
			if($s['icon'])
			{
				unlink($path.'dadeng_'.$s['icon']);
				@unlink($path.'dadeng_24_'.$s['icon']);
				@unlink($path.'dadeng_60_'.$s['icon']);
			}
			//
			$data['icon']=$uico;
			$ok=$user->save($data);
			if($ok)
				$this->redirect('user/icon');
			else
				$this->error('error');
	   }
	}
	else
	{ 
		// error reporting
		$this->error('error');
	}
 }
 public function other()
 {
 	parent::islogin();
 	parent::showSiteInfo('杂项设置--控制中心');
 	$site=F('site');
	$this->assign('site',$site);
 	$this->display();
 }
 public function hotuser()
 {
 	parent::islogin();
 	parent::showSiteInfo('推荐用户--控制中心');
 	$rs=FALSE;
 	$site=F('site');
	$id=getUserID();
	$u=M('user');
	$rs=$u->find($id);
	if(!$rs)
	{
		$this->error('用户ID错误');
	}
	else
	{
		$userscore=$rs['score'];
		if($userscore<$site['hot_score'])
		{
			$this->error('您的积分太少哦。还不能成为推荐用户!');
		}
		//计算有多少个用户
		$count=$u->where('is_top')->count();
		if($count>=18)
		{
			$this->error('当前推荐用户已经满了，请过一段时间再试！');
		}
		else
		{
			$where['is_top']=1;
			$where['hot_time']=time();
			if($u->where('id='.$id)->save($where))
			{
				execUserScore($id,$site['hot_score'],$rs['userid']."购买推荐用户,花费:".$site['hot_score']);//扣除用户的积分
				$this->success('恭喜您成为推荐用户');
			}
			else
				$this->error('发生错误');
		}
	}
 }
public function invite()
{
	parent::islogin();
 	parent::showSiteInfo('邀请注册--控制中心');
	$site=F('site');
	$this->assign('info',$site);
	$this->display();
}
}
?>