| 1 | <?php
|
| 2 | /* *
|
| 3 | * This script is part of the TYPO3 project - inspiring people to share! *
|
| 4 | * *
|
| 5 | * TYPO3 is free software; you can redistribute it and/or modify it under *
|
| 6 | * the terms of the GNU General Public License version 2 as published by *
|
| 7 | * the Free Software Foundation. *
|
| 8 | * *
|
| 9 | * This script is distributed in the hope that it will be useful, but *
|
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
|
| 11 | * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
|
| 12 | * Public License for more details. *
|
| 13 | * */
|
| 14 |
|
| 15 | /**
|
| 16 | * A pre processor validating an auth code generated by Finisher_GenerateAuthCode.
|
| 17 | *
|
| 18 | * @author Reinhard Führicht <rf@typoheads.at>
|
| 19 | */
|
| 20 | class Tx_Formhandler_PreProcessor_ValidateAuthCodeAdvanced extends Tx_Formhandler_AbstractPreProcessor {
|
| 21 |
|
| 22 | /**
|
| 23 | * The main method called by the controller
|
| 24 | *
|
| 25 | * @param array $gp The GET/POST parameters
|
| 26 | * @param array $settings The defined TypoScript settings for the finisher
|
| 27 | * @return array The probably modified GET/POST parameters
|
| 28 | */
|
| 29 | public function process() {
|
| 30 | if($this->gp['authCode']) {
|
| 31 |
|
| 32 | try {
|
| 33 | $authCode = trim($this->gp['authCode']);
|
| 34 | $table = trim($this->gp['table']);
|
| 35 | $uidField = trim($this->gp['uidField']);
|
| 36 | $uid = trim($this->gp['uid']);
|
| 37 |
|
| 38 | if(!(strlen($table) > 0 && strlen($uidField) > 0 && strlen($authCode) > 0 && strlen($uid) > 0)) {
|
| 39 | $this->utilityFuncs->throwException('validateauthcode_insufficient_params');
|
| 40 | }
|
| 41 |
|
| 42 | $uid = $GLOBALS['TYPO3_DB']->fullQuoteStr($uid, $table);
|
| 43 |
|
| 44 | $hiddenField = 'disable';
|
| 45 | if($this->settings['hiddenField']) {
|
| 46 | $hiddenField = $this->utilityFuncs->getSingle($this->settings, 'hiddenField');
|
| 47 | } elseif($TCA[$table]['ctrl']['enablecolumns']['disable']) {
|
| 48 | $hiddenField = $TCA[$table]['ctrl']['enablecolumns']['disable'];
|
| 49 | }
|
| 50 | $selectFields = '*';
|
| 51 | if($this->settings['selectFields']) {
|
| 52 | $selectFields = $this->utilityFuncs->getSingle($this->settings, 'selectFields');
|
| 53 | }
|
| 54 | $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selectFields, $table, $uidField . '=' . $uid . ' AND ' . $hiddenField . '=1' . $this->cObj->enableFields($table, 1));
|
| 55 | if(!$res || $GLOBALS['TYPO3_DB']->sql_num_rows($res) === 0) {
|
| 56 | $this->utilityFuncs->throwException('validateauthcode_no_record_found');
|
| 57 | }
|
| 58 |
|
| 59 | $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
|
| 60 | $GLOBALS['TYPO3_DB']->sql_free_result($res);
|
| 61 |
|
| 62 | $localAuthCode = md5(serialize($row));
|
| 63 |
|
| 64 | if($localAuthCode !== $authCode) {
|
| 65 | $this->utilityFuncs->throwException('validateauthcode_invalid_auth_code');
|
| 66 | }
|
| 67 |
|
| 68 | $res = $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, $uidField . '=' . $uid, array($hiddenField => 0));
|
| 69 | if(!$res) {
|
| 70 | $this->utilityFuncs->throwException('validateauthcode_update_failed');
|
| 71 | }
|
| 72 |
|
| 73 | /*
|
| 74 | * update pid
|
| 75 | */
|
| 76 | $newPid = $this->utilityFuncs->getSingle($this->settings, 'newPid');
|
| 77 | if (!empty($newPid)) {
|
| 78 | $res = $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, $uidField . '=' . $uid, array('pid' => $newPid));
|
| 79 | if(!$res) {
|
| 80 | $this->utilityFuncs->throwException('validateauthcode_update_failed');
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | /*
|
| 85 | * prepare for the loggers and mailers (and execute)
|
| 86 | */
|
| 87 | if (!empty($this->settings['availableFormFields.'])) {
|
| 88 | $availableFormFields = array();
|
| 89 | //parse mapping
|
| 90 | foreach ($this->settings['availableFormFields.'] as $fieldname => $options) {
|
| 91 | $fieldname = str_replace('.', '', $fieldname);
|
| 92 | if (isset($options) && is_array($options)) {
|
| 93 | $mapping = $options['mapping'];
|
| 94 |
|
| 95 | //if no mapping default to the name of the db field
|
| 96 | if (!$mapping) {
|
| 97 | $mapping = $fieldname;
|
| 98 | }
|
| 99 |
|
| 100 | $availableFormFields[$mapping] = $fieldname;
|
| 101 | } else {
|
| 102 | $availableFormFields[$options] = $fieldname;
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | if (!empty($availableFormFields)) {
|
| 107 | $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(implode(',', array_keys($availableFormFields)), $table, $uidField . '=' . $uid . ' AND ' . $hiddenField . '=0' . $this->cObj->enableFields($table, 1));
|
| 108 | if(!$res || $GLOBALS['TYPO3_DB']->sql_num_rows($res) === 0) {
|
| 109 | $this->utilityFuncs->throwException('validateauthcode_no_record_found');
|
| 110 | }
|
| 111 |
|
| 112 | $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
|
| 113 | $GLOBALS['TYPO3_DB']->sql_free_result($res);
|
| 114 |
|
| 115 | // Add values from database in get-post array
|
| 116 | foreach ($row as $dbField => $value) {
|
| 117 | if (is_array($availableFormFields)) {
|
| 118 | if (!empty ($availableFormFields[$dbField])) {
|
| 119 | $this->gp[$availableFormFields[$dbField]] = $value;
|
| 120 | }
|
| 121 | } else {
|
| 122 | $this->gp[$dbField] = $value;
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | $this->runClasses($this->settings['loggers.']);
|
| 127 | $this->runClasses($this->settings['mailers.']);
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | $redirectPage = $this->utilityFuncs->getSingle($this->settings, 'redirectPage');
|
| 132 | if($redirectPage) {
|
| 133 | $correctRedirectUrl = $this->utilityFuncs->getSingle($this->settings, 'correctRedirectUrl');
|
| 134 | $headerStatusCode = $this->utilityFuncs->getSingle($this->settings, 'headerStatusCode');
|
| 135 | $this->utilityFuncs->doRedirect($redirectPage, $correctRedirectUrl, $this->settings['additionalParams.'], $headerStatusCode);
|
| 136 | }
|
| 137 | } catch(Exception $e) {
|
| 138 | $redirectPage = $this->utilityFuncs->getSingle($this->settings, 'errorRedirectPage');
|
| 139 | if($redirectPage) {
|
| 140 | $correctRedirectUrl = $this->utilityFuncs->getSingle($this->settings, 'correctRedirectUrl');
|
| 141 | $headerStatusCode = $this->utilityFuncs->getSingle($this->settings, 'headerStatusCode');
|
| 142 | $this->utilityFuncs->doRedirect($redirectPage, $correctRedirectUrl, $this->settings['additionalParams.'], $headerStatusCode);
|
| 143 | } else {
|
| 144 | throw new Exception($e->getMessage());
|
| 145 | }
|
| 146 | }
|
| 147 | }
|
| 148 | return $this->gp;
|
| 149 | }
|
| 150 |
|
| 151 | /**
|
| 152 | * Adds default configuration for every Formhandler component to the given configuration array
|
| 153 | *
|
| 154 | * @param array $conf The configuration of the component set in TS
|
| 155 | * @return array The initial configuration plus the default configuration
|
| 156 | */
|
| 157 | protected function addDefaultComponentConfig($conf) {
|
| 158 | if (!$conf['langFiles']) {
|
| 159 | $conf['langFiles'] = $this->langFiles;
|
| 160 | }
|
| 161 | $conf['formValuesPrefix'] = $this->settings['formValuesPrefix'];
|
| 162 | $conf['templateSuffix'] = $this->settings['templateSuffix'];
|
| 163 | return $conf;
|
| 164 | }
|
| 165 |
|
| 166 | /**
|
| 167 | * Runs the class by calling process() method.
|
| 168 | *
|
| 169 | * @param array $classesArray: the configuration array
|
| 170 | * @return void
|
| 171 | */
|
| 172 | protected function runClasses($classesArray) {
|
| 173 | if (isset($classesArray) && is_array($classesArray) && intval($classesArray['disable']) !== 1) {
|
| 174 |
|
| 175 | foreach ($classesArray as $idx => $tsConfig) {
|
| 176 | if ($idx !== 'disable') {
|
| 177 | if (is_array($tsConfig) && isset($tsConfig['class']) && !empty($tsConfig['class'])) {
|
| 178 | if (intval($tsConfig['disable']) !== 1) {
|
| 179 | $className = $this->utilityFuncs->prepareClassName($tsConfig['class']);
|
| 180 | $this->utilityFuncs->debugMessage('calling_class', array($className));
|
| 181 | $obj = $this->componentManager->getComponent($className);
|
| 182 | $tsConfig['config.'] = $this->addDefaultComponentConfig($tsConfig['config.']);
|
| 183 | $obj->init($this->gp, $tsConfig['config.']);
|
| 184 | $obj->validateConfig();
|
| 185 | $return = $obj->process();
|
| 186 | if (is_array($return)) {
|
| 187 |
|
| 188 | //return value is an array. Treat it as the probably modified get/post parameters
|
| 189 | $this->gp = $return;
|
| 190 | $this->globals->setGP($this->gp);
|
| 191 | } else {
|
| 192 |
|
| 193 | //return value is no array. treat this return value as output.
|
| 194 | return $return;
|
| 195 | }
|
| 196 | }
|
| 197 | } else {
|
| 198 | $this->utilityFuncs->throwException('classesarray_error');
|
| 199 | }
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 | }
|
| 204 | }
|
| 205 | ?> |