Subversion Repositories SmartDukaan

Rev

Rev 16276 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Auth extends MY_Controller {
public $layoutName ='';
        function __construct() {

                // Call the CI_controller constructor
                parent::__construct();
                // $admin = $this->session->userdata('admin');
                // if(!isset($admin) || empty($admin)) {
                //      redirect(base_url().'authorize');
                // }
                $this->output->set_header("HTTP/1.0 200 OK");
                $this->output->set_header("HTTP/1.1 200 OK");
                $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s',time()).' GMT');
                $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
                $this->output->set_header("Cache-Control: post-check=0, pre-check=0");
                $this->output->set_header("Pragma: no-cache");
                $this->layout->setlayout('layout/layout_main');
                $this->load->model('oauth_model');
                $this->layoutName= $this->layout->getLayout();
                $this->layoutName =substr($this->layoutName , 0, strrpos($this->layoutName , "/")).'/';
                
                

        }
        public function index()
        {
                $this->__oauthorization();
                
                
        }
        function __oauthorization()
        {
                //sample session storage.. later i will imporve security for login 
                $check =$this->session->userdata('authorize');
                if(isset($check) and !empty($check) and $check['authority']='success')
                {
                        redirect(base_url().'home');
                }
                else
                {
                        $this->login();
                }       


        }
        function login()
        {
                //standard array
                $data = array();
                 if (isset($_SERVER['HTTP_REFERER'])) {
                        if ((strpos($_SERVER['HTTP_REFERER'],'login') == false) && (strpos($_SERVER['HTTP_REFERER'],'register') == false)) {
                                $redirect = $_SERVER['HTTP_REFERER'];
                                $location = $this->session->userdata('location');
                                if(isset($location) && $location == 'shipping'){
                                        $data['location'] = 'shipping';
                                        $location = base_url().$location;
                                        $this->session->set_userdata('location',$location);
                                }elseif(isset($location) && ($location == 'my-recharges' || $location == 'my-wallet')){
                                        $location = base_url().$location;
                                        $this->session->set_userdata('location',$location);
                                }else{
                                        $this->session->set_userdata('location',$redirect);     
                                }
                                
                        }
                }
                $authorized = $this->session->userdata('authorized');
                if(isset($authorized) && !empty($authorized) && $authorized['isLoggedIn'] == 1){
                        redirect(base_url().'home');
                }
                //get cache 
                $authorized = $this->session->userdata('authorized');
                if(isset($_POST) && !empty($_POST)){
                        if(isset($authorized) && !empty($authorized)){
                                $_POST['userId'] = $authorized['Id'];
                                $_POST['cartId'] = $authorized['cartId'];
                                if($authorized['isLoggedIn'] == 1){
                                        $_POST['isLoggedIn'] = 'true';
                                }
                                elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
                                $_POST['isLoggedIn'] = 'false';
                                }
                        }
                        //end of get cache
                        $configdata =array();
                        $configdata = $this->config->item('oauth_login');
                        unset($configdata['response'][0]);
                        $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata);
                        //error_log(print_r($data['response']['response'],1));
                        //set cache to output or renew cache
                        if(isset($data['response']['response']['login']['authorized']))
                        {
                                $this->session->set_userdata('authorized',$data['response']['response']['login']['authorized']);
                                //$this->session->set_flashdata(array('msg'=>$data['response']['response']['login']['msg']));
                                $location = $this->session->userdata('location');
                                if(isset($location) && !empty($location)){
                                        $this->session->unset_userdata('location');
                                        redirect($location);
                                }
                                else{
                                        redirect(base_url().$data['response']['response']['login']['redirect']);        
                                }
                                

                        }
                        else
                        {
                                $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
                                $data['title'] = 'Saholic';
                                $data['stylesheet'] = 'auth.css';
                                $this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
                        }
                }
                else{
                        $configdata =array();
                        $configdata = $this->config->item('oauth_login');
                        unset($configdata['response'][1]);
                        $cachemodule = array('header','footer','response');
                        $configdata = getCache($configdata,$cachemodule);
                        $data = array();
                        $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata['module']);
                        $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
                        $data['title'] = 'Saholic';
                        $data['stylesheet'] = 'auth.css';
                        if(isset($cachemodule) and !empty($cachemodule) && !isset($api))
                        {
                                foreach($cachemodule as $cm)
                                {
                                        if(isset($configdata[$cm]) and !empty($configdata[$cm]))
                                        {
                                                $data['response'][$cm]=$configdata[$cm];
                                        }
                                }
                        }
                        setCache($configdata['module'],$cachemodule,$data['response']);
                        $this->layout->view(strtolower(__CLASS__).'/login',$data);
                }
        }
        function signup()
        {
                //standard array
                $authorized = $this->session->userdata('authorized');
                if(isset($authorized) && !empty($authorized) && $authorized['isLoggedIn'] == 1){
                        redirect(base_url().'home');
                }
                $data = array();
                if (isset($_SERVER['HTTP_REFERER'])) {
                        if ((strpos($_SERVER['HTTP_REFERER'],'login') == false) && (strpos($_SERVER['HTTP_REFERER'],'register') == false)) {
                                $redirect = $_SERVER['HTTP_REFERER'];
                                $location = $this->session->userdata('location');
                                if(isset($location) && $location == 'shipping'){
                                        $data['location'] = 'shipping';
                                        $this->session->set_userdata('location',$location);
                                }elseif(isset($location) && ($location == 'my-recharges' || $location == 'my-wallet')){
                                        $location = base_url().$location;
                                        $this->session->set_userdata('location',$location);
                                }else{
                                        $this->session->set_userdata('location',$redirect);     
                                }
                        }
                }
                //get cache 
                $authorized = $this->session->userdata('authorized');
                if(isset($_POST) && !empty($_POST)){
                        if(isset($authorized) && !empty($authorized)){
                                $_POST['userId'] = $authorized['Id'];
                                $_POST['Id'] = $authorized['cartId'];
                                if($authorized['isLoggedIn'] == 1){
                                        $_POST['isLoggedIn'] = 'true';
                                }
                                elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
                                        $_POST['isLoggedIn'] = 'false';
                                }
                        }
                
                        $configdata =array();
                        $configdata = $this->config->item('oauth_signup');
                        unset($configdata['response'][0]);
                        $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata);
                        //error_log(print_r($data['response']['response'],1));
                        //set cache to output or renew cache
                        if(isset($data['response']['response']['signup']['authorized']))
                        {
                                $this->session->set_userdata('authorized',$data['response']['response']['signup']['authorized']);
                                $this->session->set_flashdata(array('msg'=>$data['response']['response']['signup']['msg']));
                                $location = $this->session->userdata('location');
                                if(isset($location) && !empty($location)){
                                        $this->session->unset_userdata('location');
                                        redirect($location);
                                }
                                redirect(base_url().$data['response']['response']['signup']['redirect']);
                        }
                        else
                        {
                                $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
                                $data['title'] = 'Saholic';
                                $data['stylesheet'] = 'auth.css';
                                $this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
                        }
                }else{
                        $configdata =array();
                        $configdata = $this->config->item('oauth_signup');
                        unset($configdata['response'][1]);
                        $cachemodule = array('header','footer','response');
                        $configdata = getCache($configdata,$cachemodule);
                        $data = array();
                        $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata['module']);
                        $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
                        $data['title'] = 'Saholic';
                        $data['stylesheet'] = 'auth.css';
                        if(isset($cachemodule) and !empty($cachemodule) && !isset($api))
                        {
                                foreach($cachemodule as $cm)
                                {
                                        if(isset($configdata[$cm]) and !empty($configdata[$cm]))
                                        {
                                                $data['response'][$cm]=$configdata[$cm];
                                        }
                                }
                        }
                        setCache($configdata['module'],$cachemodule,$data['response']);
                        $this->layout->view(strtolower(__CLASS__).'/signup',$data);
                }
        }
        function logout()
        {

                $authorized = $this->session->userdata('authorized');
                if(isset($authorized) and !empty($authorized))
                {
                        $this->session->sess_destroy();
                        redirect(base_url().'home');

                }
        }

        function changePassword()
  {
    //standard array
    $data = array();
    $authorized = $this->session->userdata('authorized');
    // if(isset($authorized) && !empty($authorized) && $authorized['isLoggedIn'] == 1){
      // redirect(base_url().'home');
    // }
    //get cache 
    $authorized = $this->session->userdata('authorized');
    if(isset($_POST) && !empty($_POST)){
      // print_r($_POST);
      // die();
    if(isset($authorized) && !empty($authorized)){
      $_POST['userId'] = $authorized['Id'];
      $_POST['cartId'] = $authorized['cartId'];
      if($authorized['isLoggedIn'] == 1){
        $_POST['isLoggedIn'] = 'true';
      }
      elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
      $_POST['isLoggedIn'] = 'false';
      }
    }
      //end of get cache
      $configdata =array();
      $configdata = $this->config->item('change_password');
      unset($configdata['response'][0]);
      $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata);
      //set cache to output or renew cache
      if(isset($data['response']['response']['changePassword']))
      {
        $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
        $data['title'] = 'Saholic';
        $data['stylesheet'] = 'auth.css';
        $this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);

      }
      else
      {
        $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
        $data['title'] = 'Saholic';
        $data['stylesheet'] = 'auth.css';
        $this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
      }
    }
    else{
      // echo 'inside else';
      // die();
      $configdata =array();
      $configdata = $this->config->item('change_password');
      unset($configdata['response'][1]);
      $cachemodule = array('header','footer','response');
      $configdata = getCache($configdata,$cachemodule);
      $data = array();
      $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata['module']);
      $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
      $data['title'] = 'Saholic';
      $data['stylesheet'] = 'auth.css';
      if(isset($cachemodule) and !empty($cachemodule) && !isset($api))
      {
        foreach($cachemodule as $cm)
        {
          if(isset($configdata[$cm]) and !empty($configdata[$cm]))
          {
            $data['response'][$cm]=$configdata[$cm];
          }
        }
      }
      setCache($configdata['module'],$cachemodule,$data['response']);
      $this->layout->view(strtolower(__CLASS__).'/changePassword',$data);
    }
  }
  
        function forgotPassword($email)
        {

                $authorized = $this->session->userdata('authorized');
                if(!isset($authorized['isLoggedIn']) || ($authorized['isLoggedIn'] != 1))
                {
                        if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){
                        $configUrl = $this->config->item('forgotpassword');
                        $configUrl = $configUrl['url'];
                        $url = $this->config->item('curl_base_url').$configUrl;
                        $params['emailId'] = $email;
                        $this->mcurl->add_call('forgotPassword','post',$url,$params);
                        $response = $this->mcurl->execute($url);
                        $data['response'] = $response['forgotPassword']['response'];
                        print_r($data['response']);
                }
                }
        }

        //social login process
        public function session($provider)
        {
  //    if (isset($_SERVER['HTTP_REFERER'])) {
                //      if(strpos($_SERVER['HTTP_REFERER'],'session') == false) {
  //                            $location = $_SERVER['HTTP_REFERER'];
  //                            $this->session->set_userdata('location',$location);
                //      }
                // }
        $type = $provider;
        $this->load->helper('url_helper');
        $social =$this->config->item('oauth_social');

        $this->load->spark('oauth2/0.4.0');

        $provider = $this->oauth2->provider($provider, array(
            'id' => $social[$provider]['id'],
            'secret' => $social[$provider]['secretkey']
            
        ));


        if ( ! $this->input->get('code'))
        {
                if($this->input->get('error')) {
                        redirect(base_url());
                }else{
                        redirect($provider->authorize());
                }
            // By sending no options it'll come back here
        }
        else
        {
            try
            {
                //manage login operation here and redirect to perspective link
                $token = $provider->access($_GET['code']);

                $user = $provider->get_user_info($token);
                if(strcasecmp('facebook', $type) == 0){
                        $_POST['isFacebookUser'] = 'true';
                        $_POST['accessToken'] = $user['access_token'];
                        $_POST['facebookId'] = $user['uid'];
                        $_POST['email'] = $user['email'];
                        if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST['email'])) {
                                                $authorized = $this->session->userdata('authorized');
                                                if(isset($_POST) && !empty($_POST)){
                                                        if(isset($authorized) && !empty($authorized)){
                                                                $_POST['userId'] = $authorized['Id'];
                                                                $_POST['Id'] = $authorized['cartId'];
                                                                if($authorized['isLoggedIn'] == 1){
                                                                        $_POST['isLoggedIn'] = 'true';
                                                                }
                                                                elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
                                                                        $_POST['isLoggedIn'] = 'false';
                                                                }
                                                        }
                                                }
                                $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$this->config->item('oauth_login'));
                                if(isset($data['response']['response']['login']['authorized']))
                                                {
                                                        $this->session->set_userdata('authorized',$data['response']['response']['login']['authorized']);
                                                        $this->session->set_flashdata(array('msg'=>$data['response']['response']['login']['msg']));
                                                        $location = $this->session->userdata('location');
                                                        if(isset($location) && !empty($location)){
                                                                $this->session->unset_userdata('location');
                                                                redirect($location);
                                                        }
                                                        else{
                                                                redirect(base_url().$data['response']['response']['login']['redirect']);        
                                                        }
                                                }
                                                else
                                                {
                                                        $inputfile = base_url().'assets/css/auth.less';
                                                        $outputfile = base_url().'assets/css/auth.less';
                                                        $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
                                                        $data['stylesheet'] = 'auth.css';
                                                        $this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
                                                }
                                        }else{
                                                $this->session->set_flashdata(array('msg'=>'Some problem occured.Please try again!'));
                                                redirect(base_url().'login');
                                        }
                                }
                // Here you should use this information to A) look for a user B) help a new user sign up with existing data.
                // If you store it all in a cookie and redirect to a registration page this is crazy-simple.
                //echo "<pre>Tokens: ";
                //var_dump($token);

                //echo "\n\nUser Info: ";
                //var_dump($user);
            }

            catch (OAuth2_Exception $e)
            {
                show_error('That didnt work: '.$e);
            }

        }
    }

    public function autologin() {
        $getdata = $this->input->get();
        //error_log(print_r($getdata,true));
        // $configdata = $this->config->item('gosf');
        // $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');        
        if(isset($getdata['data']) && !empty($getdata['data'])){
            $data = $getdata['data'];
        } else{
            // $this->layout->view(strtolower(__CLASS__).'/autologin',array('next'=>base_url()));
            redirect(base_url().'home');
        }
        $userData = unserialize(base64_decode($data));
        if(isset($getdata['next']) && !empty($getdata['next'])){
            $userData['next'] = $getdata['next'];
        }
        $userData['next'] = str_replace('&next=','',$userData['next']);
        /*$authorized = $this->session->userdata('authorized');
        //check if user is already logged in
        if(isset($authorized) && !empty($authorized) && $authorized['isLoggedIn'] == 1){
                error_log(print_r("Next ". $userData['next'],1));
            redirect(base_url().$userData['next']);
        }*/
        if(isset($getdata['v']) && !empty($getdata['v'])){
                $url = $this->config->item('profitmandi_token_validation_url').$getdata['token'];
                error_log("token validation url ".$url);
                $ch = curl_init();
                        // Now set some options (most are optional)
                        // Set URL to download
                        curl_setopt($ch, CURLOPT_URL, $url);
                        // User agent
                        curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
                        // Include header in result? (0 = yes, 1 = no)
                        curl_setopt($ch, CURLOPT_HEADER, 0);
                        // Should cURL return or print out the data? (true = return, false = print)
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        // Timeout in seconds
                        // curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                        // Download the given URL, and return output
                        $output = curl_exec($ch);
                        // Close the cURL resource, and free system resources
                        curl_close($ch);
                        $response = json_decode($output,1);
                        if(empty($response) || !$response['validToken']){
                                error_log("not authenticated");
                                redirect(base_url().$userData['next']); 
                        }
                        if(!empty($response) && $response['validToken']){
                                $userData['isLoggedIn'] = true;
                        $this->session->set_userdata('authorized',$userData);
                        error_log("authenticated user ".print_r($userData,1));
                        redirect(base_url().$userData['next']);
                        }
        }        
        // $userData['stylesheet'] = 'auth.css';
        // $userData['title'] = 'Saholic';
        // if(isset($userData['next']) && !empty($userData['next'])){
            // $next = $userData['next'];
            // $this->session->set_userdata('location',$next);
        // }
        if(isset($getdata['token']) && !empty($getdata['token'])){
            $token = $getdata['token'];
        } else{        
            redirect(base_url().$userData['next']);
        }
        $hash = md5("21dtr022015|".$userData['Id']);
        //error_log(print_r($hash,true));
        if($hash != $token){
            // $this->layout->view(strtolower(__CLASS__).'/autologin',$userData);
            redirect(base_url().$userData['next']);
        }
        $userData['isLoggedIn'] = true;
        $this->session->set_userdata('authorized',$userData);
        redirect(base_url().$userData['next']);
        // error_log("cookie ".print_r($_COOKIE,true));
        // $location = $this->session->userdata('location');
        // if(isset($location) && !empty($location)){
            // $this->layout->view(strtolower(__CLASS__).'/autologin',$userData);
        // } else {
            // $this->layout->view(strtolower(__CLASS__).'/autologin',array('next'=>base_url()));
        // }
    }

/*
    public function autologin() {    
        $getdata = $this->input->get();
        //error_log(print_r($getdata,true));
        $configdata = $this->config->item('gosf');
                $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
                if(isset($getdata['data']) && !empty($getdata['data'])){
                $data = $getdata['data'];
        }else{
                //error_log("data not set");
                $this->layout->view(strtolower(__CLASS__).'/autologin',array('next'=>base_url()));
        }       
                $userData = unserialize(base64_decode($data));
                //error_log(print_r($userData,true));
                if(isset($getdata['next']) && !empty($getdata['next'])){
                //error_log("inside condition 1", 0);
                //error_log(print_r($getdata['next'],true));
                $userData['next'] = $getdata['next'];
        }
        $userData['next'] = str_replace('&next=','',$userData['next']);
                $userData['stylesheet'] = 'auth.css';
        $userData['title'] = 'Saholic';
        //error_log(print_r($userData,true));
        if(isset($userData['next']) && !empty($userData['next'])){
                //error_log("inside condition 2", 0);
                $next = $userData['next'];
                $this->session->set_userdata('location',$next);
        }       
        if(isset($getdata['token']) && !empty($getdata['token'])){
                $token = $getdata['token'];
                //error_log(print_r($token,true));
        }else{
                $this->layout->view(strtolower(__CLASS__).'/autologin',$userData);
        }               
        $hash = md5("21dtr022015|".$userData['Id']);            
        //error_log(print_r($hash,true));
        if($hash != $token){
                 
                $this->layout->view(strtolower(__CLASS__).'/autologin',$userData);
        }
                  error_log("inside condition 5", 0);
                $userData['isLoggedIn'] = true;
                $this->session->set_userdata('authorized',$userData);
        error_log("cookie ".print_r($_COOKIE,true));
                $location = $this->session->userdata('location');
                if(isset($location) && !empty($location)){
                        error_log("inside condition 6", 0);
//                      $this->session->unset_userdata('location');
                        $this->layout->view(strtolower(__CLASS__).'/autologin',$userData);
                }
                else{
                       error_log("inside condition 7", 0);
                        $this->layout->view(strtolower(__CLASS__).'/autologin',array('next'=>base_url()));                      
                }
    }
    */
}

/***** FOR LOGIN AUTHENTICATION *****
*    AUTHORIZE ARRAY + AUTHORIZE[AUTHORITY]=SUCCESS
*        FOR SIGN UP - 
*
*

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */