Subversion Repositories SmartDukaan

Rev

Rev 17202 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
 
3
App::uses('CakeRequest', 'Network');
4
App::uses('CakeEmail', 'Network/Email');
5
 
6
class SignMeUpComponent extends Component {
7
 
8
	public $components = array('Session', 'Auth', 'RequestHandler', 'Cookie' => array('name' => 'CoPublisher'));
9
	public $defaults = array(
10
		'sendAs' => 'html',//html
11
		'username_field' => 'username',
12
		'email_field' => 'email',
13
		'activation_field' => 'activation_code',
14
		'useractive_field' => 'active',
15
		'login_after_activation' => false,
16
		'welcome_subject' => 'Welcome',
17
		'activation_subject' => 'Please Activate Your Account',
18
		'password_reset_field' => 'password_reset',
19
		'username_field' => 'username',
20
		'referrer_field' => 'referrer',
21
		'invite_id_field' => 'invite_id',
22
		'email_field' => 'email',
23
		'email_layout' => 'default',
24
 		'password_field' => 'password',
25
		'activation_template' => 'activate',
26
		'welcome_template' => 'welcome',
27
		'password_reset_template' => 'forgotten_password',
28
		'password_reset_subject' => 'Password Reset Request',
29
		'new_password_template' => 'recovered_password',
30
		'new_password_subject' => 'Your new Password'
31
	);
32
	public $helpers = array('Form', 'Html');
33
	public $name = 'SignMeUp';
34
	public $uses = array('SignMeUp');
35
 
36
	function __construct(ComponentCollection $collection, $settings = array()) {
37
		parent::__construct($collection, $settings);
38
		$this->settings = $settings;
39
	}
40
 
41
	public function initialize(&$controller) {		
42
		$this->__loadConfig();
43
		$settings = array_merge($this->settings, Configure::read('SignMeUp'));
44
		$this->settings = array_merge($this->defaults, $settings);
45
		$this->requestHandler = new CakeRequest();
46
		$this->signMeUpEmailer = new CakeEmail('signMeUp');
47
		$this->data = $this->requestHandler->data;		
48
		$this->controller = $controller;
49
	}
50
 
51
	private function __loadConfig() {
52
		if (Configure::load('sign_me_up', 'default', false) === false) {
53
			die('Could not load sign me up config');
54
		}
55
	}
56
 
57
	private function __setUpEmailParams($user) {
58
		$this->__loadConfig();
59
		extract($this->settings);
60
		if (empty($user[$username_field])) {
61
			$this->signMeUpEmailer->to($user[$email_field], $user[$email_field]);
62
		} else {
63
			$this->signMeUpEmailer->to($user[$email_field], $user[$username_field]);
64
		}
65
		$this->signMeUpEmailer->viewVars(compact('user'));
66
	}
67
 
68
	private function __parseEmailSubject($action = '', $user = array()) {
69
		$subject = $this->settings[$action.'_subject'];
70
		preg_match_all('/%(\w+?)%/', $subject, $matches);
71
		$foundMatch = false;
72
		foreach ($matches[1] as $match) {
73
			if (!empty($user[$match])) {
74
				$foundMatch = true;
75
				$this->signMeUpEmailer->subject(str_replace('%'.$match.'%', $user[$match], $subject));
76
			}
77
		}
78
 
79
		if ($foundMatch === false) {
80
			$this->signMeUpEmailer->subject($subject);
81
		}
82
	}
83
 
84
	public function register() {
85
		$this->__isLoggedIn();		
86
		if (!empty($this->data)) {			
87
			extract($this->settings);
88
			$model = $this->controller->modelClass;
89
			$invite_id = $this->Cookie->read('invite_id');
90
			$this->log("invite_id set via cookie - ".$invite_id,'debug');
91
			if(isset($invite_id)){
92
				$this->data[$model][$invite_id_field] = $invite_id;
93
			}
94
			$referrer = $this->Cookie->read('referrer');
95
			$this->log("referrer set via cookie - ".$referrer,'debug');
96
			if(isset($referrer)){
97
				$this->data[$model][$referrer_field] = $referrer;
98
			}
99
			$this->data[$model][$username_field] = $this->data[$model][$email_field];
100
			$this->log($this->data,'debug');
101
			$this->controller->loadModel($model);
102
			// error_log(print_r($this->data,1));
103
			$this->controller->{$model}->set($this->data);
104
 
105
			if (CakePlugin::loaded('Mongodb')) {
106
				$this->controller->{$model}->Behaviors->attach('Mongodb.SqlCompatible');
107
			}
108
 
109
			if ($this->controller->{$model}->validates()) {
110
 
111
				if (!empty($activation_field)) {
112
					$this->data[$model][$activation_field] = $this->controller->{$model}->generateActivationCode($this->data);
113
				} elseif (!empty($useractive_field)) {
114
					$this->data[$model][$useractive_field] = true;
115
				}
116
 
117
				if ($this->controller->{$model}->save($this->data, false)) {
118
					if(isset($invite_id)){
119
						$this->Cookie->delete('invite_id');
120
					}					
121
					if(isset($referrer)){
122
						$this->Cookie->delete('referrer');
123
					}
124
					//If an activation field is supplied send out an email
125
					if (!empty($activation_field)) {
126
						$this->__sendActivationEmail($this->data[$model]);
127
						if (!$this->controller->request->is('ajax')) {
128
							$this->controller->redirect(array('action' => 'activate'));
129
						} else {
130
							return true;
131
						}
132
					} else {
133
						$this->__sendWelcomeEmail($this->data[$model]);
134
					}
135
					if (!$this->controller->request->is('ajax')) {
136
						$this->controller->redirect($this->Auth->loginAction);
137
					} else {
138
						return true;
139
					}
140
				}
141
			} else {
142
				print_r($this->controller->{$model}->validationErrors);die;
143
				unset($this->controller->request->data[$model]['password1']);
144
				unset($this->controller->request->data[$model]['password2']);
145
			}
146
		}
147
	}
148
 
149
	private function __isLoggedIn() {
150
		if ($this->Auth->user()) {
151
			if (!$this->controller->request->is('ajax')) {
152
				$this->controller->redirect($this->Auth->loginRedirect);
153
			}
154
		}
155
	}
156
 
157
	protected function __sendActivationEmail($userData) {
158
		$this->__setUpEmailParams($userData);
159
		$this->__parseEmailSubject('activation', $userData);
160
		if ($this->signMeUpEmailer->template($this->settings['activation_template'], $this->settings['email_layout'])) {
161
			if ($this->signMeUpEmailer->send()) {
162
				return true;
163
			}
164
		}
165
	}
166
 
167
	protected function __sendWelcomeEmail($userData) {
168
		$this->__setUpEmailParams($userData);
169
		$this->__parseEmailSubject('welcome', $userData);
170
		if ($this->signMeUpEmailer->template($this->settings['welcome_template'], $this->settings['email_layout'])) {
171
			if ($this->signMeUpEmailer->send()) {
172
				return true;
173
			}
174
		}
175
	}
176
 
177
	public function activate() {
178
		$this->__isLoggedIn();
179
		extract($this->settings);
180
		//If there is no activation field specified, don't bother with activation
181
		if (!empty($activation_field)) {
182
			//Test for an activation code in the parameters
183
			if (!empty($this->controller->request->params['named'][$activation_field])) {
184
				$activation_code = $this->controller->request->params['named'][$activation_field];
185
			}
186
 
187
			//If there is an activation code supplied, either in _POST or _GET
188
			if (!empty($activation_code) || !empty($this->data)) {
189
				$model = $this->controller->modelClass;
190
				$this->controller->loadModel($model);
191
 
192
				if (!empty($this->data)) {
193
					$activation_code = $this->data[$model][$activation_field];
194
				}
195
 
196
				$inactive_user = $this->controller->{$model}->find('first', array('conditions' => array($activation_field => $activation_code), 'recursive' => -1));
197
				if (!empty($inactive_user)) {
198
					$this->controller->{$model}->id = $inactive_user[$model][$this->controller->{$model}->primaryKey];
199
					if (!empty($useractive_field)) {
200
						$data[$model][$useractive_field] = true;
201
					}
202
					$data[$model][$activation_field] = null;
203
					if ($this->controller->{$model}->save($data)) {
204
						$this->__sendWelcomeEmail($inactive_user['User']);
205
						if ($login_after_activation === true) {
206
							$this->Auth->login($inactive_user);
207
						}
208
						if (!$this->controller->request->is('ajax')) {
209
							$user = '';
210
							if (!empty($inactive_user[$model][$username_field])) {
211
								$user = ' '.$inactive_user[$model][$username_field];
212
							}
213
							$this->Session->setFlash('Thank you'.$user.', your account is now active');
214
							if ($login_after_activation === true) {
215
								$this->controller->redirect($this->Auth->loginRedirect);
216
							} else {
217
								$this->controller->redirect($this->Auth->loginAction);
218
							}
219
						} else {
220
							return true;
221
						}
222
					}
223
				} else {
224
					$this->Session->setFlash('Sorry, that code is incorrect.');
225
				}
226
			}
227
		}
228
	}
229
 
230
	public function forgottenPassword() {
231
		extract($this->settings);
232
		$model = $this->controller->modelClass;
233
		if (!empty($this->data[$model])) {
234
			$data = $this->data[$model];
235
		}
236
		//User has code to reset their password
237
		if (!empty($this->controller->request->params[$password_reset_field])) {
238
			$this->__generateNewPassword($model);
239
		} elseif (!empty($password_reset_field) && !empty($data['email'])) {
240
			$this->__requestNewPassword($data, $model);
241
		}
242
	}
243
 
244
	private function __generateNewPassword($model = '') {
245
		extract($this->settings);
246
		$user = $this->controller->{$model}->find('first', array(
247
			'conditions' => array($password_reset_field => $this->controller->request->params[$password_reset_field]),
248
			'recursive' => -1
249
		));
250
 
251
		if (!empty($user)) {
252
			$password = substr(Security::hash(String::uuid(), null, true), 0, 8);
253
			$user[$model][$password_field] = Security::hash($password, null, true);
254
			$user[$model][$password_reset_field] = null;
255
			$this->controller->set(compact('password'));
256
			if ($this->controller->{$model}->save($user) && $this->__sendNewPassword($user[$model])) {
257
				if (!$this->controller->request->is('ajax')) {
258
					$this->Session->setFlash('Thank you '.$user[$model][$username_field].', your new password has been emailed to you.');
259
					$this->controller->redirect($this->Auth->loginAction);
260
				} else {
261
					return true;
262
				}
263
			}
264
		}
265
	}
266
 
267
	private function __sendNewPassword($user = array()) {
268
		$this->__setUpEmailParams($user);
269
		if ($this->signMeUpEmailer->template($this->settings['new_password_template'], $this->settings['email_layout'])) {
270
			$this->signMeUpEmailer->subject = $this->setting['new_password_subject'];
271
			if ($this->signMeUpEmailer->send()) {
272
				return true;
273
			}
274
		}
275
	}
276
 
277
	private function __requestNewPassword($data = array(), $model = '') {
278
		extract($this->settings);
279
		$this->controller->loadModel($model);
280
		$user = $this->controller->{$model}->find('first', array('conditions' => array('email' => $data['email']), 'recursive' => -1));
281
		if (!empty($user)) {
282
			$user[$model][$password_reset_field] = md5(String::uuid());
283
 
284
			if ($this->controller->{$model}->save($user) && $this->__sendForgottenPassword($user[$model])) {
285
				if (!$this->controller->request->is('ajax')) {
286
					$this->Session->setFlash('Thank you. A password recovery email has now been sent to '.$data['email']);
287
					$this->controller->redirect($this->Auth->loginAction);
288
				} else {
289
					return true;
290
				}
291
			}else{
292
				$this->log(print_r($this->controller->{$model}->validationErrors,1),'fb');
293
			}
294
		} else {
295
			$this->controller->{$model}->invalidate('email', 'No user found with email: '.$data['email']);
296
		}
297
	}
298
 
299
	private function __sendForgottenPassword($user = array()) {
300
		$this->__setUpEmailParams($user);
301
		if ($this->signMeUpEmailer->template($this->settings['password_reset_template'], $this->settings['email_layout'])) {
302
			$this->signMeUpEmailer->subject = $this->settings['password_reset_subject'];
303
			if ($this->signMeUpEmailer->send()) {
304
				return true;
305
			}
306
		}
307
	}
308
 
309
}