aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/develop/utf_normalizer_test.php
blob: af8556507ff885423962824ece9e72efee45689b (plain)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
*
* @package phpBB3
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

if (php_sapi_name() != 'cli')
{
	die("This program must be run from the command line.\n");
}

//
// Security message:
//
// This script is potentially dangerous.
// Remove or comment the next line (die(".... ) to enable this script.
// Do NOT FORGET to either remove this script or disable it after you have used it.
//
die("Please read the first lines of this script for instructions on how to enable it");

set_time_limit(0);
error_reporting(E_ALL);

define('IN_PHPBB', true);
$phpbb_root_path = '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);


/**
* Let's download some files we need
*/
download('http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt');
download('http://www.unicode.org/Public/UNIDATA/UnicodeData.txt');

/**
* Those are the tests we run
*/
$test_suite = array(
	/**
	* NFC
	*   c2 ==  NFC(c1) ==  NFC(c2) ==  NFC(c3)
	*   c4 ==  NFC(c4) ==  NFC(c5)
	*/
	'NFC'	=>	array(
		'c2'	=>	array('c1', 'c2', 'c3'),
		'c4'	=>	array('c4', 'c5')
	),

	/**
	* NFD
	*   c3 ==  NFD(c1) ==  NFD(c2) ==  NFD(c3)
	*   c5 ==  NFD(c4) ==  NFD(c5)
	*/
	'NFD'	=>	array(
		'c3'	=>	array('c1', 'c2', 'c3'),
		'c5'	=>	array('c4', 'c5')
	),

	/**
	* NFKC
	*   c4 == NFKC(c1) == NFKC(c2) == NFKC(c3) == NFKC(c4) == NFKC(c5)
	*/
	'NFKC'	=>	array(
		'c4'	=>	array('c1', 'c2', 'c3', 'c4', 'c5')
	),

	/**
	* NFKD
	*   c5 == NFKD(c1) == NFKD(c2) == NFKD(c3) == NFKD(c4) == NFKD(c5)
	*/
	'NFKD'	=>	array(
		'c5'	=>	array('c1', 'c2', 'c3', 'c4', 'c5')
	)
);

require_once($phpbb_root_path . 'includes/utf/utf_normalizer.' . $phpEx);

$i = $n = 0;
$failed = false;
$tested_chars = array();

$fp = fopen($phpbb_root_path . 'develop/NormalizationTest.txt', 'rb');
while (!feof($fp))
{
	$line = fgets($fp);
	++$n;

	if ($line[0] == '@')
	{
		if ($i)
		{
			echo "done\n";
		}

		$i = 0;
		echo "\n", substr($line, 1), "\n\n";
		continue;
	}

	if (!strpos(' 0123456789ABCDEF', $line[0]))
	{
		continue;
	}

	if (++$i % 100 == 0)
	{
		echo $i, ' ';
	}

	list($c1, $c2, $c3, $c4, $c5) = explode(';', $line);

	if (!strpos($c1, ' '))
	{
		/**
		* We are currently testing a single character, we add it to the list of
		* characters we have processed so that we can exclude it when testing
		* for invariants
		*/
		$tested_chars[$c1] = 1;
	}

	foreach ($test_suite as $form => $serie)
	{
		foreach ($serie as $expected => $tests)
		{
			$hex_expected = ${$expected};
			$utf_expected = hexseq_to_utf($hex_expected);

			foreach ($tests as $test)
			{
				$utf_result = $utf_expected;
				call_user_func(array('utf_normalizer', $form), $utf_result);

				if (strcmp($utf_expected, $utf_result))
				{
					$failed = true;
					$hex_result = utf_to_hexseq($utf_result);

					echo "\nFAILED $expected == $form($test) ($hex_expected != $hex_result)";
				}
			}
		}

		if ($failed)
		{
			die("\n\nFailed at line $n\n");
		}
	}
}
fclose($fp);

/**
* Test for invariants
*/
echo "\n\nTesting for invariants...\n\n";

$fp = fopen($phpbb_root_path . 'develop/UnicodeData.txt', 'rt');

$n = 0;
while (!feof($fp))
{
	if (++$n % 100 == 0)
	{
		echo $n, ' ';
	}

	$line = fgets($fp, 1024);

	if (!$pos = strpos($line, ';'))
	{
		continue;
	}

	$hex_tested = $hex_expected = substr($line, 0, $pos);

	if (isset($tested_chars[$hex_tested]))
	{
		continue;
	}

	$utf_expected = hex_to_utf($hex_expected);

	if ($utf_expected >= UTF8_SURROGATE_FIRST
	 && $utf_expected <= UTF8_SURROGATE_LAST)
	{
		/**
		* Surrogates are illegal on their own, we expect the normalizer
		* to return a replacement char
		*/
		$utf_expected = UTF8_REPLACEMENT;
		$hex_expected = utf_to_hexseq($utf_expected);
	}

	foreach (array('nfc', 'nfkc', 'nfd', 'nfkd') as $form)
	{
		$utf_result = $utf_expected;
		utf_normalizer::$form($utf_result);
		$hex_result = utf_to_hexseq($utf_result);
//		echo "$form($utf_expected) == $utf_result\n";

		if (strcmp($utf_expected, $utf_result))
		{
			$failed = 1;

			echo "\nFAILED $hex_expected == $form($hex_tested) ($hex_expected != $hex_result)";
		}
	}

	if ($failed)
	{
		die("\n\nFailed at line $n\n");
	}
}
fclose($fp);

die("\n\nALL TESTS PASSED SUCCESSFULLY\n");

/**
* Download a file to the develop/ dir
*
* @param	string	$url		URL of the file to download
* @return	null
*/
function download($url)
{
	global $phpbb_root_path;

	if (file_exists($phpbb_root_path . 'develop/' . basename($url)))
	{
		return;
	}

	echo 'Downloading from ', $url, ' ';

	if (!$fpr = fopen($url, 'rb'))
	{
		die("Can't download from $url\nPlease download it yourself and put it in the develop/ dir, kthxbai");
	}

	if (!$fpw = fopen($phpbb_root_path . 'develop/' . basename($url), 'wb'))
	{
		die("Can't open develop/" . basename($url) . " for output... please check your permissions or something");
	}

	$i = 0;
	$chunk = 32768;
	$done = '';

	while (!feof($fpr))
	{
		$i += fwrite($fpw, fread($fpr, $chunk));
		echo str_repeat("\x08", strlen($done));

		$done = ($i >> 10) . ' KiB';
		echo $done;
	}
	fclose($fpr);
	fclose($fpw);

	echo "\n";
}

/**
* Convert a UTF string to a sequence of codepoints in hexadecimal
*
* @param	string	$utf	UTF string
* @return	integer			Unicode codepoints in hex
*/
function utf_to_hexseq($str)
{
	$pos = 0;
	$len = strlen($str);
	$ret = array();

	while ($pos < $len)
	{
		$c = $str[$pos];
		switch ($c & "\xF0")
		{
			case "\xC0":
			case "\xD0":
				$utf_char = substr($str, $pos, 2);
				$pos += 2;
				break;

			case "\xE0":
				$utf_char = substr($str, $pos, 3);
				$pos += 3;
				break;

			case "\xF0":
				$utf_char = substr($str, $pos, 4);
				$pos += 4;
				break;

			default:
				$utf_char = $c;
				++$pos;
		}

		$hex = dechex(utf_to_cp($utf_char));

		if (!isset($hex[3]))
		{
			$hex = substr('000' . $hex, -4);
		}

		$ret[] = $hex;
	}

	return strtr(implode(' ', $ret), 'abcdef', 'ABCDEF');
}

/**
* Convert a UTF-8 char to its codepoint
*
* @param	string	$utf_char	UTF-8 char
* @return	integer				Unicode codepoint
*/
function utf_to_cp($utf_char)
{
	switch (strlen($utf_char))
	{
		case 1:
			return ord($utf_char);

		case 2:
			return ((ord($utf_char[0]) & 0x1F) << 6) | (ord($utf_char[1]) & 0x3F);

		case 3:
			return ((ord($utf_char[0]) & 0x0F) << 12) | ((ord($utf_char[1]) & 0x3F) << 6) | (ord($utf_char[2]) & 0x3F);

		case 4:
			return ((ord($utf_char[0]) & 0x07) << 18) | ((ord($utf_char[1]) & 0x3F) << 12) | ((ord($utf_char[2]) & 0x3F) << 6) | (ord($utf_char[3]) & 0x3F);

		default:
			die('UTF-8 chars can only be 1-4 bytes long');
	}
}

/**
* Return a UTF string formed from a sequence of codepoints in hexadecimal
*
* @param	string	$seq		Sequence of codepoints, separated with a space
* @return	string				UTF-8 string
*/
function hexseq_to_utf($seq)
{
	return implode('', array_map('hex_to_utf', explode(' ', $seq)));
}

/**
* Convert a codepoint in hexadecimal to a UTF-8 char
*
* @param	string	$hex		Codepoint, in hexadecimal
* @return	string				UTF-8 char
*/
function hex_to_utf($hex)
{
	return cp_to_utf(hexdec($hex));
}

/**
* Convert a codepoint to a UTF-8 char
*
* @param	integer	$cp			Unicode codepoint
* @return	string				UTF-8 string
*/
function cp_to_utf($cp)
{
	if ($cp > 0xFFFF)
	{
		return chr(0xF0 | ($cp >> 18)) . chr(0x80 | (($cp >> 12) & 0x3F)) . chr(0x80 | (($cp >> 6) & 0x3F)) . chr(0x80 | ($cp & 0x3F));
	}
	else if ($cp > 0x7FF)
	{
		return chr(0xE0 | ($cp >> 12)) . chr(0x80 | (($cp >> 6) & 0x3F)) . chr(0x80 | ($cp & 0x3F));
	}
	else if ($cp > 0x7F)
	{
		return chr(0xC0 | ($cp >> 6)) . chr(0x80 | ($cp & 0x3F));
	}
	else
	{
		return chr($cp);
	}
}
pan class="hl str"> * Sets account presence. No additional info required (default is "online" status) * @param $message online, offline... * @param $type dnd, away, chat, xa or nothing * @param $unavailable set this to true if you want to become unavailable * @access public * @return bool */ function send_presence($message = '', $type = '', $unavailable = false) { if (!isset($this->session['jid'])) { $this->add_to_log('ERROR: send_presence() - Cannot set presence at this point, no jid given.'); return false; } $type = strtolower($type); $type = (in_array($type, array('dnd', 'away', 'chat', 'xa'))) ? '<show>'. $type .'</show>' : ''; $unavailable = ($unavailable) ? " type='unavailable'" : ''; $message = ($message) ? '<status>' . utf8_htmlspecialchars($message) .'</status>' : ''; $this->session['sent_presence'] = !$unavailable; return $this->send("<presence$unavailable>" . $type . $message . '</presence>'); } /** * This handles all the different XML elements * @param array $xml * @access public * @return bool */ function response($xml) { if (!is_array($xml) || !sizeof($xml)) { return false; } // did we get multiple elements? do one after another // array('message' => ..., 'presence' => ...) if (sizeof($xml) > 1) { foreach ($xml as $key => $value) { $this->response(array($key => $value)); } return; } else { // or even multiple elements of the same type? // array('message' => array(0 => ..., 1 => ...)) if (sizeof(reset($xml)) > 1) { foreach (reset($xml) as $value) { $this->response(array(key($xml) => array(0 => $value))); } return; } } switch (key($xml)) { case 'stream:stream': // Connection initialised (or after authentication). Not much to do here... if (isset($xml['stream:stream'][0]['#']['stream:features'])) { // we already got all info we need $this->features = $xml['stream:stream'][0]['#']; } else { $this->features = $this->listen(); } $second_time = isset($this->session['id']); $this->session['id'] = $xml['stream:stream'][0]['@']['id']; if ($second_time) { // If we are here for the second time after TLS, we need to continue logging in return $this->login(); } // go on with authentication? if (isset($this->features['stream:features'][0]['#']['bind']) || !empty($this->session['tls'])) { return $this->response($this->features); } break; case 'stream:features': // Resource binding after successful authentication if (isset($this->session['authenticated'])) { // session required? $this->session['sess_required'] = isset($xml['stream:features'][0]['#']['session']); $this->send("<iq type='set' id='bind_1'> <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'> <resource>" . utf8_htmlspecialchars($this->resource) . '</resource> </bind> </iq>'); return $this->response($this->listen()); } // Let's use TLS if SSL is not enabled and we can actually use it if (!$this->session['ssl'] && self::can_use_tls() && self::can_use_ssl() && isset($xml['stream:features'][0]['#']['starttls'])) { $this->add_to_log('Switching to TLS.'); $this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>\n"); return $this->response($this->listen()); } // Does the server support SASL authentication? // I hope so, because we do (and no other method). if (isset($xml['stream:features'][0]['#']['mechanisms'][0]['@']['xmlns']) && $xml['stream:features'][0]['#']['mechanisms'][0]['@']['xmlns'] == 'urn:ietf:params:xml:ns:xmpp-sasl') { // Now decide on method $methods = array(); foreach ($xml['stream:features'][0]['#']['mechanisms'][0]['#']['mechanism'] as $value) { $methods[] = $value['#']; } // we prefer DIGEST-MD5 // we don't want to use plain authentication (neither does the server usually) if no encryption is in place // http://www.xmpp.org/extensions/attic/jep-0078-1.7.html // The plaintext mechanism SHOULD NOT be used unless the underlying stream is encrypted (using SSL or TLS) // and the client has verified that the server certificate is signed by a trusted certificate authority. if (in_array('DIGEST-MD5', $methods)) { $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>"); } else if (in_array('PLAIN', $methods) && ($this->session['ssl'] || !empty($this->session['tls']))) { // http://www.ietf.org/rfc/rfc4616.txt (PLAIN SASL Mechanism) $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" . base64_encode($this->username . '@' . $this->server . chr(0) . $this->username . chr(0) . $this->password) . '</auth>'); } else if (in_array('ANONYMOUS', $methods)) { $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>"); } else { // not good... $this->add_to_log('Error: No authentication method supported.'); $this->disconnect(); return false; } return $this->response($this->listen()); } else { // ok, this is it. bye. $this->add_to_log('Error: Server does not offer SASL authentication.'); $this->disconnect(); return false; } break; case 'challenge': // continue with authentication...a challenge literally -_- $decoded = base64_decode($xml['challenge'][0]['#']); $decoded = $this->parse_data($decoded); if (!isset($decoded['digest-uri'])) { $decoded['digest-uri'] = 'xmpp/'. $this->server; } // better generate a cnonce, maybe it's needed $decoded['cnonce'] = base64_encode(md5(uniqid(mt_rand(), true))); // second challenge? if (isset($decoded['rspauth'])) { $this->send("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>"); } else { // Make sure we only use 'auth' for qop (relevant for $this->encrypt_password()) // If the <response> is choking up on the changed parameter we may need to adjust encrypt_password() directly if (isset($decoded['qop']) && $decoded['qop'] != 'auth' && strpos($decoded['qop'], 'auth') !== false) { $decoded['qop'] = 'auth'; } $response = array( 'username' => $this->username, 'response' => $this->encrypt_password(array_merge($decoded, array('nc' => '00000001'))), 'charset' => 'utf-8', 'nc' => '00000001', 'qop' => 'auth', // only auth being supported ); foreach (array('nonce', 'digest-uri', 'realm', 'cnonce') as $key) { if (isset($decoded[$key])) { $response[$key] = $decoded[$key]; } } $this->send("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" . base64_encode($this->implode_data($response)) . '</response>'); } return $this->response($this->listen()); break; case 'failure': $this->add_to_log('Error: Server sent "failure".'); $this->disconnect(); return false; break; case 'proceed': // continue switching to TLS $meta = stream_get_meta_data($this->connection); socket_set_blocking($this->connection, 1); if (!stream_socket_enable_crypto($this->connection, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { $this->add_to_log('Error: TLS mode change failed.'); return false; } socket_set_blocking($this->connection, $meta['blocked']); $this->session['tls'] = true; // new stream $this->send("<?xml version='1.0' encoding='UTF-8' ?" . ">\n"); $this->send("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>\n"); return $this->response($this->listen()); break; case 'success': // Yay, authentication successful. $this->send("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>\n"); $this->session['authenticated'] = true; // we have to wait for another response return $this->response($this->listen()); break; case 'iq': // we are not interested in IQs we did not expect if (!isset($xml['iq'][0]['@']['id'])) { return false; } // multiple possibilities here switch ($xml['iq'][0]['@']['id']) { case 'bind_1': $this->session['jid'] = $xml['iq'][0]['#']['bind'][0]['#']['jid'][0]['#']; // and (maybe) yet another request to be able to send messages *finally* if ($this->session['sess_required']) { $this->send("<iq to='{$this->server}' type='set' id='sess_1'> <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/> </iq>"); return $this->response($this->listen()); } return true; break; case 'sess_1': return true; break; case 'reg_1': $this->send("<iq type='set' id='reg_2'> <query xmlns='jabber:iq:register'> <username>" . utf8_htmlspecialchars($this->username) . "</username> <password>" . utf8_htmlspecialchars($this->password) . "</password> </query> </iq>"); return $this->response($this->listen()); break; case 'reg_2': // registration end if (isset($xml['iq'][0]['#']['error'])) { $this->add_to_log('Warning: Registration failed.'); return false; } return true; break; case 'unreg_1': return true; break; default: $this->add_to_log('Notice: Received unexpected IQ.'); return false; break; } break; case 'message': // we are only interested in content... if (!isset($xml['message'][0]['#']['body'])) { return false; } $message['body'] = $xml['message'][0]['#']['body'][0]['#']; $message['from'] = $xml['message'][0]['@']['from']; if (isset($xml['message'][0]['#']['subject'])) { $message['subject'] = $xml['message'][0]['#']['subject'][0]['#']; } $this->session['messages'][] = $message; break; default: // hm...don't know this response $this->add_to_log('Notice: Unknown server response (' . key($xml) . ')'); return false; break; } } function send_message($to, $text, $subject = '', $type = 'normal') { if (!isset($this->session['jid'])) { return false; } if (!in_array($type, array('chat', 'normal', 'error', 'groupchat', 'headline'))) { $type = 'normal'; } return $this->send("<message from='" . utf8_htmlspecialchars($this->session['jid']) . "' to='" . utf8_htmlspecialchars($to) . "' type='$type' id='" . uniqid('msg') . "'> <subject>" . utf8_htmlspecialchars($subject) . "</subject> <body>" . utf8_htmlspecialchars($text) . "</body> </message>" ); } /** * Encrypts a password as in RFC 2831 * @param array $data Needs data from the client-server connection * @access public * @return string */ function encrypt_password($data) { // let's me think about <challenge> again... foreach (array('realm', 'cnonce', 'digest-uri') as $key) { if (!isset($data[$key])) { $data[$key] = ''; } } $pack = md5($this->username . ':' . $data['realm'] . ':' . $this->password); if (isset($data['authzid'])) { $a1 = pack('H32', $pack) . sprintf(':%s:%s:%s', $data['nonce'], $data['cnonce'], $data['authzid']); } else { $a1 = pack('H32', $pack) . sprintf(':%s:%s', $data['nonce'], $data['cnonce']); } // should be: qop = auth $a2 = 'AUTHENTICATE:'. $data['digest-uri']; return md5(sprintf('%s:%s:%s:%s:%s:%s', md5($a1), $data['nonce'], $data['nc'], $data['cnonce'], $data['qop'], md5($a2))); } /** * parse_data like a="b",c="d",... or like a="a, b", c, d="e", f=g,... * @param string $data * @access public * @return array a => b ... */ function parse_data($data) { $data = explode(',', $data); $pairs = array(); $key = false; foreach ($data as $pair) { $dd = strpos($pair, '='); if ($dd) { $key = trim(substr($pair, 0, $dd)); $pairs[$key] = trim(trim(substr($pair, $dd + 1)), '"'); } else if (strpos(strrev(trim($pair)), '"') === 0 && $key) { // We are actually having something left from "a, b" values, add it to the last one we handled. $pairs[$key] .= ',' . trim(trim($pair), '"'); continue; } } return $pairs; } /** * opposite of jabber::parse_data() * @param array $data * @access public * @return string */ function implode_data($data) { $return = array(); foreach ($data as $key => $value) { $return[] = $key . '="' . $value . '"'; } return implode(',', $return); } /** * xmlize() * @author Hans Anderson * @copyright Hans Anderson / http://www.hansanderson.com/php/xml/ */ function xmlize($data, $skip_white = 1, $encoding = 'UTF-8') { $data = trim($data); if (substr($data, 0, 5) != '<?xml') { // mod $data = '<root>'. $data . '</root>'; } $vals = $index = $array = array(); $parser = xml_parser_create($encoding); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $skip_white); xml_parse_into_struct($parser, $data, $vals, $index); xml_parser_free($parser); $i = 0; $tagname = $vals[$i]['tag']; $array[$tagname][0]['@'] = (isset($vals[$i]['attributes'])) ? $vals[$i]['attributes'] : array(); $array[$tagname][0]['#'] = $this->_xml_depth($vals, $i); if (substr($data, 0, 5) != '<?xml') { $array = $array['root'][0]['#']; } return $array; } /** * _xml_depth() * @author Hans Anderson * @copyright Hans Anderson / http://www.hansanderson.com/php/xml/ */ function _xml_depth($vals, &$i) { $children = array(); if (isset($vals[$i]['value'])) { array_push($children, $vals[$i]['value']); } while (++$i < sizeof($vals)) { switch ($vals[$i]['type']) { case 'open': $tagname = (isset($vals[$i]['tag'])) ? $vals[$i]['tag'] : ''; $size = (isset($children[$tagname])) ? sizeof($children[$tagname]) : 0; if (isset($vals[$i]['attributes'])) { $children[$tagname][$size]['@'] = $vals[$i]['attributes']; } $children[$tagname][$size]['#'] = $this->_xml_depth($vals, $i); break; case 'cdata': array_push($children, $vals[$i]['value']); break; case 'complete': $tagname = $vals[$i]['tag']; $size = (isset($children[$tagname])) ? sizeof($children[$tagname]) : 0; $children[$tagname][$size]['#'] = (isset($vals[$i]['value'])) ? $vals[$i]['value'] : array(); if (isset($vals[$i]['attributes'])) { $children[$tagname][$size]['@'] = $vals[$i]['attributes']; } break; case 'close': return $children; break; } } return $children; } }