Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * GonitZoggo
- * @file Login.php
- * @author Atiab Jobayer <atiab@coderzwar.com>
- */
- defined('BASEPATH') OR exit('Access Denied');
- class Login extends CI_Controller
- {
- public function __construct()
- {
- parent::__construct();
- //show_error("Site is down due to some issues. Check back later.");
- }
- public function index()
- {
- if(! $this->session->userdata('referrer_url'))
- $this->session->set_userdata('referrer_url', $this->agent->referrer());
- if ($this->user->logged_in)
- redirect('/');
- $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[30]|alpha_dash');
- $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[30]');
- $data = array(
- 'notifications' => $this->session->flashdata(),
- 'error' => FALSE,
- );
- $this->load->library('facebook');
- $data['fb_login_url'] = $this->facebook->login_url();
- //echo $data['fb_login_url'];
- if($this->facebook->is_authenticated()){
- $fbUserProfile = $this->facebook->request('get', '/me?fields=id,first_name,last_name,email,link,gender,locale,cover,picture');
- print_r($fbUserProfile);
- $user = array(
- 'facebook_oauth' => $fbUserProfile['id'],
- 'first_name' => $fbUserProfile['first_name'],
- 'last_name' => $fbUserProfile['last_name'],
- 'email' => $fbUserProfile['email'],
- );
- // $userData['locale'] = $fbUserProfile['locale'];
- // $userData['cover'] = $fbUserProfile['cover']['source'];
- // $userData['picture'] = $fbUserProfile['picture']['data']['url'];
- if($this->user_model->fb_data_process($user)){
- $login_data = array(
- 'username' => $this->user_model->fb_to_username($user['facebook_oauth']),
- 'logged_in' => TRUE
- );
- $this->session->set_userdata($login_data);
- if( $this->session->userdata('referrer_url') ) {
- $redirect_back = $this->session->userdata('referrer_url');
- $this->session->unset_userdata('referrer_url');
- redirect( $redirect_back );
- }
- else
- redirect('/');
- }
- $data['fb_logout_url'] = $this->facebook->logout_url();
- }
- if($this->form_validation->run()){
- $username = $this->input->post('username');
- $password = $this->input->post('password');
- if($this->user_model->validate_user($username, $password)){
- $login_data = array(
- 'username' => $username,
- 'logged_in' => TRUE
- );
- $user = $this->user_model->user_info($username);
- if($user['verify_key'] !== NULL){
- $notif = array(
- 'text' => 'Please Verify Your Email',
- 'type' => 'warning',
- );
- $this->session->set_flashdata('message', $notif);
- redirect('login');
- }
- $this->session->set_userdata($login_data);
- if( $this->session->userdata('referrer_url') ) {
- $redirect_back = $this->session->userdata('referrer_url');
- $this->session->unset_userdata('referrer_url');
- redirect( $redirect_back );
- }
- else
- redirect('/');
- }
- else
- $data['error'] = TRUE;
- }
- $this->twig->display('pages/authentication/login.twig', $data);
- }
- public function register()
- {
- if ($this->user->logged_in)
- redirect('/');
- $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.'));
- $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'));
- $this->form_validation->set_rules('password', 'password', 'required|min_length[6]|max_length[30]');
- $this->form_validation->set_rules('password_again', 'password confirmation', 'required|matches[password]');
- if ($this->form_validation->run()){
- $user_info = array(
- 'username' => $this->input->post('username'),
- 'display_name' => $this->input->post('display_name'),
- 'email' => $this->input->post('email'),
- 'institution' => $this->input->post('institution'),
- 'password' => $this->input->post('password'),
- 'role' => 'User'
- );
- $this->user_model->add_user($user_info);
- $notif = array(
- 'text' => 'Registration Successful',
- 'type' => 'success',
- );
- $this->session->set_flashdata('message', $notif);
- redirect('login');
- }
- else
- $this->twig->display('pages/authentication/register.twig');
- }
- public function logout()
- {
- $this->load->library('facebook');
- $this->facebook->destroy_session();
- $this->session->sess_destroy();
- redirect('/');
- }
- // ------------------------------------------------------------------------
- public function lost()
- {
- if($this->user->logged_in)
- redirect('/');
- $this->form_validation->set_rules('email', 'email', 'required|max_length[254]|valid_email');
- if ($this->form_validation->run())
- {
- $email = $this->input->post('email');
- if(! $this->user_model->have_email($email)){
- $notif = array(
- 'text' => 'Account Not Found',
- 'type' => 'error',
- );
- $this->session->set_flashdata('message', $notif);
- redirect('login/lost');
- }
- $this->user_model->send_password_reset_mail($email);
- $notif = array(
- 'text' => 'Password Reset Email Sent',
- 'type' => 'success',
- );
- $this->session->set_flashdata('message', $notif);
- redirect('login');
- }
- $data = array(
- 'notifications' => $this->session->flashdata(),
- );
- $this->twig->display('pages/authentication/lost.twig', $data);
- }
- // ------------------------------------------------------------------------
- public function reset($passchange_key = FALSE)
- {
- if ($passchange_key === FALSE)
- show_404();
- $result = $this->user_model->passchange_is_valid($passchange_key);
- if ($result !== TRUE)
- show_error($result);
- $this->form_validation->set_rules('password', 'password', 'required|min_length[6]|max_length[30]');
- $this->form_validation->set_rules('password_again', 'password confirmation', 'required|matches[password]');
- $data = array(
- 'key' => $passchange_key,
- 'result' => $result,
- 'reset' => FALSE
- );
- if ($this->form_validation->run()){
- $this->user_model->reset_password($passchange_key, $this->input->post('password'));
- $notif = array(
- 'text' => 'Password Reset Successful',
- 'type' => 'success',
- );
- $this->session->set_flashdata('message', $notif);
- redirect('login');
- }
- $this->twig->display('pages/authentication/reset_password.twig', $data);
- }
- public function unsubscribe($key = false)
- {
- if(! $key)
- show_404();
- $query = $this->db->where('unsubscribe_key', $key)->get('users');
- if($query->num_rows() != 1)
- show_error("Invalid Key");
- $user = $query->row_array();
- $user['subscribed'] = 0;
- $this->db->where('id', $user['id'])->update('users', $user);
- echo "Unsubscribed Successfully";
- }
- public function verify($key = false)
- {
- if(!$key)
- show_404();
- $query = $this->db->where('verify_key', $key)->get('users');
- if($query->num_rows() != 1)
- show_error("Inavlid Key");
- $user = $query->row_array();
- $user['verify_key'] = NULL;
- $this->db->where('id', $user['id'])->update('users', $user);
- $notif = array(
- 'text' => 'Verification Successful',
- 'type' => 'success',
- );
- $this->session->set_flashdata('message', $notif);
- redirect('login');
- }
- public function facebook_login()
- {
- // Load login & profile view
- $this->twig->display('pages/authentication/lost.twig', $data);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement