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