Location: PHPKode > projects > Itshomi PHP Framework > itshomi/components/imiuser/AddUser.php
<?
/*************************************************************
*                         Add a new user                     *
*                 Copyright ALM Software Technologies             *
*                  Created by: Fernando Martinez             *
*                        05/01/06                            *
**************************************************************/

include_once($this->imipath.'/utils/FormData.php');
include_once($this->imipath."/utils/Util.php");
//include_once($path."/utils/Mailer.php");

/** add new user to users table */
class AddUser extends ImiComponent
{
    private $db;
    
    function doTask(){
		$pwd = $_REQUEST['password'];
		$pwd = md5($pwd);
		$_REQUEST['password'] = $pwd;
		$this->db = $this->navigator->getInstance('dbtable');
        if(is_object($this->db)){
		    $this->db->useTable('user');

    		if($this->userExists())
    		{
    			$formData = new FormData($_REQUEST);
    			$_REQUEST['formdata'] = serialize($formData);
    			$_REQUEST['feedback']= "Sorry, userid is already in use.<br>";
    			// forward to view
    			$this->navigator->addView($this->compPath().'NewUserForm.php');			
    			return;	
    		}
    		if(!validateSecCode($_POST['vcode']))
    		{
    			$formData = new FormData($_REQUEST);
    			$_REQUEST['formdata'] = serialize($formData);
    			$_REQUEST['feedback']= "Sorry, you must enter the given code correctly.<br>";
    			// forward to view
    			$this->addView($this->compPath().'NewUserForm.php');			
    			return;	
    		}
    		$names = array();
      	    $values = array();
      	    if(!$this->db->mapParams($names,$values))
                throw new Exception("Operation aborted due to error in submitted form");
      	    $nfields = count($names);
      	    $names[$nfields] = "regdate";
    		$values[$nfields]=$this->db->pS(date("Y/m/d H:i:s"));
            $photo = $this->uploadPhoto($_REQUEST['username']);
            if(isset($photo))
            {
                $nfields = count($names);
                $names[$nfields] = "photo";
                $values[$nfields]=$this->db->pS($photo);
            }
    
    		$this->db->addRow($names,$values);
			if(isset($_SESSION['LoggedIn']) && $_SESSION['roleid']==4)
				$_REQUEST['feedback']="The user has been successfully registered. ";
			else	
    			$_REQUEST['feedback']="You have been successfully registered. Please Login using the Login form.";
        }else
            error_log("Failed to instantiate the db");    
		//$mailer = new Mailer();
		//$msg = "A new user with username: ".$_REQUEST['userid']." has registered on MindThread";
		//$mailer->mail("Admin",$config['alt_email'],"hide@address.com","New user registered",$msg);
	}
	
	/** check if user exists */
	function userExists()
	{
		$username = $_REQUEST['username'];
		$rows = $this->db->getRows("username='".$username."'");
		return (count($rows)>0);
	}
	
	/** check random code */
	function checkCode()
	{
		$usercode = $_REQUEST['vcode'];
		$expectedCode = $_SESSION["tmpvar1"];
		if(!isset($usercode) || strpos($expectedCode,$usercode)===false)
		{
			return false;
		}
		return true;
	}
    
    function uploadPhoto($userid)
    {
        $f = pathinfo('dir/' . $_FILES['photo']['name']);
        $ext = strtolower($f['extension']);
        $allowed = array();
        $allowed[0] = "image/jpeg";
        $allowed[1] = "image/pjpeg";
        $allowed[2] = "image/gif";
        if(uploadFile("photo","media/",$userid.".".$ext,$allowed,200000))
            return $userid.".".$ext;
        else
            return null;    
    }
}
Return current item: Itshomi PHP Framework