Subversion Repositories SmartDukaan

Rev

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