first
This commit is contained in:
@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* SerializeParser
|
||||
*
|
||||
* @copyright Jason Judge
|
||||
*
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*
|
||||
* @link https://github.com/academe/SerializeParser/blob/master/src/Parser.php
|
||||
*
|
||||
* The original code was modified by Nextendweb. We stripped the not relevant parts.
|
||||
*/
|
||||
|
||||
namespace Nextend\SmartSlider3\BackupSlider\Serialize;
|
||||
|
||||
use Nextend\Framework\Notification\Notification;
|
||||
|
||||
class Parser {
|
||||
|
||||
protected $allowedClassNames = [
|
||||
'Nextend\SmartSlider3\BackupSlider\BackupData',
|
||||
'Nextend\SmartSlider3\Slider\SliderParams'
|
||||
];
|
||||
|
||||
/**
|
||||
* This is the recursive parser.
|
||||
*/
|
||||
protected function doParse(StringReader $string) {
|
||||
|
||||
// May be : or ; as a terminator, depending on what the data
|
||||
// type is.
|
||||
|
||||
$type = substr($string->read(2), 0, 1);
|
||||
|
||||
switch ($type) {
|
||||
case 'a':
|
||||
// Associative array: a:length:{[index][value]...}
|
||||
$count = (int)$string->readUntil(':');
|
||||
|
||||
// Eat the opening "{" of the array.
|
||||
$string->read(1);
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$this->doParse($string);//array key
|
||||
$this->doParse($string);//array value
|
||||
}
|
||||
|
||||
// Eat "}" terminating the array.
|
||||
$string->read(1);
|
||||
|
||||
break;
|
||||
|
||||
case 'O':
|
||||
// Object: O:length:"class":length:{[property][value]...}
|
||||
$len = (int)$string->readUntil(':');
|
||||
|
||||
// +2 for quotes
|
||||
$class = $string->read(2 + $len);
|
||||
|
||||
if (!in_array($class, $this->allowedClassNames)) {
|
||||
Notification::error(sprintf(n2_('The importing failed as the slider export contained an invalid class name: %1$s'), '<br>' . $class));
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Eat the separator
|
||||
$string->read(1);
|
||||
|
||||
|
||||
// Read the number of properties.
|
||||
$len = (int)$string->readUntil(':');
|
||||
|
||||
// Eat "{" holding the properties.
|
||||
$string->read(1);
|
||||
|
||||
for ($i = 0; $i < $len; $i++) {
|
||||
$this->doParse($string);//prop key
|
||||
$this->doParse($string);//prop value
|
||||
}
|
||||
|
||||
// Eat "}" terminating properties.
|
||||
$string->read(1);
|
||||
|
||||
break;
|
||||
|
||||
case 's':
|
||||
$len = (int)$string->readUntil(':');
|
||||
$string->read($len + 2);
|
||||
|
||||
// Eat the separator
|
||||
$string->read(1);
|
||||
break;
|
||||
|
||||
case 'i':
|
||||
$string->readUntil(';');
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
$string->readUntil(';');
|
||||
break;
|
||||
|
||||
case 'b':
|
||||
// Boolean is 0 or 1
|
||||
$string->read(2);
|
||||
break;
|
||||
|
||||
case 'N':
|
||||
break;
|
||||
|
||||
default:
|
||||
Notification::error(sprintf(n2_('The importing failed as we are not able to unserialize the type: "%s"'), '<br>' . $type));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $string
|
||||
*
|
||||
*
|
||||
* Checks if the $string contains any Class names which are not allowed by us!
|
||||
* This is the initial entry point into the recursive parser.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function isValidData($string) {
|
||||
return $this->doParse(new StringReader($string));
|
||||
}
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* SerializeParser
|
||||
*
|
||||
* @copyright Jason Judge
|
||||
*
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*
|
||||
* @link https://github.com/academe/SerializeParser/blob/master/src/StringReader.php
|
||||
*/
|
||||
|
||||
namespace Nextend\SmartSlider3\BackupSlider\Serialize;
|
||||
|
||||
/**
|
||||
* Given a string, this class will read through the string using
|
||||
* one of a number of terminating rules:
|
||||
* - One character.
|
||||
* - A specified number of characters.
|
||||
* - Until a matching character is found.
|
||||
*/
|
||||
|
||||
class StringReader
|
||||
{
|
||||
protected $pos = 0;
|
||||
protected $max = 0;
|
||||
protected $string = [];
|
||||
|
||||
public function __construct($string)
|
||||
{
|
||||
// Split the string up into an array of UTF-8 characters.
|
||||
// As an array we can read through it one character at a time.
|
||||
|
||||
//$this->string = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
|
||||
//$this->max = count($this->string) - 1;
|
||||
$this->string = $string;
|
||||
$this->max = strlen($this->string) - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the next character from the supplied string.
|
||||
* Return null when we have run out of characters.
|
||||
*/
|
||||
public function readOne()
|
||||
{
|
||||
if ($this->pos <= $this->max) {
|
||||
$value = $this->string[$this->pos];
|
||||
$this->pos += 1;
|
||||
} else {
|
||||
$value = null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read characters until we reach the given character $char.
|
||||
* By default, discard that final matching character and return
|
||||
* the rest.
|
||||
*/
|
||||
public function readUntil($char, $discard_char = true)
|
||||
{
|
||||
$value = '';
|
||||
|
||||
while(null !== ($one = $this->readOne())) {
|
||||
if ($one !== $char || !$discard_char) {
|
||||
$value .= $one;
|
||||
}
|
||||
|
||||
if ($one === $char) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read $count characters, or until we have reached the end,
|
||||
* whichever comes first.
|
||||
* By default, remove enclosing double-quotes from the result.
|
||||
*/
|
||||
public function read($count, $strip_quotes = true)
|
||||
{
|
||||
$value = '';
|
||||
|
||||
while($count > 0 && null != ($one = $this->readOne())) {
|
||||
$value .= $one;
|
||||
$count -= 1;
|
||||
}
|
||||
|
||||
return $strip_quotes ? $this->stripQuotes($value) : $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a single set of double-quotes from around a string.
|
||||
* abc => abc
|
||||
* "abc" => abc
|
||||
* ""abc"" => "abc"
|
||||
*
|
||||
* @param string string
|
||||
* @returns string
|
||||
*/
|
||||
public function stripQuotes($string)
|
||||
{
|
||||
// Only remove exactly one quote from the start and the end,
|
||||
// and then only if there is one at each end.
|
||||
|
||||
if (strlen($string) < 2 || substr($string, 0, 1) !== '"' || substr($string, -1, 1) !== '"') {
|
||||
// Too short, or does not start or end with a quote.
|
||||
return $string;
|
||||
}
|
||||
|
||||
// Return the middle of the string, from the second character to the second-but-last.
|
||||
return substr($string, 1, -1);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user