Skip to content

Commit d261f76

Browse files
committed
XML to JSON functions bug fixes
1 parent 909f5a5 commit d261f76

File tree

1 file changed

+107
-23
lines changed

1 file changed

+107
-23
lines changed

system/functions.php

+107-23
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function getCurrentTemplate() {
167167
return System\Core\View :: getInstance()->template();
168168
}
169169

170-
function getUrlTemplate($url) {
170+
function getUrlRoute($url) {
171171
$urlData = \Vvveb\System\Routes::getUrlData($url);
172172

173173
return $urlData;
@@ -957,7 +957,7 @@ function getTemplateList($theme = null, $skip = []) {
957957
}
958958
$name = $title = str_replace('.html', '', $filename);
959959
$description = '';
960-
$name = ! empty($folder) ? "$folder-$name" : $name;
960+
$name = (! empty($folder) && $folder != '/') ? "$folder-$name" : $name;
961961

962962
if (isset($friendlyNames[$name])) {
963963
if (isset($friendlyNames[$name]['description'])) {
@@ -1024,7 +1024,7 @@ function dd(...$variables) {
10241024
echo highlight_string("<?php\n" . var_export($variable, true), true);
10251025
}
10261026

1027-
die();
1027+
die(0);
10281028
}
10291029

10301030
function encrypt($key, $value, $cipher = 'aes-256-gcm', $digest = 'sha256') {
@@ -1779,45 +1779,47 @@ function array2xml($array, $xml = false) {
17791779
$i = 0;
17801780
$start = 0;
17811781

1782-
while (($start = strpos($key, '@@', $start))) {
1783-
$i++;
1784-
$start += 2;
1785-
$end = (int)strpos($key,' ', $start);
1786-
$name = substr($key, $start, $end ? $end - $start : null);
1782+
if ($key) {
1783+
while (($start = strpos($key, '@@', $start))) {
1784+
$i++;
1785+
$start += 2;
1786+
$end = (int)strpos($key,' ', $start);
1787+
$name = substr($key, $start, $end ? $end - $start : null);
1788+
1789+
if ($end == 0) {
1790+
$start += strlen($name);
1791+
} else {
1792+
$start = (int)$end;
1793+
}
17871794

1788-
if ($end == 0) {
1789-
$start += strlen($name);
1790-
} else {
1791-
$start = (int)$end;
1795+
if ($name) {
1796+
$name = explode('=', $name);
1797+
$attributes[$name[0]] = trim($name[1] ?? '', '\'"');
1798+
}
17921799
}
17931800

1794-
if ($name) {
1795-
$name = explode('=', $name);
1796-
$attributes[$name[0]] = trim($name[1] ?? '', '\'"');
1797-
}
1801+
$key = strstr($key, ' @@', true) ?: $key;
17981802
}
1799-
1800-
$key = strstr($key, ' @@', true) ?: $key;
18011803
};
18021804

18031805
$processAttr($key);
18041806

1805-
if (is_array($value)) {
1807+
if (is_array($value) || is_object($value)) {
18061808
$node = $xml->addChild($key);
18071809

18081810
if ($attributes) {
18091811
foreach ($attributes as $key => $val) {
1810-
$node->addAttribute($key, $val);
1812+
$node->addAttribute($key, htmlentities($val));
18111813
}
18121814
}
18131815
array2xml($value, $node);
18141816
} else {
18151817
if (substr_compare($key, '--xmlattr', -9, 9) === 0) {
18161818
$key = substr($key, 0, -9);
1817-
$xml->addAttribute($key, $value);
1819+
$xml->addAttribute($key, htmlentities($value));
18181820
} else {
18191821
$processAttr($value);
1820-
$node = $xml->addChild($key, $value);
1822+
$node = $xml->addChild($key, htmlentities($value));
18211823

18221824
if ($attributes) {
18231825
foreach ($attributes as $key => $val) {
@@ -1837,9 +1839,34 @@ function removeJsonComments($json) {
18371839
return $json;
18381840
}
18391841

1842+
function encodeXmlName($name) {
1843+
$len = strlen($name);
1844+
$newName = '';
1845+
1846+
for ($i = 0; $i < $len; $i++) {
1847+
$char = $name[$i];
1848+
$code = ord($char);
1849+
1850+
// = @ _ - . A-Z a-z 0-9
1851+
if (! ($code == 32 || $code == 34 || $code == 61 || $code == 64 || $code == 95 || $code == 45 || $code == 46 || ($code <= 90 && $code >= 65) || ($code <= 122 && $code >= 97) || ($code <= 57 && $code >= 48))) {
1852+
$char = '_x' . sprintf('%03d', $code) . '_';
1853+
}
1854+
1855+
$newName .= $char;
1856+
}
1857+
1858+
return $newName;
1859+
}
1860+
1861+
function decodeXmlName($name) {
1862+
return preg_replace_callback('/_x(\d+)_/',function ($matches) {
1863+
return chr(ltrim($matches[1], '0'));
1864+
}, $name);
1865+
}
1866+
18401867
function prepareJson($array) {
18411868
if (! is_array($array)) {
1842-
return;
1869+
//return;
18431870
}
18441871
$helper = [];
18451872

@@ -1859,6 +1886,32 @@ function prepareJson($array) {
18591886
if (strpos($key, '@')) {
18601887
}
18611888

1889+
//keep empty arrays as array type
1890+
if (is_object($value) && ! $value) {
1891+
$key .= '--object';
1892+
} else {
1893+
if (is_array($value) && ! $value) {
1894+
$key .= '--array';
1895+
}
1896+
}
1897+
1898+
//keep boolean type
1899+
if (is_bool($value)) {
1900+
$key .= '--boolean';
1901+
}
1902+
1903+
//keep int type
1904+
if (is_int($value)) {
1905+
$key .= '--int';
1906+
}
1907+
1908+
//keep null type
1909+
if ($value === null) {
1910+
$key .= '--null';
1911+
}
1912+
1913+
$key = encodeXmlName(trim($key));
1914+
18621915
$helper[$key] = is_array($value) ? prepareJson($value) : $value;
18631916
}
18641917

@@ -1877,6 +1930,37 @@ function reconstructJson(&$array, $removeAttrs = false) {
18771930
continue;
18781931
}
18791932

1933+
$key = decodeXmlName($key);
1934+
1935+
if (substr_compare($key, '--array', -7, 7) === 0) {
1936+
$key = substr($key, 0, -7);
1937+
$value = (array)$value;
1938+
} else {
1939+
if (! $value) {
1940+
$value = '';
1941+
}
1942+
}
1943+
1944+
if (substr_compare($key, '--boolean', -9, 9) === 0) {
1945+
$key = substr($key, 0, -9);
1946+
$value = (boolean)$value;
1947+
}
1948+
1949+
if (substr_compare($key, '--null', -6, 6) === 0) {
1950+
$key = substr($key, 0, -6);
1951+
$value = null;
1952+
}
1953+
1954+
if (substr_compare($key, '--int', -5, 5) === 0) {
1955+
$key = substr($key, 0, -5);
1956+
$value = (int)$value;
1957+
}
1958+
1959+
if (substr_compare($key, '--object', -8, 8) === 0) {
1960+
$key = substr($key, 0, -8);
1961+
$value = (object)$value;
1962+
}
1963+
18801964
if (substr_compare($key, 'n--', 0, 3) === 0) {
18811965
$newkey = substr($key, 3);
18821966

0 commit comments

Comments
 (0)