Subversion Repositories SmartDukaan

Rev

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