Fixed bug #11267: Use PHP5 version of function microtime()
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / class.tslib_fetce.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2009 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Form-data processing
29 * included from index_ts.php
30 *
31 * $Id$
32 * Revised for TYPO3 3.6 June/2003 by Kasper Skaarhoj
33 *
34 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 83: class tslib_feTCE
42 * 100: function start($data,$FEData)
43 * 187: function checkDoublePostExist($table,$doublePostField,$key)
44 * 200: function calcDoublePostKey($array)
45 * 212: function includeScripts()
46 * 232: function execNEWinsert($table, $dataArr)
47 * 258: function clear_cacheCmd($cacheCmd)
48 * 274: function getConf($table)
49 *
50 * TOTAL FUNCTIONS: 7
51 * (This index is automatically created/updated by the extension "extdeveval")
52 *
53 */
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 /**
74 * Form-data processing class.
75 * Used by the FE_DATA object found in TSref. Quite old fashioned and used only by a few extensions, like good old 'tt_guest' and 'tt_board'
76 *
77 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
78 * @package TYPO3
79 * @subpackage tslib
80 * @deprecated since TYPO3 3.6
81 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=342&cHash=fdf55adb3b
82 */
83 class tslib_feTCE {
84
85 var $extScripts=array();
86 var $extScriptsConf=array();
87 var $newData=array();
88 var $extraList = 'pid';
89
90 /**
91 * Starting the processing of user input.
92 * Traverses the input data and fills in the array, $this->extScripts with references to files which are then included by includeScripts() (called AFTER start() in tslib_fe)
93 * These scripts will then put the content into the database.
94 *
95 * @param array Input data coming from typ. $_POST['data'] vars
96 * @param array TypoScript configuration for the FEDATA object, $this->config['FEData.']
97 * @return void
98 * @see tslib_fe::fe_tce(), includeScripts()
99 */
100 function start($data,$FEData) {
101 reset($data);
102 while(list($table,$id_arr)=each($data)) {
103 t3lib_div::loadTCA($table);
104 if (is_array($id_arr)) {
105 $sep=$FEData[$table.'.']['separator']?$FEData[$table.'.']['separator']:chr(10);
106 reset($id_arr);
107 while(list($id,$field_arr)=each($id_arr)) {
108 $this->newData[$table][$id]=Array();
109 if (strstr($id,'NEW')) { // NEW
110 // Defaults:
111 if ($FEData[$table.'.']['default.']) {
112 $this->newData[$table][$id] = $FEData[$table.'.']['default.'];
113 }
114 if ($FEData[$table.'.']['autoInsertPID']) {
115 $this->newData[$table][$id]['pid'] = intval($GLOBALS['TSFE']->page['uid']);
116 }
117 // Insert external data:
118 if (is_array($field_arr)) {
119 reset($field_arr);
120 while(list($field,$value)=each($field_arr)) {
121 if ($FEData[$table.'.']['allowNew.'][$field]) {
122 if (is_array($value)) {
123 $this->newData[$table][$id][$field] = implode($sep,$value);
124 } else {
125 $this->newData[$table][$id][$field] = $value;
126 }
127 }
128 }
129 }
130 // Double post check
131 $dPC_field = $FEData[$table.'.']['doublePostCheck'];
132 if (is_array($this->newData[$table][$id]) && $dPC_field) {
133 $doublePostCheckKey = $this->calcDoublePostKey($this->newData[$table][$id]);
134 if ($this->checkDoublePostExist($table,$dPC_field,$doublePostCheckKey)) {
135 unset($this->newData[$table][$id]); // Unsetting the whole thing, because it's not going to be saved.
136 $GLOBALS['TT']->setTSlogMessage('"FEData": Submitted record to table $table was doublePosted (key: $doublePostCheckKey). Nothing saved.',2);
137 } else {
138 $this->newData[$table][$id][$dPC_field] = $doublePostCheckKey; // Setting key value
139 $this->extraList.=','.$dPC_field;
140 }
141 }
142 } else { // EDIT
143 // Insert external data:
144 if (is_array($field_arr)) {
145 reset($field_arr);
146 while(list($field,$value)=each($field_arr)) {
147 if ($FEData[$table.'.']['allowEdit.'][$field]) {
148 if (is_array($value)) {
149 $this->newData[$table][$id][$field] = implode($sep,$value);
150 } else {
151 $this->newData[$table][$id][$field] = $value;
152 }
153 }
154 }
155 }
156 // Internal Override
157 if (is_array($FEData[$table.'.']['overrideEdit.'])) {
158 reset($FEData[$table.'.']['overrideEdit.']);
159 while(list($field,$value)=each($FEData[$table.'.']['overrideEdit.'])) {
160 $this->newData[$table][$id][$field] = $value;
161 }
162 }
163 }
164 if ($FEData[$table.'.']['userIdColumn']) {
165 $this->newData[$table][$id][$FEData[$table.'.']['userIdColumn']] = intval($GLOBALS['TSFE']->fe_user->user['uid']);
166 }
167 }
168 $incFile = $GLOBALS['TSFE']->tmpl->getFileName($FEData[$table.'.']['processScript']);
169 if ($incFile) {
170 $this->extScripts[$table]=$incFile;
171 $this->extScriptsConf[$table]=$FEData[$table.'.']['processScript.'];
172 }
173 }
174 }
175 }
176
177 /**
178 * Checking if a "double-post" exists already.
179 * "Double-posting" is if someone refreshes a page with a form for the message board or guestbook and thus submits the element twice. Checking for double-posting prevents the second submission from being stored. This is done by saving the first record with a MD5 hash of the content - if this hash exists already, the record cannot be saved.
180 *
181 * @param string The database table to check
182 * @param string The fieldname from the database table to search
183 * @param integer The hash value to search for.
184 * @return integer The number of found rows. If zero then no "double-post" was found and its all OK.
185 * @access private
186 */
187 function checkDoublePostExist($table,$doublePostField,$key) {
188 return $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
189 '*',
190 $table,
191 $doublePostField . '=' . intval($key)
192 );
193 }
194
195 /**
196 * Creates the double-post hash value from the input array
197 *
198 * @param array The array with key/values to hash
199 * @return integer And unsigned 32bit integer hash
200 * @access private
201 */
202 function calcDoublePostKey($array) {
203 ksort($array); // Sorting by key
204 $doublePostCheckKey = hexdec(substr(md5(serialize($array)),0,8)); // Making key
205 return $doublePostCheckKey;
206 }
207
208 /**
209 * Includes the submit scripts found in ->extScripts (filled in by the start() function)
210 *
211 * @return void
212 * @see tslib_fe::fe_tce(), includeScripts()
213 */
214 function includeScripts() {
215 reset($this->extScripts);
216 while(list($incFile_table,$incFile)=each($this->extScripts)) {
217 if (@is_file($incFile) && $GLOBALS['TSFE']->checkFileInclude($incFile)) {
218 include($incFile); // Always start the incFiles with a check of the object fe_tce. is_object($this);
219 $GLOBALS['TT']->setTSlogMessage('Included '.$incFile,0);
220 } else $GLOBALS['TT']->setTSlogMessage('"'.$incFile.'" was not found!',2);
221 }
222 }
223
224 /**
225 * Method available to the submit scripts for creating insert queries.
226 * Automatically adds tstamp, crdate, cruser_id field/value pairs.
227 * Will allow only field names which are either found in $TCA[...][columns] OR in the $this->extraList
228 * Executes an insert query!
229 *
230 * @param string The table name for which to create the insert statement
231 * @param array Array with key/value pairs being field/values (already escaped)
232 * @return void
233 */
234 function execNEWinsert($table, $dataArr) {
235 $extraList=$this->extraList;
236 if ($GLOBALS['TCA'][$table]['ctrl']['tstamp']) {$field=$GLOBALS['TCA'][$table]['ctrl']['tstamp']; $dataArr[$field]=time(); $extraList.=','.$field;}
237 if ($GLOBALS['TCA'][$table]['ctrl']['crdate']) {$field=$GLOBALS['TCA'][$table]['ctrl']['crdate']; $dataArr[$field]=time(); $extraList.=','.$field;}
238 if ($GLOBALS['TCA'][$table]['ctrl']['cruser_id']) {$field=$GLOBALS['TCA'][$table]['ctrl']['cruser_id']; $dataArr[$field]=0; $extraList.=','.$field;}
239
240 unset($dataArr['uid']); // uid can never be set
241 $insertFields = array();
242
243 foreach($dataArr as $f => $v) {
244 if (t3lib_div::inList($extraList,$f) || isset($GLOBALS['TCA'][$table]['columns'][$f])) {
245 $insertFields[$f] = $v;
246 }
247 }
248
249 $GLOBALS['TYPO3_DB']->exec_INSERTquery($table, $insertFields);
250 }
251
252 /**
253 * Clear cache for page id.
254 * If the page id is the current page, then set_no_cache() is called (so page caching is disabled)
255 *
256 * @param integer The page id for which to clear the cache
257 * @return void
258 * @see tslib_fe::set_no_cache()
259 */
260 function clear_cacheCmd($cacheCmd) {
261 $cacheCmd = intval($cacheCmd);
262
263 if ($cacheCmd) {
264
265 $GLOBALS['typo3CacheManager']->getCache('cache_pages')->flushByTag(
266 'pageId_' . $cacheCmd
267 );
268
269 if ($cacheCmd == intval($GLOBALS['TSFE']->id)) {
270 // Setting no_cache true if the cleared-cache page is the current page!
271 $GLOBALS['TSFE']->set_no_cache();
272 }
273 }
274 }
275
276 /**
277 * Return TypoScript configuration for a table name
278 *
279 * @param string The table name for which to return TypoScript configuration (From TS: FEData.[table])
280 * @return array TypoScript properties from FEData.[table] - if exists.
281 */
282 function getConf($table) {
283 return $this->extScriptsConf[$table];
284 }
285 }
286
287
288
289 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_fetce.php']) {
290 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_fetce.php']);
291 }
292
293 ?>