digitickets /
lalit
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LaLit; |
||
| 4 | |||
| 5 | use DOMDocument; |
||
| 6 | use DOMNode; |
||
| 7 | use Exception; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Array2XML: A class to convert array in PHP to XML |
||
| 11 | * It also takes into account attributes names unlike SimpleXML in PHP |
||
| 12 | * It returns the XML in form of DOMDocument class for further manipulation. |
||
| 13 | * It throws exception if the tag name or attribute name has illegal chars. |
||
| 14 | * |
||
| 15 | * Author : Lalit Patel |
||
| 16 | * Website: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes |
||
| 17 | * License: Apache License 2.0 |
||
| 18 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
| 19 | * |
||
| 20 | * Usage: |
||
| 21 | * $xml = Array2XML::createXML('root_node_name', $php_array); |
||
| 22 | * echo $xml->saveXML(); |
||
| 23 | */ |
||
| 24 | class Array2XML |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private static $encoding = 'UTF-8'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var DomDocument|null |
||
| 33 | */ |
||
| 34 | private static $xml = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Convert an Array to XML. |
||
| 38 | * |
||
| 39 | * @param string $node_name - name of the root node to be converted |
||
| 40 | * @param array $arr - array to be converted |
||
| 41 | * |
||
| 42 | * @return DomDocument |
||
| 43 | */ |
||
| 44 | public static function createXML($node_name, $arr = []) |
||
| 45 | { |
||
| 46 | $xml = self::getXMLRoot(); |
||
| 47 | $xml->appendChild(self::convert($node_name, $arr)); |
||
| 48 | self::$xml = null; // clear the xml node in the class for 2nd time use. |
||
| 49 | |||
| 50 | return $xml; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Initialize the root XML node [optional]. |
||
| 55 | * |
||
| 56 | * @param string $version |
||
| 57 | * @param string $encoding |
||
| 58 | * @param bool $standalone |
||
| 59 | * @param bool $format_output |
||
| 60 | */ |
||
| 61 | View Code Duplication | public static function init($version = '1.0', $encoding = 'utf-8', $standalone = false, $format_output = true) |
|
| 62 | { |
||
| 63 | self::$xml = new DomDocument($version, $encoding); |
||
|
0 ignored issues
–
show
|
|||
| 64 | self::$xml->xmlStandalone = $standalone; |
||
| 65 | self::$xml->formatOutput = $format_output; |
||
| 66 | self::$encoding = $encoding; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get string representation of boolean value. |
||
| 71 | * |
||
| 72 | * @param mixed $v |
||
| 73 | * |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | private static function bool2str($v) |
||
| 77 | { |
||
| 78 | //convert boolean to text value. |
||
| 79 | $v = $v === true ? 'true' : $v; |
||
| 80 | $v = $v === false ? 'false' : $v; |
||
| 81 | |||
| 82 | return $v; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Convert an Array to XML. |
||
| 87 | * |
||
| 88 | * @param string $node_name - name of the root node to be converted |
||
| 89 | * @param array $arr - array to be converted |
||
| 90 | * |
||
| 91 | * @return DOMNode |
||
| 92 | * |
||
| 93 | * @throws Exception |
||
| 94 | */ |
||
| 95 | private static function convert($node_name, $arr = []) |
||
| 96 | { |
||
| 97 | //print_arr($node_name); |
||
| 98 | $xml = self::getXMLRoot(); |
||
| 99 | $node = $xml->createElement($node_name); |
||
| 100 | |||
| 101 | if (is_array($arr)) { |
||
| 102 | // get the attributes first.; |
||
| 103 | if (array_key_exists('@attributes', $arr) && is_array($arr['@attributes'])) { |
||
| 104 | foreach ($arr['@attributes'] as $key => $value) { |
||
| 105 | if (!self::isValidTagName($key)) { |
||
| 106 | throw new Exception('[Array2XML] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name); |
||
| 107 | } |
||
| 108 | $node->setAttribute($key, self::bool2str($value)); |
||
| 109 | } |
||
| 110 | unset($arr['@attributes']); //remove the key from the array once done. |
||
| 111 | } |
||
| 112 | |||
| 113 | // check if it has a value stored in @value, if yes store the value and return |
||
| 114 | // else check if its directly stored as string |
||
| 115 | if (array_key_exists('@value', $arr)) { |
||
| 116 | $node->appendChild($xml->createTextNode(self::bool2str($arr['@value']))); |
||
| 117 | unset($arr['@value']); //remove the key from the array once done. |
||
| 118 | //return from recursion, as a note with value cannot have child nodes. |
||
| 119 | return $node; |
||
| 120 | } elseif (array_key_exists('@cdata', $arr)) { |
||
| 121 | $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata']))); |
||
| 122 | unset($arr['@cdata']); //remove the key from the array once done. |
||
| 123 | //return from recursion, as a note with cdata cannot have child nodes. |
||
| 124 | return $node; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | //create subnodes using recursion |
||
| 129 | if (is_array($arr)) { |
||
| 130 | // recurse to get the node for that key |
||
| 131 | foreach ($arr as $key => $value) { |
||
| 132 | if (!self::isValidTagName($key)) { |
||
| 133 | throw new Exception('[Array2XML] Illegal character in tag name. tag: '.$key.' in node: '.$node_name); |
||
| 134 | } |
||
| 135 | if (is_array($value) && is_numeric(key($value))) { |
||
| 136 | // MORE THAN ONE NODE OF ITS KIND; |
||
| 137 | // if the new array is numeric index, means it is array of nodes of the same kind |
||
| 138 | // it should follow the parent key name |
||
| 139 | foreach ($value as $k => $v) { |
||
| 140 | $node->appendChild(self::convert($key, $v)); |
||
| 141 | } |
||
| 142 | } else { |
||
| 143 | // ONLY ONE NODE OF ITS KIND |
||
| 144 | $node->appendChild(self::convert($key, $value)); |
||
| 145 | } |
||
| 146 | unset($arr[$key]); //remove the key from the array once done. |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | // after we are done with all the keys in the array (if it is one) |
||
| 151 | // we check if it has any text value, if yes, append it. |
||
| 152 | if (!is_array($arr)) { |
||
| 153 | $node->appendChild($xml->createTextNode(self::bool2str($arr))); |
||
| 154 | } |
||
| 155 | |||
| 156 | return $node; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the root XML node, if there isn't one, create it. |
||
| 161 | * |
||
| 162 | * @return DomDocument|null |
||
| 163 | */ |
||
| 164 | private static function getXMLRoot() |
||
| 165 | { |
||
| 166 | if (empty(self::$xml)) { |
||
| 167 | self::init(); |
||
| 168 | } |
||
| 169 | |||
| 170 | return self::$xml; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Check if the tag name or attribute name contains illegal characters |
||
| 175 | * Ref: http://www.w3.org/TR/xml/#sec-common-syn. |
||
| 176 | * |
||
| 177 | * @param string $tag |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | private static function isValidTagName($tag) |
||
| 182 | { |
||
| 183 | $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i'; |
||
| 184 | |||
| 185 | return preg_match($pattern, $tag, $matches) && $matches[0] == $tag; |
||
| 186 | } |
||
| 187 | } |
||
| 188 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..