|
|
| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @file classes/notification/NotificationSettingsDAODAO.inc.php |
| 5 |
* |
| 6 |
* Copyright (c) 2003-2008 John Willinsky |
| 7 |
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING. |
| 8 |
* |
| 9 |
* @class NotificationSettingsDAO |
| 10 |
* @ingroup notification |
| 11 |
* @see Notification |
| 12 |
* |
| 13 |
* @brief Operations for retrieving and modifying user's notification settings. |
| 14 |
*/ |
| 15 |
|
| 16 |
// $Id: NotificationSettingsDAO.inc.php,v 1.19 2008/11/05 00:46:39 mcrider Exp $ |
| 17 |
|
| 18 |
class NotificationSettingsDAO extends DAO { |
| 19 |
/** |
| 20 |
* Constructor. |
| 21 |
*/ |
| 22 |
function NotificationSettingsDAO() { |
| 23 |
parent::DAO(); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Retrieve Notifications settings by user id |
| 28 |
* Returns an array of notification types that the user |
| 29 |
* does NOT want to be notified of |
| 30 |
* @param $userId int |
| 31 |
* @return array |
| 32 |
*/ |
| 33 |
function &getNotificationSettings($userId) { |
| 34 |
$application =& PKPApplication::getApplication(); |
| 35 |
$productName = $application->getName(); |
| 36 |
$context =& Request::getContext(); |
| 37 |
$contextId = $context->getId(); |
| 38 |
|
| 39 |
$notificationSettings = array(); |
| 40 |
|
| 41 |
$result =& $this->retrieve( |
| 42 |
'SELECT setting_value FROM notification_settings WHERE user_id = ? AND product = ? AND setting_name = ? AND context = ?', |
| 43 |
array((int) $userId, $productName, 'notify', (int) $contextId) |
| 44 |
); |
| 45 |
|
| 46 |
while (!$result->EOF) { |
| 47 |
$row = $result->GetRowAssoc(false); |
| 48 |
$notificationSettings[] = (int) $row['setting_value']; |
| 49 |
$result->moveNext(); |
| 50 |
} |
| 51 |
|
| 52 |
$result->Close(); |
| 53 |
unset($result); |
| 54 |
|
| 55 |
return $notificationSettings; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Retrieve Notifications email settings by user id |
| 60 |
* Returns an array of notification types that the user |
| 61 |
* DOES want to be emailed about |
| 62 |
* @param $userId int |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
function &getNotificationEmailSettings($userId) { |
| 66 |
$application =& PKPApplication::getApplication(); |
| 67 |
$productName = $application->getName(); |
| 68 |
$context =& Request::getContext(); |
| 69 |
$contextId = $context->getId(); |
| 70 |
|
| 71 |
$emailSettings = array(); |
| 72 |
|
| 73 |
$result =& $this->retrieve( |
| 74 |
'SELECT setting_value FROM notification_settings WHERE user_id = ? AND product = ? AND setting_name = ? AND context = ?', |
| 75 |
array((int) $userId, $productName, 'email', (int) $contextId) |
| 76 |
); |
| 77 |
|
| 78 |
while (!$result->EOF) { |
| 79 |
$row = $result->GetRowAssoc(false); |
| 80 |
$emailSettings[] = (int) $row['setting_value']; |
| 81 |
$result->moveNext(); |
| 82 |
} |
| 83 |
|
| 84 |
$result->Close(); |
| 85 |
unset($result); |
| 86 |
|
| 87 |
return $emailSettings; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Update a user's notification settings |
| 92 |
* @param $notificationSettings array |
| 93 |
* @param $userId int |
| 94 |
*/ |
| 95 |
function updateNotificationSettings($notificationSettings, $userId) { |
| 96 |
$application =& PKPApplication::getApplication(); |
| 97 |
$productName = $application->getName(); |
| 98 |
$context =& Request::getContext(); |
| 99 |
$contextId = $context->getId(); |
| 100 |
|
| 101 |
// Delete old settings first, then insert new settings |
| 102 |
$this->update('DELETE FROM notification_settings WHERE user_id = ? AND product = ? AND setting_name = ? AND context = ?', |
| 103 |
array((int) $userId, $productName, 'notify', (int) $contextId)); |
| 104 |
|
| 105 |
for ($i=0; $i<count($notificationSettings); $i++) { |
| 106 |
$this->update( |
| 107 |
'INSERT INTO notification_settings |
| 108 |
(setting_name, setting_value, user_id, product, context) |
| 109 |
VALUES |
| 110 |
(?, ?, ?, ?, ?)', |
| 111 |
array( |
| 112 |
"notify", |
| 113 |
$notificationSettings[$i], |
| 114 |
(int) $userId, |
| 115 |
$productName, |
| 116 |
(int) $contextId |
| 117 |
) |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Update a user's notification email settings |
| 124 |
* @param $notificationEmailSettings array |
| 125 |
* @param $userId int |
| 126 |
*/ |
| 127 |
function updateNotificationEmailSettings($emailSettings, $userId) { |
| 128 |
$application =& PKPApplication::getApplication(); |
| 129 |
$productName = $application->getName(); |
| 130 |
$context =& Request::getContext(); |
| 131 |
$contextId = $context->getId(); |
| 132 |
|
| 133 |
// Delete old settings first, then insert new settings |
| 134 |
$this->update('DELETE FROM notification_settings WHERE user_id = ? AND product = ? AND setting_name = ? AND context = ?', |
| 135 |
array($userId, $productName, 'email', $contextId)); |
| 136 |
|
| 137 |
for ($i=0; $i<count($emailSettings); $i++) { |
| 138 |
$this->update( |
| 139 |
'INSERT INTO notification_settings |
| 140 |
(setting_name, setting_value, user_id, product, context) |
| 141 |
VALUES |
| 142 |
(?, ?, ?, ?, ?)', |
| 143 |
array( |
| 144 |
"email", |
| 145 |
$emailSettings[$i], |
| 146 |
(int) $userId, |
| 147 |
$productName, |
| 148 |
(int) $contextId |
| 149 |
) |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Gets a user id by an RSS token value |
| 156 |
* @param $token int |
| 157 |
* @return int |
| 158 |
*/ |
| 159 |
function getUserIdByRSSToken($token) { |
| 160 |
$application =& PKPApplication::getApplication(); |
| 161 |
$productName = $application->getName(); |
| 162 |
$context =& Request::getContext(); |
| 163 |
$contextId = $context->getId(); |
| 164 |
|
| 165 |
$result =& $this->retrieve( |
| 166 |
'SELECT user_id FROM notification_settings WHERE setting_value = ? AND setting_name = ? AND product = ? AND context = ?', |
| 167 |
array($token, 'token', $productName, (int) $contextId) |
| 168 |
); |
| 169 |
|
| 170 |
$row = $result->GetRowAssoc(false); |
| 171 |
$userId = $row['user_id']; |
| 172 |
|
| 173 |
$result->Close(); |
| 174 |
unset($result); |
| 175 |
|
| 176 |
return $userId; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Gets an RSS token for a user id |
| 181 |
* @param $userId int |
| 182 |
* @return int |
| 183 |
*/ |
| 184 |
function getRSSTokenByUserId($userId) { |
| 185 |
$application =& PKPApplication::getApplication(); |
| 186 |
$productName = $application->getName(); |
| 187 |
$context =& Request::getContext(); |
| 188 |
$contextId = $context->getId(); |
| 189 |
|
| 190 |
$result =& $this->retrieve( |
| 191 |
'SELECT setting_value FROM notification_settings WHERE user_id = ? AND setting_name = ? AND product = ? AND context = ?', |
| 192 |
array((int) $userId, 'token', $productName, (int) $contextId) |
| 193 |
); |
| 194 |
|
| 195 |
$row = $result->GetRowAssoc(false); |
| 196 |
$userId = $row['setting_value']; |
| 197 |
|
| 198 |
$result->Close(); |
| 199 |
unset($result); |
| 200 |
|
| 201 |
return $userId; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Generates and inserts a new token for a user's RSS feed |
| 206 |
* @param $userId int |
| 207 |
* @return int |
| 208 |
*/ |
| 209 |
function insertNewRSSToken($userId) { |
| 210 |
$application =& PKPApplication::getApplication(); |
| 211 |
$productName = $application->getName(); |
| 212 |
$context =& Request::getContext(); |
| 213 |
$contextId = $context->getId(); |
| 214 |
|
| 215 |
$token = uniqid(rand()); |
| 216 |
|
| 217 |
$this->update( |
| 218 |
'INSERT INTO notification_settings |
| 219 |
(setting_name, setting_value, user_id, product, context) |
| 220 |
VALUES |
| 221 |
(?, ?, ?, ?, ?)', |
| 222 |
array( |
| 223 |
'token', |
| 224 |
$token, |
| 225 |
(int) $userId, |
| 226 |
$productName, |
| 227 |
(int) $contextId |
| 228 |
) |
| 229 |
); |
| 230 |
|
| 231 |
return $token; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Generates an access key for the guest user and adds them to the settings table |
| 236 |
* @param $userId int |
| 237 |
* @return int |
| 238 |
*/ |
| 239 |
function subscribeGuest($email) { |
| 240 |
$application =& PKPApplication::getApplication(); |
| 241 |
$productName = $application->getName(); |
| 242 |
$context =& Request::getContext(); |
| 243 |
$contextId = $context->getId(); |
| 244 |
|
| 245 |
// Check that the email doesn't already exist |
| 246 |
$result =& $this->retrieve( |
| 247 |
'SELECT * FROM notification_settings WHERE setting_name = ? AND setting_value = ? AND product = ? AND context = ?', |
| 248 |
array( |
| 249 |
'mailList', |
| 250 |
$email, |
| 251 |
$productName, |
| 252 |
(int) $contextId |
| 253 |
) |
| 254 |
); |
| 255 |
|
| 256 |
if ($result->RecordCount() != 0) { |
| 257 |
return false; |
| 258 |
} else { |
| 259 |
$this->update( |
| 260 |
'INSERT INTO notification_settings |
| 261 |
(setting_name, setting_value, user_id, product, context) |
| 262 |
VALUES |
| 263 |
(?, ?, ?, ?, ?)', |
| 264 |
array( |
| 265 |
'mailListUncomfirmed', |
| 266 |
$email, |
| 267 |
0, |
| 268 |
$productName, |
| 269 |
(int) $contextId |
| 270 |
) |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
// Get assoc_id into notification_settings table, also used as user_id for access key |
| 275 |
$assocId = $this->getInsertNotificationSettingId(); |
| 276 |
|
| 277 |
import('security.AccessKeyManager'); |
| 278 |
$accessKeyManager = new AccessKeyManager(); |
| 279 |
|
| 280 |
$password = $accessKeyManager->createKey('MailListContext', $assocId, $assocId, 10000); |
| 281 |
return $password; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
/** |
| 287 |
* Removes an email address and associated access key from email notifications |
| 288 |
* @param $email string |
| 289 |
* @param $password string |
| 290 |
* @return boolean |
| 291 |
*/ |
| 292 |
function unsubscribeGuest($email, $password) { |
| 293 |
$application =& PKPApplication::getApplication(); |
| 294 |
$productName = $application->getName(); |
| 295 |
$context =& Request::getContext(); |
| 296 |
$contextId = $context->getId(); |
| 297 |
|
| 298 |
$result =& $this->retrieve( |
| 299 |
'SELECT setting_id FROM notification_settings WHERE setting_name = ? AND product = ? AND context = ?', |
| 300 |
array( |
| 301 |
'mailList', |
| 302 |
$productName, |
| 303 |
(int) $contextId |
| 304 |
) |
| 305 |
); |
| 306 |
|
| 307 |
$row = $result->GetRowAssoc(false); |
| 308 |
$userId = (int) $row['setting_id']; |
| 309 |
|
| 310 |
$result->Close(); |
| 311 |
unset($result); |
| 312 |
|
| 313 |
import('security.AccessKeyManager'); |
| 314 |
$accessKeyManager = new AccessKeyManager(); |
| 315 |
$accessKeyHash = AccessKeyManager::generateKeyHash($password); |
| 316 |
$accessKey = $accessKeyManager->validateKey('MailListContext', $userId, $accessKeyHash); |
| 317 |
|
| 318 |
if ($accessKey) { |
| 319 |
$this->update( |
| 320 |
'DELETE FROM notification_settings WHERE setting_name = ? AND setting_value = ? AND product = ? AND context = ?', |
| 321 |
array( |
| 322 |
'mailList', |
| 323 |
$email, |
| 324 |
$productName, |
| 325 |
(int) $contextId |
| 326 |
) |
| 327 |
); |
| 328 |
$accessKeyDao =& DAORegistry::getDAO('AccessKeyDAO'); |
| 329 |
$accessKeyDao->deleteAccessKey($accessKey); |
| 330 |
return true; |
| 331 |
} else return false; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Gets the setting id for a maillist member (to access the accompanying access key) |
| 336 |
* @return array |
| 337 |
*/ |
| 338 |
function getMailListSettingId($email, $settingName = 'mailListUncomfirmed') { |
| 339 |
$application =& PKPApplication::getApplication(); |
| 340 |
$productName = $application->getName(); |
| 341 |
$context =& Request::getContext(); |
| 342 |
$contextId = $context->getId(); |
| 343 |
|
| 344 |
$result =& $this->retrieve( |
| 345 |
'SELECT setting_id FROM notification_settings WHERE setting_name = ? AND setting_value = ? AND product = ? AND context = ?', |
| 346 |
array( |
| 347 |
$settingName, |
| 348 |
$email, |
| 349 |
$productName, |
| 350 |
(int) $contextId |
| 351 |
) |
| 352 |
); |
| 353 |
|
| 354 |
$row = $result->GetRowAssoc(false); |
| 355 |
$settingId = (int) $row['setting_id']; |
| 356 |
|
| 357 |
return $settingId; |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Update the notification settings table to confirm the mailing list subscription |
| 362 |
* @return boolean |
| 363 |
*/ |
| 364 |
function confirmMailListSubscription($settingId) { |
| 365 |
return $this->update( |
| 366 |
'UPDATE notification_settings SET setting_name = ? WHERE setting_id = ?', |
| 367 |
array('mailList', (int) $settingId) |
| 368 |
); |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Gets a list of email addresses of users subscribed to the mailing list |
| 373 |
* @return array |
| 374 |
*/ |
| 375 |
function getMailList() { |
| 376 |
$application =& PKPApplication::getApplication(); |
| 377 |
$productName = $application->getName(); |
| 378 |
$context =& Request::getContext(); |
| 379 |
$contextId = $context->getId(); |
| 380 |
$mailList = array(); |
| 381 |
|
| 382 |
$result =& $this->retrieve( |
| 383 |
'SELECT setting_value FROM notification_settings WHERE setting_name = ? AND product = ? AND context = ?', |
| 384 |
array( |
| 385 |
'mailList', |
| 386 |
$productName, |
| 387 |
(int) $contextId |
| 388 |
) |
| 389 |
); |
| 390 |
|
| 391 |
while (!$result->EOF) { |
| 392 |
$row = $result->GetRowAssoc(false); |
| 393 |
$mailList[] = $row['setting_value']; |
| 394 |
$result->moveNext(); |
| 395 |
} |
| 396 |
|
| 397 |
$result->Close(); |
| 398 |
unset($result); |
| 399 |
|
| 400 |
return $mailList; |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Generates and inserts a new password for a mailing list user |
| 405 |
* @param $email string |
| 406 |
* @return string |
| 407 |
*/ |
| 408 |
function resetPassword($email) { |
| 409 |
$application =& PKPApplication::getApplication(); |
| 410 |
$productName = $application->getName(); |
| 411 |
$context =& Request::getContext(); |
| 412 |
$contextId = $context->getId(); |
| 413 |
|
| 414 |
$result =& $this->retrieve( |
| 415 |
'SELECT setting_id FROM notification_settings WHERE setting_name = ? AND setting_value = ? AND product = ? AND context = ?', |
| 416 |
array( |
| 417 |
'mailList', |
| 418 |
$email, |
| 419 |
$productName, |
| 420 |
(int) $contextId |
| 421 |
) |
| 422 |
); |
| 423 |
|
| 424 |
$row = $result->GetRowAssoc(false); |
| 425 |
$settingId = $row['setting_id']; |
| 426 |
|
| 427 |
$result->Close(); |
| 428 |
unset($result); |
| 429 |
|
| 430 |
$accessKeyDao =& DAORegistry::getDAO('AccessKeyDAO'); |
| 431 |
$accessKey = $accessKeyDao->getAccessKeyByUserId('MailListContext', $settingId); |
| 432 |
|
| 433 |
if ($accessKey) { |
| 434 |
$key = Validation::generatePassword(); |
| 435 |
$accessKey->setKeyHash(md5($key)); |
| 436 |
|
| 437 |
$accessKeyDao =& DAORegistry::getDAO('AccessKeyDAO'); |
| 438 |
$accessKeyDao->updateAccessKey($accessKey); |
| 439 |
return $key; |
| 440 |
} else return false; |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Get the ID of the last inserted notification |
| 445 |
* @return int |
| 446 |
*/ |
| 447 |
function getInsertNotificationSettingId() { |
| 448 |
return $this->getInsertId('notification_settings', 'setting_id'); |
| 449 |
} |
| 450 |
|
| 451 |
} |
| 452 |
|
| 453 |
?> |