<?php

class MRegister extends Model{

	function MRegister(){
		parent::Model();
		
		$this->users_table = 'users';
	}

//Functions only for the user registration/authentication/login NEW
function getUserId($email){
      $data = array();
      $options = array('email' => $email);
      $Q = $this->db->getwhere('users',$options,1);
      if ($Q->num_rows() > 0){
        $data = $Q->row_array();
      }

      $Q->free_result();    
      return $data;  		

}


function checkUserEmail($email,$table)
    {
        $Q = $this->db->where("email",$email);
        $Q = $this->db->limit(1,0);
        $Q = $this->db->get($table);
 
        if ($Q->num_rows() == 0) {
            return NULL;
        }
		$Q->free_result();
 
        return TRUE;
    }
	
function birthday ($birthday){
	  list($year,$month,$day) = explode("-",$birthday);
	  $year_diff = date("Y") - $year;
	  $month_diff = date("m") - $month;
	  $day_diff = date("d") - $day;
	  if ($month_diff < 0) $year_diff--;
	  elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
	  return $year_diff;
}

function insertUser(){
	  $id= $this->session->userdata('ses_user_id');
	  if (db_clean($this->validation->birthday)) {
	  list($year,$month,$day) = explode("-",$_POST['birthday']);
			//$date = db_clean($_POST['birthday']);
			//$temp_date = explode("-",$date);
			//$stamp = mktime(0,0,0,$temp_date[1],$temp_date[0],$temp_date[2]);
			//$stamp = mktime(0,0,0,$temp_date[0],$temp_date[1],$temp_date[2]);
			//$new_date = date('Y/m/d',$stamp);
			$new_date = $year.'/'.$month.'/'.$day;
	  }
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	 $data_info = array( 
		'fname' => db_clean($this->input->post('fname')),
		'lname' => db_clean($this->input->post('lname')),
		'email' => $this->input->post('email'),
		'password' => dohash($this->input->post('password'), 'md5'),
		'sex' => $this->input->post('sex'),
		'birthday' => $new_date,
		'date_added' => $now
	);
 	$this->db->insert($this->users_table, $data_info);
	$email = $this->input->post('email');
	$data['user'] = $this->getUserId($email);
	
	$data_details = array( 
		'user_id' => $data['user']['id'],
		'about' => '',
		'occupation' => '',
		'marital_status' => '',
		'telephone' => '',
		'mobile' => '',
		'address' => '',
		//'photo' => '',
		'interests' => '',
		'hobbies' =>''
	);
	$this->db->insert('user_details', $data_details);
	return $data['user'];
 }
/** 	Validate an email address.
		Provide email address (raw input)
		Returns true if the email address has the email 
		address format and the domain exists.
*/
function validEmail($email){
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex){
   		$isValid = false;
   }else{
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64){
         // local part length exceeded
         $isValid = false;
      }else if ($domainLen < 1 || $domainLen > 255){
         // domain part length exceeded
         $isValid = false;
      }else if ($local[0] == '.' || $local[$localLen-1] == '.'){
         // local part starts or ends with '.'
         $isValid = false;
      }else if (preg_match('/\\.\\./', $local)){
         // local part has two consecutive dots
         $isValid = false;
      }else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)){
         // character not valid in domain part
         $isValid = false;
      }else if (preg_match('/\\.\\./', $domain)){
         // domain part has two consecutive dots
         $isValid = false;
      }else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',str_replace("\\\\","",$local))){
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',str_replace("\\\\","",$local))){
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))){
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

	
}


?>