Subversion Repositories SmartDukaan

Rev

Rev 11412 | Rev 11631 | 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
	}
243
	function forgotPassword($email)
244
	{
245
 
246
		$authorized = $this->session->userdata('authorized');
247
		if(!isset($authorized['isLoggedIn']) || ($authorized['isLoggedIn'] != 1))
248
		{
249
			if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){
250
       			$configUrl = $this->config->item('forgotpassword');
251
		      	$configUrl = $configUrl['url'];
252
		      	$url = $this->config->item('curl_base_url').$configUrl;
253
		      	$params['emailId'] = $email;
254
		      	$this->mcurl->add_call('forgotPassword','post',$url,$params);
255
		      	$response = $this->mcurl->execute($url);
256
		      	$data['response'] = $response['forgotPassword']['response'];
257
      			print_r($data['response']);
258
       		}
259
		}
260
	}
261
 
262
	//social login process
263
	public function session($provider)
264
    {
265
  //   	if (isset($_SERVER['HTTP_REFERER'])) {
266
		// 	if(strpos($_SERVER['HTTP_REFERER'],'session') == false) {
267
  //  				$location = $_SERVER['HTTP_REFERER'];
268
  //  				$this->session->set_userdata('location',$location);
269
		// 	}
270
		// }
271
    	$type = $provider;
272
        $this->load->helper('url_helper');
273
        $social =$this->config->item('oauth_social');
274
 
275
        $this->load->spark('oauth2/0.4.0');
276
 
277
        $provider = $this->oauth2->provider($provider, array(
278
            'id' => $social[$provider]['id'],
279
            'secret' => $social[$provider]['secretkey']
280
 
281
        ));
282
 
283
 
284
        if ( ! $this->input->get('code'))
285
        {
286
        	if($this->input->get('error')) {
287
        		redirect(base_url());
288
        	}else{
289
        		redirect($provider->authorize());
290
        	}
291
            // By sending no options it'll come back here
292
        }
293
        else
294
        {
295
            try
296
            {
297
            	//manage login operation here and redirect to perspective link
298
                $token = $provider->access($_GET['code']);
299
 
300
                $user = $provider->get_user_info($token);
301
                if(strcasecmp('facebook', $type) == 0){
302
                	$_POST['isFacebookUser'] = 'true';
303
                	$_POST['accessToken'] = $user['access_token'];
304
                	$_POST['facebookId'] = $user['uid'];
305
                	$_POST['email'] = $user['email'];
306
                	$authorized = $this->session->userdata('authorized');
307
					if(isset($_POST) && !empty($_POST)){
308
						if(isset($authorized) && !empty($authorized)){
309
							$_POST['userId'] = $authorized['Id'];
310
							$_POST['Id'] = $authorized['cartId'];
311
							if($authorized['isLoggedIn'] == 1){
312
								$_POST['isLoggedIn'] = 'true';
313
							}
314
							elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
315
								$_POST['isLoggedIn'] = 'false';
316
							}
317
						}
318
					}
319
                }
320
                $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$this->config->item('oauth_login'));
321
                if(isset($data['response']['response']['login']['authorized']))
322
				{
323
					$this->session->set_userdata('authorized',$data['response']['response']['login']['authorized']);
324
					$this->session->set_flashdata(array('msg'=>$data['response']['response']['login']['msg']));
325
					$location = $this->session->userdata('location');
326
					if(isset($location) && !empty($location)){
327
						$this->session->unset_userdata('location');
328
						redirect($location);
329
					}
330
					else{
331
						redirect(base_url().$data['response']['response']['login']['redirect']);	
332
					}
333
				}
334
				else
335
				{
336
					$inputfile = base_url().'assets/css/auth.less';
337
					$outputfile = base_url().'assets/css/auth.less';
338
					$this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
339
					$data['stylesheet'] = 'auth.css';
340
					$this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
341
				}
342
                // Here you should use this information to A) look for a user B) help a new user sign up with existing data.
343
                // If you store it all in a cookie and redirect to a registration page this is crazy-simple.
344
                //echo "<pre>Tokens: ";
345
                //var_dump($token);
346
 
347
                //echo "\n\nUser Info: ";
348
                //var_dump($user);
349
            }
350
 
351
            catch (OAuth2_Exception $e)
352
            {
353
                show_error('That didnt work: '.$e);
354
            }
355
 
356
        }
357
    }
358
 
359
}
360
 
361
/***** FOR LOGIN AUTHENTICATION *****
362
*    AUTHORIZE ARRAY + AUTHORIZE[AUTHORITY]=SUCCESS
363
*	 FOR SIGN UP - 
364
*
365
*
366
 
367
/* End of file welcome.php */
368
/* Location: ./application/controllers/welcome.php */