Subversion Repositories SmartDukaan

Rev

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