Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
12694 anikendra 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();
11
		// $admin = $this->session->userdata('admin');
12
		// if(!isset($admin) || empty($admin)) {
13
		// 	redirect(base_url().'authorize');
14
		// }
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);
62
   				}elseif(isset($location) && ($location == 'my-recharges' || $location == 'my-wallet')){
63
   					$location = base_url().$location;
64
   					$this->session->set_userdata('location',$location);
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
			}
88
			//end of get cache
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);
93
			//set cache to output or renew cache
94
			if(isset($data['response']['response']['login']['authorized']))
95
			{
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);
102
				}
103
				else{
104
					redirect(base_url().$data['response']['response']['login']['redirect']);	
105
				}
106
 
107
 
108
			}
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);
115
			}
116
		}
117
		else{
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);
123
			$data = array();
124
			$data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata['module']);
125
			$this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
126
			$data['title'] = 'Saholic';
127
			$data['stylesheet'] = 'auth.css';
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']);
139
			$this->layout->view(strtolower(__CLASS__).'/login',$data);
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);
157
   				}elseif(isset($location) && ($location == 'my-recharges' || $location == 'my-wallet')){
158
   					$location = base_url().$location;
159
   					$this->session->set_userdata('location',$location);
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
			}
178
 
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);
183
			//set cache to output or renew cache
184
			if(isset($data['response']['response']['signup']['authorized']))
185
			{
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);
192
				}
193
				redirect(base_url().$data['response']['response']['signup']['redirect']);
194
			}
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);
201
			}
202
		}else{
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);
208
			$data = array();
209
			$data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata['module']);
210
			$this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
211
			$data['title'] = 'Saholic';
212
			$data['stylesheet'] = 'auth.css';
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']);
224
			$this->layout->view(strtolower(__CLASS__).'/signup',$data);
225
		}
226
	}
227
	function logout()
228
	{
229
 
12738 anikendra 230
		$fosauthorized = $this->session->userdata('fosauthorized');
231
		if(isset($fosauthorized) && !empty($fosauthorized)){
232
			$this->session->unset_userdata('fosauthorized');
233
		}
12694 anikendra 234
		$authorized = $this->session->userdata('authorized');
235
		if(isset($authorized) and !empty($authorized))
236
		{
237
			$this->session->unset_userdata('authorized');
238
			$this->session->unset_userdata('addressId');
239
			$this->session->unset_userdata('hotspotId');
240
			$this->session->unset_userdata('cod');
241
			$this->session->unset_userdata('userId');
12751 anikendra 242
			$this->session->unset_userdata('captcha');			
12694 anikendra 243
		}
12751 anikendra 244
		redirect(base_url().'fos');
12694 anikendra 245
	}
246
 
247
	function changePassword()
248
  {
249
    //standard array
250
    $data = array();
251
    $authorized = $this->session->userdata('authorized');
252
    // if(isset($authorized) && !empty($authorized) && $authorized['isLoggedIn'] == 1){
253
      // redirect(base_url().'home');
254
    // }
255
    //get cache 
256
    $authorized = $this->session->userdata('authorized');
257
    if(isset($_POST) && !empty($_POST)){
258
      // print_r($_POST);
259
      // die();
260
    if(isset($authorized) && !empty($authorized)){
261
      $_POST['userId'] = $authorized['Id'];
262
      $_POST['cartId'] = $authorized['cartId'];
263
      if($authorized['isLoggedIn'] == 1){
264
        $_POST['isLoggedIn'] = 'true';
265
      }
266
      elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
267
      $_POST['isLoggedIn'] = 'false';
268
      }
269
    }
270
      //end of get cache
271
      $configdata =array();
272
      $configdata = $this->config->item('change_password');
273
      unset($configdata['response'][0]);
274
      $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata);
275
      //set cache to output or renew cache
276
      if(isset($data['response']['response']['changePassword']))
277
      {
278
        $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
279
        $data['title'] = 'Saholic';
280
        $data['stylesheet'] = 'auth.css';
281
        $this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
282
 
283
      }
284
      else
285
      {
286
        $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
287
        $data['title'] = 'Saholic';
288
        $data['stylesheet'] = 'auth.css';
289
        $this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
290
      }
291
    }
292
    else{
293
      // echo 'inside else';
294
      // die();
295
      $configdata =array();
296
      $configdata = $this->config->item('change_password');
297
      unset($configdata['response'][1]);
298
      $cachemodule = array('header','footer','response');
299
      $configdata = getCache($configdata,$cachemodule);
300
      $data = array();
301
      $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$configdata['module']);
302
      $this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
303
      $data['title'] = 'Saholic';
304
      $data['stylesheet'] = 'auth.css';
305
      if(isset($cachemodule) and !empty($cachemodule) && !isset($api))
306
      {
307
        foreach($cachemodule as $cm)
308
        {
309
          if(isset($configdata[$cm]) and !empty($configdata[$cm]))
310
          {
311
            $data['response'][$cm]=$configdata[$cm];
312
          }
313
        }
314
      }
315
      setCache($configdata['module'],$cachemodule,$data['response']);
316
      $this->layout->view(strtolower(__CLASS__).'/changePassword',$data);
317
    }
318
  }
319
 
320
	function forgotPassword($email)
321
	{
322
 
323
		$authorized = $this->session->userdata('authorized');
324
		if(!isset($authorized['isLoggedIn']) || ($authorized['isLoggedIn'] != 1))
325
		{
326
			if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){
327
       			$configUrl = $this->config->item('forgotpassword');
328
		      	$configUrl = $configUrl['url'];
329
		      	$url = $this->config->item('curl_base_url').$configUrl;
330
		      	$params['emailId'] = $email;
331
		      	$this->mcurl->add_call('forgotPassword','post',$url,$params);
332
		      	$response = $this->mcurl->execute($url);
333
		      	$data['response'] = $response['forgotPassword']['response'];
334
      			print_r($data['response']);
335
       		}
336
		}
337
	}
338
 
339
	//social login process
340
	public function session($provider)
341
	{
342
  //   	if (isset($_SERVER['HTTP_REFERER'])) {
343
		// 	if(strpos($_SERVER['HTTP_REFERER'],'session') == false) {
344
  //  				$location = $_SERVER['HTTP_REFERER'];
345
  //  				$this->session->set_userdata('location',$location);
346
		// 	}
347
		// }
348
    	$type = $provider;
349
        $this->load->helper('url_helper');
350
        $social =$this->config->item('oauth_social');
351
 
352
        $this->load->spark('oauth2/0.4.0');
353
 
354
        $provider = $this->oauth2->provider($provider, array(
355
            'id' => $social[$provider]['id'],
356
            'secret' => $social[$provider]['secretkey']
357
 
358
        ));
359
 
360
 
361
        if ( ! $this->input->get('code'))
362
        {
363
        	if($this->input->get('error')) {
364
        		redirect(base_url());
365
        	}else{
366
        		redirect($provider->authorize());
367
        	}
368
            // By sending no options it'll come back here
369
        }
370
        else
371
        {
372
            try
373
            {
374
            	//manage login operation here and redirect to perspective link
375
                $token = $provider->access($_GET['code']);
376
 
377
                $user = $provider->get_user_info($token);
378
                if(strcasecmp('facebook', $type) == 0){
379
                	$_POST['isFacebookUser'] = 'true';
380
                	$_POST['accessToken'] = $user['access_token'];
381
                	$_POST['facebookId'] = $user['uid'];
382
                	$_POST['email'] = $user['email'];
383
                	if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST['email'])) {
384
  						$authorized = $this->session->userdata('authorized');
385
						if(isset($_POST) && !empty($_POST)){
386
							if(isset($authorized) && !empty($authorized)){
387
								$_POST['userId'] = $authorized['Id'];
388
								$_POST['Id'] = $authorized['cartId'];
389
								if($authorized['isLoggedIn'] == 1){
390
									$_POST['isLoggedIn'] = 'true';
391
								}
392
								elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
393
									$_POST['isLoggedIn'] = 'false';
394
								}
395
							}
396
						}
397
		                $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$this->config->item('oauth_login'));
398
		                if(isset($data['response']['response']['login']['authorized']))
399
						{
400
							$this->session->set_userdata('authorized',$data['response']['response']['login']['authorized']);
401
							$this->session->set_flashdata(array('msg'=>$data['response']['response']['login']['msg']));
402
							$location = $this->session->userdata('location');
403
							if(isset($location) && !empty($location)){
404
								$this->session->unset_userdata('location');
405
								redirect($location);
406
							}
407
							else{
408
								redirect(base_url().$data['response']['response']['login']['redirect']);	
409
							}
410
						}
411
						else
412
						{
413
							$inputfile = base_url().'assets/css/auth.less';
414
							$outputfile = base_url().'assets/css/auth.less';
415
							$this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
416
							$data['stylesheet'] = 'auth.css';
417
							$this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
418
						}
419
					}else{
420
						$this->session->set_flashdata(array('msg'=>'Some problem occured.Please try again!'));
421
						redirect(base_url().'login');
422
					}
423
				}
424
                // Here you should use this information to A) look for a user B) help a new user sign up with existing data.
425
                // If you store it all in a cookie and redirect to a registration page this is crazy-simple.
426
                //echo "<pre>Tokens: ";
427
                //var_dump($token);
428
 
429
                //echo "\n\nUser Info: ";
430
                //var_dump($user);
431
            }
432
 
433
            catch (OAuth2_Exception $e)
434
            {
435
                show_error('That didnt work: '.$e);
436
            }
437
 
438
        }
439
    }
440
 
441
}
442
 
443
/***** FOR LOGIN AUTHENTICATION *****
444
*    AUTHORIZE ARRAY + AUTHORIZE[AUTHORITY]=SUCCESS
445
*	 FOR SIGN UP - 
446
*
447
*
448
 
449
/* End of file welcome.php */
12738 anikendra 450
/* Location: ./application/controllers/welcome.php */