File: class.t3lib_xml.php

package info (click to toggle)
typo3-src 4.0.2%2Bdebian-3
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 29,856 kB
  • ctags: 33,382
  • sloc: php: 134,523; xml: 6,976; sql: 1,084; sh: 168; makefile: 45
file content (324 lines) | stat: -rw-r--r-- 9,944 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/***************************************************************
*  Copyright notice
*
*  (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*  A copy is found in the textfile GPL.txt and important notices to the license
*  from the author is found in LICENSE.txt distributed with these scripts.
*
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * Contains class for creating XML output from records
 *
 * $Id: class.t3lib_xml.php 593 2005-04-01 14:37:15Z typo3 $
 * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
 *
 * @author	Kasper Skaarhoj <kasperYYYY@typo3.com>
 */
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 *
 *
 *   86: class t3lib_xml
 *  102:     function t3lib_xml($topLevelName)
 *  113:     function setRecFields($table,$list)
 *  122:     function getResult()
 *  132:     function WAPHeader()
 *  144:     function renderHeader()
 *  155:     function renderFooter()
 *  167:     function newLevel($name,$beginEndFlag=0,$params=array())
 *  192:     function output($content)
 *  208:     function indent($b)
 *  224:     function renderRecords($table,$res)
 *  237:     function addRecord($table,$row)
 *  255:     function getRowInXML($table,$row)
 *  271:     function utf8($content)
 *  281:     function substNewline($string)
 *  292:     function fieldWrap($field,$value)
 *  301:     function WAPback()
 *  315:     function addLine($str)
 *
 * TOTAL FUNCTIONS: 17
 * (This index is automatically created/updated by the extension "extdeveval")
 *
 */













/**
 * XML class, Used to create XML output from input rows.
 * Doesn't contain a lot of advanced features - pretty straight forward, practical stuff
 * You are encouraged to use this class in your own applications.
 *
 * @author	Kasper Skaarhoj <kasperYYYY@typo3.com>
 * @package TYPO3
 * @subpackage t3lib
 * @see user_xmlversion, user_wapversion
 */
class t3lib_xml {
	var $topLevelName = 'typo3_test';	// Top element name
	var $XML_recFields = array();		// Contains a list of fields for each table which should be presented in the XML output

	var $XMLIndent=0;
	var $Icode='';
	var $XMLdebug=0;
	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.
	var $lines=array();

	/**
	 * Constructor, setting topLevelName to the input var
	 *
	 * @param	string		Top Level Name
	 * @return	void
	 */
	function t3lib_xml($topLevelName)	{
		$this->topLevelName = $topLevelName;
	}

	/**
	 * When outputting a input record in XML only fields listed in $this->XML_recFields for the current table will be rendered.
	 *
	 * @param	string		Table name
	 * @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.
	 * @return	void
	 */
	function setRecFields($table,$list)	{
		$this->XML_recFields[$table]=$list;
	}

	/**
	 * Returns the result of the XML rendering, basically this is imploding the internal ->lines array with linebreaks.
	 *
	 * @return	string
	 */
	function getResult()	{
		$content = implode(chr(10),$this->lines);
		return $this->output($content);
	}

	/**
	 * Initialize WML (WAP) document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
	 *
	 * @return	void
	 */
	function WAPHeader()	{
		$this->lines[]='<?xml version="1.0"?>';
		$this->lines[]='<!DOCTYPE '.$this->topLevelName.' PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
		$this->newLevel($this->topLevelName,1);
	}

	/**
	 * Initialize "anonymous" XML document with <?xml + <!DOCTYPE header tags and setting ->topLevelName as the first level.
	 * Encoding is set to UTF-8!
	 *
	 * @return	void
	 */
	function renderHeader()	{
		$this->lines[]='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
		$this->lines[]='<!DOCTYPE '.$this->topLevelName.'>';
		$this->newLevel($this->topLevelName,1);
	}

	/**
	 * Sets the footer (of ->topLevelName)
	 *
	 * @return	void
	 */
	function renderFooter()	{
		$this->newLevel($this->topLevelName,0);
	}

	/**
	 * Indents/Outdents a new level named, $name
	 *
	 * @param	string		The name of the new element for this level
	 * @param	boolean		If false, then this function call will *end* the level, otherwise create it.
	 * @param	array		Array of attributes in key/value pairs which will be added to the element (tag), $name
	 * @return	void
	 */
	function newLevel($name,$beginEndFlag=0,$params=array())	{
		if ($beginEndFlag)	{
			$pList='';
			if (count($params))	{
				$par=array();
				reset($params);
				while(list($key,$val)=each($params))	{
					$par[]=$key.'="'.htmlspecialchars($val).'"';
				}
				$pList=' '.implode(' ',$par);
			}
			$this->lines[]=$this->Icode.'<'.$name.$pList.'>';
			$this->indent(1);
		} else {
			$this->indent(0);
			$this->lines[]=$this->Icode.'</'.$name.'>';
		}
	}

	/**
	 * 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
	 *
	 * @param	string		The XML content to output
	 * @return	string		Output
	 */
	function output($content)	{
		if ($this->XMLdebug)	{
			return '<pre>'.htmlspecialchars($content).'</pre>
			<hr /><font color="red">Size: '.strlen($content).'</font>';
		} else {
			return $content;
		}
	}

	/**
	 * Increments/Decrements Indentation counter, ->XMLIndent
	 * 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)
	 *
	 * @param	boolean		If true the XMLIndent var is increased, otherwise decreased
	 * @return	string		->Icode - the prefix string with TAB-chars.
	 */
	function indent($b)	{
		if ($b)	$this->XMLIndent++; else $this->XMLIndent--;
		$this->Icode='';
		for ($a=0;$a<$this->XMLIndent;$a++)	{
			$this->Icode.=chr(9);
		}
		return $this->Icode;
	}

	/**
	 * Takes a SQL result for $table and traverses it, adding rows
	 *
	 * @param	string		Tablename
	 * @param	pointer		SQL resource pointer, should be reset
	 * @return	void
	 */
	function renderRecords($table,$res) {
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))	{
			$this->addRecord($table,$row);
		}
	}

	/**
	 * Adds record, $row, from table, $table, to the internal array of XML-lines
	 *
	 * @param	string		Table name
	 * @param	array		The row to add to XML structure from the table name
	 * @return	void
	 */
	function addRecord($table,$row)	{
		$this->lines[]=$this->Icode.'<'.$table.' uid="'.$row["uid"].'">';
			$this->indent(1);
			$this->getRowInXML($table,$row);
			$this->indent(0);
		$this->lines[]=$this->Icode.'</'.$table.'>';
	}

	/**
	 * Internal function for adding the actual content of the $row from $table to the internal structure.
	 * 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!)
	 * 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.
	 *
	 * @param	string		Table name
	 * @param	array		Row from table to add.
	 * @return	void
	 * @access private
	 */
	function getRowInXML($table,$row)	{
		$fields = t3lib_div::trimExplode(',',$this->XML_recFields[$table],1);
		reset($fields);
		while(list(,$field)=each($fields))	{
			if ($row[$field] || $this->includeNonEmptyValues)	{
				$this->lines[]=$this->Icode.$this->fieldWrap($field,$this->substNewline($this->utf8(htmlspecialchars($row[$field]))));
			}
		}
	}

	/**
	 * UTF-8 encodes the input content (from ISO-8859-1!)
	 *
	 * @param	string		String content to UTF-8 encode
	 * @return	string		Encoded content.
	 */
	function utf8($content)	{
		return utf8_encode($content);
	}

	/**
	 * Substitutes chr(10) characters with a '<newline/>' tag.
	 *
	 * @param	string		Input value
	 * @return	string		Processed input value
	 */
	function substNewline($string)	{
		return ereg_replace(chr(10),'<newline/>',$string);
	}

	/**
	 * Wraps the value in tags with element name, $field.
	 *
	 * @param	string		Fieldname from a record - will be the element name
	 * @param	string		Value from the field - will be wrapped in the elements.
	 * @return	string		The wrapped string.
	 */
	function fieldWrap($field,$value)	{
		return '<'.$field.'>'.$value.'</'.$field.'>';
	}

	/**
	 * Creates the BACK button for WAP documents
	 *
	 * @return	void
	 */
	function WAPback()	{
		$this->newLevel('template',1);
			$this->newLevel('do',1,array('type'=>'accept','label'=>'Back'));
			$this->addLine('<prev/>');
			$this->newLevel('do');
		$this->newLevel('template');
	}

	/**
	 * Add a line to the internal XML structure (automatically prefixed with ->Icode.
	 *
	 * @param	string		Line to add to the $this->lines array
	 * @return	void
	 */
	function addLine($str)	{
		$this->lines[]=$this->Icode.$str;
	}
}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_xml.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_xml.php']);
}
?>