Subversion Repositories SmartDukaan

Rev

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