Subversion Repositories SmartDukaan

Rev

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