Subversion Repositories SmartDukaan

Rev

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