Source

myswap / 1 / code / App / Lib / Action / EmptyAction.class.php

<?php
Class EmptyAction extends Action{ 
    public function _empty()
    {
		$this->deal_with_single_swap();
    }
    public function deal_with_single_swap($alias=0)
    {
		$this->setSettings();
		if($alias){
			$swap=$this->getSwapModel($alias);
		}else{
			$swap=$this->getSwapModel(ACTION_NAME);
		}
		if($swap){
			$this->assign('swap',$swap);
		}else{
			$this->assign('msg_error',"have't create yet");
			$this->assign('about_to_create_alias',ACTION_NAME);
		}
		$this->display('Public/single_swap');
    }
    

	public function setSettings(){
		$settings=$this->getUserModel();
		$this->assign('settings',$settings);
		return $settings;
	}
	public function getUserModel($username=0){ 
			if (is_numeric(MODULE_NAME)){
				$User=D("User");
				$user=$User->getById(MODULE_NAME);
				return $user[0];
			}else{
				$User=D("User");
				if($username){
					$user=$User->getByUsername($username);
				}else{
					$user=$User->getByUsername(MODULE_NAME);
				}
				return $user[0];
			}
	}
	public function getSwapModel($alias){ 
			if (is_numeric($alias)){
					$Swap=D("Swap");
					$swap=$Swap->getById($alias);
					return $swap[0];
			}else{
				$Swap=D("Swap");
				$swap=$Swap->getByAlias($this->getUserModel(),$alias);
				return $swap[0];
			}
	}

	public function getPublicState(){
		$user=$this->getUserModel();
		return $user["public_state"];
	}
	public function getUserID(){ 
			if (is_numeric(MODULE_NAME)){
				return MODULE_NAME;
			}else{
				$user=$this->getUserModel();
				return $user["user_id"];
			}
	}
	public function getUsername(){ 
			if (is_numeric(MODULE_NAME)){
				$user=$this->getUserModel();
				return strtolower($user["username"]);
			}else{
				return strtolower(MODULE_NAME);
			}
	}


    public function index()
    {
		if(!$this->setSettings()){
			$this->assign('msg_error',"have't create yet");
		}
		$this->outputSwap($this->getUserID());
    }
    public function getFriends()
    {
		$settings=$this->getUserModel();
		$friends=preg_split("/ /", $settings["friends"]);
		return $friends;
    }
    public function getAliases()
    {
		$settings=$this->getUserModel();
		$aliases=preg_split("/ /", $settings["userAliases"]);
		return $aliases;
    }
    public function goHome()
    {
		// header("Location: http://".$_SERVER["HTTP_HOST"]."/".MODULE_NAME."/");
		$this->redirect('index', array(),0,'');
    }
    public function outputSwap($user_id)
    {
    	$Swap=D("Swap");
    	$SWAPS=$Swap->getSwaps($user_id);

		$this->assign('friends',$this->getFriends());
		$this->assign('aliases',$this->getAliases());
		$this->assign('SWAPS',$SWAPS);
		$this->display('Public/swap');
    }
    public function setting()
	{
		//echo $this->getUsername();
		$msg_error="";
		if($this->authWithPassword($_POST["password"])){
		//成功修改资料
			$data=array();
			$User=M("User");
			if($_POST["userAliases"]){
				$data["userAliases"]=trim($_POST["userAliases"]);
			}
			if($_POST["friends"]){
				$data["friends"]=trim($_POST["friends"]);
			}
			if($_POST["new_password"]){
				if($_POST["new_password"]==$_POST["confirm_password"]){
					$data["password"]=sha1($_POST["new_password"]);
				}else{
					$msg_error.="password did not match";
				}			
			}
			if($_POST["username"]!=$this->getUsername()){
				if(valid_username($_POST["username"])=="valid"){
					$data["username"]=$_POST["username"];
					$User->where('user_id='.$this->getUserID())->save($data);
					// echo $User->getLastSql();
					$settings=$this->getUserModel($_POST["username"]);
				}else{
					$msg_error.=valid_username($_POST["username"]);
				}

			}else{
				$User->where('user_id='.$this->getUserID())->save($data);
				// echo $User->getLastSql();
				$settings=$this->getUserModel();
			}

			if($msg_error==""){
				$this->assign('msg_success',"saved");
				$this->assign('settings',$settings);
				$this->display('Public/setting');
			}

		}else if($_POST["password"]){
			$msg_error.="wrong password";
		}else{
			$this->setSettings();
			$this->display('Public/setting');
		}
		
		if($msg_error){
			$this->assign('msg_error',$msg_error);
			$this->setSettings();
			$this->display('Public/setting');
		}

    }
    public function auth()
	{    	
		$this->setSettings();
		if($this->authWithPassword($_POST["password"])){
			$this->index(1);
		}else if($_POST["password"]){
			$this->assign('msg_error',"wrong password");
			$this->display('Public/auth');
		}else{
			$this->display('Public/auth');
		}
    }
    public function authWithPassword($password)
	{    	
		$settings=$this->getUserModel();
		if(sha1($password)==$settings["password"]){
			return (1);
		}else{
			return (0);
		}
    }
	// public function swap()
    // {
		// $Swap=M("Swap");
		// $Swap->find($_GET["id"]);
		// echo '<textarea cols="160"   rows="40" >'.htmlspecialchars($Swap->content).'</textarea>';
    // }
    public function addSwap()
	{
		$msg_error="";

		if($this->authWithPassword($_POST["password"])){
			if(valid_alias($this->getUserID(),$_POST["alias"])=="valid" || $_POST["alias"]==""){
				$content=$_POST["content"];
				$alias=$_POST["alias"];
				if($content){
					$Swap=D("Swap");
					$Swap->user_id=$this->getUserID();
					$Swap->content=$content;
					$Swap->alias=$_POST["alias"];
					$swap_id=$Swap->add();
					if($_POST["alias"]==""){
						//$Swap->find($swap_id);
						//$Swap->alias=$swap_id;
						//$Swap->save();
						$alias=$swap_id;
					}
					$this->deal_with_single_swap($alias);
				}
			}else{
				$msg_error.=valid_alias($this->getUserID(),$_POST["alias"]);
			}
		}else if($_POST["password"]){
			$msg_error.="wrong password";
		}
		
		if($msg_error){
			$this->assign("msg_error",$msg_error);
			$this->setSettings();
			$this->display('Public/swap');
		}
	}
    // }
    public function updateSwap()
	{
		$msg_error="";

		if($this->authWithPassword($_POST["password"])){
			$data=array();
			if($_POST["content"]){
				$data["content"]=$_POST["content"];
			}
			if($_POST["alias"]!=$_POST["last_alias"]){
				if(valid_alias($this->getUserID(),$_POST["alias"])=="valid"){
					$data["alias"]=$_POST["alias"];
				}else{
					$msg_error.=valid_alias($this->getUserID(),$_POST["alias"]);
				}
			}
			if($msg_error==""){
				$this->assign("msg_success","updated");
				$Swap=M("Swap");
				$Swap->where('swap_id='.$_POST["swap_id"])->save($data);
				$this->deal_with_single_swap($_POST["alias"]);
			}
		}else if($_POST["password"]){
			$msg_error.="wrong password";
		}
		if($msg_error!=""){
			$this->assign("msg_error",$msg_error);
			$this->setSettings();
			$this->display("Public/single_swap");
		}

	}
}

?>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.