2 /***************************************************************
5 * (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 * Contains class for creating XML output from records
31 * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
33 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
36 * [CLASS/FUNCTION INDEX of SCRIPT]
41 * 102: function t3lib_xml($topLevelName)
42 * 113: function setRecFields($table,$list)
43 * 122: function getResult()
44 * 132: function WAPHeader()
45 * 144: function renderHeader()
46 * 155: function renderFooter()
47 * 167: function newLevel($name,$beginEndFlag=0,$params=array())
48 * 192: function output($content)
49 * 208: function indent($b)
50 * 224: function renderRecords($table,$res)
51 * 237: function addRecord($table,$row)
52 * 255: function getRowInXML($table,$row)
53 * 271: function utf8($content)
54 * 281: function substNewline($string)
55 * 292: function fieldWrap($field,$value)
56 * 301: function WAPback()
57 * 315: function addLine($str)
60 * (This index is automatically created/updated by the extension "extdeveval")
77 * XML class, Used to create XML output from input rows.
78 * Doesn't contain a lot of advanced features - pretty straight forward, practical stuff
79 * You are encouraged to use this class in your own applications.
81 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
84 * @see user_xmlversion, user_wapversion
87 var $topLevelName = 'typo3_test'; // Top element name
88 var $XML_recFields = array(); // Contains a list of fields for each table which should be presented in the XML output
93 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.
97 * Constructor, setting topLevelName to the input var
99 * @param string Top Level Name
102 function t3lib_xml($topLevelName) {
103 $this->topLevelName
= $topLevelName;
107 * When outputting a input record in XML only fields listed in $this->XML_recFields for the current table will be rendered.
109 * @param string Table name
110 * @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.
113 function setRecFields($table,$list) {
114 $this->XML_recFields
[$table]=$list;
118 * Returns the result of the XML rendering, basically this is imploding the internal ->lines array with linebreaks.
122 function getResult() {
123 $content = implode(chr(10),$this->lines
);
124 return $this->output($content);
128 * Initialize WML (WAP) document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
132 function WAPHeader() {
133 $this->lines
[]='<?xml version="1.0"?>';
134 $this->lines
[]='<!DOCTYPE '.$this->topLevelName
.' PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
135 $this->newLevel($this->topLevelName
,1);
139 * Initialize "anonymous" XML document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
140 * Encoding is set to UTF-8!
144 function renderHeader() {
145 $this->lines
[]='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
146 $this->lines
[]='<!DOCTYPE '.$this->topLevelName
.'>';
147 $this->newLevel($this->topLevelName
,1);
151 * Sets the footer (of ->topLevelName)
155 function renderFooter() {
156 $this->newLevel($this->topLevelName
,0);
160 * Indents/Outdents a new level named, $name
162 * @param string The name of the new element for this level
163 * @param boolean If false, then this function call will *end* the level, otherwise create it.
164 * @param array Array of attributes in key/value pairs which will be added to the element (tag), $name
167 function newLevel($name,$beginEndFlag=0,$params=array()) {
170 if (count($params)) {
173 while(list($key,$val)=each($params)) {
174 $par[]=$key.'="'.htmlspecialchars($val).'"';
176 $pList=' '.implode(' ',$par);
178 $this->lines
[]=$this->Icode
.'<'.$name.$pList.'>';
182 $this->lines
[]=$this->Icode
.'</'.$name.'>';
187 * 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
189 * @param string The XML content to output
190 * @return string Output
192 function output($content) {
193 if ($this->XMLdebug
) {
194 return '<pre>'.htmlspecialchars($content).'</pre>
195 <hr /><font color="red">Size: '.strlen($content).'</font>';
202 * Increments/Decrements Indentation counter, ->XMLIndent
203 * 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)
205 * @param boolean If true the XMLIndent var is increased, otherwise decreased
206 * @return string ->Icode - the prefix string with TAB-chars.
208 function indent($b) {
209 if ($b) $this->XMLIndent++
; else $this->XMLIndent
--;
211 for ($a=0;$a<$this->XMLIndent
;$a++
) {
212 $this->Icode
.=chr(9);
218 * Takes a SQL result for $table and traverses it, adding rows
220 * @param string Tablename
221 * @param pointer SQL resource pointer, should be reset
224 function renderRecords($table,$res) {
225 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
226 $this->addRecord($table,$row);
231 * Adds record, $row, from table, $table, to the internal array of XML-lines
233 * @param string Table name
234 * @param array The row to add to XML structure from the table name
237 function addRecord($table,$row) {
238 $this->lines
[]=$this->Icode
.'<'.$table.' uid="'.$row["uid"].'">';
240 $this->getRowInXML($table,$row);
242 $this->lines
[]=$this->Icode
.'</'.$table.'>';
246 * Internal function for adding the actual content of the $row from $table to the internal structure.
247 * 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!)
248 * Content from the row will be htmlspecialchar()'ed, UTF-8 encoded and have chr(10) (newlines) exchanged for '<newline/>' tags. The element name for a value equals the fieldname from the record.
250 * @param string Table name
251 * @param array Row from table to add.
255 function getRowInXML($table,$row) {
256 $fields = t3lib_div
::trimExplode(',',$this->XML_recFields
[$table],1);
258 while(list(,$field)=each($fields)) {
259 if ($row[$field] ||
$this->includeNonEmptyValues
) {
260 $this->lines
[]=$this->Icode
.$this->fieldWrap($field,$this->substNewline($this->utf8(htmlspecialchars($row[$field]))));
266 * UTF-8 encodes the input content (from ISO-8859-1!)
268 * @param string String content to UTF-8 encode
269 * @return string Encoded content.
271 function utf8($content) {
272 return utf8_encode($content);
276 * Substitutes chr(10) characters with a '<newline/>' tag.
278 * @param string Input value
279 * @return string Processed input value
281 function substNewline($string) {
282 return ereg_replace(chr(10),'<newline/>',$string);
286 * Wraps the value in tags with element name, $field.
288 * @param string Fieldname from a record - will be the element name
289 * @param string Value from the field - will be wrapped in the elements.
290 * @return string The wrapped string.
292 function fieldWrap($field,$value) {
293 return '<'.$field.'>'.$value.'</'.$field.'>';
297 * Creates the BACK button for WAP documents
302 $this->newLevel('template',1);
303 $this->newLevel('do',1,array('type'=>'accept','label'=>'Back'));
304 $this->addLine('<prev/>');
305 $this->newLevel('do');
306 $this->newLevel('template');
310 * Add a line to the internal XML structure (automatically prefixed with ->Icode.
312 * @param string Line to add to the $this->lines array
315 function addLine($str) {
316 $this->lines
[]=$this->Icode
.$str;
321 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_xml.php']) {
322 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_xml.php']);