| 1 | <?php |
|---|
| 2 | require_once(WCF_DIR.'lib/form/AbstractForm.class.php'); |
|---|
| 3 | require_once(WCF_DIR.'lib/system/session/UserSession.class.php'); |
|---|
| 4 | require_once(WCF_DIR.'lib/system/exception/IllegalLinkException.class.php'); |
|---|
| 5 | require_once(WCF_DIR.'lib/page/util/menu/UserCPMenu.class.php'); |
|---|
| 6 | require_once(WCF_DIR.'lib/util/UserRegistrationUtil.class.php'); |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | * invitation edit form |
|---|
| 10 | * |
|---|
| 11 | * @author Torben Brodt |
|---|
| 12 | * @package de.easy-coding.wcf.buddyloo |
|---|
| 13 | * @license GNU General Public License <http://opensource.org/licenses/gpl-3.0.html> |
|---|
| 14 | */ |
|---|
| 15 | class InvitationEditForm extends AbstractForm { |
|---|
| 16 | public $input = ''; |
|---|
| 17 | public $body = ''; |
|---|
| 18 | public $inputArray = array(); |
|---|
| 19 | public $emails = array(); |
|---|
| 20 | public $templateName = 'buddylooInvitationEdit'; |
|---|
| 21 | |
|---|
| 22 | protected $flag = false; |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * @see Page::readParameters() |
|---|
| 26 | */ |
|---|
| 27 | public function readParameters() { |
|---|
| 28 | parent::readParameters(); |
|---|
| 29 | |
|---|
| 30 | if (isset($_GET['remove'])) { |
|---|
| 31 | // delete invitation |
|---|
| 32 | $sql = "DELETE FROM wcf".WCF_N."_buddyloo_invitation |
|---|
| 33 | WHERE userID = ".WCF::getUser()->userID." |
|---|
| 34 | AND email = '".escapeString($_GET['remove'])."'"; |
|---|
| 35 | WCF::getDB()->sendQuery($sql); |
|---|
| 36 | |
|---|
| 37 | // show success message |
|---|
| 38 | WCF::getTPL()->assign(array( |
|---|
| 39 | 'success' => 'remove', |
|---|
| 40 | 'changedEmails' => $_GET['remove'] |
|---|
| 41 | )); |
|---|
| 42 | } |
|---|
| 43 | else if (isset($_GET['add'])) { |
|---|
| 44 | $this->submit(); |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * @see Form::readFormParameters() |
|---|
| 50 | */ |
|---|
| 51 | public function readFormParameters() { |
|---|
| 52 | parent::readFormParameters(); |
|---|
| 53 | |
|---|
| 54 | if (isset($_POST['input'])) $this->input = StringUtil::trim($_POST['input']); |
|---|
| 55 | if (isset($_POST['body'])) $this->body = StringUtil::trim($_POST['body']); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * @see Form::validate() |
|---|
| 60 | */ |
|---|
| 61 | public function validate() { |
|---|
| 62 | parent::validate(); |
|---|
| 63 | |
|---|
| 64 | if (count($this->inputArray) == 0) { |
|---|
| 65 | $this->readInvitations(); |
|---|
| 66 | if (empty($this->input)) { |
|---|
| 67 | throw new UserInputException('input'); |
|---|
| 68 | } |
|---|
| 69 | $this->validateEmails(); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * @see Form::save() |
|---|
| 75 | */ |
|---|
| 76 | public function save() { |
|---|
| 77 | parent::save(); |
|---|
| 78 | |
|---|
| 79 | // save user |
|---|
| 80 | $inserts = ''; |
|---|
| 81 | foreach ($this->inputArray as $email) { |
|---|
| 82 | if (!empty($inserts)) $inserts .= ','; |
|---|
| 83 | $inserts .= "(".WCF::getUser()->userID.", '".escapeString($email)."')"; |
|---|
| 84 | $this->emails[] = $email; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | if (!empty($inserts)) { |
|---|
| 88 | $sql = "INSERT IGNORE INTO wcf".WCF_N."_buddyloo_invitation |
|---|
| 89 | (userID, email) |
|---|
| 90 | VALUES ".$inserts; |
|---|
| 91 | WCF::getDB()->sendQuery($sql); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | $this->saved(); |
|---|
| 95 | |
|---|
| 96 | // show success message |
|---|
| 97 | WCF::getTPL()->assign(array( |
|---|
| 98 | 'success' => 'add', |
|---|
| 99 | 'changedEmails' => $this->input |
|---|
| 100 | )); |
|---|
| 101 | |
|---|
| 102 | // reset input field |
|---|
| 103 | $this->input = ''; |
|---|
| 104 | $this->body = ''; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * |
|---|
| 109 | */ |
|---|
| 110 | protected function readInvitations() { |
|---|
| 111 | if($this->flag) return; |
|---|
| 112 | $this->flag = true; |
|---|
| 113 | |
|---|
| 114 | $sql = "SELECT email |
|---|
| 115 | FROM wcf".WCF_N."_buddyloo_invitation |
|---|
| 116 | WHERE userID = ".WCF::getUser()->userID; |
|---|
| 117 | $result = WCF::getDB()->sendQuery($sql); |
|---|
| 118 | while ($row = WCF::getDB()->fetchArray($result)) { |
|---|
| 119 | $this->emails[] = $row['email']; |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * @see Page::readData() |
|---|
| 125 | */ |
|---|
| 126 | public function readData() { |
|---|
| 127 | parent::readData(); |
|---|
| 128 | $this->readInvitations(); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * @see Page::assignVariables() |
|---|
| 133 | */ |
|---|
| 134 | public function assignVariables() { |
|---|
| 135 | parent::assignVariables(); |
|---|
| 136 | |
|---|
| 137 | WCF::getTPL()->assign(array( |
|---|
| 138 | 'input' => $this->input, |
|---|
| 139 | 'emails' => $this->emails, |
|---|
| 140 | 'body' => $this->body, |
|---|
| 141 | )); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /** |
|---|
| 145 | * @see Page::show() |
|---|
| 146 | */ |
|---|
| 147 | public function show() { |
|---|
| 148 | if (!WCF::getUser()->userID) { |
|---|
| 149 | require_once(WCF_DIR.'lib/system/exception/PermissionDeniedException.class.php'); |
|---|
| 150 | throw new PermissionDeniedException(); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | // set active tab |
|---|
| 154 | UserCPMenu::getInstance()->setActiveMenuItem('wcf.user.usercp.menu.link.management.invitation'); |
|---|
| 155 | |
|---|
| 156 | // show form |
|---|
| 157 | parent::show(); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | /** |
|---|
| 161 | * Checks the given emails. |
|---|
| 162 | */ |
|---|
| 163 | protected function validateEmails() { |
|---|
| 164 | // explode multiple usernames to an array |
|---|
| 165 | $emailArray = explode(',', $this->input); |
|---|
| 166 | $error = array(); |
|---|
| 167 | $in = array(); |
|---|
| 168 | |
|---|
| 169 | // loop through recipients and check if the email is valid |
|---|
| 170 | foreach ($emailArray as $email) { |
|---|
| 171 | $email = StringUtil::trim($email); |
|---|
| 172 | $in[] = "'$email'"; |
|---|
| 173 | if (empty($email)) continue; |
|---|
| 174 | |
|---|
| 175 | if(in_array($email, $this->emails)) { |
|---|
| 176 | $error[] = array('type' => 'duplicate', 'email' => $email); |
|---|
| 177 | } else if (UserRegistrationUtil::isValidEmail($email)) { |
|---|
| 178 | $this->inputArray[] = $email; |
|---|
| 179 | } else { |
|---|
| 180 | $error[] = array('type' => 'invalid', 'email' => $email); |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | // search for already registered users |
|---|
| 185 | $sql = "SELECT email |
|---|
| 186 | FROM wcf".WCF_N."_user |
|---|
| 187 | WHERE email IN (".implode(',',$in).")"; |
|---|
| 188 | $result = WCF::getDB()->sendQuery($sql); |
|---|
| 189 | while ($row = WCF::getDB()->fetchArray($result)) { |
|---|
| 190 | $error[] = array('type' => 'duplicate', 'email' => $row['email']); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | if (count($error)) { |
|---|
| 194 | throw new UserInputException('usernames', $error); |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | * Sends the email notification for recipients. |
|---|
| 200 | */ |
|---|
| 201 | protected function sendNotification() { |
|---|
| 202 | require_once(WCF_DIR.'lib/data/mail/Mail.class.php'); |
|---|
| 203 | |
|---|
| 204 | // send notifications |
|---|
| 205 | foreach ($this->inputArray as $recipient) { |
|---|
| 206 | // send mail |
|---|
| 207 | $subjectData = array( |
|---|
| 208 | '$author' => WCF::getUser()->username, |
|---|
| 209 | 'PAGE_TITLE' => PAGE_TITLE |
|---|
| 210 | ); |
|---|
| 211 | $messageData = array( |
|---|
| 212 | 'PAGE_TITLE' => PAGE_TITLE, |
|---|
| 213 | '$recipient' => $recipient->username, |
|---|
| 214 | '$author' => WCF::getUser()->username, |
|---|
| 215 | '$pageurl' => FileUtil::addTrailingSlash(PAGE_URL), |
|---|
| 216 | '$notice' => (REGISTER_INVITATIONS ? WCF::getLanguage()->get('wcf.buddyloo.invitation.register_invitation') : ""), |
|---|
| 217 | '$body' => $this->body |
|---|
| 218 | ); |
|---|
| 219 | |
|---|
| 220 | $mail = new Mail(array($recipient->email), WCF::getLanguage()->get('wcf.buddyloo.invitation.mail.subject', $subjectData), WCF::getLanguage()->get('wcf.buddyloo.invitation.mail.body', $messageData)); |
|---|
| 221 | $mail->send(); |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | ?> |
|---|