<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Forget extends Controller {

    public function action_index(){
        
        $view = View::factory("forget");
	$this->request->response = $view;
    }
    public function action_forget()
    {
    	if(Auth::instance()->get_user()){
    	    Request::instance()->redirect("");
	}
        
            $post = $_POST;
          //          ->pre_filter('trim')
            //        ->add_rules('email', 'required', 'length[4,127]');						;
       
            if(1)//$post->validate())
            {
                    //$email_exists = (bool) ORM::factory('user')->where('email','=' ,$post['email'])->count_all();
                    if(1)//$email_exists)
                    {
                            $object = ORM::factory('user')->where('email','=' ,$post['email'])->find();
                            // throw new Exception($object->id);
                                    $length=9;
                                    $strength=4;				
                                    $vowels = 'aeuy';
                                    $consonants = 'bdghjmnpqrstvz';
                                    if ($strength & 1) {
                                            $consonants .= 'BDGHJLMNPQRSTVWXZ';
                                    }
                                    if ($strength & 2) {
                                            $vowels .= "AEUY";
                                    }
                                    if ($strength & 4) {
                                            $consonants .= '23456789';
                                    }
                                    if ($strength & 8) {
                                            $consonants .= '@#$%';
                                    }
                                    $password = '';
                                    $alt = time() % 2;
                                    for ($i = 0; $i < $length; $i++) {
                                            if ($alt == 1) {
                                                    $password .= $consonants[(rand() % strlen($consonants))];
                                                    $alt = 0;
                                            }
                                            else {
                                                    $password .= $vowels[(rand() % strlen($vowels))];
                                                    $alt = 1;
                                            }
                                    }
                            $object->change = $password; 
                            $object->save();
                            
                            $this->session = Session::instance()->set('mail',$object->email);
                            Mailer::factory('user')->send_welcome();
                    }
                    else
                        ; //   Session::instance()->set_flash("login_error", gcb::lang("login_error", "error"));
            }
    	    $view = View::factory("forget");
	    $this->request->response = $view;	
    }
    public function action_submit($arg1,$arg2) 
    {
        $object = ORM::factory('user')->where('email','=' ,$arg2)->find();
        if($object->change==$arg1){
                    $object->password = $object->change;
                    $object->change = NULL;
                    $object->save();            
         Request::instance()->redirect("");
        }
    
    }

}
