Advertisement
Guest User

Gonitzoggo Login

a guest
Feb 3rd, 2019
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.78 KB | None | 0 0
  1. <?php
  2. /**
  3.  * GonitZoggo
  4.  * @file Login.php
  5.  * @author Atiab Jobayer <atiab@coderzwar.com>
  6.  */
  7. defined('BASEPATH') OR exit('Access Denied');
  8.  
  9. class Login extends CI_Controller
  10. {
  11.  
  12.     public function __construct()
  13.     {
  14.         parent::__construct();
  15.  
  16.         //show_error("Site is down due to some issues. Check back later.");
  17.     }
  18.  
  19.     public function index()
  20.     {
  21.         if(! $this->session->userdata('referrer_url'))
  22.             $this->session->set_userdata('referrer_url', $this->agent->referrer());
  23.        
  24.         if ($this->user->logged_in)
  25.             redirect('/');
  26.  
  27.         $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[30]|alpha_dash');
  28.         $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[30]');
  29.  
  30.         $data = array(
  31.             'notifications' => $this->session->flashdata(),
  32.             'error' => FALSE,
  33.         );
  34.  
  35.         $this->load->library('facebook');
  36.  
  37.         $data['fb_login_url'] =  $this->facebook->login_url();
  38.  
  39.         //echo $data['fb_login_url'];
  40.  
  41.         if($this->facebook->is_authenticated()){
  42.             $fbUserProfile = $this->facebook->request('get', '/me?fields=id,first_name,last_name,email,link,gender,locale,cover,picture');
  43.  
  44.             print_r($fbUserProfile);
  45.  
  46.             $user = array(
  47.                 'facebook_oauth' => $fbUserProfile['id'],
  48.                 'first_name' => $fbUserProfile['first_name'],
  49.                 'last_name' => $fbUserProfile['last_name'],
  50.                 'email' => $fbUserProfile['email'],
  51.             );
  52.             // $userData['locale'] = $fbUserProfile['locale'];
  53.             // $userData['cover'] = $fbUserProfile['cover']['source'];
  54.             // $userData['picture'] = $fbUserProfile['picture']['data']['url'];
  55.  
  56.             if($this->user_model->fb_data_process($user)){
  57.                 $login_data = array(
  58.                     'username'  => $this->user_model->fb_to_username($user['facebook_oauth']),
  59.                     'logged_in' => TRUE
  60.                 );
  61.  
  62.                 $this->session->set_userdata($login_data);
  63.  
  64.                 if( $this->session->userdata('referrer_url') ) {
  65.                     $redirect_back = $this->session->userdata('referrer_url');
  66.                     $this->session->unset_userdata('referrer_url');
  67.  
  68.                     redirect( $redirect_back );
  69.                 }
  70.                 else
  71.                     redirect('/');
  72.             }
  73.            
  74.  
  75.             $data['fb_logout_url'] = $this->facebook->logout_url();
  76.         }
  77.  
  78.         if($this->form_validation->run()){
  79.             $username = $this->input->post('username');
  80.             $password = $this->input->post('password');
  81.  
  82.             if($this->user_model->validate_user($username, $password)){
  83.                 $login_data = array(
  84.                     'username'  => $username,
  85.                     'logged_in' => TRUE
  86.                 );
  87.  
  88.                 $user = $this->user_model->user_info($username);
  89.  
  90.                 if($user['verify_key'] !== NULL){
  91.                     $notif = array(
  92.                         'text' => 'Please Verify Your Email',
  93.                         'type' => 'warning',
  94.                     );
  95.  
  96.                     $this->session->set_flashdata('message', $notif);
  97.  
  98.                     redirect('login');
  99.                 }
  100.  
  101.                 $this->session->set_userdata($login_data);
  102.  
  103.                 if( $this->session->userdata('referrer_url') ) {
  104.                     $redirect_back = $this->session->userdata('referrer_url');
  105.                     $this->session->unset_userdata('referrer_url');
  106.  
  107.                     redirect( $redirect_back );
  108.                 }
  109.                 else
  110.                     redirect('/');
  111.             }
  112.             else
  113.                 $data['error'] = TRUE;
  114.         }
  115.  
  116.         $this->twig->display('pages/authentication/login.twig', $data);
  117.     }
  118.  
  119.     public function register()
  120.     {
  121.         if ($this->user->logged_in)
  122.             redirect('/');
  123.  
  124.         $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[30]|alpha_dash|is_unique[users.username]', array('is_unique' => 'This %s Already Exists.'));
  125.  
  126.         $this->form_validation->set_rules('email', 'Email', 'required|max_length[40]|valid_email|is_unique[users.email]', array('is_unique' => 'This %s Has Already Been Used'));
  127.  
  128.         $this->form_validation->set_rules('password', 'password', 'required|min_length[6]|max_length[30]');
  129.         $this->form_validation->set_rules('password_again', 'password confirmation', 'required|matches[password]');
  130.  
  131.         if ($this->form_validation->run()){
  132.             $user_info = array(
  133.                 'username' => $this->input->post('username'),
  134.                 'display_name' => $this->input->post('display_name'),
  135.                 'email' => $this->input->post('email'),
  136.                 'institution' => $this->input->post('institution'),
  137.                 'password' => $this->input->post('password'),
  138.                 'role' => 'User'
  139.             );
  140.  
  141.             $this->user_model->add_user($user_info);
  142.  
  143.             $notif = array(
  144.                 'text' => 'Registration Successful',
  145.                 'type' => 'success',
  146.             );
  147.  
  148.             $this->session->set_flashdata('message', $notif);
  149.  
  150.             redirect('login');
  151.         }
  152.         else
  153.             $this->twig->display('pages/authentication/register.twig');
  154.     }
  155.  
  156.     public function logout()
  157.     {
  158.         $this->load->library('facebook');
  159.  
  160.         $this->facebook->destroy_session();
  161.         $this->session->sess_destroy();
  162.  
  163.         redirect('/');
  164.     }
  165.  
  166.  
  167.     // ------------------------------------------------------------------------
  168.  
  169.  
  170.     public function lost()
  171.     {
  172.         if($this->user->logged_in)
  173.             redirect('/');
  174.  
  175.         $this->form_validation->set_rules('email', 'email', 'required|max_length[254]|valid_email');
  176.  
  177.         if ($this->form_validation->run())
  178.         {
  179.             $email = $this->input->post('email');
  180.  
  181.             if(! $this->user_model->have_email($email)){
  182.                 $notif = array(
  183.                     'text' => 'Account Not Found',
  184.                     'type' => 'error',
  185.                 );
  186.  
  187.                 $this->session->set_flashdata('message', $notif);
  188.  
  189.                 redirect('login/lost');
  190.             }
  191.  
  192.             $this->user_model->send_password_reset_mail($email);
  193.            
  194.             $notif = array(
  195.                 'text' => 'Password Reset Email Sent',
  196.                 'type' => 'success',
  197.             );
  198.  
  199.             $this->session->set_flashdata('message', $notif);
  200.  
  201.             redirect('login');
  202.         }
  203.  
  204.         $data = array(
  205.             'notifications' => $this->session->flashdata(),
  206.         );
  207.  
  208.         $this->twig->display('pages/authentication/lost.twig', $data);
  209.     }
  210.  
  211.  
  212.     // ------------------------------------------------------------------------
  213.  
  214.  
  215.     public function reset($passchange_key = FALSE)
  216.     {
  217.         if ($passchange_key === FALSE)
  218.             show_404();
  219.  
  220.         $result = $this->user_model->passchange_is_valid($passchange_key);
  221.  
  222.         if ($result !== TRUE)
  223.             show_error($result);
  224.  
  225.         $this->form_validation->set_rules('password', 'password', 'required|min_length[6]|max_length[30]');
  226.         $this->form_validation->set_rules('password_again', 'password confirmation', 'required|matches[password]');
  227.        
  228.         $data = array(
  229.             'key' => $passchange_key,
  230.             'result' => $result,
  231.             'reset' => FALSE
  232.         );
  233.         if ($this->form_validation->run()){
  234.             $this->user_model->reset_password($passchange_key, $this->input->post('password'));
  235.            
  236.             $notif = array(
  237.                 'text' => 'Password Reset Successful',
  238.                 'type' => 'success',
  239.             );
  240.  
  241.             $this->session->set_flashdata('message', $notif);
  242.  
  243.             redirect('login');
  244.         }
  245.  
  246.         $this->twig->display('pages/authentication/reset_password.twig', $data);
  247.     }
  248.  
  249.     public function unsubscribe($key = false)
  250.     {
  251.         if(! $key)
  252.             show_404();
  253.  
  254.         $query = $this->db->where('unsubscribe_key', $key)->get('users');
  255.  
  256.         if($query->num_rows() != 1)
  257.             show_error("Invalid Key");
  258.  
  259.         $user = $query->row_array();
  260.         $user['subscribed'] = 0;
  261.  
  262.         $this->db->where('id', $user['id'])->update('users', $user);
  263.  
  264.         echo "Unsubscribed Successfully";
  265.     }
  266.  
  267.     public function verify($key = false)
  268.     {
  269.         if(!$key)
  270.             show_404();
  271.  
  272.         $query = $this->db->where('verify_key', $key)->get('users');
  273.  
  274.         if($query->num_rows() != 1)
  275.             show_error("Inavlid Key");
  276.  
  277.         $user = $query->row_array();
  278.         $user['verify_key'] = NULL;
  279.  
  280.         $this->db->where('id', $user['id'])->update('users', $user);
  281.  
  282.         $notif = array(
  283.             'text' => 'Verification Successful',
  284.             'type' => 'success',
  285.         );
  286.  
  287.         $this->session->set_flashdata('message', $notif);
  288.  
  289.         redirect('login');
  290.     }
  291.  
  292.     public function facebook_login()
  293.     {
  294.  
  295.        
  296.         // Load login & profile view
  297.         $this->twig->display('pages/authentication/lost.twig', $data);
  298.     }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement