<?php


	      
class users_register{

	private $error = false;
	private $act = 'form';

	
	function __construct(){
	global $MAIN_PAGE;
		define('M_PATH',MAIN_SOURCE_PATH.'/modules/'.$MAIN_PAGE -> dirname() );
		define('M_URL',MAIN_SOURCE_URL.'/modules/'.$MAIN_PAGE -> dirname() );
		include_once(M_PATH.'/inc/url.php');
		include_once(MAIN_PATH.'/tmp/config/users.php');
		include_once(MAIN_PATH.'/tmp/meta/users.php');
		
		include_once(M_PATH.'/inc/class.users.php');
		
		include_once(MAIN_SOURCE_PATH.'/inc/class.html.php');
		include_once(MAIN_SOURCE_PATH.'/ext/ajax/JsHttpRequest.php');
		new JsHttpRequest($MAIN_PAGE -> charset());
		$this -> act = !empty($_REQUEST['act']) ? $_REQUEST['act'] : $this -> act;
		$GLOBALS['_RESULT']['id_request'] = !empty($_REQUEST['id_request']) ? $_REQUEST['id_request'] : 0;
	}
	
	public function get(){
	global $MAIN_PAGE;
		$func = $this -> act;
		if ( !method_exists($this ,$func) ){
			sys_error(ERROR_404);
		}
		$MAIN_PAGE -> set_key($func);
		return  $this -> $func();
	}
	
	/**
	* Форма регистрации пользователей / сообщение о активации / итоговая регистрация
	*
	*/
	private function form(){
	global $MAIN_PAGE;
	  $t = new PHPTAL( false );
	  $t -> setSnippet( 'users','user_register_form');
	  $t -> row = $_POST['form'];
	  $t -> error = $this -> error;
	  $MAIN_PAGE -> set_title( 2, $MAIN_PAGE -> meta_title( 'form' ) );

	  return $t -> execute();
	}
	
	
	
  private function save(){
  global $MAIN_PAGE;
  
    $u = users::create();
      //  активация аккаунта
    $active = $MAIN_PAGE -> config('users_active')==1 ? 0 : 1;
	  try{
	    
	    foreach( $_POST['form'] as $k=>$v ){
	      $u -> set($k,$v);
	    }
	    $u -> set('u_active',$active);
      $u -> set('u_time_reg',MAIN_TIME);
      $u -> set('u_act_code', sys_str_rand(5));
	    $u -> set('g_id', $MAIN_PAGE -> config('users_register_group') );
	    //$u -> set_debug(1);
	    $id = $u -> insert() ;
	    
	  }catch(Exception $e){
		  $this -> error =  $e -> getMessage();
		  return $this -> form();
		}
    if ( empty($active) ){
      include_once(MAIN_SOURCE_PATH.'/inc/class.mail.php');
	    $m =  mail::load('register_confirm');
	    if ( false===$m ){
	      sys_error(ERROR_500);
	    }
	    $m -> set('MAIN_URL',MAIN_URL);
	    $m -> set('uname',$u -> name());
	    $m -> set('login',$u -> login());
	    $m -> set('URL_USERS_REGISTER_ACTIV', sys_url(URL_USERS_REGISTER_ACTIV,$u -> id(),$u -> act_code()) );
	    $m -> send_mail(  $u -> email() );
	  }
	  
	  $t = new PHPTAL( false );
	  $t -> setSnippet( 'users','users_register_save');
	  $t -> active = $active;
		return $t -> execute();
  }
  
  /**
  * Активация аккаунта
  *
  */	
	private function active(){ 
	global $MAIN_PAGE;
	  try{
	  $u = users::load_id($_GET['id']);
	  
	  if ( false===$u ){
	     throw new Exception('Ошибка: пользователь не найден!');
	  } 
	  if ( $u -> act_code()!=$_GET['code'] ){
	    throw new Exception('Ошибка: неверная ссылка!');
	  }

	  if ( $u -> active()==1){
	    throw new Exception('Ошибка: пользователь уже активирован!');
	  }
	  $u -> set('u_active',1);
	  $u -> set('u_act_code',sys_str_rand(5) );
	  $u -> update();

	  }catch( Exception $e ){
	  
	    
	    $error = $e -> getMessage(); 
	  }
	  	    //  регистрация окончена
	  $t = new PHPTAL( false );
	  $t -> setSnippet( 'users','users_register_active');
	  $t -> error = $error;
		
     
    //$MAIN_PAGE -> set_title( 2, $MAIN_PAGE -> meta_title( 'form' ) );
      
    return $t -> execute();
	}
  
  private function ajax_form($end=0){
	  sys_block_disable();
	  $t = new PHPTAL( false );
	  $t -> setSnippet( 'users','ajax_register_form');
	  $captcha = captcha::show();
	  $t -> image_src = $captcha['img'];
	  $t -> register_end = 0;
	  
    $GLOBALS['_RESULT']['content'] =  $t -> execute();
		$GLOBALS['_RESULT']['title'] = 'Регистрация';
	  exit();
	}

  private function ajax_save($end=0){ 
   sys_block_disable();
   $u = users::create();
	  try{
	    foreach( $_POST['form'] as $k=>$v ){
	      $u -> set($k,$v);
	    }
	    $u -> set('u_active',0);
      $u -> set('u_time_reg',MAIN_TIME);
      $u -> set('u_act_code', sys_str_rand(5));
	    $u -> set('g_id',3);
	    if ( !captcha::check() ){
		    throw new Exception( 'Ошибка:Защитный код ведён неверно!' );
		  }
		  $id = $u -> insert();
		  
	  }catch(Exception $e){
		  echo $e -> getMessage();
		  exit();
		}
		
		

	 
	  include_once(MAIN_SOURCE_PATH.'/inc/class.mail.php');
	  $m =  mail::load('register_confirm');
	  $m -> set('uname',$u -> name());
	  $m -> set('URL_USER_REGISTER_ACTIV', sys_url(URL_USERS_REGISTER_ACTIV,$u -> id(),$u -> act_code()) );
	  $m -> send_mail(  $u -> email() );
	  
	    //  спасибо за регисрацию ....
	  $t = new PHPTAL( false );
    $t -> setSnippet( 'users','ajax_register_form');
    $t -> register_end = 1;
    $GLOBALS['_RESULT']['msg'] =  $t -> execute();
	  exit();
  }

}
?>
