Subversion Repositories SmartDukaan

Rev

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