Skip to content

Commit

Permalink
Replace custom config parser #889
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrichblock committed Jul 22, 2016
1 parent 0ebcdca commit e007e84
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 35 deletions.
71 changes: 36 additions & 35 deletions stuff/methods/class_app.php
Original file line number Diff line number Diff line change
Expand Up @@ -1088,9 +1088,11 @@ private function generateIniString($array) {

private function replaceIni($stored, $replacements) {

$this->undefinedRequiredVars = $replacements;
$iniString = "";
if (!$stored) {
$stored = array();
}

$iniString = "";
$arrayKeys = array_keys($stored);

if (count($arrayKeys) !== 0) {
Expand All @@ -1112,17 +1114,31 @@ private function replaceIni($stored, $replacements) {
return $iniString;
}

private function replaceYaml($stored, $replacements) {
private function replaceArray($stored, $replacements) {

$this->undefinedRequiredVars = $replacements;
if (!$stored) {
$stored = array();
}

$replacedArray = $this->replaceArrayValues($stored, $replacements);

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

return Spyc::YAMLDump($replacedArray);
return $replacedArray;
}

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

private function replaceJSON($stored, $replacements) {
return json_encode($this->replaceArray($stored, $replacements), JSON_PRETTY_PRINT);
}

private function replaceLua($stored, $replacements) {
return Lua::arrayToLua($this->replaceArray($stored, $replacements));
}

private function correctProtectedFiles () {
Expand Down Expand Up @@ -1158,17 +1174,12 @@ private function correctProtectedFiles () {

// We have one temp handle for all files to reduce the amount of needed ram
$ftpObect->tempHandle = null;
$this->undefinedRequiredVars = $values['cvars'];

//TODO handle each type of file with specific parser
if ($values['type'] === 'ini') {

$parsedConfig = @parse_ini_string($configFileContent, false, INI_SCANNER_RAW);

if (!$parsedConfig) {
$parsedConfig = array();
}

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

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

Expand All @@ -1178,11 +1189,21 @@ private function correctProtectedFiles () {

$parsedConfig = Spyc::YAMLLoadString($configFileContent);

if (!$parsedConfig) {
$parsedConfig = array();
$ftpObect->writeContentToTemp($this->replaceYaml($parsedConfig, $values['cvars']));

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

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

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

if (!class_exists('Lua')) {
include(EASYWIDIR . '/stuff/methods/class_lua.php');
}

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

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

} else {

Expand Down Expand Up @@ -1225,26 +1246,6 @@ private function correctProtectedFiles () {

$ftpObect->writeContentToTemp($cvar . ':' . $value);

} else if ($values['type'] == 'lua' and preg_match("/^(.*)" . strtolower($cvar) . "[\s+]{0,}\=[\s+]{0,}(.*)[\,]$/", $loweredSingleLine)) {

$edited = true;

unset($cvarsNotFound[$cvar]);

$splitLine = preg_split('/' . $cvar . '/', $singeLine, -1, PREG_SPLIT_NO_EMPTY);

$ftpObect->writeContentToTemp((isset($splitLine[1])) ? $splitLine[0] . $cvar. ' = ' .$value : $cvar . '=' . $value);

} else if ($values['type'] == 'json' and preg_match("/^(.*)[\"]" . strtolower($cvar) . "[\s+]{0,}:[\s+]{0,}(.*)[\,]{0,1}$/", $loweredSingleLine)) {

$edited = true;

unset($cvarsNotFound[$cvar]);

$splitLine = preg_split('/' . $cvar . '/', $singeLine, -1, PREG_SPLIT_NO_EMPTY);

$ftpObect->writeContentToTemp((isset($splitLine[1])) ? $splitLine[0] . $cvar. ' : ' .$value : $cvar . ':' . $value);

} else if ($values['type'] == 'xml' and @preg_match("/^(.*)\<" . strtolower($cvar) . "\>(.*)\<\/" . strtolower($cvar) . "\>(.*)$/", $loweredSingleLine)) {

$edited = true;
Expand Down
115 changes: 115 additions & 0 deletions stuff/methods/class_lua.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* File: class_lua.php.
* Author: Ulrich Block
* Date: 17.07.16
* Contact: <[email protected]>
*
* This file is part of Easy-WI.
*
* Easy-WI is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Easy-WI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Easy-WI. If not, see <http://www.gnu.org/licenses/>.
*
* Diese Datei ist Teil von Easy-WI.
*
* Easy-WI ist Freie Software: Sie koennen es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder spaeteren
* veroeffentlichten Version, weiterverbreiten und/oder modifizieren.
*
* Easy-WI wird in der Hoffnung, dass es nuetzlich sein wird, aber
* OHNE JEDE GEWAEHELEISTUNG, bereitgestellt; sogar ohne die implizite
* Gewaehrleistung der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License fuer weitere Details.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*/

class Lua {

public static function luaToArray($string) {
$Lua = new Lua;
return $Lua->parseBlocks($string);
}

private function parseBlocks($string) {

$parsedBlocks = array();

preg_match_all('/([^\s]+)[\s]*=[\s]*[\s]+(\{[\s\S]+?[\}](?:[\r\n\s,]*\})*)/m', $string, $blocks, PREG_SET_ORDER);

foreach ($blocks as $block) {

if (isset($block[1]) and isset($block[2])) {

$parsedBlocks[$block[1]] = $this->parseBlocks($block[2]);

foreach ($this->parseBlock($block[2], $this->getIndention($block[2])) as $key => $value) {
$parsedBlocks[$block[1]][$key] = $value;
}
}
}

return $parsedBlocks;
}

private function parseBlock($string, $indention) {

$parsedBlock = array();

preg_match_all('/' . $indention . '([^\s]+)[\s]*=[\s]*(?:([\d.]+)|"([\s\S]+?)"),?/', $string, $keyValues, PREG_SET_ORDER);

foreach($keyValues as $keyValue) {
if (isset($keyValue[1]) and (isset($keyValue[2]) or isset($keyValue[3]))) {
$parsedBlock[$keyValue[1]] = (isset($keyValue[3])) ? $keyValue[3] : $keyValue[2];
}
}

return $parsedBlock;
}

private function getIndention($string) {

preg_match('/[\r\n]+(\s+)/', $string, $indention);

return '[\r\n]+' . $indention[1];
}

public static function arrayToLua($array) {
$Lua = new Lua;
return $Lua->arrayOrValue($array, '');
}

private function arrayOrValue($array, $indention) {

$newIndention = ' ' . $indention;
$luaStringParts = array();

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

if (is_array($value)) {
$luaStringParts[] = $indention . $key . " =\n" . $indention . "{" . $this->arrayOrValue($value, $newIndention) . "\n$indention}";
} else {
$luaStringParts[] = $indention . $key . ' = ' . $value;
}
}

if ($indention === '') {
return implode(",\n", $luaStringParts);
}

return "\n" . implode(",\n", $luaStringParts);
}
}

0 comments on commit e007e84

Please sign in to comment.