/usr/share/php/cake/libs/xml.php is in cakephp 1.3.7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 | <?php
/**
* XML handling for Cake.
*
* The methods in these classes enable the datasources that use XML to work.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP v .0.10.3.1400
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::import('Core', 'Set');
/**
* XML node.
*
* Single XML node in an XML tree.
*
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP v .0.10.3.1400
*/
class XmlNode extends Object {
/**
* Name of node
*
* @var string
* @access public
*/
var $name = null;
/**
* Node namespace
*
* @var string
* @access public
*/
var $namespace = null;
/**
* Namespaces defined for this node and all child nodes
*
* @var array
* @access public
*/
var $namespaces = array();
/**
* Value of node
*
* @var string
* @access public
*/
var $value;
/**
* Attributes on this node
*
* @var array
* @access public
*/
var $attributes = array();
/**
* This node's children
*
* @var array
* @access public
*/
var $children = array();
/**
* Reference to parent node.
*
* @var XmlNode
* @access private
*/
var $__parent = null;
/**
* Constructor.
*
* @param string $name Node name
* @param array $attributes Node attributes
* @param mixed $value Node contents (text)
* @param array $children Node children
*/
function __construct($name = null, $value = null, $namespace = null) {
if (strpos($name, ':') !== false) {
list($prefix, $name) = explode(':', $name);
if (!$namespace) {
$namespace = $prefix;
}
}
$this->name = $name;
if ($namespace) {
$this->namespace = $namespace;
}
if (is_array($value) || is_object($value)) {
$this->normalize($value);
} elseif (!empty($value) || $value === 0 || $value === '0') {
$this->createTextNode($value);
}
}
/**
* Adds a namespace to the current node
*
* @param string $prefix The namespace prefix
* @param string $url The namespace DTD URL
* @return void
*/
function addNamespace($prefix, $url) {
if ($ns = Xml::addGlobalNs($prefix, $url)) {
$this->namespaces = array_merge($this->namespaces, $ns);
return true;
}
return false;
}
/**
* Adds a namespace to the current node
*
* @param string $prefix The namespace prefix
* @param string $url The namespace DTD URL
* @return void
*/
function removeNamespace($prefix) {
if (Xml::removeGlobalNs($prefix)) {
return true;
}
return false;
}
/**
* Creates an XmlNode object that can be appended to this document or a node in it
*
* @param string $name Node name
* @param string $value Node value
* @param string $namespace Node namespace
* @return object XmlNode
*/
function &createNode($name = null, $value = null, $namespace = false) {
$node =& new XmlNode($name, $value, $namespace);
$node->setParent($this);
return $node;
}
/**
* Creates an XmlElement object that can be appended to this document or a node in it
*
* @param string $name Element name
* @param string $value Element value
* @param array $attributes Element attributes
* @param string $namespace Node namespace
* @return object XmlElement
*/
function &createElement($name = null, $value = null, $attributes = array(), $namespace = false) {
$element =& new XmlElement($name, $value, $attributes, $namespace);
$element->setParent($this);
return $element;
}
/**
* Creates an XmlTextNode object that can be appended to this document or a node in it
*
* @param string $value Node value
* @return object XmlTextNode
*/
function &createTextNode($value = null) {
$node = new XmlTextNode($value);
$node->setParent($this);
return $node;
}
/**
* Gets the XML element properties from an object.
*
* @param object $object Object to get properties from
* @return array Properties from object
* @access public
*/
function normalize($object, $keyName = null, $options = array()) {
if (is_a($object, 'XmlNode')) {
return $object;
}
$name = null;
$options += array('format' => 'attributes');
if ($keyName !== null && !is_numeric($keyName)) {
$name = $keyName;
} elseif (!empty($object->_name_)) {
$name = $object->_name_;
} elseif (isset($object->name)) {
$name = $object->name;
} elseif ($options['format'] == 'attributes') {
$name = get_class($object);
}
$tagOpts = $this->__tagOptions($name);
if ($tagOpts === false) {
return;
}
if (isset($tagOpts['name'])) {
$name = $tagOpts['name'];
} elseif ($name != strtolower($name) && $options['slug'] !== false) {
$name = Inflector::slug(Inflector::underscore($name));
}
if (!empty($name)) {
$node =& $this->createElement($name);
} else {
$node =& $this;
}
$namespace = array();
$attributes = array();
$children = array();
$chldObjs = array();
if (is_object($object)) {
$chldObjs = get_object_vars($object);
} elseif (is_array($object)) {
$chldObjs = $object;
} elseif (!empty($object) || $object === 0 || $object === '0') {
$node->createTextNode($object);
}
$attr = array();
if (isset($tagOpts['attributes'])) {
$attr = $tagOpts['attributes'];
}
if (isset($tagOpts['value']) && isset($chldObjs[$tagOpts['value']])) {
$node->createTextNode($chldObjs[$tagOpts['value']]);
unset($chldObjs[$tagOpts['value']]);
}
$n = $name;
if (isset($chldObjs['_name_'])) {
$n = null;
unset($chldObjs['_name_']);
}
$c = 0;
foreach ($chldObjs as $key => $val) {
if (in_array($key, $attr) && !is_object($val) && !is_array($val)) {
$attributes[$key] = $val;
} else {
if (!isset($tagOpts['children']) || $tagOpts['children'] === array() || (is_array($tagOpts['children']) && in_array($key, $tagOpts['children']))) {
if (!is_numeric($key)) {
$n = $key;
}
if (is_array($val)) {
foreach ($val as $n2 => $obj2) {
if (is_numeric($n2)) {
$n2 = $n;
}
$node->normalize($obj2, $n2, $options);
}
} else {
if (is_object($val)) {
$node->normalize($val, $n, $options);
} elseif ($options['format'] == 'tags' && $this->__tagOptions($key) !== false) {
$tmp =& $node->createElement($key);
if (!empty($val) || $val === 0 || $val === '0') {
$tmp->createTextNode($val);
}
} elseif ($options['format'] == 'attributes') {
$node->addAttribute($key, $val);
}
}
}
}
$c++;
}
if (!empty($name)) {
return $node;
}
return $children;
}
/**
* Gets the tag-specific options for the given node name
*
* @param string $name XML tag name
* @param string $option The specific option to query. Omit for all options
* @return mixed A specific option value if $option is specified, otherwise an array of all options
* @access private
*/
function __tagOptions($name, $option = null) {
if (isset($this->__tags[$name])) {
$tagOpts = $this->__tags[$name];
} elseif (isset($this->__tags[strtolower($name)])) {
$tagOpts = $this->__tags[strtolower($name)];
} else {
return null;
}
if ($tagOpts === false) {
return false;
}
if (empty($option)) {
return $tagOpts;
}
if (isset($tagOpts[$option])) {
return $tagOpts[$option];
}
return null;
}
/**
* Returns the fully-qualified XML node name, with namespace
*
* @access public
*/
function name() {
if (!empty($this->namespace)) {
$_this =& XmlManager::getInstance();
if (!isset($_this->options['verifyNs']) || !$_this->options['verifyNs'] || in_array($this->namespace, array_keys($_this->namespaces))) {
return $this->namespace . ':' . $this->name;
}
}
return $this->name;
}
/**
* Sets the parent node of this XmlNode.
*
* @access public
*/
function setParent(&$parent) {
if (strtolower(get_class($this)) == 'xml') {
return;
}
if (isset($this->__parent) && is_object($this->__parent)) {
if ($this->__parent->compare($parent)) {
return;
}
foreach ($this->__parent->children as $i => $child) {
if ($this->compare($child)) {
array_splice($this->__parent->children, $i, 1);
break;
}
}
}
if ($parent == null) {
unset($this->__parent);
} else {
$parent->children[] =& $this;
$this->__parent =& $parent;
}
}
/**
* Returns a copy of self.
*
* @return object Cloned instance
* @access public
*/
function cloneNode() {
return clone($this);
}
/**
* Compares $node to this XmlNode object
*
* @param object An XmlNode or subclass instance
* @return boolean True if the nodes match, false otherwise
* @access public
*/
function compare($node) {
$keys = array(get_object_vars($this), get_object_vars($node));
return ($keys[0] === $keys[1]);
}
/**
* Append given node as a child.
*
* @param object $child XmlNode with appended child
* @param array $options XML generator options for objects and arrays
* @return object A reference to the appended child node
* @access public
*/
function &append(&$child, $options = array()) {
if (empty($child)) {
$return = false;
return $return;
}
if (is_object($child)) {
if ($this->compare($child)) {
trigger_error(__('Cannot append a node to itself.', true));
$return = false;
return $return;
}
} else if (is_array($child)) {
$child = Set::map($child);
if (is_array($child)) {
if (!is_a(current($child), 'XmlNode')) {
foreach ($child as $i => $childNode) {
$child[$i] = $this->normalize($childNode, null, $options);
}
} else {
foreach ($child as $childNode) {
$this->append($childNode, $options);
}
}
return $child;
}
} else {
$attributes = array();
if (func_num_args() >= 2) {
$attributes = func_get_arg(1);
}
$child =& $this->createNode($child, null, $attributes);
}
$child = $this->normalize($child, null, $options);
if (empty($child->namespace) && !empty($this->namespace)) {
$child->namespace = $this->namespace;
}
if (is_a($child, 'XmlNode')) {
$child->setParent($this);
}
return $child;
}
/**
* Returns first child node, or null if empty.
*
* @return object First XmlNode
* @access public
*/
function &first() {
if (isset($this->children[0])) {
return $this->children[0];
} else {
$return = null;
return $return;
}
}
/**
* Returns last child node, or null if empty.
*
* @return object Last XmlNode
* @access public
*/
function &last() {
if (count($this->children) > 0) {
return $this->children[count($this->children) - 1];
} else {
$return = null;
return $return;
}
}
/**
* Returns child node with given ID.
*
* @param string $id Name of child node
* @return object Child XmlNode
* @access public
*/
function &child($id) {
$null = null;
if (is_int($id)) {
if (isset($this->children[$id])) {
return $this->children[$id];
} else {
return null;
}
} elseif (is_string($id)) {
for ($i = 0; $i < count($this->children); $i++) {
if ($this->children[$i]->name == $id) {
return $this->children[$i];
}
}
}
return $null;
}
/**
* Gets a list of childnodes with the given tag name.
*
* @param string $name Tag name of child nodes
* @return array An array of XmlNodes with the given tag name
* @access public
*/
function children($name) {
$nodes = array();
$count = count($this->children);
for ($i = 0; $i < $count; $i++) {
if ($this->children[$i]->name == $name) {
$nodes[] =& $this->children[$i];
}
}
return $nodes;
}
/**
* Gets a reference to the next child node in the list of this node's parent.
*
* @return object A reference to the XmlNode object
* @access public
*/
function &nextSibling() {
$null = null;
$count = count($this->__parent->children);
for ($i = 0; $i < $count; $i++) {
if ($this->__parent->children[$i] == $this) {
if ($i >= $count - 1 || !isset($this->__parent->children[$i + 1])) {
return $null;
}
return $this->__parent->children[$i + 1];
}
}
return $null;
}
/**
* Gets a reference to the previous child node in the list of this node's parent.
*
* @return object A reference to the XmlNode object
* @access public
*/
function &previousSibling() {
$null = null;
$count = count($this->__parent->children);
for ($i = 0; $i < $count; $i++) {
if ($this->__parent->children[$i] == $this) {
if ($i == 0 || !isset($this->__parent->children[$i - 1])) {
return $null;
}
return $this->__parent->children[$i - 1];
}
}
return $null;
}
/**
* Returns parent node.
*
* @return object Parent XmlNode
* @access public
*/
function &parent() {
return $this->__parent;
}
/**
* Returns the XML document to which this node belongs
*
* @return object Parent XML object
* @access public
*/
function &document() {
$document =& $this;
while (true) {
if (get_class($document) == 'Xml' || $document == null) {
break;
}
$document =& $document->parent();
}
return $document;
}
/**
* Returns true if this structure has child nodes.
*
* @return bool
* @access public
*/
function hasChildren() {
if (is_array($this->children) && !empty($this->children)) {
return true;
}
return false;
}
/**
* Returns this XML structure as a string.
*
* @return string String representation of the XML structure.
* @access public
*/
function toString($options = array(), $depth = 0) {
if (is_int($options)) {
$depth = $options;
$options = array();
}
$defaults = array('cdata' => true, 'whitespace' => false, 'convertEntities' => false, 'showEmpty' => true, 'leaveOpen' => false);
$options = array_merge($defaults, Xml::options(), $options);
$tag = !(strpos($this->name, '#') === 0);
$d = '';
if ($tag) {
if ($options['whitespace']) {
$d .= str_repeat("\t", $depth);
}
$d .= '<' . $this->name();
if (!empty($this->namespaces) > 0) {
foreach ($this->namespaces as $key => $val) {
$val = str_replace('"', '\"', $val);
$d .= ' xmlns:' . $key . '="' . $val . '"';
}
}
$parent =& $this->parent();
if ($parent->name === '#document' && !empty($parent->namespaces)) {
foreach ($parent->namespaces as $key => $val) {
$val = str_replace('"', '\"', $val);
$d .= ' xmlns:' . $key . '="' . $val . '"';
}
}
if (is_array($this->attributes) && !empty($this->attributes)) {
foreach ($this->attributes as $key => $val) {
if (is_bool($val) && $val === false) {
$val = 0;
}
$d .= ' ' . $key . '="' . htmlspecialchars($val, ENT_QUOTES, Configure::read('App.encoding')) . '"';
}
}
}
if (!$this->hasChildren() && empty($this->value) && $this->value !== 0 && $tag) {
if (!$options['leaveOpen']) {
$d .= ' />';
}
if ($options['whitespace']) {
$d .= "\n";
}
} elseif ($tag || $this->hasChildren()) {
if ($tag) {
$d .= '>';
}
if ($this->hasChildren()) {
if ($options['whitespace']) {
$d .= "\n";
}
$count = count($this->children);
$cDepth = $depth + 1;
for ($i = 0; $i < $count; $i++) {
$d .= $this->children[$i]->toString($options, $cDepth);
}
if ($tag) {
if ($options['whitespace'] && $tag) {
$d .= str_repeat("\t", $depth);
}
if (!$options['leaveOpen']) {
$d .= '</' . $this->name() . '>';
}
if ($options['whitespace']) {
$d .= "\n";
}
}
}
}
return $d;
}
/**
* Return array representation of current object.
*
* @param boolean $camelize true will camelize child nodes, false will not alter node names
* @return array Array representation
* @access public
*/
function toArray($camelize = true) {
$out = $this->attributes;
$multi = null;
foreach ($this->children as $child) {
$key = $camelize ? Inflector::camelize($child->name) : $child->name;
if (is_a($child, 'XmlTextNode')) {
$out['value'] = $child->value;
continue;
} elseif (isset($child->children[0]) && is_a($child->children[0], 'XmlTextNode')) {
$value = $child->children[0]->value;
if ($child->attributes) {
$value = array_merge(array('value' => $value), $child->attributes);
}
if (isset($out[$child->name]) || isset($multi[$key])) {
if (!isset($multi[$key])) {
$multi[$key] = array($out[$child->name]);
unset($out[$child->name]);
}
$multi[$key][] = $value;
} else {
$out[$child->name] = $value;
}
continue;
} elseif (count($child->children) === 0 && $child->value == '') {
$value = $child->attributes;
if (isset($out[$key]) || isset($multi[$key])) {
if (!isset($multi[$key])) {
$multi[$key] = array($out[$key]);
//unset($out[$key]);
}
$multi[$key][] = $value;
} elseif (!empty($value)) {
$out[$key] = $value;
} else {
$out[$child->name] = $value;
}
continue;
} else {
$value = $child->toArray($camelize);
}
if (!isset($out[$key])) {
$out[$key] = $value;
} else {
if (!is_array($out[$key]) || !isset($out[$key][0])) {
$out[$key] = array($out[$key]);
}
$out[$key][] = $value;
}
}
if (isset($multi)) {
$out = array_merge($out, $multi);
}
return $out;
}
/**
* Returns data from toString when this object is converted to a string.
*
* @return string String representation of this structure.
* @access private
*/
function __toString() {
return $this->toString();
}
/**
* Debug method. Deletes the parent. Also deletes this node's children,
* if given the $recursive parameter.
*
* @param boolean $recursive Recursively delete elements.
* @access protected
*/
function _killParent($recursive = true) {
unset($this->__parent, $this->_log);
if ($recursive && $this->hasChildren()) {
for ($i = 0; $i < count($this->children); $i++) {
$this->children[$i]->_killParent(true);
}
}
}
}
/**
* Main XML class.
*
* Parses and stores XML data, representing the root of an XML document
*
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP v .0.10.3.1400
*/
class Xml extends XmlNode {
/**
* Resource handle to XML parser.
*
* @var resource
* @access private
*/
var $__parser;
/**
* File handle to XML indata file.
*
* @var resource
* @access private
*/
var $__file;
/**
* Raw XML string data (for loading purposes)
*
* @var string
* @access private
*/
var $__rawData = null;
/**
* XML document header
*
* @var string
* @access private
*/
var $__header = null;
/**
* Default array keys/object properties to use as tag names when converting objects or array
* structures to XML. Set by passing $options['tags'] to this object's constructor.
*
* @var array
* @access private
*/
var $__tags = array();
/**
* XML document version
*
* @var string
* @access private
*/
var $version = '1.0';
/**
* XML document encoding
*
* @var string
* @access private
*/
var $encoding = 'UTF-8';
/**
* Constructor. Sets up the XML parser with options, gives it this object as
* its XML object, and sets some variables.
*
* ### Options
* - 'root': The name of the root element, defaults to '#document'
* - 'version': The XML version, defaults to '1.0'
* - 'encoding': Document encoding, defaults to 'UTF-8'
* - 'namespaces': An array of namespaces (as strings) used in this document
* - 'format': Specifies the format this document converts to when parsed or
* rendered out as text, either 'attributes' or 'tags', defaults to 'attributes'
* - 'tags': An array specifying any tag-specific formatting options, indexed
* by tag name. See XmlNode::normalize().
* - 'slug': A boolean to indicate whether or not you want the string version of the XML document
* to have its tags run through Inflector::slug(). Defaults to true
*
* @param mixed $input The content with which this XML document should be initialized. Can be a
* string, array or object. If a string is specified, it may be a literal XML
* document, or a URL or file path to read from.
* @param array $options Options to set up with, for valid options see above:
* @see XmlNode::normalize()
*/
function __construct($input = null, $options = array()) {
$defaults = array(
'root' => '#document', 'tags' => array(), 'namespaces' => array(),
'version' => '1.0', 'encoding' => 'UTF-8', 'format' => 'attributes',
'slug' => true
);
$options = array_merge($defaults, Xml::options(), $options);
foreach (array('version', 'encoding', 'namespaces') as $key) {
$this->{$key} = $options[$key];
}
$this->__tags = $options['tags'];
parent::__construct('#document');
if ($options['root'] !== '#document') {
$Root =& $this->createNode($options['root']);
} else {
$Root =& $this;
}
if (!empty($input)) {
if (is_string($input)) {
$Root->load($input);
} elseif (is_array($input) || is_object($input)) {
$Root->append($input, $options);
}
}
}
/**
* Initialize XML object from a given XML string. Returns false on error.
*
* @param string $input XML string, a path to a file, or an HTTP resource to load
* @return boolean Success
* @access public
*/
function load($input) {
if (!is_string($input)) {
return false;
}
$this->__rawData = null;
$this->__header = null;
if (strstr($input, "<")) {
$this->__rawData = $input;
} elseif (strpos($input, 'http://') === 0 || strpos($input, 'https://') === 0) {
App::import('Core', 'HttpSocket');
$socket = new HttpSocket();
$this->__rawData = $socket->get($input);
} elseif (file_exists($input)) {
$this->__rawData = file_get_contents($input);
} else {
trigger_error(__('XML cannot be read', true));
return false;
}
return $this->parse();
}
/**
* Parses and creates XML nodes from the __rawData property.
*
* @return boolean Success
* @access public
* @see Xml::load()
* @todo figure out how to link attributes and namespaces
*/
function parse() {
$this->__initParser();
$this->__rawData = trim($this->__rawData);
$this->__header = trim(str_replace(
array('<' . '?', '?' . '>'),
array('', ''),
substr($this->__rawData, 0, strpos($this->__rawData, '?' . '>'))
));
xml_parse_into_struct($this->__parser, $this->__rawData, $vals);
$xml =& $this;
$count = count($vals);
for ($i = 0; $i < $count; $i++) {
$data = $vals[$i];
$data += array('tag' => null, 'value' => null, 'attributes' => array());
switch ($data['type']) {
case "open" :
$xml =& $xml->createElement($data['tag'], $data['value'], $data['attributes']);
break;
case "close" :
$xml =& $xml->parent();
break;
case "complete" :
$xml->createElement($data['tag'], $data['value'], $data['attributes']);
break;
case 'cdata':
$xml->createTextNode($data['value']);
break;
}
}
xml_parser_free($this->__parser);
$this->__parser = null;
return true;
}
/**
* Initializes the XML parser resource
*
* @return void
* @access private
*/
function __initParser() {
if (empty($this->__parser)) {
$this->__parser = xml_parser_create();
xml_set_object($this->__parser, $this);
xml_parser_set_option($this->__parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($this->__parser, XML_OPTION_SKIP_WHITE, 1);
}
}
/**
* Returns a string representation of the XML object
*
* @param mixed $options If boolean: whether to include the XML header with the document
* (defaults to true); if an array, overrides the default XML generation options
* @return string XML data
* @access public
* @deprecated
* @see Xml::toString()
*/
function compose($options = array()) {
return $this->toString($options);
}
/**
* If debug mode is on, this method echoes an error message.
*
* @param string $msg Error message
* @param integer $code Error code
* @param integer $line Line in file
* @access public
*/
function error($msg, $code = 0, $line = 0) {
if (Configure::read('debug')) {
echo $msg . " " . $code . " " . $line;
}
}
/**
* Returns a string with a textual description of the error code, or FALSE if no description was found.
*
* @param integer $code Error code
* @return string Error message
* @access public
*/
function getError($code) {
$r = @xml_error_string($code);
return $r;
}
// Overridden functions from superclass
/**
* Get next element. NOT implemented.
*
* @return object
* @access public
*/
function &next() {
$return = null;
return $return;
}
/**
* Get previous element. NOT implemented.
*
* @return object
* @access public
*/
function &previous() {
$return = null;
return $return;
}
/**
* Get parent element. NOT implemented.
*
* @return object
* @access public
*/
function &parent() {
$return = null;
return $return;
}
/**
* Adds a namespace to the current document
*
* @param string $prefix The namespace prefix
* @param string $url The namespace DTD URL
* @return void
*/
function addNamespace($prefix, $url) {
if ($count = count($this->children)) {
for ($i = 0; $i < $count; $i++) {
$this->children[$i]->addNamespace($prefix, $url);
}
return true;
}
return parent::addNamespace($prefix, $url);
}
/**
* Removes a namespace to the current document
*
* @param string $prefix The namespace prefix
* @return void
*/
function removeNamespace($prefix) {
if ($count = count($this->children)) {
for ($i = 0; $i < $count; $i++) {
$this->children[$i]->removeNamespace($prefix);
}
return true;
}
return parent::removeNamespace($prefix);
}
/**
* Return string representation of current object.
*
* @return string String representation
* @access public
*/
function toString($options = array()) {
if (is_bool($options)) {
$options = array('header' => $options);
}
$defaults = array('header' => false, 'encoding' => $this->encoding);
$options = array_merge($defaults, Xml::options(), $options);
$data = parent::toString($options, 0);
if ($options['header']) {
if (!empty($this->__header)) {
return $this->header($this->__header) . "\n" . $data;
}
return $this->header() . "\n" . $data;
}
return $data;
}
/**
* Return a header used on the first line of the xml file
*
* @param mixed $attrib attributes of the header element
* @return string formated header
*/
function header($attrib = array()) {
$header = 'xml';
if (is_string($attrib)) {
$header = $attrib;
} else {
$attrib = array_merge(array('version' => $this->version, 'encoding' => $this->encoding), $attrib);
foreach ($attrib as $key=>$val) {
$header .= ' ' . $key . '="' . $val . '"';
}
}
return '<' . '?' . $header . ' ?' . '>';
}
/**
* Destructor, used to free resources.
*
* @access private
*/
function __destruct() {
$this->_killParent(true);
}
/**
* Adds a namespace to any XML documents generated or parsed
*
* @param string $name The namespace name
* @param string $url The namespace URI; can be empty if in the default namespace map
* @return boolean False if no URL is specified, and the namespace does not exist
* default namespace map, otherwise true
* @access public
* @static
*/
function addGlobalNs($name, $url = null) {
$_this =& XmlManager::getInstance();
if ($ns = Xml::resolveNamespace($name, $url)) {
$_this->namespaces = array_merge($_this->namespaces, $ns);
return $ns;
}
return false;
}
/**
* Resolves current namespace
*
* @param string $name
* @param string $url
* @return array
*/
function resolveNamespace($name, $url) {
$_this =& XmlManager::getInstance();
if ($url == null && isset($_this->defaultNamespaceMap[$name])) {
$url = $_this->defaultNamespaceMap[$name];
} elseif ($url == null) {
return false;
}
if (!strpos($url, '://') && isset($_this->defaultNamespaceMap[$name])) {
$_url = $_this->defaultNamespaceMap[$name];
$name = $url;
$url = $_url;
}
return array($name => $url);
}
/**
* Alias to Xml::addNs
*
* @access public
* @static
*/
function addGlobalNamespace($name, $url = null) {
return Xml::addGlobalNs($name, $url);
}
/**
* Removes a namespace added in addNs()
*
* @param string $name The namespace name or URI
* @access public
* @static
*/
function removeGlobalNs($name) {
$_this =& XmlManager::getInstance();
if (isset($_this->namespaces[$name])) {
unset($_this->namespaces[$name]);
unset($this->namespaces[$name]);
return true;
} elseif (in_array($name, $_this->namespaces)) {
$keys = array_keys($_this->namespaces);
$count = count($keys);
for ($i = 0; $i < $count; $i++) {
if ($_this->namespaces[$keys[$i]] == $name) {
unset($_this->namespaces[$keys[$i]]);
unset($this->namespaces[$keys[$i]]);
return true;
}
}
}
return false;
}
/**
* Alias to Xml::removeNs
*
* @access public
* @static
*/
function removeGlobalNamespace($name) {
return Xml::removeGlobalNs($name);
}
/**
* Sets/gets global XML options
*
* @param array $options
* @return array
* @access public
* @static
*/
function options($options = array()) {
$_this =& XmlManager::getInstance();
$_this->options = array_merge($_this->options, $options);
return $_this->options;
}
}
/**
* The XML Element
*
*/
class XmlElement extends XmlNode {
/**
* Construct an Xml element
*
* @param string $name name of the node
* @param string $value value of the node
* @param array $attributes
* @param string $namespace
* @return string A copy of $data in XML format
*/
function __construct($name = null, $value = null, $attributes = array(), $namespace = false) {
parent::__construct($name, $value, $namespace);
$this->addAttribute($attributes);
}
/**
* Get all the attributes for this element
*
* @return array
*/
function attributes() {
return $this->attributes;
}
/**
* Add attributes to this element
*
* @param string $name name of the node
* @param string $value value of the node
* @return boolean
*/
function addAttribute($name, $val = null) {
if (is_object($name)) {
$name = get_object_vars($name);
}
if (is_array($name)) {
foreach ($name as $key => $val) {
$this->addAttribute($key, $val);
}
return true;
}
if (is_numeric($name)) {
$name = $val;
$val = null;
}
if (!empty($name)) {
if (strpos($name, 'xmlns') === 0) {
if ($name == 'xmlns') {
$this->namespace = $val;
} else {
list($pre, $prefix) = explode(':', $name);
$this->addNamespace($prefix, $val);
return true;
}
}
$this->attributes[$name] = $val;
return true;
}
return false;
}
/**
* Remove attributes to this element
*
* @param string $name name of the node
* @return boolean
*/
function removeAttribute($attr) {
if (array_key_exists($attr, $this->attributes)) {
unset($this->attributes[$attr]);
return true;
}
return false;
}
}
/**
* XML text or CDATA node
*
* Stores XML text data according to the encoding of the parent document
*
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP v .1.2.6000
*/
class XmlTextNode extends XmlNode {
/**
* Harcoded XML node name, represents this object as a text node
*
* @var string
*/
var $name = '#text';
/**
* The text/data value which this node contains
*
* @var string
*/
var $value = null;
/**
* Construct text node with the given parent object and data
*
* @param object $parent Parent XmlNode/XmlElement object
* @param mixed $value Node value
*/
function __construct($value = null) {
$this->value = $value;
}
/**
* Looks for child nodes in this element
*
* @return boolean False - not supported
*/
function hasChildren() {
return false;
}
/**
* Append an XML node: XmlTextNode does not support this operation
*
* @return boolean False - not supported
* @todo make convertEntities work without mb support, convert entities to number entities
*/
function append() {
return false;
}
/**
* Return string representation of current text node object.
*
* @return string String representation
* @access public
*/
function toString($options = array(), $depth = 0) {
if (is_int($options)) {
$depth = $options;
$options = array();
}
$defaults = array('cdata' => true, 'whitespace' => false, 'convertEntities' => false);
$options = array_merge($defaults, Xml::options(), $options);
$val = $this->value;
if ($options['convertEntities'] && function_exists('mb_convert_encoding')) {
$val = mb_convert_encoding($val,'UTF-8', 'HTML-ENTITIES');
}
if ($options['cdata'] === true && !is_numeric($val)) {
$val = '<![CDATA[' . $val . ']]>';
}
if ($options['whitespace']) {
return str_repeat("\t", $depth) . $val . "\n";
}
return $val;
}
}
/**
* Manages application-wide namespaces and XML parsing/generation settings.
* Private class, used exclusively within scope of XML class.
*
* @access private
*/
class XmlManager {
/**
* Global XML namespaces. Used in all XML documents processed by this application
*
* @var array
* @access public
*/
var $namespaces = array();
/**
* Global XML document parsing/generation settings.
*
* @var array
* @access public
*/
var $options = array();
/**
* Map of common namespace URIs
*
* @access private
* @var array
*/
var $defaultNamespaceMap = array(
'dc' => 'http://purl.org/dc/elements/1.1/', // Dublin Core
'dct' => 'http://purl.org/dc/terms/', // Dublin Core Terms
'g' => 'http://base.google.com/ns/1.0', // Google Base
'rc' => 'http://purl.org/rss/1.0/modules/content/', // RSS 1.0 Content Module
'wf' => 'http://wellformedweb.org/CommentAPI/', // Well-Formed Web Comment API
'fb' => 'http://rssnamespace.org/feedburner/ext/1.0', // FeedBurner extensions
'lj' => 'http://www.livejournal.org/rss/lj/1.0/', // Live Journal
'itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd', // iTunes
'xhtml' => 'http://www.w3.org/1999/xhtml', // XHTML,
'atom' => 'http://www.w3.org/2005/Atom' // Atom
);
/**
* Returns a reference to the global XML object that manages app-wide XML settings
*
* @return object
* @access public
*/
function &getInstance() {
static $instance = array();
if (!$instance) {
$instance[0] =& new XmlManager();
}
return $instance[0];
}
}
|