Advertisement
j0h4n54ntr1

mainnnnn

Feb 23rd, 2018
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.41 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Main extends CI_Controller {
  5.        
  6.         public $status;
  7.         public $aturan;
  8.    
  9.         function __construct(){
  10.             parent::__construct();
  11.             $this->load->model('User_model', 'user_model', TRUE);
  12.             $this->load->library('form_validation');    
  13.             $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
  14.             $this->status = $this->config->item('status');
  15.             $this->aturan = $this->config->item('aturan');
  16.         }      
  17.    
  18.     public function index()
  19.     {  
  20.             if(empty($this->session->userdata['email'])){
  21.                 redirect(site_url().'main/login');
  22.             }            
  23.             $data = $this->session->userdata;
  24.             $this->load->view('login/header');
  25.             $this->load->view('login/nav');                  
  26.             $this->load->view('home',$data);
  27.             $this->load->view('login/footer');
  28.          
  29.     }
  30.        
  31.        
  32.         public function register()
  33.         {
  34.              
  35.             $this->form_validation->set_rules('nama_depan', 'nama depan', 'required');
  36.             $this->form_validation->set_rules('nama_belakang', 'nama belakang', 'required');    
  37.             $this->form_validation->set_rules('email', 'Email', 'required|valid_email');    
  38.                        
  39.             if ($this->form_validation->run() == FALSE) {  
  40.                  $this->load->view('login/header');
  41.                
  42.                 $this->load->view('login/register');
  43.                  $this->load->view('login/footer');
  44.                
  45.             }else{                
  46.                 if($this->user_model->isDuplicate($this->input->post('email'))){
  47.                     $this->session->set_flashdata('flash_message', 'User email already exists');
  48.                     redirect(site_url().'main/login');
  49.                 }else{
  50.                    
  51.                     $clean = $this->security->xss_clean($this->input->post(NULL, TRUE));
  52.                     $id = $this->user_model->insertUser($clean);
  53.                     $token = $this->user_model->insertToken($id);                                    
  54.                     $qstring = $this->base64url_encode($token);                    
  55.                     $url = site_url() . 'main/complete/token/' . $qstring;
  56.                     $link = '<a href="' . $url . '">' . $url . '</a>';
  57.  
  58.                     $message = '';                    
  59.                     $message .= '' . $link;      
  60.                     echo $message; //send this in email
  61.  
  62.             $config = Array(  
  63.                     'protocol' => 'smtp',  
  64.                     'smtp_host' => 'ssl://smtp.googlemail.com',  
  65.                     'smtp_port' => 465,  
  66.                     'smtp_user' => 'xxxxxx',   //email google
  67.                     'smtp_pass' => 'xxxxx',   //passsword google
  68.                     'mailtype' => 'html',  
  69.                     'charset' => 'iso-8859-1'  
  70.                     );  
  71.                     $this->load->library('email', $config);  
  72.                     $this->email->set_newline("\r\n");  
  73.                     $this->email->from(($this->input->post('email',TRUE)), ($this->input->post('nama_depan',TRUE)));  
  74.                     $this->email->to($this->input->post('email',TRUE));  
  75.                     $this->email->cc('xxxx');   //email sesuaikan
  76.                     $this->email->subject('proses pendaftaran');  
  77.                     $this->email->message('untuk melanjutkan register klik link ini   .'.$message);  
  78.                     if (!$this->email->send()) {  
  79.                     echo 'what this is';  
  80.                     }else{  
  81.                     echo 'Success to send email';
  82.                      return  
  83.                      
  84.                      redirect(site_url().'main/login'); //redirect();
  85.                     }  
  86.                     exit;                    
  87.                    
  88.                 };              
  89.             }
  90.         }
  91.        
  92.        
  93.         protected function _islocal(){
  94.             return strpos($_SERVER['HTTP_HOST'], 'local');
  95.         }
  96.        
  97.         public function complete()
  98.         {                                  
  99.             $token = base64_decode($this->uri->segment(4));      
  100.             $cleanToken = $this->security->xss_clean($token);
  101.            
  102.             $user_info = $this->user_model->isTokenValid($cleanToken); //either false or array();          
  103.            
  104.             if(!$user_info){
  105.                 $this->session->set_flashdata('flash_message', 'Token is invalid or expired');
  106.                 redirect(site_url().'main/login');
  107.             }            
  108.             $data = array(
  109.                 'nama_depan'=> $user_info->nama_depan,
  110.                 'email'=>$user_info->email,
  111.                 'user_id'=>$user_info->id,
  112.                 'token'=>$this->base64url_encode($token)
  113.             );
  114.            
  115.             $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');
  116.             $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]');              
  117.            
  118.             if ($this->form_validation->run() == FALSE) {  
  119.                  $this->load->view('login/header');
  120.                 $this->load->view('login/complete', $data);
  121.                  $this->load->view('login/footer');
  122.             }else{
  123.                
  124.                 $this->load->library('password');                
  125.                 $post = $this->input->post(NULL, TRUE);
  126.                
  127.                 $cleanPost = $this->security->xss_clean($post);
  128.                
  129.                 $hashed = $this->password->create_hash($cleanPost['password']);                
  130.                 $cleanPost['password'] = $hashed;
  131.                 unset($cleanPost['passconf']);
  132.                 $userInfo = $this->user_model->updateUserInfo($cleanPost);
  133.                
  134.                 if(!$userInfo){
  135.                     $this->session->set_flashdata('flash_message', 'There was a problem updating your record');
  136.                     redirect(site_url().'main/login');
  137.                 }
  138.                
  139.                 unset($userInfo->password);
  140.                
  141.                 foreach($userInfo as $key=>$val){
  142.                     $this->session->set_userdata($key, $val);
  143.                 }
  144.                 redirect(site_url().'main/');
  145.                
  146.             }
  147.         }
  148.        
  149.         public function login()
  150.         {
  151.             $this->form_validation->set_rules('email', 'Email', 'required|valid_email');    
  152.             $this->form_validation->set_rules('password', 'Password', 'required');
  153.            
  154.             if($this->form_validation->run() == FALSE) {
  155.                
  156.                 $this->load->view('login/header');
  157.                 $this->load->view('login/login');
  158.                 $this->load->view('login/footer');
  159.             }else{
  160.                
  161.                 $post = $this->input->post();  
  162.                 $clean = $this->security->xss_clean($post);
  163.                
  164.                 $userInfo = $this->user_model->checkLogin($clean);
  165.                
  166.                 if(!$userInfo){
  167.                     $this->session->set_flashdata('flash_message', 'The login was unsucessful');
  168.                     redirect(site_url().'main/login');
  169.                 }                
  170.                 foreach($userInfo as $key=>$val){
  171.                     $this->session->set_userdata($key, $val);
  172.                 }
  173.                 redirect(site_url().'main/');
  174.             }
  175.            
  176.         }
  177.        
  178.         public function logout()
  179.         {
  180.             $this->session->sess_destroy();
  181.             redirect(site_url().'main/login');
  182.         }
  183.        
  184.         public function forgot()
  185.         {
  186.            
  187.             $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
  188.            
  189.             if($this->form_validation->run() == FALSE) {
  190.                
  191.                 $this->load->view('login/header');
  192.                 $this->load->view('login/forgot');
  193.                 $this->load->view('login/footer');
  194.              
  195.             }else{
  196.                 $email = $this->input->post('email');  
  197.                 $clean = $this->security->xss_clean($email);
  198.                 $userInfo = $this->user_model->getUserInfoByEmail($clean);
  199.                
  200.                 if(!$userInfo){
  201.                     $this->session->set_flashdata('flash_message', 'We cant find your email address');
  202.                     redirect(site_url().'main/login');
  203.                 }  
  204.                
  205.                 if($userInfo->status != $this->status[1]){ //if status is not approved
  206.                     $this->session->set_flashdata('flash_message', 'Your account is not in approved status');
  207.                     redirect(site_url().'main/login');
  208.                 }
  209.                
  210.                 //build token
  211.                
  212.                 $token = $this->user_model->insertToken($userInfo->id);                        
  213.                 $qstring = $this->base64url_encode($token);                  
  214.                 $url = site_url() . 'main/reset_password/token/' . $qstring;
  215.                 $link = '<a href="' . $url . '">' . $url . '</a>';
  216.                
  217.                 $message = '';                    
  218.                
  219.                 $message .= ' ' . $link;            
  220.  
  221.                 echo $message; //send this through mail
  222.  
  223.                   $config = Array(  
  224.                     'protocol' => 'smtp',  
  225.                     'smtp_host' => 'ssl://smtp.googlemail.com',  
  226.                     'smtp_port' => 465,  
  227.                     'smtp_user' => 'xxxxxx',   //email google
  228.                     'smtp_pass' => 'xxxxx',   //pasword google
  229.                     'mailtype' => 'html',  
  230.                     'charset' => 'iso-8859-1'  
  231.                     );  
  232.                     $this->load->library('email', $config);  
  233.                     $this->email->set_newline("\r\n");  
  234.                     $this->email->from(($this->input->post('email',TRUE)), ($this->input->post('nama_depan',TRUE)));  
  235.                     $this->email->to($this->input->post('email',TRUE));  
  236.                     $this->email->cc('xxxxxxx');   //sesuaikan
  237.                     $this->email->subject('reset password');  
  238.                     $this->email->message('untuk melanjutkan reset klik link ini   .'.$message);  
  239.                     if (!$this->email->send()) {  
  240.                     echo 'what this is';  
  241.                     }else{  
  242.                     echo 'Success to send email';
  243.                      return  
  244.                      
  245.                      redirect(site_url().'main/login'); //redirect();
  246.                     }
  247.                 exit;
  248.                
  249.             }
  250.            
  251.         }
  252.        
  253.         public function reset_password()
  254.         {
  255.             $token = $this->base64url_decode($this->uri->segment(4));                  
  256.             $cleanToken = $this->security->xss_clean($token);
  257.            
  258.             $user_info = $this->user_model->isTokenValid($cleanToken); //either false or array();              
  259.            
  260.             if(!$user_info){
  261.                 $this->session->set_flashdata('flash_message', 'Token is invalid or expired');
  262.                 redirect(site_url().'main/login');
  263.             }            
  264.             $data = array(
  265.                 'nama_depan'=> $user_info->nama_depan,
  266.                 'email'=>$user_info->email,
  267. //                'user_id'=>$user_info->id,
  268.                 'token'=>$this->base64url_encode($token)
  269.             );
  270.            
  271.             $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');
  272.             $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]');              
  273.            
  274.             if ($this->form_validation->run() == FALSE) {  
  275.                
  276.                 $this->load->view('login/header');
  277.                 $this->load->view('login/reset', $data);
  278.                 $this->load->view('login/footer');
  279.             }else{
  280.                                
  281.                 $this->load->library('password');                
  282.                 $post = $this->input->post(NULL, TRUE);                
  283.                 $cleanPost = $this->security->xss_clean($post);                
  284.                 $hashed = $this->password->create_hash($cleanPost['password']);                
  285.                 $cleanPost['password'] = $hashed;
  286.                 $cleanPost['user_id'] = $user_info->id;
  287.                 unset($cleanPost['passconf']);                
  288.                 if(!$this->user_model->updatePassword($cleanPost)){
  289.                     $this->session->set_flashdata('flash_message', 'There was a problem updating your password');
  290.                 }else{
  291.                     $this->session->set_flashdata('flash_message', 'Your password has been updated. You may now login');
  292.                 }
  293.                 redirect(site_url().'main/login');                
  294.             }
  295.         }
  296.        
  297.     public function base64url_encode($data) {
  298.       return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
  299.     }
  300.  
  301.     public function base64url_decode($data) {
  302.       return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
  303.     }  
  304.  
  305.        
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement