Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 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
			$this->controller->{$model}->set($this->data);
103
 
104
			if (CakePlugin::loaded('Mongodb')) {
105
				$this->controller->{$model}->Behaviors->attach('Mongodb.SqlCompatible');
106
			}
107
 
108
			if ($this->controller->{$model}->validates()) {
109
 
110
				if (!empty($activation_field)) {
111
					$this->data[$model][$activation_field] = $this->controller->{$model}->generateActivationCode($this->data);
112
				} elseif (!empty($useractive_field)) {
113
					$this->data[$model][$useractive_field] = true;
114
				}
115
 
116
				if ($this->controller->{$model}->save($this->data, false)) {
117
					if(isset($invite_id)){
118
						$this->Cookie->delete('invite_id');
119
					}					
120
					if(isset($referrer)){
121
						$this->Cookie->delete('referrer');
122
					}
123
					//If an activation field is supplied send out an email
124
					if (!empty($activation_field)) {
125
						$this->__sendActivationEmail($this->data[$model]);
126
						if (!$this->controller->request->is('ajax')) {
127
							$this->controller->redirect(array('action' => 'activate'));
128
						} else {
129
							return true;
130
						}
131
					} else {
132
						$this->__sendWelcomeEmail($this->data[$model]);
133
					}
134
					if (!$this->controller->request->is('ajax')) {
135
						$this->controller->redirect($this->Auth->loginAction);
136
					} else {
137
						return true;
138
					}
139
				}
140
			} else {
141
				unset($this->controller->request->data[$model]['password1']);
142
				unset($this->controller->request->data[$model]['password2']);
143
			}
144
		}
145
	}
146
 
147
	private function __isLoggedIn() {
148
		if ($this->Auth->user()) {
149
			if (!$this->controller->request->is('ajax')) {
150
				$this->controller->redirect($this->Auth->loginRedirect);
151
			}
152
		}
153
	}
154
 
155
	protected function __sendActivationEmail($userData) {
156
		$this->__setUpEmailParams($userData);
157
		$this->__parseEmailSubject('activation', $userData);
158
		if ($this->signMeUpEmailer->template($this->settings['activation_template'], $this->settings['email_layout'])) {
159
			if ($this->signMeUpEmailer->send()) {
160
				return true;
161
			}
162
		}
163
	}
164
 
165
	protected function __sendWelcomeEmail($userData) {
166
		$this->__setUpEmailParams($userData);
167
		$this->__parseEmailSubject('welcome', $userData);
168
		if ($this->signMeUpEmailer->template($this->settings['welcome_template'], $this->settings['email_layout'])) {
169
			if ($this->signMeUpEmailer->send()) {
170
				return true;
171
			}
172
		}
173
	}
174
 
175
	public function activate() {
176
		$this->__isLoggedIn();
177
		extract($this->settings);
178
		//If there is no activation field specified, don't bother with activation
179
		if (!empty($activation_field)) {
180
			//Test for an activation code in the parameters
181
			if (!empty($this->controller->request->params['named'][$activation_field])) {
182
				$activation_code = $this->controller->request->params['named'][$activation_field];
183
			}
184
 
185
			//If there is an activation code supplied, either in _POST or _GET
186
			if (!empty($activation_code) || !empty($this->data)) {
187
				$model = $this->controller->modelClass;
188
				$this->controller->loadModel($model);
189
 
190
				if (!empty($this->data)) {
191
					$activation_code = $this->data[$model][$activation_field];
192
				}
193
 
194
				$inactive_user = $this->controller->{$model}->find('first', array('conditions' => array($activation_field => $activation_code), 'recursive' => -1));
195
				if (!empty($inactive_user)) {
196
					$this->controller->{$model}->id = $inactive_user[$model][$this->controller->{$model}->primaryKey];
197
					if (!empty($useractive_field)) {
198
						$data[$model][$useractive_field] = true;
199
					}
200
					$data[$model][$activation_field] = null;
201
					if ($this->controller->{$model}->save($data)) {
202
						$this->__sendWelcomeEmail($inactive_user['User']);
203
						if ($login_after_activation === true) {
204
							$this->Auth->login($inactive_user);
205
						}
206
						if (!$this->controller->request->is('ajax')) {
207
							$user = '';
208
							if (!empty($inactive_user[$model][$username_field])) {
209
								$user = ' '.$inactive_user[$model][$username_field];
210
							}
211
							$this->Session->setFlash('Thank you'.$user.', your account is now active');
212
							if ($login_after_activation === true) {
213
								$this->controller->redirect($this->Auth->loginRedirect);
214
							} else {
215
								$this->controller->redirect($this->Auth->loginAction);
216
							}
217
						} else {
218
							return true;
219
						}
220
					}
221
				} else {
222
					$this->Session->setFlash('Sorry, that code is incorrect.');
223
				}
224
			}
225
		}
226
	}
227
 
228
	public function forgottenPassword() {
229
		extract($this->settings);
230
		$model = $this->controller->modelClass;
231
		if (!empty($this->data[$model])) {
232
			$data = $this->data[$model];
233
		}
234
		//User has code to reset their password
235
		if (!empty($this->controller->request->params[$password_reset_field])) {
236
			$this->__generateNewPassword($model);
237
		} elseif (!empty($password_reset_field) && !empty($data['email'])) {
238
			$this->__requestNewPassword($data, $model);
239
		}
240
	}
241
 
242
	private function __generateNewPassword($model = '') {
243
		extract($this->settings);
244
		$user = $this->controller->{$model}->find('first', array(
245
			'conditions' => array($password_reset_field => $this->controller->request->params[$password_reset_field]),
246
			'recursive' => -1
247
		));
248
 
249
		if (!empty($user)) {
250
			$password = substr(Security::hash(String::uuid(), null, true), 0, 8);
251
			$user[$model][$password_field] = Security::hash($password, null, true);
252
			$user[$model][$password_reset_field] = null;
253
			$this->controller->set(compact('password'));
254
			if ($this->controller->{$model}->save($user) && $this->__sendNewPassword($user[$model])) {
255
				if (!$this->controller->request->is('ajax')) {
256
					$this->Session->setFlash('Thank you '.$user[$model][$username_field].', your new password has been emailed to you.');
257
					$this->controller->redirect($this->Auth->loginAction);
258
				} else {
259
					return true;
260
				}
261
			}
262
		}
263
	}
264
 
265
	private function __sendNewPassword($user = array()) {
266
		$this->__setUpEmailParams($user);
267
		if ($this->signMeUpEmailer->template($this->settings['new_password_template'], $this->settings['email_layout'])) {
268
			$this->signMeUpEmailer->subject = $this->setting['new_password_subject'];
269
			if ($this->signMeUpEmailer->send()) {
270
				return true;
271
			}
272
		}
273
	}
274
 
275
	private function __requestNewPassword($data = array(), $model = '') {
276
		extract($this->settings);
277
		$this->controller->loadModel($model);
278
		$user = $this->controller->{$model}->find('first', array('conditions' => array('email' => $data['email']), 'recursive' => -1));
279
		if (!empty($user)) {
280
			$user[$model][$password_reset_field] = md5(String::uuid());
281
 
282
			if ($this->controller->{$model}->save($user) && $this->__sendForgottenPassword($user[$model])) {
283
				if (!$this->controller->request->is('ajax')) {
284
					$this->Session->setFlash('Thank you. A password recovery email has now been sent to '.$data['email']);
285
					$this->controller->redirect($this->Auth->loginAction);
286
				} else {
287
					return true;
288
				}
289
			}else{
290
				$this->log(print_r($this->controller->{$model}->validationErrors,1),'fb');
291
			}
292
		} else {
293
			$this->controller->{$model}->invalidate('email', 'No user found with email: '.$data['email']);
294
		}
295
	}
296
 
297
	private function __sendForgottenPassword($user = array()) {
298
		$this->__setUpEmailParams($user);
299
		if ($this->signMeUpEmailer->template($this->settings['password_reset_template'], $this->settings['email_layout'])) {
300
			$this->signMeUpEmailer->subject = $this->settings['password_reset_subject'];
301
			if ($this->signMeUpEmailer->send()) {
302
				return true;
303
			}
304
		}
305
	}
306
 
307
}