2013-02-16 04:30:44 +04:00
|
|
|
<?php
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-02-16 04:30:44 +04:00
|
|
|
/**
|
2015-02-26 13:37:37 +03:00
|
|
|
* @author Robin Appelman
|
|
|
|
* @copyright 2013 Robin Appelman icewind@owncloud.com
|
2013-02-16 04:30:44 +04:00
|
|
|
*
|
2015-02-26 13:37:37 +03:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
2013-02-16 04:30:44 +04:00
|
|
|
*
|
2015-02-26 13:37:37 +03:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2013-02-16 04:30:44 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-02-26 13:37:37 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
2013-02-16 04:30:44 +04:00
|
|
|
*
|
2015-02-26 13:37:37 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2013-02-16 04:30:44 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-02-16 04:30:44 +04:00
|
|
|
namespace OC;
|
|
|
|
|
2015-03-13 15:04:31 +03:00
|
|
|
/**
|
|
|
|
* Class ArrayParser
|
|
|
|
*
|
|
|
|
* @package OC
|
|
|
|
*/
|
2013-02-16 04:30:44 +04:00
|
|
|
class ArrayParser {
|
|
|
|
const TYPE_NUM = 1;
|
|
|
|
const TYPE_BOOL = 2;
|
|
|
|
const TYPE_STRING = 3;
|
|
|
|
const TYPE_ARRAY = 4;
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $string
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return array|bool|int|null|string
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2015-03-13 15:04:31 +03:00
|
|
|
public function parsePHP($string) {
|
2013-02-16 04:30:44 +04:00
|
|
|
$string = $this->stripPHPTags($string);
|
|
|
|
$string = $this->stripAssignAndReturn($string);
|
|
|
|
return $this->parse($string);
|
|
|
|
}
|
|
|
|
|
2014-02-19 12:31:54 +04:00
|
|
|
/**
|
|
|
|
* @param string $string
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return string
|
2014-02-19 12:31:54 +04:00
|
|
|
*/
|
2015-03-13 15:04:31 +03:00
|
|
|
private function stripPHPTags($string) {
|
2013-02-16 04:30:44 +04:00
|
|
|
$string = trim($string);
|
|
|
|
if (substr($string, 0, 5) === '<?php') {
|
|
|
|
$string = substr($string, 5);
|
|
|
|
}
|
|
|
|
if (substr($string, -2) === '?>') {
|
|
|
|
$string = substr($string, 0, -2);
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $string
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return string
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2015-03-13 15:04:31 +03:00
|
|
|
private function stripAssignAndReturn($string) {
|
2013-02-16 04:30:44 +04:00
|
|
|
$string = trim($string);
|
|
|
|
if (substr($string, 0, 6) === 'return') {
|
|
|
|
$string = substr($string, 6);
|
|
|
|
}
|
|
|
|
if (substr($string, 0, 1) === '$') {
|
|
|
|
list(, $string) = explode('=', $string, 2);
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
|
|
|
* @param string $string
|
|
|
|
* @return array|bool|int|null|string
|
|
|
|
*/
|
2015-03-13 15:04:31 +03:00
|
|
|
private function parse($string) {
|
2013-02-16 04:30:44 +04:00
|
|
|
$string = trim($string);
|
|
|
|
$string = trim($string, ';');
|
|
|
|
switch ($this->getType($string)) {
|
|
|
|
case self::TYPE_NUM:
|
|
|
|
return $this->parseNum($string);
|
|
|
|
case self::TYPE_BOOL:
|
|
|
|
return $this->parseBool($string);
|
|
|
|
case self::TYPE_STRING:
|
|
|
|
return $this->parseString($string);
|
|
|
|
case self::TYPE_ARRAY:
|
|
|
|
return $this->parseArray($string);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $string
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return int
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2015-03-13 15:04:31 +03:00
|
|
|
private function getType($string) {
|
2013-02-16 04:30:44 +04:00
|
|
|
$string = strtolower($string);
|
|
|
|
$first = substr($string, 0, 1);
|
|
|
|
$last = substr($string, -1, 1);
|
|
|
|
$arrayFirst = substr($string, 0, 5);
|
|
|
|
if (($first === '"' or $first === "'") and ($last === '"' or $last === "'")) {
|
|
|
|
return self::TYPE_STRING;
|
|
|
|
} elseif ($string === 'false' or $string === 'true') {
|
|
|
|
return self::TYPE_BOOL;
|
|
|
|
} elseif ($arrayFirst === 'array' and $last === ')') {
|
|
|
|
return self::TYPE_ARRAY;
|
|
|
|
} else {
|
|
|
|
return self::TYPE_NUM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $string
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return string
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2015-03-13 15:04:31 +03:00
|
|
|
private function parseString($string) {
|
2013-02-16 04:30:44 +04:00
|
|
|
return substr($string, 1, -1);
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $string
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return int
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2015-03-13 15:04:31 +03:00
|
|
|
private function parseNum($string) {
|
2013-02-16 04:30:44 +04:00
|
|
|
return intval($string);
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $string
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return bool
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2015-03-13 15:04:31 +03:00
|
|
|
private function parseBool($string) {
|
2013-02-16 04:30:44 +04:00
|
|
|
$string = strtolower($string);
|
|
|
|
return $string === 'true';
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $string
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return array
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2015-03-13 15:04:31 +03:00
|
|
|
private function parseArray($string) {
|
2013-02-16 04:30:44 +04:00
|
|
|
$body = substr($string, 5);
|
|
|
|
$body = trim($body);
|
|
|
|
$body = substr($body, 1, -1);
|
|
|
|
$items = $this->splitArray($body);
|
2015-03-13 15:04:31 +03:00
|
|
|
$result = [];
|
2013-02-16 04:30:44 +04:00
|
|
|
$lastKey = -1;
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$item = trim($item);
|
|
|
|
if ($item) {
|
|
|
|
if (strpos($item, '=>')) {
|
|
|
|
list($key, $value) = explode('=>', $item, 2);
|
|
|
|
$key = $this->parse($key);
|
|
|
|
$value = $this->parse($value);
|
|
|
|
} else {
|
|
|
|
$key = ++$lastKey;
|
|
|
|
$value = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($key)) {
|
|
|
|
$lastKey = $key;
|
|
|
|
}
|
|
|
|
$result[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $body
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return array
|
2015-03-13 15:04:31 +03:00
|
|
|
* @throws \UnexpectedValueException
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2015-03-13 15:04:31 +03:00
|
|
|
private function splitArray($body) {
|
|
|
|
$inSingleQuote = false; //keep track if we are inside quotes
|
2013-02-16 04:30:44 +04:00
|
|
|
$inDoubleQuote = false;
|
2015-03-13 15:04:31 +03:00
|
|
|
$bracketDepth = 0; //keep track if we are inside brackets
|
|
|
|
$parts = [];
|
2013-02-16 04:30:44 +04:00
|
|
|
$start = 0;
|
2015-03-13 15:04:31 +03:00
|
|
|
$escaped = false; //keep track if we are after an escape character
|
|
|
|
$skips = []; //keep track of the escape characters we need to remove from the result
|
2013-02-16 04:30:44 +04:00
|
|
|
if (substr($body, -1, 1) !== ',') {
|
|
|
|
$body .= ',';
|
|
|
|
}
|
2014-10-24 01:27:15 +04:00
|
|
|
|
|
|
|
$bodyLength = strlen($body);
|
|
|
|
for ($i = 0; $i < $bodyLength; $i++) {
|
2013-02-16 04:30:44 +04:00
|
|
|
$char = substr($body, $i, 1);
|
|
|
|
if ($char === '\\') {
|
|
|
|
if ($escaped) {
|
|
|
|
array_unshift($skips, $i - 1);
|
|
|
|
}
|
|
|
|
$escaped = !$escaped;
|
|
|
|
} else {
|
|
|
|
if ($char === '"' and !$inSingleQuote) {
|
|
|
|
if ($escaped) {
|
|
|
|
array_unshift($skips, $i - 1);
|
|
|
|
} else {
|
|
|
|
$inDoubleQuote = !$inDoubleQuote;
|
|
|
|
}
|
|
|
|
} elseif ($char === "'" and !$inDoubleQuote) {
|
|
|
|
if ($escaped) {
|
|
|
|
array_unshift($skips, $i - 1);
|
|
|
|
} else {
|
|
|
|
$inSingleQuote = !$inSingleQuote;
|
|
|
|
}
|
|
|
|
} elseif (!$inDoubleQuote and !$inSingleQuote) {
|
|
|
|
if ($char === '(') {
|
|
|
|
$bracketDepth++;
|
|
|
|
} elseif ($char === ')') {
|
|
|
|
if ($bracketDepth <= 0) {
|
2015-03-13 15:04:31 +03:00
|
|
|
throw new \UnexpectedValueException();
|
2013-02-16 04:30:44 +04:00
|
|
|
} else {
|
|
|
|
$bracketDepth--;
|
|
|
|
}
|
|
|
|
} elseif ($bracketDepth === 0 and $char === ',') {
|
|
|
|
$part = substr($body, $start, $i - $start);
|
|
|
|
foreach ($skips as $skip) {
|
|
|
|
$part = substr($part, 0, $skip - $start) . substr($part, $skip - $start + 1);
|
|
|
|
}
|
|
|
|
$parts[] = $part;
|
|
|
|
$start = $i + 1;
|
2015-03-13 15:04:31 +03:00
|
|
|
$skips = [];
|
2013-02-16 04:30:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$escaped = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $parts;
|
|
|
|
}
|
|
|
|
}
|