Skip to content

Commit

Permalink
#923 replace Spyc with Symfony YAML class
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrichblock committed Oct 3, 2016
1 parent 26516b1 commit ec05408
Show file tree
Hide file tree
Showing 17 changed files with 2,219 additions and 1,188 deletions.
4 changes: 2 additions & 2 deletions THIRDPARTY
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ https://github.com/milesj/decoda
Released under the MIT license
http://opensource.org/licenses/MIT

* Spyc -- A Simple PHP YAML Class
https://github.com/mustangostang/spyc/
* Symfony YAML
https://github.com/symfony/yaml
Released under the MIT license
http://opensource.org/licenses/MIT

Expand Down
1 change: 1 addition & 0 deletions js/default/monstaftp_ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@ function showFileContextMenu(e,file,folder,isLin) {
editableExts[14] = 'properties';
editableExts[15] = 'example';
editableExts[16] = 'yml';
editableExts[16] = 'yaml';
editableExts[17] = 'json';

globalContextHeight=12; // top and bottom padding
Expand Down
105 changes: 80 additions & 25 deletions stuff/methods/class_app.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
include(EASYWIDIR . '/stuff/methods/class_ftp.php');
}

if (!class_exists('Yaml')) {
include(EASYWIDIR . '/third_party/Symfony/autoloader_yaml.php');
}

use Symfony\Component\Yaml\Yaml;

class AppServer {

private $uniqueHex, $winCmds = array(), $shellScriptHeader, $shellScripts = array('user' => '', 'server' => array()), $commandReturns = array(), $undefinedRequiredVars = array();
Expand Down Expand Up @@ -600,7 +606,7 @@ private function linuxAddApp ($templates, $standalone = true) {
$serverDir = ($this->appServerDetails['protectionModeStarted'] == 'Y') ? 'pserver/' : 'server/';
$absolutePath = $this->removeSlashes($this->appServerDetails['homeDir'] . '/' . $this->appServerDetails['userName'] . '/' . $serverDir . $this->appServerDetails['serverIP'] . '_' . $this->appServerDetails['port']);

$copyFileExtensions = array('xml', 'vdf', 'cfg', 'con', 'conf', 'config', 'ini', 'gam', 'txt', 'log', 'smx', 'sp', 'db', 'lang', 'lua', 'props', 'properties', 'json', 'example', 'html', 'yml');
$copyFileExtensions = array('xml', 'vdf', 'cfg', 'con', 'conf', 'config', 'ini', 'gam', 'txt', 'log', 'smx', 'sp', 'db', 'lang', 'lua', 'props', 'properties', 'json', 'example', 'html', 'yml', 'yaml');

if ($standalone and isset($scriptName)) {
$script = $this->shellScriptHeader;
Expand Down Expand Up @@ -896,11 +902,39 @@ public function stopAppHard () {
}
}

private function protectedYaml($replaceSettings, $parsedConfig) {

$customColumns = customColumns('G', $this->appServerDetails['id']);

foreach ($parsedConfig as $key => $value) {

if (is_array($value) or is_object($value)) {

$parsedConfig[$key] = $this->protectedYaml($replaceSettings, $value);

} else {

if (is_string($value) and strpos($value, '%') !== false) {
$parsedConfig[$key] = str_replace($replaceSettings['placeholder'], $replaceSettings['replacePlaceholderWith'], $value);
} else {
$parsedConfig[$key] = $value;
}

foreach ($customColumns as $customColumn) {
$parsedConfig[$key] = str_replace('%' . $customColumn['name'] . '%', $customColumn['value'], $parsedConfig[$key]);
}
}
}

return $parsedConfig;
}

private function protectedSettingsToArray () {

$protectedString = '';
$cvarProtectArray = array();
$lendServerReplaceMents = array();
$debugger= array();
$debugger = array();

$replaceSettings = $this->getReplacements();
@parse_ini_string($this->appServerDetails['template']['configedit'], true, INI_SCANNER_RAW);
Expand All @@ -909,7 +943,11 @@ private function protectedSettingsToArray () {

$line = str_replace(array("\r"), '', $line);

if (preg_match('/^(\[[\w\/\.\-\_]{1,}\]|\[[\w\/\.\-\_]{1,}\] (xml|ini|cfg|lua|json|ddot|yml|yaml))$/', $line)) {
if (preg_match('/^(\[[\w\/\.\-\_]{1,}\]|\[[\w\/\.\-\_]{1,}\] (xml|ini|cfg|lua|json|ddot|yml|Yaml|yaml))$/', $line)) {

if (strlen($protectedString) > 0 and isset($configPathAndFile) and !isset($cvarProtectArray[$configPathAndFile]['cvars']) and in_array($cvarProtectArray[$configPathAndFile]['type'], array('yml', 'yaml', 'Yaml'))) {
$cvarProtectArray[$configPathAndFile]['cvars'] = $this->protectedYaml($replaceSettings, Yaml::parse($protectedString));
}

$exploded = preg_split("/\s+/", $line, -1, PREG_SPLIT_NO_EMPTY);

Expand All @@ -919,6 +957,8 @@ private function protectedSettingsToArray () {

$cvarProtectArray[$configPathAndFile]['type'] = $cvarType;

$protectedString = '';

} else if (isset($configPathAndFile) and isset($cvarProtectArray[$configPathAndFile]['type'])) {

unset($splitLine);
Expand All @@ -927,9 +967,9 @@ private function protectedSettingsToArray () {

$splitLine = preg_split("/\s+/", $line, -1, PREG_SPLIT_NO_EMPTY);

} else if ($cvarProtectArray[$configPathAndFile]['type'] == 'yml' or $cvarProtectArray[$configPathAndFile]['type'] == 'yaml') {
} else if (in_array($cvarProtectArray[$configPathAndFile]['type'], array('yml','yaml','Yaml'))) {

$splitLine = preg_split("/(?:(?<!-))\s*:\s*/", $line);
$protectedString .= $line . "\r\n";

} else if (in_array($cvarProtectArray[$configPathAndFile]['type'], array('ini','lua'))) {

Expand Down Expand Up @@ -977,6 +1017,10 @@ private function protectedSettingsToArray () {
}
}

if (strlen($protectedString) > 0 and isset($configPathAndFile) and !isset($cvarProtectArray[$configPathAndFile]['cvars']) and in_array($cvarProtectArray[$configPathAndFile]['type'], array('yml', 'yaml', 'Yaml'))) {
$cvarProtectArray[$configPathAndFile]['cvars'] = $this->protectedYaml($replaceSettings, Yaml::parse($protectedString));
}

if ($this->appServerDetails['lendServer'] == 'Y') {

if ($this->appServerDetails['lendServer'] == 'Y') {
Expand Down Expand Up @@ -1047,15 +1091,18 @@ private function replaceArrayValues($givenArray, $replacements) {
foreach(array_keys($givenArray) as $key) {

if (is_array($givenArray[$key])) {
$givenArray[$key] = $this->replaceArrayValues($givenArray[$key], $replacements);
}

if (isset($this->undefinedRequiredVars[$key])) {
unset($this->undefinedRequiredVars[$key]);
}
$givenArray[$key] = $this->replaceArrayValues($givenArray[$key], ((is_array($replacements) or is_object($replacements)) and isset($replacements[$key])) ? $replacements[$key] : $replacements);

} else {

if (isset($this->undefinedRequiredVars[$key])) {
unset($this->undefinedRequiredVars[$key]);
}

if (isset($replacements[$key])) {
$givenArray[$key] = $replacements[$key];
if (isset($replacements[$key])) {
$givenArray[$key] = $replacements[$key];
}
}
}

Expand Down Expand Up @@ -1119,6 +1166,22 @@ private function replaceIni($stored, $replacements) {
return $iniString;
}

private function addNotFoundVars($replacedArray, $key, $value) {

if (!isset($replacedArray[$key]) or (!is_array($replacedArray[$key]) and !is_object($replacedArray[$key]))) {

$replacedArray[$key] = $value;

} else {

foreach ($replacedArray[$key] as $k => $v) {
$replacedArray[$key] = $this->addNotFoundVars($replacedArray[$key], $k, $v);
}
}

return $replacedArray;
}

private function replaceArray($stored, $replacements) {

if (!$stored) {
Expand All @@ -1128,14 +1191,14 @@ private function replaceArray($stored, $replacements) {
$replacedArray = $this->replaceArrayValues($stored, $replacements);

foreach ($this->undefinedRequiredVars as $key => $value) {
$replacedArray[$key] = $value;
$replacedArray = $this->addNotFoundVars($replacedArray, $key, $value);
}

return $replacedArray;
}

private function replaceYaml($stored, $replacements) {
return Spyc::YAMLDump($this->replaceArray($stored, $replacements));
return Yaml::dump($this->replaceArray($stored, $replacements), 2, 4);
}

private function replaceJSON($stored, $replacements) {
Expand Down Expand Up @@ -1186,15 +1249,9 @@ private function correctProtectedFiles () {

$ftpObect->writeContentToTemp($this->replaceIni(@parse_ini_string($configFileContent, false, INI_SCANNER_RAW), $values['cvars']));

} else if ($values['type'] === 'yml' or $values['type'] === 'yaml') {

if (!class_exists('Spyc')) {
include(EASYWIDIR . '/third_party/spyc/Spyc.php');
}

$parsedConfig = Spyc::YAMLLoadString($configFileContent);
} else if ($values['type'] === 'yml' or $values['type'] === 'Yaml' or $values['type'] === 'yaml') {

$ftpObect->writeContentToTemp($this->replaceYaml($parsedConfig, $values['cvars']));
$ftpObect->writeContentToTemp($this->replaceYaml(Yaml::parse($configFileContent), $values['cvars']));

} else if ($values['type'] == 'json') {

Expand All @@ -1206,9 +1263,7 @@ private function correctProtectedFiles () {
include(EASYWIDIR . '/stuff/methods/class_lua.php');
}

$parsedConfig = Lua::luaToArray($configFileContent);

$ftpObect->writeContentToTemp($this->replaceLua($parsedConfig, $values['cvars']));
$ftpObect->writeContentToTemp($this->replaceLua(Lua::luaToArray($configFileContent), $values['cvars']));

} else {

Expand Down
77 changes: 77 additions & 0 deletions third_party/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Yaml;

/**
* Dumper dumps PHP variables to YAML strings.
*
* @author Fabien Potencier <[email protected]>
*/
class Dumper
{
/**
* The amount of spaces to use for indentation of nested nodes.
*
* @var int
*/
protected $indentation = 4;

/**
* Sets the indentation.
*
* @param int $num The amount of spaces to use for indentation of nested nodes
*/
public function setIndentation($num)
{
if ($num < 1) {
throw new \InvalidArgumentException('The indentation must be greater than zero.');
}

$this->indentation = (int) $num;
}

/**
* Dumps a PHP value to YAML.
*
* @param mixed $input The PHP value
* @param int $inline The level where you switch to inline YAML
* @param int $indent The level of indentation (used internally)
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
* @param bool $objectSupport true if object support is enabled, false otherwise
*
* @return string The YAML representation of the PHP value
*/
public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $objectSupport = false)
{
$output = '';
$prefix = $indent ? str_repeat(' ', $indent) : '';

if ($inline <= 0 || !is_array($input) || empty($input)) {
$output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $objectSupport);
} else {
$isAHash = Inline::isHash($input);

foreach ($input as $key => $value) {
$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);

$output .= sprintf('%s%s%s%s',
$prefix,
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $objectSupport).':' : '-',
$willBeInlined ? ' ' : "\n",
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $objectSupport)
).($willBeInlined ? "\n" : '');
}
}

return $output;
}
}
99 changes: 99 additions & 0 deletions third_party/Symfony/Component/Yaml/Escaper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Yaml;

/**
* Escaper encapsulates escaping rules for single and double-quoted
* YAML strings.
*
* @author Matthew Lewinski <[email protected]>
*
* @internal
*/
class Escaper
{
// Characters that would cause a dumped string to require double quoting.
const REGEX_CHARACTER_TO_ESCAPE = "[\\x00-\\x1f]|\xc2\x85|\xc2\xa0|\xe2\x80\xa8|\xe2\x80\xa9";

// Mapping arrays for escaping a double quoted string. The backslash is
// first to ensure proper escaping because str_replace operates iteratively
// on the input arrays. This ordering of the characters avoids the use of strtr,
// which performs more slowly.
private static $escapees = array('\\', '\\\\', '\\"', '"',
"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07",
"\x08", "\x09", "\x0a", "\x0b", "\x0c", "\x0d", "\x0e", "\x0f",
"\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17",
"\x18", "\x19", "\x1a", "\x1b", "\x1c", "\x1d", "\x1e", "\x1f",
"\xc2\x85", "\xc2\xa0", "\xe2\x80\xa8", "\xe2\x80\xa9");
private static $escaped = array('\\\\', '\\"', '\\\\', '\\"',
'\\0', '\\x01', '\\x02', '\\x03', '\\x04', '\\x05', '\\x06', '\\a',
'\\b', '\\t', '\\n', '\\v', '\\f', '\\r', '\\x0e', '\\x0f',
'\\x10', '\\x11', '\\x12', '\\x13', '\\x14', '\\x15', '\\x16', '\\x17',
'\\x18', '\\x19', '\\x1a', '\\e', '\\x1c', '\\x1d', '\\x1e', '\\x1f',
'\\N', '\\_', '\\L', '\\P');

/**
* Determines if a PHP value would require double quoting in YAML.
*
* @param string $value A PHP value
*
* @return bool True if the value would require double quotes
*/
public static function requiresDoubleQuoting($value)
{
return preg_match('/'.self::REGEX_CHARACTER_TO_ESCAPE.'/u', $value);
}

/**
* Escapes and surrounds a PHP value with double quotes.
*
* @param string $value A PHP value
*
* @return string The quoted, escaped string
*/
public static function escapeWithDoubleQuotes($value)
{
return sprintf('"%s"', str_replace(self::$escapees, self::$escaped, $value));
}

/**
* Determines if a PHP value would require single quoting in YAML.
*
* @param string $value A PHP value
*
* @return bool True if the value would require single quotes
*/
public static function requiresSingleQuoting($value)
{
// Determines if a PHP value is entirely composed of a value that would
// require single quoting in YAML.
if (in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off'))) {
return true;
}

// Determines if the PHP value contains any single characters that would
// cause it to require single quoting in YAML.
return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value);
}

/**
* Escapes and surrounds a PHP value with single quotes.
*
* @param string $value A PHP value
*
* @return string The quoted, escaped string
*/
public static function escapeWithSingleQuotes($value)
{
return sprintf("'%s'", str_replace('\'', '\'\'', $value));
}
}
Loading

0 comments on commit ec05408

Please sign in to comment.