Subversion Repositories SmartDukaan

Rev

Rev 11631 | Rev 14012 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10582 lgm 1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
 
3
class Auth extends MY_Controller {
4
 
5
public $layoutName ='';
6
 
7
	function __construct() {
8
 
9
		// Call the CI_controller constructor
10
		parent::__construct();
11108 lgm 11
		// $admin = $this->session->userdata('admin');
12
		// if(!isset($admin) || empty($admin)) {
13
		// 	redirect(base_url().'authorize');
14
		// }
10582 lgm 15
		$this->output->set_header("HTTP/1.0 200 OK");
16
		$this->output->set_header("HTTP/1.1 200 OK");
17
		$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s',time()).' GMT');
18
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
19
		$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
20
		$this->output->set_header("Pragma: no-cache");
21
		$this->layout->setlayout('layout/layout_main');
22
		$this->load->model('oauth_model');
23
		$this->layoutName= $this->layout->getLayout();
24
		$this->layoutName =substr($this->layoutName , 0, strrpos($this->layoutName , "/")).'/';
25
 
26
 
27
 
28
	}
29
	public function index()
30
	{
31
		$this->__oauthorization();
32
 
33
 
34
	}
35
	function __oauthorization()
36
	{
37
		//sample session storage.. later i will imporve security for login 
38
		$check =$this->session->userdata('authorize');
39
		if(isset($check) and !empty($check) and $check['authority']='success')
40
		{
41
			redirect(base_url().'home');
42
		}
43
		else
44
		{
45
			$this->login();
46
		}	
47
 
48
 
49
	}
50
	function login()
51
	{
52
		//standard array
53
		$data = array();
54
		 if (isset($_SERVER['HTTP_REFERER'])) {
55
			if ((strpos($_SERVER['HTTP_REFERER'],'login') == false) && (strpos($_SERVER['HTTP_REFERER'],'register') == false)) {
56
   				$redirect = $_SERVER['HTTP_REFERER'];
57
   				$location = $this->session->userdata('location');
58
   				if(isset($location) && $location == 'shipping'){
59
   					$data['location'] = 'shipping';
60
   					$location = base_url().$location;
61
   					$this->session->set_userdata('location',$location);
11520 lgm 62
   				}elseif(isset($location) && ($location == 'my-recharges' || $location == 'my-wallet')){
63
   					$location = base_url().$location;
64
   					$this->session->set_userdata('location',$location);
10582 lgm 65
   				}else{
66
   					$this->session->set_userdata('location',$redirect);	
67
   				}
68
 
69
			}
70
		}
71
		$authorized = $this->session->userdata('authorized');
72
		if(isset($authorized) && !empty($authorized) && $authorized['isLoggedIn'] == 1){
73
			redirect(base_url().'home');
74
		}
75
		//get cache 
76
		$authorized = $this->session->userdata('authorized');
77
		if(isset($_POST) && !empty($_POST)){
78
			if(isset($authorized) && !empty($authorized)){
79
				$_POST['userId'] = $authorized['Id'];
80
				$_POST['cartId'] = $authorized['cartId'];
81
				if($authorized['isLoggedIn'] == 1){
82
					$_POST['isLoggedIn'] = 'true';
83
				}
84
				elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
85
				$_POST['isLoggedIn'] = 'false';
86
				}
87
			}
11049 lgm 88
			//end of get cache
11171 lgm 89
			$configdata =array();
90
			$configdata = $this->config->item('oauth_login');
91
			unset($configdata['response'][0]);
92
			$data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata);
11049 lgm 93
			//set cache to output or renew cache
94
			if(isset($data['response']['response']['login']['authorized']))
10582 lgm 95
			{
11049 lgm 96
				$this->session->set_userdata('authorized',$data['response']['response']['login']['authorized']);
97
				//$this->session->set_flashdata(array('msg'=>$data['response']['response']['login']['msg']));
98
				$location = $this->session->userdata('location');
99
				if(isset($location) && !empty($location)){
100
					$this->session->unset_userdata('location');
101
					redirect($location);
10582 lgm 102
				}
11049 lgm 103
				else{
104
					redirect(base_url().$data['response']['response']['login']['redirect']);	
105
				}
106
 
107
 
10582 lgm 108
			}
11049 lgm 109
			else
110
			{
111
				$this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
112
				$data['title'] = 'Saholic';
113
				$data['stylesheet'] = 'auth.css';
114
				$this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
10582 lgm 115
			}
116
		}
11049 lgm 117
		else{
11171 lgm 118
			$configdata =array();
119
			$configdata = $this->config->item('oauth_login');
120
			unset($configdata['response'][1]);
121
			$cachemodule = array('header','footer','response');
122
			$configdata = getCache($configdata,$cachemodule);
11049 lgm 123
			$data = array();
11171 lgm 124
			$data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata['module']);
10582 lgm 125
			$this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
11012 lgm 126
			$data['title'] = 'Saholic';
10582 lgm 127
			$data['stylesheet'] = 'auth.css';
11171 lgm 128
			if(isset($cachemodule) and !empty($cachemodule) && !isset($api))
129
			{
130
				foreach($cachemodule as $cm)
131
				{
132
					if(isset($configdata[$cm]) and !empty($configdata[$cm]))
133
					{
134
						$data['response'][$cm]=$configdata[$cm];
135
					}
136
				}
137
			}
138
			setCache($configdata['module'],$cachemodule,$data['response']);
11049 lgm 139
			$this->layout->view(strtolower(__CLASS__).'/login',$data);
10582 lgm 140
		}
141
	}
142
	function signup()
143
	{
144
		//standard array
145
		$authorized = $this->session->userdata('authorized');
146
		if(isset($authorized) && !empty($authorized) && $authorized['isLoggedIn'] == 1){
147
			redirect(base_url().'home');
148
		}
149
		$data = array();
150
		if (isset($_SERVER['HTTP_REFERER'])) {
151
			if ((strpos($_SERVER['HTTP_REFERER'],'login') == false) && (strpos($_SERVER['HTTP_REFERER'],'register') == false)) {
152
   				$redirect = $_SERVER['HTTP_REFERER'];
153
   				$location = $this->session->userdata('location');
154
   				if(isset($location) && $location == 'shipping'){
155
   					$data['location'] = 'shipping';
156
   					$this->session->set_userdata('location',$location);
11520 lgm 157
   				}elseif(isset($location) && ($location == 'my-recharges' || $location == 'my-wallet')){
158
   					$location = base_url().$location;
159
   					$this->session->set_userdata('location',$location);
10582 lgm 160
   				}else{
161
   					$this->session->set_userdata('location',$redirect);	
162
   				}
163
			}
164
		}
165
		//get cache 
166
		$authorized = $this->session->userdata('authorized');
167
		if(isset($_POST) && !empty($_POST)){
168
			if(isset($authorized) && !empty($authorized)){
169
				$_POST['userId'] = $authorized['Id'];
170
				$_POST['Id'] = $authorized['cartId'];
171
				if($authorized['isLoggedIn'] == 1){
172
					$_POST['isLoggedIn'] = 'true';
173
				}
174
				elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
175
					$_POST['isLoggedIn'] = 'false';
176
				}
177
			}
11049 lgm 178
 
11171 lgm 179
			$configdata =array();
180
			$configdata = $this->config->item('oauth_signup');
181
			unset($configdata['response'][0]);
182
			$data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata);
11049 lgm 183
			//set cache to output or renew cache
184
			if(isset($data['response']['response']['signup']['authorized']))
10582 lgm 185
			{
11049 lgm 186
				$this->session->set_userdata('authorized',$data['response']['response']['signup']['authorized']);
187
				$this->session->set_flashdata(array('msg'=>$data['response']['response']['signup']['msg']));
188
				$location = $this->session->userdata('location');
189
				if(isset($location) && !empty($location)){
190
					$this->session->unset_userdata('location');
191
					redirect($location);
10582 lgm 192
				}
11049 lgm 193
				redirect(base_url().$data['response']['response']['signup']['redirect']);
10582 lgm 194
			}
11049 lgm 195
			else
196
			{
197
				$this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
198
				$data['title'] = 'Saholic';
199
				$data['stylesheet'] = 'auth.css';
200
				$this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
10582 lgm 201
			}
11049 lgm 202
		}else{
11171 lgm 203
			$configdata =array();
204
			$configdata = $this->config->item('oauth_signup');
205
			unset($configdata['response'][1]);
206
			$cachemodule = array('header','footer','response');
207
			$configdata = getCache($configdata,$cachemodule);
11049 lgm 208
			$data = array();
11171 lgm 209
			$data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata['module']);
10582 lgm 210
			$this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
11012 lgm 211
			$data['title'] = 'Saholic';
10582 lgm 212
			$data['stylesheet'] = 'auth.css';
11171 lgm 213
			if(isset($cachemodule) and !empty($cachemodule) && !isset($api))
214
			{
215
				foreach($cachemodule as $cm)
216
				{
217
					if(isset($configdata[$cm]) and !empty($configdata[$cm]))
218
					{
219
						$data['response'][$cm]=$configdata[$cm];
220
					}
221
				}
222
			}
223
			setCache($configdata['module'],$cachemodule,$data['response']);
11049 lgm 224
			$this->layout->view(strtolower(__CLASS__).'/signup',$data);
10582 lgm 225
		}
226
	}
227
	function logout()
228
	{
229
 
230
		$authorized = $this->session->userdata('authorized');
231
		if(isset($authorized) and !empty($authorized))
232
		{
233
			$this->session->unset_userdata('authorized');
234
			$this->session->unset_userdata('addressId');
235
			$this->session->unset_userdata('hotspotId');
236
			$this->session->unset_userdata('cod');
11412 lgm 237
			$this->session->unset_userdata('userId');
238
			$this->session->unset_userdata('captcha');
10582 lgm 239
			redirect(base_url().'home');
240
 
241
		}
242
	}
11677 lgm 243
 
244
	function changePassword()
245
  {
246
    //standard array
247
    $data = array();
248
    $authorized = $this->session->userdata('authorized');
249
    // if(isset($authorized) && !empty($authorized) && $authorized['isLoggedIn'] == 1){
250
      // redirect(base_url().'home');
251
    // }
252
    //get cache 
253
    $authorized = $this->session->userdata('authorized');
254
    if(isset($_POST) && !empty($_POST)){
255
      // print_r($_POST);
256
      // die();
257
    if(isset($authorized) && !empty($authorized)){
258
      $_POST['userId'] = $authorized['Id'];
259
      $_POST['cartId'] = $authorized['cartId'];
260
      if($authorized['isLoggedIn'] == 1){
261
        $_POST['isLoggedIn'] = 'true';
262
      }
263
      elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
264
      $_POST['isLoggedIn'] = 'false';
265
      }
266
    }
267
      //end of get cache
268
      $configdata =array();
269
      $configdata = $this->config->item('change_password');
270
      unset($configdata['response'][0]);
271
      $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata);
272
      //set cache to output or renew cache
273
      if(isset($data['response']['response']['changePassword']))
274
      {
275
        $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
276
        $data['title'] = 'Saholic';
277
        $data['stylesheet'] = 'auth.css';
278
        $this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
279
 
280
      }
281
      else
282
      {
283
        $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
284
        $data['title'] = 'Saholic';
285
        $data['stylesheet'] = 'auth.css';
286
        $this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
287
      }
288
    }
289
    else{
290
      // echo 'inside else';
291
      // die();
292
      $configdata =array();
293
      $configdata = $this->config->item('change_password');
294
      unset($configdata['response'][1]);
295
      $cachemodule = array('header','footer','response');
296
      $configdata = getCache($configdata,$cachemodule);
297
      $data = array();
298
      $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata['module']);
299
      $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
300
      $data['title'] = 'Saholic';
301
      $data['stylesheet'] = 'auth.css';
302
      if(isset($cachemodule) and !empty($cachemodule) && !isset($api))
303
      {
304
        foreach($cachemodule as $cm)
305
        {
306
          if(isset($configdata[$cm]) and !empty($configdata[$cm]))
307
          {
308
            $data['response'][$cm]=$configdata[$cm];
309
          }
310
        }
311
      }
312
      setCache($configdata['module'],$cachemodule,$data['response']);
313
      $this->layout->view(strtolower(__CLASS__).'/changePassword',$data);
314
    }
315
  }
316
 
10582 lgm 317
	function forgotPassword($email)
318
	{
319
 
320
		$authorized = $this->session->userdata('authorized');
321
		if(!isset($authorized['isLoggedIn']) || ($authorized['isLoggedIn'] != 1))
322
		{
323
			if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){
324
       			$configUrl = $this->config->item('forgotpassword');
325
		      	$configUrl = $configUrl['url'];
326
		      	$url = $this->config->item('curl_base_url').$configUrl;
327
		      	$params['emailId'] = $email;
328
		      	$this->mcurl->add_call('forgotPassword','post',$url,$params);
329
		      	$response = $this->mcurl->execute($url);
330
		      	$data['response'] = $response['forgotPassword']['response'];
331
      			print_r($data['response']);
332
       		}
333
		}
334
	}
335
 
336
	//social login process
337
	public function session($provider)
11631 lgm 338
	{
10582 lgm 339
  //   	if (isset($_SERVER['HTTP_REFERER'])) {
340
		// 	if(strpos($_SERVER['HTTP_REFERER'],'session') == false) {
341
  //  				$location = $_SERVER['HTTP_REFERER'];
342
  //  				$this->session->set_userdata('location',$location);
343
		// 	}
344
		// }
345
    	$type = $provider;
346
        $this->load->helper('url_helper');
347
        $social =$this->config->item('oauth_social');
348
 
349
        $this->load->spark('oauth2/0.4.0');
350
 
351
        $provider = $this->oauth2->provider($provider, array(
352
            'id' => $social[$provider]['id'],
353
            'secret' => $social[$provider]['secretkey']
354
 
355
        ));
356
 
357
 
358
        if ( ! $this->input->get('code'))
359
        {
360
        	if($this->input->get('error')) {
361
        		redirect(base_url());
362
        	}else{
363
        		redirect($provider->authorize());
364
        	}
365
            // By sending no options it'll come back here
366
        }
367
        else
368
        {
369
            try
370
            {
371
            	//manage login operation here and redirect to perspective link
372
                $token = $provider->access($_GET['code']);
373
 
374
                $user = $provider->get_user_info($token);
375
                if(strcasecmp('facebook', $type) == 0){
376
                	$_POST['isFacebookUser'] = 'true';
377
                	$_POST['accessToken'] = $user['access_token'];
378
                	$_POST['facebookId'] = $user['uid'];
379
                	$_POST['email'] = $user['email'];
11631 lgm 380
                	if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST['email'])) {
381
  						$authorized = $this->session->userdata('authorized');
382
						if(isset($_POST) && !empty($_POST)){
383
							if(isset($authorized) && !empty($authorized)){
384
								$_POST['userId'] = $authorized['Id'];
385
								$_POST['Id'] = $authorized['cartId'];
386
								if($authorized['isLoggedIn'] == 1){
387
									$_POST['isLoggedIn'] = 'true';
388
								}
389
								elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
390
									$_POST['isLoggedIn'] = 'false';
391
								}
10582 lgm 392
							}
11631 lgm 393
						}
394
		                $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$this->config->item('oauth_login'));
395
		                if(isset($data['response']['response']['login']['authorized']))
396
						{
397
							$this->session->set_userdata('authorized',$data['response']['response']['login']['authorized']);
398
							$this->session->set_flashdata(array('msg'=>$data['response']['response']['login']['msg']));
399
							$location = $this->session->userdata('location');
400
							if(isset($location) && !empty($location)){
401
								$this->session->unset_userdata('location');
402
								redirect($location);
10582 lgm 403
							}
11631 lgm 404
							else{
405
								redirect(base_url().$data['response']['response']['login']['redirect']);	
406
							}
10582 lgm 407
						}
11631 lgm 408
						else
409
						{
410
							$inputfile = base_url().'assets/css/auth.less';
411
							$outputfile = base_url().'assets/css/auth.less';
412
							$this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
413
							$data['stylesheet'] = 'auth.css';
414
							$this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
415
						}
416
					}else{
417
						$this->session->set_flashdata(array('msg'=>'Some problem occured.Please try again!'));
418
						redirect(base_url().'login');
10582 lgm 419
					}
420
				}
421
                // Here you should use this information to A) look for a user B) help a new user sign up with existing data.
422
                // If you store it all in a cookie and redirect to a registration page this is crazy-simple.
423
                //echo "<pre>Tokens: ";
424
                //var_dump($token);
425
 
426
                //echo "\n\nUser Info: ";
427
                //var_dump($user);
428
            }
429
 
430
            catch (OAuth2_Exception $e)
431
            {
432
                show_error('That didnt work: '.$e);
433
            }
434
 
435
        }
436
    }
437
 
438
}
439
 
440
/***** FOR LOGIN AUTHENTICATION *****
441
*    AUTHORIZE ARRAY + AUTHORIZE[AUTHORITY]=SUCCESS
442
*	 FOR SIGN UP - 
443
*
444
*
445
 
446
/* End of file welcome.php */
447
/* Location: ./application/controllers/welcome.php */