Subversion Repositories SmartDukaan

Rev

Rev 13739 | Rev 13758 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Users Controller
5
 *
6
 * @property User $User
7
 */
8
class UsersController extends AppController {
9
 
10
	public $components = array('SignMeUp.SignMeUp','RequestHandler','Cookie');
11
 
12
    public function beforeFilter() {
13
    	parent::beforeFilter();
14
    	// $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');
15
    	$this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display','home');
13740 anikendra 16
        $this->Auth->allow(array('mine','login', 'forgotten_password', 'register', 'activate', 'checkemail','checkfbuser','registertwitteruser','skipmobileverification','reauthenticate')); 
13736 anikendra 17
		$this->Cookie->name = 'profittill';
18
		$this->Cookie->time = 86400*30; 
19
		$this->Cookie->path = '/';
20
		$this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#H23s~#^';
21
		$this->Cookie->httpOnly = true;
13532 anikendra 22
    }
23
 
24
	public function bookmarklet() {
13714 anikendra 25
		$this->set('title_for_layout','Profittill Bookmarklet');
13532 anikendra 26
	}
27
 
13740 anikendra 28
	public function mine() {
29
		$this->layout = 'innerpages';
30
		$userId = $this->request->query('user_id');
31
		if(isset($userId) && !empty($userId)){
32
			$this->loadModel('User');
33
			$dbuser = $this->User->findById($userId);
34
			$this->Auth->login($dbuser['User']);
35
		}
36
		$this->checkMobileNumber();
37
		$this->set('user',$dbuser);
38
	}
39
 
13532 anikendra 40
	function registertwitteruser(){
41
		$this->twitterObj->setToken($_GET['oauth_token']);
42
		$token = $this->twitterObj->getAccessToken();
43
		$this->twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
44
		$twitterInfo= $this->twitterObj->get_accountVerify_credentials();
45
		$data = array();
46
		$data['twitter_id'] = $twitterInfo->id;		
47
		$this->Session->write('twtusername',$twitterInfo->screen_name);
48
		$data['twitter_token'] = $token->oauth_token;
49
		$data['twitter_secret'] = $token->oauth_token_secret;
50
		$data['twitter_screen_name'] = $twitterInfo->screen_name;					
51
		$data['active'] = 1;
52
 
53
		$id = $this->Auth->user('id');
54
		if(!isset($id) || $id == ''){
55
		//	$count = $this->User->find('count',array('conditions' => array('twitter_id' => $data['twitter_id'])));
56
			$user = $this->User->find('first',array('conditions' => array('twitter_id' => $data['twitter_id'])));
57
			//if($count<1){
58
			if(empty($user)){
59
				$this->User->create();
60
				$this->User->save($data);
61
//				$dbuser = $this->User->read(null,$this->User->id);
62
				$this->Auth->login($this->User->data);
63
			}else{
64
		//		$data = $this->User->find('first',array('conditions' => array('twitter_id' => $data['twitter_id'])));
65
				$this->Auth->login($user['User']);
66
			}
67
		}else{
68
			//$data['id'] = $this->Auth->user('id');
69
			$user = $this->Auth->user;
70
//			$this->User->save($data);
71
//			$this->Auth->login($data);
72
		}                
73
		$user = $this->User->read(null, $this->Auth->User('id'));
74
		if ($user['User']['group_id'] == 1) {
75
			//$this->redirect(array('controller'=>'users','action'=>'dashboard','?userid='.$this->Auth->user('id')));
76
			header('location:users/dashboard?userid='.$this->Auth->user('id'));
77
			exit();
78
		}
79
		elseif ($user['User']['group_id'] == 2) {
80
			$this->redirect(array('controller' => 'administration', 'action' => 'dashboard'));
81
			exit();
82
		}
83
	}
84
 
85
    public function checkfbuser($accessToken=null){
86
		$sessionState = $this->Session->read('state');
87
		if($sessionState != $_GET['state']) {
88
			$this->redirect(array('controller'=>'users','action'=>'login'));
89
		}
90
		else {
91
			if(isset($_GET['code'])){
92
				$code = $_GET['code'];
93
				$facebookConfig = Configure::read("Facebook");		
94
				$token_url = "https://graph.facebook.com/oauth/access_token?"
95
			       . "client_id=" . $facebookConfig['fbappid'] . "&redirect_uri=" . urlencode($facebookConfig['base_url'].'/users/checkfbuser/')
96
			       . "&client_secret=" . $facebookConfig['secret'] . "&code=" . $code;
97
 
98
			    $response = file_get_contents($token_url);
99
			    $params = null;
100
			    parse_str($response, $params);
101
			    $accessToken = $params['access_token'];
102
			}else{
103
				$this->redirect(array('controller'=>'users','action'=>'login'));
104
			}
105
		}
106
		$this->User->recursive = -1;
107
		if (!$accessToken) {
108
		    $this->redirect(array('controller'=>'users','action'=>'login'));
109
		    //$this->redirect(array('controller'=>'pages','action'=>'display','home'));
110
		}        
111
		if (isset($accessToken) && $accessToken != 'undefined') {			
112
 
113
		    $graph_url = "https://graph.facebook.com/me?access_token=" . $accessToken;
114
 
115
		    $user = json_decode(file_get_contents($graph_url));
116
			$this->log("FB user = ".print_r($user,1),'fb');
117
		    if (!empty($user)) {
118
			if (!$this->isAuthorized()) {
119
			    $id = $this->User->checkFbUser($user,$accessToken);
120
				$dbuser = $this->User->findById($id);
121
			    $this->Session->write('facebook_id',$user->id);
122
				$this->Auth->login($dbuser['User']);
123
				if($this->Auth->login()){
124
					$this->log("logged = ".print_r($this->Auth->user('id'),1)."\n",'fb');
125
				}
126
			    $next = $this->Session->read('next');
127
				$this->log("next= ".$next."\n",'fb');
128
 
129
			    if (!empty($next)) {
130
					header('Location:' . $next);
131
					exit();
132
			    }else {
133
					//$this->redirect(array('controller'=>'users','action'=>'dashboard','?userid='.$this->Auth->user('id')));
134
					header('location:users/dashboard?userid='.$this->Auth->user('id'));
135
					exit();
136
			            }
137
					//$this->redirect(array('controller'=>'users','action'=>'dashboard','?userid='.$this->Auth->user('id')));
138
					header('location:users/dashboard?userid='.$this->Auth->user('id'));
139
					exit();
140
				} else {                	
141
					//$this->redirect(array('controller'=>'users','action'=>'dashboard','?userid='.$this->Auth->user('id')));
142
					header('location:users/dashboard?userid='.$this->Auth->user('id'));
143
					exit();
144
				}
145
		    }
146
		}
147
    }
148
 
149
	public function uploadavatar(){
150
		$this->log(print_r($_FILES,1),'debug');
151
		$path = "img/avatars/";
152
		$valid_formats = array("jpg", "png", "gif", "bmp");
153
		if ($this->request->is('post')) {
154
			$name = $_FILES['photoimg']['name'];
155
			$size = $_FILES['photoimg']['size'];
156
 
157
			if(strlen($name))
158
				{
159
					list($txt, $ext) = explode(".", $name);
160
					if(in_array($ext,$valid_formats))
161
					{
162
					if($size<(1024*1024))
163
						{
164
							$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
165
							$tmp = $_FILES['photoimg']['tmp_name'];
166
							if(move_uploaded_file($tmp, $path.$actual_image_name))
167
								{
168
									echo "<img width='250' src='../".$path.$actual_image_name."'  class='preview'>";
169
									echo "<input type='hidden' id='imageUrl' name='photo_url' value='".'http://' . $_SERVER['SERVER_NAME'] . Router::url('/').$path.$actual_image_name."'></input>";
170
								}
171
							else
172
								echo __('Upload Failed');
173
						}
174
						else
175
						echo __("Maximum allowed image file size is 1 MB");					
176
						}
177
						else
178
						echo __("Invalid file format");	
179
				}
180
			else
181
				echo __("Please select image");
182
			exit;
183
		}
184
	}
185
 
186
    function checkemail() {
187
        $this->User->recursive = -1;
188
        $this->layout = 'ajax';
189
        if (!empty($this->params['url']['data']['User']['email'])) {
190
            $username = $this->params['url']['data']['User']['email'];
191
        } elseif (!empty($this->params['url']['data']['Doctor']['email'])) {
192
            $username = $this->params['url']['data']['Doctor']['email'];
193
        } else {
194
            $result = false;
195
            $this->set('result', $result);
196
        }
197
        if ($this->Auth->user('id') != null) {
198
            $conditions = array('User.email' => $username, 'User.id !=' => $this->Auth->user('id'));
199
        } else {
200
            $conditions = array('User.email' => $username);
201
        }
202
        $count = $this->User->find('count', array('conditions' => $conditions));
203
        if ($count > 0) {
204
            $result = false;
205
        } else {
206
            $result = true;
207
        }
208
        $this->set('result', $result);
209
    }
210
 
211
/**
212
 * view method
213
 *
214
 * @throws NotFoundException
215
 * @param string $id
216
 * @return void
217
 */
218
	public function view($id = null) {
219
		$this->response->type('json');
220
		$this->layout = 'ajax';
221
		$callback = $this->request->query('callback');
222
		$this->User->id = $id;
223
		if (!$this->User->exists()) {
224
			throw new NotFoundException(__('Invalid user'));
225
		}
226
		$user = $this->User->find('first', array('conditions' =>array('id' => $id),'recursive'=>-1));
227
		$result = array('user'=>$user);
228
		$this->set(array(
229
		    'result' => $result,
230
		    'callback' => $callback,
231
		    '_serialize' => array('result')
232
		));
13736 anikendra 233
		$this->render('/Elements/json');
13532 anikendra 234
	}
235
/**
236
 * edit method
237
 *
238
 * @throws NotFoundException
239
 * @param string $id
240
 * @return void
241
 */
242
 
13736 anikendra 243
    public function edit($id = null) {
244
    	$this->response->type('json');
245
		$this->layout = 'ajax';
13532 anikendra 246
        $this->User->recursive = -1;
13736 anikendra 247
        if (!$this->User->exists($id)) {
248
			$result = array('success'=>false,'message'=>'Invalid user');
249
		} else {
250
	        if ($this->request->is('post') || $this->request->is('put')) {
251
	            if ($this->User->save($this->request->data)) {
252
	                $result = array('success'=>true,'message'=>'Your profile has been saved');
253
	            } else {
254
	                $result = array('success'=>false,'message'=> 'The user could not be saved. Please, try again.');
255
	            }
256
	        }  
257
	    }
258
        $this->set(array(
259
		    'result' => $result,
260
		    '_serialize' => array('result')
261
		)); 
262
		$this->render('/Elements/json');      
13532 anikendra 263
    }
264
 
265
/**
266
 * admin_index method
267
 *
268
 * @return void
269
 */
270
	public function admin_index() {
271
		$this->User->recursive = 0;
272
		$users = $this->paginate();
273
		$groups = $this->User->Group->find('list');
274
		$this->set(compact('groups','users'));
275
	}
276
 
277
/**
278
 * admin_view method
279
 *
280
 * @throws NotFoundException
281
 * @param string $id
282
 * @return void
283
 */
284
	public function admin_view($id = null) {
285
		$this->User->id = $id;
286
		if (!$this->User->exists()) {
287
			throw new NotFoundException(__('Invalid user'));
288
		}
289
		$this->set('user', $this->User->read(null, $id));
290
	}
291
 
292
/**
293
 * admin_add method
294
 *
295
 * @return void
296
 */
297
	public function admin_add() {
298
		if ($this->request->is('post')) {
299
			$this->User->create();
300
			if ($this->User->save($this->request->data)) {
301
				$this->Session->setFlash(__('The user has been saved'));
302
				$this->redirect(array('action' => 'index'));
303
			} else {
304
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
305
			}
306
		}
307
		$groups = $this->User->Group->find('list');
308
		$this->set(compact('groups'));
309
	}
310
 
311
/**
312
 * admin_edit method
313
 *
314
 * @throws NotFoundException
315
 * @param string $id
316
 * @return void
317
 */
318
	public function admin_edit($id = null) {
319
		$this->User->id = $id;
320
		if (!$this->User->exists()) {
321
			throw new NotFoundException(__('Invalid user'));
322
		}
323
		if ($this->request->is('post') || $this->request->is('put')) {
324
			if ($this->User->save($this->request->data)) {
325
				$this->Session->setFlash(__('The user has been saved'));
326
				$this->redirect(array('action' => 'index'));
327
			} else {
328
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
329
			}
330
		} else {
331
			$this->request->data = $this->User->read(null, $id);
332
		}
333
		$groups = $this->User->Group->find('list');
334
		$this->set(compact('groups'));
335
	}
336
 
337
/**
338
 * admin_delete method
339
 *
340
 * @throws MethodNotAllowedException
341
 * @throws NotFoundException
342
 * @param string $id
343
 * @return void
344
 */
345
	public function admin_delete($id = null) {
346
		if (!$this->request->is('post')) {
347
			throw new MethodNotAllowedException();
348
		}
349
		$this->User->id = $id;
350
		if (!$this->User->exists()) {
351
			throw new NotFoundException(__('Invalid user'));
352
		}
353
		if ($this->User->delete()) {
354
			$this->Session->setFlash(__('User deleted'));
355
			$this->redirect(array('action' => 'index'));
356
		}
357
		$this->Session->setFlash(__('User was not deleted'));
358
		$this->redirect(array('action' => 'index'));
359
	}
360
 
361
	public function dashboard() {
362
		App::uses('CakeTime', 'Utility');
363
		$user = $this->User->read(null,$this->Auth->user('id'));
364
		$this->set('user',$user);
365
		$this->set('title_for_layout', "Dashboard");
366
	}
367
 
368
/**
369
* user authentication funtions
370
*/
371
	public function register() {
372
		$referrer = $this->request->query('referrer');
373
		$this->log('referrer = '.$referrer);
374
		$this->Cookie->write('referrer',$referrer);
375
		$this->layout = 'auth';
376
		$this->set('title_for_layout','Register your free letushaggle.com account');
377
		$this->SignMeUp->register();
378
	}
379
 
380
	public function activate() {
13714 anikendra 381
		$this->layout = 'auth';
13532 anikendra 382
		$this->set('title_for_layout','Activate your copublish.in account');
13714 anikendra 383
		$this->SignMeUp->activate();
13532 anikendra 384
	}
385
 
386
	public function forgotten_password() {
13714 anikendra 387
		$this->layout = 'auth';
13532 anikendra 388
		$this->set('title_for_layout','Reset your copublish.in password');
13714 anikendra 389
		$this->SignMeUp->forgottenPassword();
13532 anikendra 390
	}	
391
 
392
	public function login() {
393
		$this->layout = 'auth';
394
		if (!empty($this->data['User']['next'])) {
395
		    $next = $this->data['User']['next'];
396
		    $this->Session->write('next', $next);
397
		} elseif (!empty($this->params['url']['next'])) {
398
		    $next = $this->params['url']['next'];
399
		    $this->Session->write('next', $next);
400
		}
401
		if ($this->Auth->login()) {
402
		    $next = $this->Session->read('next');
403
		    if (!empty($next)) {
404
				header('location:' . $next);
405
				exit();
406
		    }
13736 anikendra 407
		    $user = $this->User->read(null, $this->Auth->User('id'));		 
13532 anikendra 408
		    if ($user['User']['group_id'] == 2) {
409
				$this->redirect(array('controller' => 'administration', 'action' => 'dashboard'));
410
				exit();
411
		    }
412
			if ($user['User']['group_id'] == 1) {
13736 anikendra 413
				$this->redirect(array('controller' => 'deals', 'action' => 'mine?user_id='.$user['User']['id']));
13532 anikendra 414
				exit();
415
			}
416
		} else {
417
		    if (!empty($this->data)) {                
13736 anikendra 418
				$this->Session->setFlash(__('Invalid username or password'),'error_message');
13532 anikendra 419
		    }
420
		}
421
		$this->set('title_for_layout', "Login to letushaggle.com");
422
	}
423
 
424
	public function logout() {
425
		$this->Session->setFlash(__('You have been logged out now'),'success_message');
426
		$this->redirect($this->Auth->logout());
427
	}
428
 
429
	public function updatetimezone() {
430
		$this->layout = 'ajax';		
431
		$timezone = $_GET['tz'];
432
		Configure::load('constant');
433
		$timeZones = Configure::read("timeZones");
434
		$query = "UPDATE users SET user_timezone = $timezone WHERE id = ".$this->Auth->User('id');
435
		if($this->User->query($query)){
436
			$result = array('success' => true,'tz' => $timeZones[$timezone]);
437
		}else{
438
			$result = array('success' => false);
439
		}
440
		$this->set('result',$result);
441
	}
442
 
13736 anikendra 443
	public function verifymobile() {
444
		$this->layout = 'innerpages';
445
	}
13532 anikendra 446
 
13736 anikendra 447
	public function getdial2verifynumber() {
448
		$TelNumber=substr($this->request->data("phone_number"),-10);
449
 
450
	  // Replace with your Dial2Verify API Passkey generated using ( http://kb.dial2verify.in/?q=5 )
451
	    $API_KEY = Configure::read("dial2verifyapikey");
452
 
453
	  	//Get API Image Response
454
	    $url ="http://engine.dial2verify.in/Integ/API.dvf?mobile=$TelNumber&passkey=$API_KEY&notify=http://engine.dial2verify.in/Integ/CatchAll.dvf&e-notify=support@dial2verify.in&out=JSON&cn=IN";
455
	    // $json=file_get_contents($APIUrl);
456
	    $json = json_encode($this->make_request($url,null));
457
	  	$this->response->type('json');
458
		$this->layout = 'ajax';
459
	    echo($json);die;
460
	}
461
 
462
	public function getdial2verifystatus() { 
463
 
464
		$SID=$_REQUEST["SID"];
465
 
466
	    $json = array();
467
		$VerificationCall="http://engine.dial2verify.in/Integ/UserLayer/DataFeed_APIV2.dvf?SID=$SID";
468
 
469
	   // Make a call to Dial2Verify API & Parse The JSON Response
470
		// $RequestPayload=json_decode(file_get_contents($VerificationCall),true);
471
		$RequestPayload = $this->make_request($VerificationCall,null);
472
		// $RequestPayload = json_decode($response,true);
473
 
474
		$VerifStatus=$RequestPayload["VerificationStatus"];
475
 
476
		$json["VerificationStatus"]=$VerifStatus;
477
 
478
	   	$this->response->type('json');
479
		$this->layout = 'ajax';
480
	    echo(json_encode($json));die;
481
	}
482
 
483
	public function skipmobileverification() {
484
		$this->Session->write('skipmobileverification',1);
13739 anikendra 485
		$this->redirect(array('controller'=>'store_products','action'=>'mine',"?" => array( "user_id" => $this->Auth->User('id'))));
13736 anikendra 486
	}
487
 
488
	public function reauthenticate($userId) {
489
		$user = $this->User->read(null,$userId);
490
		$this->Auth->login($user['User']);
491
		$json = array('success'=>true);
492
		$this->response->type('json');
493
		$this->layout = 'ajax';
13739 anikendra 494
		    echo(json_encode($json));die;
13736 anikendra 495
	}
13739 anikendra 496
}