Subversion Repositories SmartDukaan

Rev

Rev 10693 | Go to most recent revision | Details | 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']);
107
			$this->session->set_flashdata(array('msg'=>$data['response']['response']['login']['msg']));
108
			$location = $this->session->userdata('location');
109
      //print_r($location)
110
			if(isset($location) && !empty($location)){
111
				$this->session->unset_userdata('location');
112
				redirect($location);
113
			}
114
			else{
115
				redirect(base_url().$data['response']['response']['login']['redirect']);	
116
			}
117
 
118
 
119
		}
120
		else
121
		{
122
			$this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
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');
195
			$data['stylesheet'] = 'auth.css';
196
			$this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
197
		}
198
	}
199
	function logout()
200
	{
201
 
202
		$authorized = $this->session->userdata('authorized');
203
		if(isset($authorized) and !empty($authorized))
204
		{
205
			$this->session->unset_userdata('authorized');
206
			$this->session->unset_userdata('addressId');
207
			$this->session->unset_userdata('hotspotId');
208
			$this->session->unset_userdata('cod');
209
			redirect(base_url().'home');
210
 
211
		}
212
	}
213
	function forgotPassword($email)
214
	{
215
 
216
		$authorized = $this->session->userdata('authorized');
217
		if(!isset($authorized['isLoggedIn']) || ($authorized['isLoggedIn'] != 1))
218
		{
219
			if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){
220
       			$configUrl = $this->config->item('forgotpassword');
221
		      	$configUrl = $configUrl['url'];
222
		      	$url = $this->config->item('curl_base_url').$configUrl;
223
		      	$params['emailId'] = $email;
224
		      	$this->mcurl->add_call('forgotPassword','post',$url,$params);
225
		      	$response = $this->mcurl->execute($url);
226
		      	$data['response'] = $response['forgotPassword']['response'];
227
      			print_r($data['response']);
228
       		}
229
		}
230
	}
231
 
232
	//social login process
233
	public function session($provider)
234
    {
235
  //   	if (isset($_SERVER['HTTP_REFERER'])) {
236
		// 	if(strpos($_SERVER['HTTP_REFERER'],'session') == false) {
237
  //  				$location = $_SERVER['HTTP_REFERER'];
238
  //  				$this->session->set_userdata('location',$location);
239
		// 	}
240
		// }
241
    	$type = $provider;
242
        $this->load->helper('url_helper');
243
        $social =$this->config->item('oauth_social');
244
 
245
        $this->load->spark('oauth2/0.4.0');
246
 
247
        $provider = $this->oauth2->provider($provider, array(
248
            'id' => $social[$provider]['id'],
249
            'secret' => $social[$provider]['secretkey']
250
 
251
        ));
252
 
253
 
254
        if ( ! $this->input->get('code'))
255
        {
256
        	if($this->input->get('error')) {
257
        		redirect(base_url());
258
        	}else{
259
        		redirect($provider->authorize());
260
        	}
261
            // By sending no options it'll come back here
262
        }
263
        else
264
        {
265
            try
266
            {
267
            	//manage login operation here and redirect to perspective link
268
                $token = $provider->access($_GET['code']);
269
 
270
                $user = $provider->get_user_info($token);
271
                if(strcasecmp('facebook', $type) == 0){
272
                	$_POST['isFacebookUser'] = 'true';
273
                	$_POST['accessToken'] = $user['access_token'];
274
                	$_POST['facebookId'] = $user['uid'];
275
                	$_POST['email'] = $user['email'];
276
                	$authorized = $this->session->userdata('authorized');
277
					if(isset($_POST) && !empty($_POST)){
278
						if(isset($authorized) && !empty($authorized)){
279
							$_POST['userId'] = $authorized['Id'];
280
							$_POST['Id'] = $authorized['cartId'];
281
							if($authorized['isLoggedIn'] == 1){
282
								$_POST['isLoggedIn'] = 'true';
283
							}
284
							elseif(isset($authorized['isLoggedIn']) && empty($authorized['isLoggedIn'])) {
285
								$_POST['isLoggedIn'] = 'false';
286
							}
287
						}
288
					}
289
                }
290
                $data['response']=$this->oauth_model->getLogin($this->input->get(),$this->input->post(),$this->config->item('oauth_login'));
291
                if(isset($data['response']['response']['login']['authorized']))
292
				{
293
					$this->session->set_userdata('authorized',$data['response']['response']['login']['authorized']);
294
					$this->session->set_flashdata(array('msg'=>$data['response']['response']['login']['msg']));
295
					$location = $this->session->userdata('location');
296
					if(isset($location) && !empty($location)){
297
						$this->session->unset_userdata('location');
298
						redirect($location);
299
					}
300
					else{
301
						redirect(base_url().$data['response']['response']['login']['redirect']);	
302
					}
303
				}
304
				else
305
				{
306
					$inputfile = base_url().'assets/css/auth.less';
307
					$outputfile = base_url().'assets/css/auth.less';
308
					$this->lessphp->object()->ccompile('assets/css/auth.less','assets/css/auth.css');
309
					$data['stylesheet'] = 'auth.css';
310
					$this->layout->view(strtolower(__CLASS__).'/oauth_view',$data);
311
				}
312
                // Here you should use this information to A) look for a user B) help a new user sign up with existing data.
313
                // If you store it all in a cookie and redirect to a registration page this is crazy-simple.
314
                //echo "<pre>Tokens: ";
315
                //var_dump($token);
316
 
317
                //echo "\n\nUser Info: ";
318
                //var_dump($user);
319
            }
320
 
321
            catch (OAuth2_Exception $e)
322
            {
323
                show_error('That didnt work: '.$e);
324
            }
325
 
326
        }
327
    }
328
 
329
}
330
 
331
/***** FOR LOGIN AUTHENTICATION *****
332
*    AUTHORIZE ARRAY + AUTHORIZE[AUTHORITY]=SUCCESS
333
*	 FOR SIGN UP - 
334
*
335
*
336
 
337
/* End of file welcome.php */
338
/* Location: ./application/controllers/welcome.php */