| 1 | <?php |
|---|
| 2 | require_once(WCF_DIR.'lib/form/AbstractForm.class.php'); |
|---|
| 3 | |
|---|
| 4 | // gwsp imports |
|---|
| 5 | require_once(GWSP_DIR.'lib/data/competition/CompetitionGame.class.php'); |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | /* |
|---|
| 9 | * Erstellt das Formular zum Gewinnspiel. |
|---|
| 10 | * GeprÃŒft wird Berechtigung (Gruppe), ob schon mal mitgespielt wurde |
|---|
| 11 | * und gÃŒltige gwsID. |
|---|
| 12 | * Nur wenn alle Felder ausgefÃŒllt wurden kann das Formular abgeschickt werden. |
|---|
| 13 | * Die Daten werden per E-Mail an den Teilnehmer und den Ersteller geschickt. |
|---|
| 14 | * @author Robert "Tatzelwurm" Hempel |
|---|
| 15 | * @copyright 2007-2008 INSIDE das Hörspiel |
|---|
| 16 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
|---|
| 17 | * @package de.inside.gwsp |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | class GewinnspielForm extends AbstractForm { |
|---|
| 21 | |
|---|
| 22 | public $templateName = 'gewinnspiel'; |
|---|
| 23 | // Gewinnspiel und Teilnehmer Daten |
|---|
| 24 | public $game; // Game object |
|---|
| 25 | public $gwsID; // Game id |
|---|
| 26 | public $gwsOptionsArray = array (); // the Questions |
|---|
| 27 | public $gwsGroupIDs = array(); // Berechtigte Gruppen |
|---|
| 28 | public $userreal; // aktueller Teilnehmer Name |
|---|
| 29 | public $username; // aktueller Teilnehmer Nick |
|---|
| 30 | public $useremail; // E-Mail des Teilnehmers |
|---|
| 31 | public $useremailconfirm; // E-Mail Kontrolle |
|---|
| 32 | public $useraddr; // Anschrift des Teilnehmers |
|---|
| 33 | public $ipAddress = 0; // IP des Teilnehmers |
|---|
| 34 | public $usercount; // Anzahl der User die bereits mitgemacht haben |
|---|
| 35 | public $answer = array(); // Die Antworten |
|---|
| 36 | // Voreinstellungen fuer die Textausgabe |
|---|
| 37 | private $enableSmilies = true; |
|---|
| 38 | private $enableBBCodes = true; |
|---|
| 39 | protected $akzept; // auf "AKZEPTIERT" testen |
|---|
| 40 | protected $valid = false; // Variable zu Gueltikeitsberpruefung |
|---|
| 41 | protected $recipient; // Empfaenger zur Auswertung |
|---|
| 42 | protected $frageantwort = ''; // Die Fragen und Antworten fuer die Mail |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * @see Page::readParameters() |
|---|
| 46 | */ |
|---|
| 47 | public function readParameters() { |
|---|
| 48 | parent::readParameters(); |
|---|
| 49 | |
|---|
| 50 | // get game id |
|---|
| 51 | if (isset($_REQUEST['gwsID'])) $this->gwsID = intval($_REQUEST['gwsID']); |
|---|
| 52 | // game id OK? |
|---|
| 53 | if (!$this->gwsID){ |
|---|
| 54 | require_once(WCF_DIR.'lib/system/exception/NamedUserException.class.php'); |
|---|
| 55 | throw new NamedUserException(GWSPCore::getLanguage()->get('gws.gewinnspiel.wrongid')); |
|---|
| 56 | exit; |
|---|
| 57 | } |
|---|
| 58 | // new game object |
|---|
| 59 | $this->game = new CompetitionGame($this->gwsID); |
|---|
| 60 | |
|---|
| 61 | // check game permissions |
|---|
| 62 | $this->checkPermissions(); |
|---|
| 63 | |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * @see Form::readFormParameters() |
|---|
| 68 | */ |
|---|
| 69 | public function readFormParameters() { |
|---|
| 70 | parent::readFormParameters(); |
|---|
| 71 | |
|---|
| 72 | if (isset($_POST['gwsID'])) $this->gwsID = intval($_POST['gwsID']); |
|---|
| 73 | if (isset($_POST['gwsTitel'])) $this->gwsTitel = escapeString($_POST['gwsTitel']); |
|---|
| 74 | if (isset($_POST['useremail'])) $this->useremail = StringUtil::trim($_POST['useremail']); |
|---|
| 75 | if (isset($_POST['useremailconfirm'])) $this->useremailconfirm = StringUtil::trim($_POST['useremailconfirm']); |
|---|
| 76 | if (isset($_POST['username'])) $this->username = escapeString($_POST['username']); |
|---|
| 77 | if (isset($_POST['userreal'])) $this->userreal = escapeString($_POST['userreal']); |
|---|
| 78 | if (isset($_POST['useraddr'])) $this->useraddr = escapeString($_POST['useraddr']); |
|---|
| 79 | if (isset($_POST['akzept'])) $this->akzept = escapeString($_POST['akzept']); |
|---|
| 80 | if (isset($_POST['answer'])) $this->answer = ArrayUtil::trim($_POST['answer'], false); |
|---|
| 81 | // Gast oder User ? Bei Usern entfllt die E-Mail berprfung |
|---|
| 82 | if ($this->username) $this->useremailconfirm = $this->useremail; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * @see Form::validate() |
|---|
| 87 | */ |
|---|
| 88 | public function validate() { |
|---|
| 89 | |
|---|
| 90 | // check E-Mail |
|---|
| 91 | // email input OK? |
|---|
| 92 | if (empty($this->useremail)) { |
|---|
| 93 | throw new UserInputException('useremail', 'empty'); |
|---|
| 94 | } |
|---|
| 95 | // validateEmail input OK? |
|---|
| 96 | if (empty($this->useremailconfirm)) { |
|---|
| 97 | throw new UserInputException('useremailconfirm', 'empty'); |
|---|
| 98 | } |
|---|
| 99 | // check for valid email (one @ etc.) |
|---|
| 100 | if (!UserRegistrationUtil::isValidEmail($this->useremail)) { |
|---|
| 101 | throw new UserInputException('useremail', 'notValid'); |
|---|
| 102 | } |
|---|
| 103 | // check confirm input |
|---|
| 104 | if (StringUtil::toLowerCase($this->useremail) != StringUtil::toLowerCase($this->useremailconfirm)) { |
|---|
| 105 | throw new UserInputException('useremailconfirm', 'notEqual'); |
|---|
| 106 | } |
|---|
| 107 | // Realname input valid? |
|---|
| 108 | if (empty($this->userreal)) { |
|---|
| 109 | throw new UserInputException('userreal', 'empty'); |
|---|
| 110 | } |
|---|
| 111 | // check for valid address |
|---|
| 112 | if (empty($this->useraddr)) { |
|---|
| 113 | throw new UserInputException('useraddr', 'empty'); |
|---|
| 114 | } |
|---|
| 115 | // All questions answerd? |
|---|
| 116 | foreach ($this->answer as $key => $nummer){ |
|---|
| 117 | if (empty($nummer)){ |
|---|
| 118 | $notok = "answer[".$key."]"; |
|---|
| 119 | throw new UserInputException($notok, 'empty'); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | // rules accepted? |
|---|
| 123 | if (empty($this->akzept) || $this->akzept != 'AKZEPTIERT') { |
|---|
| 124 | throw new UserInputException('akzept', 'empty'); |
|---|
| 125 | } |
|---|
| 126 | // parent::validate(); |
|---|
| 127 | // Kontrolle ob Gast schon teilgenommen hat |
|---|
| 128 | $id = GWSPCore::getUser()->userID; |
|---|
| 129 | if (!$id){ |
|---|
| 130 | $sql = "SELECT `ipaddress`, `email`, `realName` |
|---|
| 131 | FROM gwsp".GWSP_N."_spiel |
|---|
| 132 | WHERE `gwsID` = ".$this->game->gwsID." AND `userID` = 0"; |
|---|
| 133 | $result = GWSPCore::getDB()->sendQuery($sql); |
|---|
| 134 | while ($row = GWSPCore::getDB()->fetchArray($result)) { |
|---|
| 135 | $ipArray[] = "$row[ipaddress]"; |
|---|
| 136 | $mailArray[] = "$row[email]"; |
|---|
| 137 | $rnArray[] = "$row[realName]"; |
|---|
| 138 | } |
|---|
| 139 | if (!empty($ipArray)){ |
|---|
| 140 | foreach ($ipArray as $key => $option){ |
|---|
| 141 | if (StringUtil::toLowerCase($mailArray[$key]) == StringUtil::toLowerCase($this->useremail) || StringUtil::toLowerCase($rnArray[$key]) == StringUtil::toLowerCase($this->userreal)){ |
|---|
| 142 | require_once(WCF_DIR.'lib/system/exception/NamedUserException.class.php'); |
|---|
| 143 | throw new NamedUserException(GWSPCore::getLanguage()->get('gws.gewinnspiel.alreadyplayed')); |
|---|
| 144 | exit; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | $this->valid = true; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /** |
|---|
| 153 | * @see Form::save() |
|---|
| 154 | */ |
|---|
| 155 | public function save() { |
|---|
| 156 | parent::save(); |
|---|
| 157 | // Teilnahme Erfolgreich dann Gewinnspiel speichern |
|---|
| 158 | if ($this->valid){ |
|---|
| 159 | // Userdaten speichern |
|---|
| 160 | $sql = "INSERT INTO gwsp".GWSP_N."_spiel |
|---|
| 161 | (gwsID, userID, ipaddress, email, realName) |
|---|
| 162 | VALUES (".intval($this->game->gwsID).", |
|---|
| 163 | ".intval(GWSPCore::getUser()->userID).", |
|---|
| 164 | '".escapeString(GWSPCore::getSession()->ipAddress)."', |
|---|
| 165 | '".$this->useremail."', |
|---|
| 166 | '".$this->userreal."')"; |
|---|
| 167 | GWSPCore::getDB()->sendQuery($sql); |
|---|
| 168 | |
|---|
| 169 | // Competition Mod E-Mail. |
|---|
| 170 | $sql = "SELECT `recipient` |
|---|
| 171 | FROM gwsp".GWSP_N."_game |
|---|
| 172 | WHERE gwsID = ".$this->game->gwsID; |
|---|
| 173 | $result = GWSPCore::getDB()->getFirstRow($sql); |
|---|
| 174 | $this->recipient = $result['recipient']; |
|---|
| 175 | // E-Mail vorbeiten und senden |
|---|
| 176 | if (!$this->username) $this->username = 'Gast'; |
|---|
| 177 | // Frage/Antwort Variable vorbereiten |
|---|
| 178 | $sql = "SELECT * |
|---|
| 179 | FROM gwsp".GWSP_N."_options |
|---|
| 180 | WHERE gwsID = ".$this->game->gwsID." |
|---|
| 181 | ORDER BY showOrder"; |
|---|
| 182 | $result = GWSPCore::getDB()->sendQuery($sql); |
|---|
| 183 | while ($row = GWSPCore::getDB()->fetchArray($result)) { |
|---|
| 184 | $this->gwsOptionsArray[] .= "$row[gwsOption]"; |
|---|
| 185 | } |
|---|
| 186 | $frage = GWSPCore::getLanguage()->get('gws.gewinnspiel.frage'); |
|---|
| 187 | $antwort = GWSPCore::getLanguage()->get('gws.gewinnspiel.antwort'); |
|---|
| 188 | foreach ($this->gwsOptionsArray as $key =>$option){ |
|---|
| 189 | $this->frageantwort .= $frage.": "."$option"."\n".$antwort.": ".$this->answer[$key]."\n"."\n"; |
|---|
| 190 | } |
|---|
| 191 | $languages = array(0 => GWSPCore::getLanguage(), GWSPCore::getLanguage()->getLanguageID() => GWSPCore::getLanguage()); |
|---|
| 192 | require_once(WCF_DIR.'lib/data/mail/Mail.class.php'); |
|---|
| 193 | require_once(WCF_DIR.'lib/data/user/User.class.php'); |
|---|
| 194 | // get language |
|---|
| 195 | // enable language |
|---|
| 196 | $languages[GWSPCore::getLanguage()->getLanguageID()]->setLocale(); |
|---|
| 197 | |
|---|
| 198 | $subjectData = array('PAGE_TITLE' => PAGE_TITLE); |
|---|
| 199 | $messageData = array( |
|---|
| 200 | 'PAGE_TITLE' => PAGE_TITLE, |
|---|
| 201 | '$username' => $this->username, |
|---|
| 202 | '$userreal' => $this->userreal, |
|---|
| 203 | '$useremail' => $this->useremail, |
|---|
| 204 | '$useraddr' => $this->useraddr, |
|---|
| 205 | '$recipient' => $this->recipient, |
|---|
| 206 | '$FrageAntwort' => $this->frageantwort, |
|---|
| 207 | '$datetime' => $this->getTimeDate() |
|---|
| 208 | ); |
|---|
| 209 | // Mail an den User |
|---|
| 210 | $mail = new Mail( array($this->userreal => $this->useremail), |
|---|
| 211 | $languages[GWSPCore::getLanguage()->getLanguageID()]->get('gws.gewinnspiel.mail.subject', $subjectData), |
|---|
| 212 | $languages[GWSPCore::getLanguage()->getLanguageID()]->get('gws.gewinnspiel.mail.message', $messageData)); |
|---|
| 213 | $mail->send(); |
|---|
| 214 | // Mail an den Veranstalter |
|---|
| 215 | $mail = new Mail( $this->recipient, |
|---|
| 216 | $languages[GWSPCore::getLanguage()->getLanguageID()]->get('gws.gewinnspiel.mail.subject.recipient', $subjectData), |
|---|
| 217 | $languages[GWSPCore::getLanguage()->getLanguageID()]->get('gws.gewinnspiel.mail.message.recipient', $messageData)); |
|---|
| 218 | $mail->send(); |
|---|
| 219 | |
|---|
| 220 | // enable user language |
|---|
| 221 | GWSPCore::getLanguage()->setLocale(); |
|---|
| 222 | // reset cache |
|---|
| 223 | GWSPCore::getCache()->clearResource('competition-user-'.PACKAGE_ID); |
|---|
| 224 | // forward to index page |
|---|
| 225 | GWSPCore::getTPL()->assign(array( |
|---|
| 226 | 'url' => 'index.php?page=Index'.SID_ARG_2ND_NOT_ENCODED, |
|---|
| 227 | 'message' => GWSPCore::getLanguage()->get('gws.gewinnspiel.ok_redirect'), |
|---|
| 228 | 'wait' => 3 |
|---|
| 229 | )); |
|---|
| 230 | GWSPCore::getTPL()->display('redirect'); |
|---|
| 231 | exit; |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | /** |
|---|
| 236 | * @see Page::readData() |
|---|
| 237 | */ |
|---|
| 238 | public function readData() { |
|---|
| 239 | parent::readData(); |
|---|
| 240 | if ((GWSPCore::getUser()->userID) > 0){ |
|---|
| 241 | $this->username = GWSPCore::getUser()->username; |
|---|
| 242 | $this->useremail = $this->useremailconfirm = GWSPCore::getUser()->email; |
|---|
| 243 | }else{ |
|---|
| 244 | if (!count($_POST)) { |
|---|
| 245 | $this->username = GWSPCore::getLanguage()->get('gws.gewinnspiel.username.gast'); |
|---|
| 246 | $this->useremail = $this->useremailconfirm = ''; |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | // Gewinnspiel Daten auslesen |
|---|
| 251 | $this->readGewinnspiel(); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | /** |
|---|
| 255 | * @see Page::assignVariables(); |
|---|
| 256 | */ |
|---|
| 257 | public function assignVariables() { |
|---|
| 258 | parent::assignVariables(); |
|---|
| 259 | |
|---|
| 260 | GWSPCore::getTPL()->assign(array( |
|---|
| 261 | 'game' => $this->game, |
|---|
| 262 | 'username' => $this->username, |
|---|
| 263 | 'useremail' => $this->useremail, |
|---|
| 264 | 'useremailconfirm' => $this->useremailconfirm, |
|---|
| 265 | 'userreal' => $this->userreal, |
|---|
| 266 | 'useraddr' => $this->useraddr, |
|---|
| 267 | 'usercount' => $this->usercount, |
|---|
| 268 | 'gwsOptionsArray' => $this->gwsOptionsArray, |
|---|
| 269 | 'answer' => $this->answer, |
|---|
| 270 | 'akzept' => $this->akzept, |
|---|
| 271 | )); |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | /** |
|---|
| 275 | * @see Page::show() |
|---|
| 276 | */ |
|---|
| 277 | public function show() { |
|---|
| 278 | // get the default langauge id |
|---|
| 279 | $this->languageID = GWSPCore::getLanguage()->getLanguageID(); |
|---|
| 280 | // show form |
|---|
| 281 | parent::show(); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | protected function checkPermissions() { |
|---|
| 285 | // Kontrolle ob User schon teilgenommen hat (nicht bei Gaesten) |
|---|
| 286 | $id = GWSPCore::getUser()->userID; |
|---|
| 287 | if ($id){ |
|---|
| 288 | $sql = "SELECT COUNT(userID) AS users |
|---|
| 289 | FROM gwsp".GWSP_N."_spiel |
|---|
| 290 | WHERE `gwsID` = ".$this->game->gwsID." AND `userID` = ".$id; |
|---|
| 291 | $row = GWSPCore::getDB()->getFirstRow($sql); |
|---|
| 292 | if ($row['users']) { |
|---|
| 293 | require_once(WCF_DIR.'lib/system/exception/NamedUserException.class.php'); |
|---|
| 294 | throw new NamedUserException(GWSPCore::getLanguage()->get('gws.gewinnspiel.alreadyplayed')); |
|---|
| 295 | exit; |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | // Kontrolle ob Teilnehmer in einer berechtigten Gruppe ist |
|---|
| 299 | $sql = "SELECT `groupIDs` |
|---|
| 300 | FROM gwsp".GWSP_N."_game |
|---|
| 301 | WHERE gwsID = ".$this->game->gwsID; |
|---|
| 302 | $result = GWSPCore::getDB()->sendQuery($sql); |
|---|
| 303 | $row = GWSPCore::getDB()->fetchArray($result); |
|---|
| 304 | // gwsID valid? |
|---|
| 305 | if(empty($row)){ |
|---|
| 306 | require_once(WCF_DIR.'lib/system/exception/NamedUserException.class.php'); |
|---|
| 307 | throw new NamedUserException(GWSPCore::getLanguage()->get('gws.gewinnspiel.wrongid')); |
|---|
| 308 | exit; |
|---|
| 309 | } |
|---|
| 310 | $this->gwsGroupIDs = explode(',', $row['groupIDs']); |
|---|
| 311 | require_once(WCF_DIR.'lib/data/user/group/Group.class.php'); |
|---|
| 312 | $canplay = false; |
|---|
| 313 | foreach ($this->gwsGroupIDs as $groupID){ |
|---|
| 314 | if (Group::isMember($groupID)) $canplay = true; |
|---|
| 315 | } |
|---|
| 316 | // Fehlermeldung wenn Teilnemer keiner Berechtigten Gruppe angehoert |
|---|
| 317 | if (!$canplay) { |
|---|
| 318 | require_once(WCF_DIR.'lib/system/exception/NamedUserException.class.php'); |
|---|
| 319 | throw new NamedUserException(GWSPCore::getLanguage()->get('gws.gewinnspiel.nogroupe')); |
|---|
| 320 | } |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | protected function readGewinnspiel() { |
|---|
| 324 | // get game |
|---|
| 325 | $this->game->rules = $this->getFormattedMessage($this->game->rules, false); |
|---|
| 326 | $this->game->disclaimer = $this->getFormattedMessage($this->game->disclaimer, false); |
|---|
| 327 | $this->game->description = $this->getFormattedMessage($this->game->description, false); |
|---|
| 328 | //Check Guthaben |
|---|
| 329 | if (defined('GUTHABEN_ENABLE_GLOBAL')){ |
|---|
| 330 | if(GWSPCore::getUser()->userID && $this->game->money >= 1) { |
|---|
| 331 | require_once(WCF_DIR.'lib/data/user/UserGuthaben.class.php'); |
|---|
| 332 | $um = new UserGuthaben(GWSPCore::getUser()->userID); |
|---|
| 333 | if (!$um->subGuthaben($this->game->money,'gws.gewinnspiel.kosten')) { |
|---|
| 334 | require_once(WCF_DIR.'lib/system/exception/NamedUserException.class.php'); |
|---|
| 335 | throw new NamedUserException(GWSPCore::getLanguage()->get('gws.gewinnspiel.errormoney')); |
|---|
| 336 | } |
|---|
| 337 | } |
|---|
| 338 | } |
|---|
| 339 | // Kontrolle ob das Gewinnspiel schon gestartet ist |
|---|
| 340 | if ($this->game->starttime > TIME_NOW) { |
|---|
| 341 | require_once(WCF_DIR.'lib/system/exception/NamedUserException.class.php'); |
|---|
| 342 | throw new NamedUserException(GWSPCore::getLanguage()->get('gws.gewinnspiel.statustocome')); |
|---|
| 343 | exit; |
|---|
| 344 | } |
|---|
| 345 | // Kontrolle ob das Gewinnspiel schon beendet ist |
|---|
| 346 | if ($this->game->timeout < TIME_NOW) { |
|---|
| 347 | require_once(WCF_DIR.'lib/system/exception/NamedUserException.class.php'); |
|---|
| 348 | throw new NamedUserException(GWSPCore::getLanguage()->get('gws.gewinnspiel.statusended')); |
|---|
| 349 | exit; |
|---|
| 350 | } |
|---|
| 351 | // get game options |
|---|
| 352 | $this->gwsOptionsArray = $this->game->getQuestions(); |
|---|
| 353 | foreach ($this->gwsOptionsArray as $key => $val){ |
|---|
| 354 | $this->gwsOptionsArray[$key] = $this->getFormattedMessage($this->gwsOptionsArray[$key], true); |
|---|
| 355 | } |
|---|
| 356 | // for the first time clear answers |
|---|
| 357 | if (empty($this->answer)){ |
|---|
| 358 | foreach ($this->gwsOptionsArray as $key => $nummer){ |
|---|
| 359 | $this->answer[$key] = ''; |
|---|
| 360 | } |
|---|
| 361 | } |
|---|
| 362 | // count users |
|---|
| 363 | $this->usercount = $this->game->countUsers(); |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | /** |
|---|
| 367 | * @see ViewablePost::getFormattedMessage() |
|---|
| 368 | */ |
|---|
| 369 | protected function getFormattedMessage($text, $enableHtml = false) { |
|---|
| 370 | // parse message |
|---|
| 371 | require_once(WCF_DIR.'lib/data/message/bbcode/MessageParser.class.php'); |
|---|
| 372 | $parser = MessageParser::getInstance(); |
|---|
| 373 | $parser->setOutputType('text/html'); |
|---|
| 374 | return $parser->parse($text, $this->enableSmilies, $enableHtml, $this->enableBBCodes, false); |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | protected function getTimeDate() { |
|---|
| 378 | $datumzeit = DateUtil::formatTime(null,null,null,true); |
|---|
| 379 | return $datumzeit; |
|---|
| 380 | } |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | ?> |
|---|