Location: PHPKode > scripts > Upble > upble-master/application/controllers/pm.php
<?php if (!defined('BASEPATH')) exit('Access denied!');
Class Pm extends CI_Controller
{
	private $touser=null;
	function __construct()
	{
		
		parent::__construct();
		if(!$this->tank_auth->is_logged_in())
		{
			redirect('/ucp/login/');
		}
		$this->load->model('messages');
	}
	
	public function compose()
	{
		$username=$this->uri->segment(3,'');
		$message=array(
			'reciever'=>!empty($username) ? $username : $this->input->post('reciever'),
			'title'=>$this->input->post('title'),
			'content'=>$this->input->post('content'),
		);

		$this->load->library('form_validation');
		$this->form_validation->set_rules('reciever', 'Reciever', 'trim|required|xss_clean|max_length[20]|callback_username_check');
		$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean|max_length[225]');
		$this->form_validation->set_rules('content', 'Content', 'trim|required|xss_clean|max_length[1000]');
		
		if($this->form_validation->run())
		{
			$reciever=$this->form_validation->set_value('reciever');
			
			$message=array(
			'uid'=>$this->tank_auth->get_user_id(),
			'username'=>$this->tank_auth->get_username(),
			'touid'=>$this->touser->id,
			'tousername'=>$this->touser->username,
			'title'=>$this->form_validation->set_value('title'),
			'content'=>$this->form_validation->set_value('content'),
			'created_at'=>time()
			
			);
			if($this->messages->add($message))
			{
			   $this->users->pm_count($this->touser->id);
			   $this->session->set_flashdata('success','Message sent!');
			}
			
			
			redirect('/pm/sent');
			
				
			
		}
		
		$data['message']=$message;
		$data['type']='add';
		$data['partial']="message/_form";
		$data['heading']='Compose Message';
		$this->load->view('message/message',$data);
	}
	public function reply($id)
	{
		if(!is_numeric($id))
		{
			show_404();
		}
		$viewer=$this->tank_auth->get_user_id();
		if((!$message=$this->messages->get($id))||$viewer!=$message->touid)
		{
			show_404();
		}
	    $this->load->helper(array('form'));
		$data['message']=array(
		'reciever'=>$message->username,
		'title'=>'RE: '.$message->title,
		'content'=>''
		
		);
		
		$data['type']='add';
		$data['partial']="message/_form";
		$data['heading']='Compose Message';
		$this->load->view('message/message',$data);
		
	}
	public function inbox()
	{
		$this->listing();
	}
	public function sent()
	{
		$this->listing();
	}
	private function listing()
	{
		$type=$this->uri->segment(2,'inbox');
		$page=$this->uri->segment(3,0);
		if(!in_array($type,array('inbox','sent')))
		{
			show_404();
		}
		
		//pagination
		$conditions = array('uid' => $this->tank_auth->get_user_id(),'type' => $type);
		$pageData = $this->messages->getPageData('pm/'.$type,3,$conditions);
		$data['pagination_links'] = $pageData['pagination_links'];
		$data['pms']= $pageData['list'];
		
		$data['partial'] = "message/_box";
		$data['type'] = $type;
		$data['heading']= $type;
		$this->load->view('message/message',$data);
		
	}
	public function show($id)
	{
		if(!is_numeric($id))
		{
			show_404();
		}
		$viewer=$this->tank_auth->get_user_id();
		$message=$this->messages->get($id);
		if($message->uid==$viewer)
		{
			if($message->sentbox==0)show_404();
			$type='sent';
		}
		elseif($message->touid==$viewer)
		{
			$type='inbox';
			if($message->inbox==0)show_404();
			if($message->unread==1)
			{
				
				$this->messages->update(array('unread'=>0),$message->id);
				 $this->users->pm_count($viewer,'del');
			}
		}
		else
		{
			show_404();
		}
		
		$data['type']=$type;
		$data['pm']=$message;
		$data['heading'] = htmlspecialchars($message->title);
		$data['partial']="message/_show";
		$this->load->view('message/message',$data);
	}
	
	public function del($id)
	{
		if(!is_numeric($id))
		{
			show_404();
		}
		$viewer=$this->tank_auth->get_user_id();
		$message=$this->messages->get($id);
		if($message->uid==$viewer)
		{
			$type='sent';
			if($message->sentbox==0)
			{
				show_404();
			}
			elseif($message->inbox==1)
			{
				$this->messages->update(array('sentbox'=>0),$message->id);
			}
			else
			{
				$this->messages->delete($id);
			}
		}
		elseif($message->touid==$viewer)
		{
			$type='inbox';
			if($message->inbox==0)
			{
				show_404();
			}
			else
			{
				if($message->unread==1)
				{
					$this->users->pm_count($viewer,'del');
				}
				if($message->sentbox==1)
				{
					$this->messages->update(array('inbox'=>0),$message->id);
				}
				else
				{
					$this->messages->delete($id);
				}
			}
			
			
		}
		else
		{
			show_404();
		}
		$this->session->set_flashdata('notice','Message deleted!');
		redirect('pm/'.$type);
	}
	public function username_check($username)
	{
		if(preg_match('/\W+/',$username))
		{
			$this->form_validation->set_message('username_check', 'The %s field contains illegal characters');
			return FALSE;

		}
		if(!$user=$this->users-> get_user_by_username($username))
		{
			$this->form_validation->set_message('username_check', "user $username doesn't exist");
			return FALSE;
		}
		if($user->username==$this->tank_auth->get_username())
		{
			$this->form_validation->set_message('username_check', "Sending message to yourself is unnecessary");
			return FALSE;
		}
		$this->touser=$user;
		return true;
	}

}

?>
Return current item: Upble