Subversion Repositories SmartDukaan

Rev

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