[TASK] Change "true" into "TRUE"
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_xml.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (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 * Contains class for creating XML output from records
29 *
30 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
31 *
32 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 86: class t3lib_xml
40 * 102: function t3lib_xml($topLevelName)
41 * 113: function setRecFields($table,$list)
42 * 122: function getResult()
43 * 132: function WAPHeader()
44 * 144: function renderHeader()
45 * 155: function renderFooter()
46 * 167: function newLevel($name,$beginEndFlag=0,$params=array())
47 * 192: function output($content)
48 * 208: function indent($b)
49 * 224: function renderRecords($table,$res)
50 * 237: function addRecord($table,$row)
51 * 255: function getRowInXML($table,$row)
52 * 271: function utf8($content)
53 * 281: function substNewline($string)
54 * 292: function fieldWrap($field,$value)
55 * 301: function WAPback()
56 * 315: function addLine($str)
57 *
58 * TOTAL FUNCTIONS: 17
59 * (This index is automatically created/updated by the extension "extdeveval")
60 *
61 */
62
63
64 /**
65 * XML class, Used to create XML output from input rows.
66 * Doesn't contain a lot of advanced features - pretty straight forward, practical stuff
67 * You are encouraged to use this class in your own applications.
68 *
69 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
70 * @package TYPO3
71 * @subpackage t3lib
72 * @see user_xmlversion, user_wapversion
73 */
74 class t3lib_xml {
75 var $topLevelName = 'typo3_test'; // Top element name
76 var $XML_recFields = array(); // Contains a list of fields for each table which should be presented in the XML output
77
78 var $XMLIndent = 0;
79 var $Icode = '';
80 var $XMLdebug = 0;
81 var $includeNonEmptyValues = 0; // if set, all fields from records are rendered no matter their content. If not set, only 'true' (that is '' or zero) fields make it to the document.
82 var $lines = array();
83
84 /**
85 * Constructor, setting topLevelName to the input var
86 *
87 * @param string Top Level Name
88 * @return void
89 */
90 function t3lib_xml($topLevelName) {
91 $this->topLevelName = $topLevelName;
92 }
93
94 /**
95 * When outputting a input record in XML only fields listed in $this->XML_recFields for the current table will be rendered.
96 *
97 * @param string Table name
98 * @param string Commalist of fields names from the table, $table, which is supposed to be rendered in the XML output. If a field is not in this list, it is not rendered.
99 * @return void
100 */
101 function setRecFields($table, $list) {
102 $this->XML_recFields[$table] = $list;
103 }
104
105 /**
106 * Returns the result of the XML rendering, basically this is imploding the internal ->lines array with linebreaks.
107 *
108 * @return string
109 */
110 function getResult() {
111 $content = implode(LF, $this->lines);
112 return $this->output($content);
113 }
114
115 /**
116 * Initialize WML (WAP) document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
117 *
118 * @return void
119 */
120 function WAPHeader() {
121 $this->lines[] = '<?xml version="1.0"?>';
122 $this->lines[] = '<!DOCTYPE ' . $this->topLevelName . ' PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
123 $this->newLevel($this->topLevelName, 1);
124 }
125
126 /**
127 * Initialize "anonymous" XML document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
128 * Encoding is set to UTF-8!
129 *
130 * @return void
131 */
132 function renderHeader() {
133 $this->lines[] = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
134 $this->lines[] = '<!DOCTYPE ' . $this->topLevelName . '>';
135 $this->newLevel($this->topLevelName, 1);
136 }
137
138 /**
139 * Sets the footer (of ->topLevelName)
140 *
141 * @return void
142 */
143 function renderFooter() {
144 $this->newLevel($this->topLevelName, 0);
145 }
146
147 /**
148 * Indents/Outdents a new level named, $name
149 *
150 * @param string The name of the new element for this level
151 * @param boolean If false, then this function call will *end* the level, otherwise create it.
152 * @param array Array of attributes in key/value pairs which will be added to the element (tag), $name
153 * @return void
154 */
155 function newLevel($name, $beginEndFlag = 0, $params = array()) {
156 if ($beginEndFlag) {
157 $pList = '';
158 if (count($params)) {
159 $par = array();
160 foreach ($params as $key => $val) {
161 $par[] = $key . '="' . htmlspecialchars($val) . '"';
162 }
163 $pList = ' ' . implode(' ', $par);
164 }
165 $this->lines[] = $this->Icode . '<' . $name . $pList . '>';
166 $this->indent(1);
167 } else {
168 $this->indent(0);
169 $this->lines[] = $this->Icode . '</' . $name . '>';
170 }
171 }
172
173 /**
174 * Function that will return the content from string $content. If the internal ->XMLdebug flag is set the content returned will be formatted in <pre>-tags
175 *
176 * @param string The XML content to output
177 * @return string Output
178 */
179 function output($content) {
180 if ($this->XMLdebug) {
181 return '<pre>' . htmlspecialchars($content) . '</pre>
182 <hr /><font color="red">Size: ' . strlen($content) . '</font>';
183 } else {
184 return $content;
185 }
186 }
187
188 /**
189 * Increments/Decrements Indentation counter, ->XMLIndent
190 * Sets and returns ->Icode variable which is a line prefix consisting of a number of tab-chars corresponding to the indent-levels of the current posision (->XMLindent)
191 *
192 * @param boolean If TRUE the XMLIndent var is increased, otherwise decreased
193 * @return string ->Icode - the prefix string with TAB-chars.
194 */
195 function indent($b) {
196 if ($b) {
197 $this->XMLIndent++;
198 } else {
199 $this->XMLIndent--;
200 }
201 $this->Icode = '';
202 for ($a = 0; $a < $this->XMLIndent; $a++) {
203 $this->Icode .= TAB;
204 }
205 return $this->Icode;
206 }
207
208 /**
209 * Takes a SQL result for $table and traverses it, adding rows
210 *
211 * @param string Tablename
212 * @param pointer SQL resource pointer, should be reset
213 * @return void
214 */
215 function renderRecords($table, $res) {
216 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
217 $this->addRecord($table, $row);
218 }
219 }
220
221 /**
222 * Adds record, $row, from table, $table, to the internal array of XML-lines
223 *
224 * @param string Table name
225 * @param array The row to add to XML structure from the table name
226 * @return void
227 */
228 function addRecord($table, $row) {
229 $this->lines[] = $this->Icode . '<' . $table . ' uid="' . $row["uid"] . '">';
230 $this->indent(1);
231 $this->getRowInXML($table, $row);
232 $this->indent(0);
233 $this->lines[] = $this->Icode . '</' . $table . '>';
234 }
235
236 /**
237 * Internal function for adding the actual content of the $row from $table to the internal structure.
238 * Notice that only fields from $table that are listed in $this->XML_recFields[$table] (set by setRecFields()) will be rendered (and in the order found in that array!)
239 * Content from the row will be htmlspecialchar()'ed, UTF-8 encoded and have LF (newlines) exchanged for '<newline/>' tags. The element name for a value equals the fieldname from the record.
240 *
241 * @param string Table name
242 * @param array Row from table to add.
243 * @return void
244 * @access private
245 */
246 function getRowInXML($table, $row) {
247 $fields = t3lib_div::trimExplode(',', $this->XML_recFields[$table], 1);
248 foreach ($fields as $field) {
249 if ($row[$field] || $this->includeNonEmptyValues) {
250 $this->lines[] = $this->Icode . $this->fieldWrap($field, $this->substNewline($this->utf8(htmlspecialchars($row[$field]))));
251 }
252 }
253 }
254
255 /**
256 * UTF-8 encodes the input content (from ISO-8859-1!)
257 *
258 * @param string String content to UTF-8 encode
259 * @return string Encoded content.
260 */
261 function utf8($content) {
262 return utf8_encode($content);
263 }
264
265 /**
266 * Substitutes LF characters with a '<newline/>' tag.
267 *
268 * @param string Input value
269 * @return string Processed input value
270 */
271 function substNewline($string) {
272 return str_replace(LF, '<newline/>', $string);
273 }
274
275 /**
276 * Wraps the value in tags with element name, $field.
277 *
278 * @param string Fieldname from a record - will be the element name
279 * @param string Value from the field - will be wrapped in the elements.
280 * @return string The wrapped string.
281 */
282 function fieldWrap($field, $value) {
283 return '<' . $field . '>' . $value . '</' . $field . '>';
284 }
285
286 /**
287 * Creates the BACK button for WAP documents
288 *
289 * @return void
290 */
291 function WAPback() {
292 $this->newLevel('template', 1);
293 $this->newLevel('do', 1, array('type' => 'accept', 'label' => 'Back'));
294 $this->addLine('<prev/>');
295 $this->newLevel('do');
296 $this->newLevel('template');
297 }
298
299 /**
300 * Add a line to the internal XML structure (automatically prefixed with ->Icode.
301 *
302 * @param string Line to add to the $this->lines array
303 * @return void
304 */
305 function addLine($str) {
306 $this->lines[] = $this->Icode . $str;
307 }
308 }
309
310
311 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_xml.php'])) {
312 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_xml.php']);
313 }
314 ?>