Fixing file upload params ($_FILES) normalization. Closes #75
[akelos.git] / vendor / domit / xml_domit_doctor.php
blobb65c6a1d909f1c2c56389e8d4ff4b4543b3b165f
1 <?php
2 /**
3 * DOMIT! Doctor is a set of utilities for repairing malformed XML
4 * @package domit-xmlparser
5 * @copyright (C) 2004 John Heinstein. All rights reserved
6 * @license http://www.gnu.org/copyleft/lesser.html LGPL License
7 * @author John Heinstein <johnkarl@nbnet.nb.ca>
8 * @link http://www.engageinteractive.com/domit/ DOMIT! Home Page
9 * DOMIT! is Free Software
10 **/
12 /**
13 * A (static) class containing utilities for repairing malformed XML
15 * @package domit-xmlparser
16 * @author John Heinstein <johnkarl@nbnet.nb.ca>
18 class domit_doctor {
20 /**
21 * Looks for illegal ampersands and converts them to entities
22 * @param string The xml text to be repaired
23 * @return string The repaired xml text
25 function fixAmpersands($xmlText) {
26 $xmlText = trim($xmlText);
27 $startIndex = -1;
28 $processing = true;
29 $illegalChar = '&';
31 while ($processing) {
32 $startIndex = strpos($xmlText, $illegalChar, ($startIndex + 1));
34 if ($startIndex !== false) {
35 $xmlText = domit_doctor::evaluateCharacter($xmlText,
36 $illegalChar, ($startIndex + 1));
38 else {
39 $processing = false;
43 return $xmlText;
44 } //fixAmpersands
46 /**
47 * Evaluates whether an ampersand should be converted to an entity, and performs the conversion
48 * @param string The xml text
49 * @param string The (ampersand) character
50 * @param int The character index immediately following the ampersand in question
51 * @return string The repaired xml text
53 function evaluateCharacter($xmlText, $illegalChar, $startIndex) {
54 $total = strlen($xmlText);
55 $searchingForCDATASection = false;
57 for ($i = $startIndex; $i < $total; $i++) {
58 $currChar = substr($xmlText, $i, 1);
60 if (!$searchingForCDATASection) {
61 switch ($currChar) {
62 case ' ':
63 case "'":
64 case '"':
65 case "\n":
66 case "\r":
67 case "\t":
68 case '&':
69 case "]":
70 $searchingForCDATASection = true;
71 break;
72 case ";":
73 return $xmlText;
74 break;
77 else {
78 switch ($currChar) {
79 case '<':
80 case '>':
81 return (substr_replace($xmlText, '&amp;',
82 ($startIndex - 1) , 1));
83 break;
84 case "]":
85 return $xmlText;
86 break;
91 return $xmlText;
92 } //evaluateCharacter
93 } //domit_doctor