Subversion Repositories SmartDukaan

Rev

Rev 13532 | Rev 13736 | 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');
16
        $this->Auth->allow(array('migrate','login', 'forgotten_password', 'register', 'activate', 'checkemail','checkfbuser','registertwitteruser')); 
17
	$this->Cookie->name = 'instafolow';
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;
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
 * index method
201
 *
202
 * @return void
203
 */
204
/*
205
	public function index() {
206
		$this->User->recursive = 0;
207
		$this->set('users', $this->paginate());
208
	}
209
*/
210
/**
211
 * view method
212
 *
213
 * @throws NotFoundException
214
 * @param string $id
215
 * @return void
216
 */
217
	public function view($id = null) {
218
		$this->response->type('json');
219
		$this->layout = 'ajax';
220
		$callback = $this->request->query('callback');
221
		$this->User->id = $id;
222
		if (!$this->User->exists()) {
223
			throw new NotFoundException(__('Invalid user'));
224
		}
225
		$user = $this->User->find('first', array('conditions' =>array('id' => $id),'recursive'=>-1));
226
		$result = array('user'=>$user);
227
		$this->set(array(
228
		    'result' => $result,
229
		    'callback' => $callback,
230
		    '_serialize' => array('result')
231
		));
232
	}
233
 
234
/**
235
 * add method
236
 *
237
 * @return void
238
 */
239
/*
240
	public function add() {
241
		if ($this->request->is('post')) {
242
			$this->User->create();
243
			if ($this->User->save($this->request->data)) {
244
				$this->Session->setFlash(__('The user has been saved'));
245
				$this->redirect(array('action' => 'index'));
246
			} else {
247
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
248
			}
249
		}
250
		$groups = $this->User->Group->find('list');
251
		$this->set(compact('groups'));
252
	}
253
*/
254
/**
255
 * edit method
256
 *
257
 * @throws NotFoundException
258
 * @param string $id
259
 * @return void
260
 */
261
 
262
    public function edit() {
263
        $this->User->recursive = -1;
264
        $id = $this->Auth->User('id');
265
        if ($this->request->is('post') || $this->request->is('put')) {
266
            if ($this->User->save($this->request->data)) {
267
                $this->Session->setFlash(__('Your profile has been saved', true),'success_message');
268
                $this->redirect(array('action' => 'dashboard'));
269
            } else {
270
                $this->Session->setFlash(__('The user could not be saved. <br>'.print_r($this->User->validationErrors).' <br>Please, try again.', true),'error_message');
271
            }
272
        } else {        
273
            $this->request->data = $this->User->read(null, $id);
274
        }
275
        $this->loadModel('Country');
276
        $this->Country->recursive = -1;
277
        $fields = array('country', 'FIPS104');
278
        $result = $this->Country->find('all', array('fields' => $fields));
279
        $countries = array();
280
        foreach ($result AS $country) {
281
            $countries[$country['Country']['FIPS104']] = $country['Country']['country'];
282
        }        
283
        Configure::load('constant');
284
		$this->set('timeZones' , Configure::read("timeZones"));
285
        $this->set(compact('countries','id'));
286
        $this->set('title_for_layout', "Update Profile");
287
    }
288
 
289
/**
290
 * delete method
291
 *
292
 * @throws MethodNotAllowedException
293
 * @throws NotFoundException
294
 * @param string $id
295
 * @return void
296
 */
297
/*
298
	public function delete($id = null) {
299
		if (!$this->request->is('post')) {
300
			throw new MethodNotAllowedException();
301
		}
302
		$this->User->id = $id;
303
		if (!$this->User->exists()) {
304
			throw new NotFoundException(__('Invalid user'));
305
		}
306
		if ($this->User->delete()) {
307
			$this->Session->setFlash(__('User deleted'));
308
			$this->redirect(array('action' => 'index'));
309
		}
310
		$this->Session->setFlash(__('User was not deleted'));
311
		$this->redirect(array('action' => 'index'));
312
	}
313
*/
314
/**
315
 * admin_index method
316
 *
317
 * @return void
318
 */
319
	public function admin_index() {
320
		$this->User->recursive = 0;
321
		$users = $this->paginate();
322
		$groups = $this->User->Group->find('list');
323
		$this->set(compact('groups','users'));
324
	}
325
 
326
/**
327
 * admin_view method
328
 *
329
 * @throws NotFoundException
330
 * @param string $id
331
 * @return void
332
 */
333
	public function admin_view($id = null) {
334
		$this->User->id = $id;
335
		if (!$this->User->exists()) {
336
			throw new NotFoundException(__('Invalid user'));
337
		}
338
		$this->set('user', $this->User->read(null, $id));
339
	}
340
 
341
/**
342
 * admin_add method
343
 *
344
 * @return void
345
 */
346
	public function admin_add() {
347
		if ($this->request->is('post')) {
348
			$this->User->create();
349
			if ($this->User->save($this->request->data)) {
350
				$this->Session->setFlash(__('The user has been saved'));
351
				$this->redirect(array('action' => 'index'));
352
			} else {
353
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
354
			}
355
		}
356
		$groups = $this->User->Group->find('list');
357
		$this->set(compact('groups'));
358
	}
359
 
360
/**
361
 * admin_edit method
362
 *
363
 * @throws NotFoundException
364
 * @param string $id
365
 * @return void
366
 */
367
	public function admin_edit($id = null) {
368
		$this->User->id = $id;
369
		if (!$this->User->exists()) {
370
			throw new NotFoundException(__('Invalid user'));
371
		}
372
		if ($this->request->is('post') || $this->request->is('put')) {
373
			if ($this->User->save($this->request->data)) {
374
				$this->Session->setFlash(__('The user has been saved'));
375
				$this->redirect(array('action' => 'index'));
376
			} else {
377
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
378
			}
379
		} else {
380
			$this->request->data = $this->User->read(null, $id);
381
		}
382
		$groups = $this->User->Group->find('list');
383
		$this->set(compact('groups'));
384
	}
385
 
386
/**
387
 * admin_delete method
388
 *
389
 * @throws MethodNotAllowedException
390
 * @throws NotFoundException
391
 * @param string $id
392
 * @return void
393
 */
394
	public function admin_delete($id = null) {
395
		if (!$this->request->is('post')) {
396
			throw new MethodNotAllowedException();
397
		}
398
		$this->User->id = $id;
399
		if (!$this->User->exists()) {
400
			throw new NotFoundException(__('Invalid user'));
401
		}
402
		if ($this->User->delete()) {
403
			$this->Session->setFlash(__('User deleted'));
404
			$this->redirect(array('action' => 'index'));
405
		}
406
		$this->Session->setFlash(__('User was not deleted'));
407
		$this->redirect(array('action' => 'index'));
408
	}
409
 
410
	public function dashboard() {
411
		App::uses('CakeTime', 'Utility');
412
		$user = $this->User->read(null,$this->Auth->user('id'));
413
		$this->set('user',$user);
414
		$this->set('title_for_layout', "Dashboard");
415
	}
416
 
417
/**
418
* user authentication funtions
419
*/
420
	public function register() {
421
		$referrer = $this->request->query('referrer');
422
		$this->log('referrer = '.$referrer);
423
		$this->Cookie->write('referrer',$referrer);
424
		$this->layout = 'auth';
425
		$this->set('title_for_layout','Register your free letushaggle.com account');
426
		$this->SignMeUp->register();
427
	}
428
 
429
	public function activate() {
13714 anikendra 430
		$this->layout = 'auth';
13532 anikendra 431
		$this->set('title_for_layout','Activate your copublish.in account');
13714 anikendra 432
		$this->SignMeUp->activate();
13532 anikendra 433
	}
434
 
435
	public function forgotten_password() {
13714 anikendra 436
		$this->layout = 'auth';
13532 anikendra 437
		$this->set('title_for_layout','Reset your copublish.in password');
13714 anikendra 438
		$this->SignMeUp->forgottenPassword();
13532 anikendra 439
	}	
440
 
441
	public function login() {
442
		$this->layout = 'auth';
443
		if (!empty($this->data['User']['next'])) {
444
		    $next = $this->data['User']['next'];
445
		    $this->Session->write('next', $next);
446
		} elseif (!empty($this->params['url']['next'])) {
447
		    $next = $this->params['url']['next'];
448
		    $this->Session->write('next', $next);
449
		}
450
		if ($this->Auth->login()) {
451
		    $next = $this->Session->read('next');
452
		    if (!empty($next)) {
453
				header('location:' . $next);
454
				exit();
455
		    }
456
		    $user = $this->User->read(null, $this->Auth->User('id'));
457
		  //   if ($user['User']['active'] == 0) {
458
				// $this->Session->setFlash(__('Your account has not been activated yet. Contact the administrator.', true),'error_message');
459
				// $this->redirect($this->Auth->logout());
460
				// exit();
461
		  //   } 
462
		    if ($user['User']['group_id'] == 2) {
463
				$this->redirect(array('controller' => 'administration', 'action' => 'dashboard'));
464
				exit();
465
		    }
466
			if ($user['User']['group_id'] == 1) {
467
				header('location:/users/dashboard?userid='.$this->Auth->user('id'));
468
				exit();
469
			}
470
		} else {
471
		    if (!empty($this->data)) {                
472
			$this->Session->setFlash(__('Invalid username or password'),'error_message');
473
		    }
474
		}
475
		$this->set('title_for_layout', "Login to letushaggle.com");
476
	}
477
 
478
	public function logout() {
479
		$this->Session->setFlash(__('You have been logged out now'),'success_message');
480
		$this->redirect($this->Auth->logout());
481
	}
482
 
483
	public function updatetimezone() {
484
		$this->layout = 'ajax';		
485
		$timezone = $_GET['tz'];
486
		Configure::load('constant');
487
		$timeZones = Configure::read("timeZones");
488
		$query = "UPDATE users SET user_timezone = $timezone WHERE id = ".$this->Auth->User('id');
489
		if($this->User->query($query)){
490
			$result = array('success' => true,'tz' => $timeZones[$timezone]);
491
		}else{
492
			$result = array('success' => false);
493
		}
494
		$this->set('result',$result);
495
	}
496
 
497
	/*public function migrate() {
498
		$users = $this->User->Find('all',array('fields'=>array('id','first_name','last_name'),'recursive'=>-1));
499
		foreach($users AS $user){
500
			$data = array();
501
	        $data['UserAnswerProfile']['user_id'] = $user['User']['id'];
502
	        $data['UserAnswerProfile']['alias'] = $user['User']['first_name']. ' '.substr($user['User']['last_name'], 0, 1);
503
	        $this->User->UserAnswerProfile->create();
504
	        $this->User->UserAnswerProfile->save($data);
505
 
506
	        $data['UserStatistic']['user_id'] = $user['User']['id'];
507
	        $this->User->UserStatistic->create();
508
	        $this->User->UserStatistic->save($data);
509
		}
510
	}*/
511
}