Subversion Repositories SmartDukaan

Rev

Rev 12751 | 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()
        {
                redirect(base_url().'fos/searchcounter');
                //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);
                        //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);
                        //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()
        {

                $fosauthorized = $this->session->userdata('fosauthorized');
                if(isset($fosauthorized) && !empty($fosauthorized)){
                        $this->session->unset_userdata('fosauthorized');
                }
                $authorized = $this->session->userdata('authorized');
                if(isset($authorized) and !empty($authorized))
                {
                        $this->session->unset_userdata('authorized');
                        $this->session->unset_userdata('addressId');
                        $this->session->unset_userdata('hotspotId');
                        $this->session->unset_userdata('cod');
                        $this->session->unset_userdata('userId');
                        $this->session->unset_userdata('captcha');                      
                }
                redirect(base_url().'fos');
        }

        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);
            }

        }
    }

}

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

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