| [270] | 1 | <?php |
|---|
| 2 | // WCF include |
|---|
| 3 | require_once(WCF_DIR.'lib/system/event/EventListener.class.php'); |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * checks if the friend is invited |
|---|
| 7 | * |
|---|
| 8 | * @author Torben Brodt |
|---|
| 9 | * @package de.easy-coding.wcf.buddyloo |
|---|
| 10 | * @license GNU General Public License <http://opensource.org/licenses/gpl-3.0.html> |
|---|
| 11 | */ |
|---|
| 12 | class BuddylooRegisterFormListener implements EventListener { |
|---|
| 13 | protected $eventObj; |
|---|
| 14 | protected $className; |
|---|
| 15 | |
|---|
| 16 | //data |
|---|
| 17 | protected $friends = array(); |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * @see EventListener::execute() |
|---|
| 21 | */ |
|---|
| 22 | public function execute($eventObj, $className, $eventName) { |
|---|
| 23 | $this->eventObj = $eventObj; |
|---|
| 24 | $this->className = $className; |
|---|
| 25 | |
|---|
| 26 | switch ($eventName) { |
|---|
| 27 | case 'assignVariables': |
|---|
| [319] | 28 | if(!REGISTER_INVITATIONS) return; |
|---|
| [270] | 29 | $this->assignVariables(); |
|---|
| 30 | break; |
|---|
| 31 | case 'validate': |
|---|
| [319] | 32 | if(!REGISTER_INVITATIONS) return; |
|---|
| 33 | $this->fetchInvitations(); |
|---|
| [270] | 34 | $this->validate(); |
|---|
| 35 | break; |
|---|
| 36 | case 'saved': |
|---|
| [319] | 37 | $this->fetchInvitations(); |
|---|
| [270] | 38 | $this->saved(); |
|---|
| 39 | break; |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * show info... registration just for invited users |
|---|
| 45 | */ |
|---|
| 46 | protected function assignVariables() { |
|---|
| 47 | WCF::getTPL()->append('userMessages', '<p class="info">'.WCF::getLanguage()->get('wcf.buddyloo.invitation.register.description').'</p>'); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| [319] | 51 | * fetchs invitations |
|---|
| [270] | 52 | */ |
|---|
| [319] | 53 | protected function fetchInvitations() { |
|---|
| [270] | 54 | $email = $this->eventObj->email; |
|---|
| 55 | $sql = "SELECT userID |
|---|
| 56 | FROM wcf".WCF_N."_buddyloo_invitation |
|---|
| 57 | WHERE email = '".escapeString($this->eventObj->email)."'"; |
|---|
| 58 | $result = WCF::getDB()->sendQuery($sql); |
|---|
| 59 | while ($row = WCF::getDB()->fetchArray($result)) { |
|---|
| 60 | $this->friends[] = $row['userID']; |
|---|
| 61 | } |
|---|
| [319] | 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * check if the user had an invitation and add him to friendlist |
|---|
| 66 | */ |
|---|
| 67 | protected function validate() { |
|---|
| [270] | 68 | if(count($this->friends) == 0) { |
|---|
| 69 | throw new UserInputException('email', 'notValid'); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * check if the user had an invitation and add him to friendlist |
|---|
| 75 | */ |
|---|
| 76 | protected function saved() { |
|---|
| 77 | $inserts = ''; |
|---|
| 78 | foreach($this->friends as $friend) { |
|---|
| 79 | if (!empty($inserts)) $inserts .= ','; |
|---|
| [319] | 80 | $inserts .= "(".$friend.", ".$this->eventObj->user->userID.")"; |
|---|
| [270] | 81 | } |
|---|
| [315] | 82 | |
|---|
| [319] | 83 | if(!empty($inserts)) { |
|---|
| 84 | $sql = "INSERT IGNORE INTO wcf".WCF_N."_buddyloo |
|---|
| 85 | (userID, whiteUserID) |
|---|
| 86 | VALUES ".$inserts; |
|---|
| 87 | WCF::getDB()->sendQuery($sql); |
|---|
| 88 | |
|---|
| 89 | $sql = "DELETE FROM wcf".WCF_N."_buddyloo_invitation |
|---|
| 90 | WHERE email = '".escapeString($this->eventObj->user->email)."'; "; |
|---|
| 91 | WCF::getDB()->sendQuery($sql); |
|---|
| [323] | 92 | |
|---|
| 93 | $this->updateCache(); |
|---|
| [319] | 94 | } |
|---|
| [270] | 95 | } |
|---|
| [323] | 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * updates cache |
|---|
| 101 | */ |
|---|
| 102 | protected function updateCache() { |
|---|
| 103 | // clear cache |
|---|
| 104 | WCF::getCache()->clear(WBB_DIR.'cache', 'cache.buddyloo.*.php'); |
|---|
| 105 | |
|---|
| 106 | // add resource |
|---|
| 107 | WCF::getCache()->addResource('buddyloo.whitelist', |
|---|
| 108 | WCF_DIR.'cache/cache.buddyloo.whitelist.php', |
|---|
| 109 | WCF_DIR.'lib/system/cache/CacheBuilderBuddylooWhiteList.class.php'); |
|---|
| 110 | } |
|---|
| [270] | 111 | } |
|---|
| 112 | ?> |
|---|