2016-02-14 22:38:27 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - gallery
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Olivier Paroz <owncloud@interfasys.ch>
|
|
|
|
*
|
2016-06-18 19:33:27 +03:00
|
|
|
* @copyright Olivier Paroz 2016
|
2016-02-14 22:38:27 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Gallery\Config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates parsed configuration elements
|
|
|
|
*
|
|
|
|
* @package OCA\Gallery\Config
|
|
|
|
*/
|
|
|
|
class ConfigValidator {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the content of that sub-section is safe for web use
|
|
|
|
*
|
|
|
|
* @param string $key the configuration sub-section identifier
|
|
|
|
* @param array $parsedConfigItem the configuration for a sub-section
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isConfigSafe($key, $parsedConfigItem) {
|
|
|
|
$safe = true;
|
|
|
|
|
|
|
|
switch ($key) {
|
|
|
|
case 'sorting':
|
2016-03-11 00:49:46 +03:00
|
|
|
$safe = $this->isSortingSafe('type',$parsedConfigItem, $safe);
|
2016-03-10 22:33:07 +03:00
|
|
|
$safe = $this->isSortingSafe('order',$parsedConfigItem, $safe);
|
2016-02-14 22:38:27 +03:00
|
|
|
break;
|
|
|
|
case 'design':
|
|
|
|
$safe = $this->isDesignColourSafe($parsedConfigItem, $safe);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $safe;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the sorting type found in the config file is safe for web use
|
2016-03-11 00:49:46 +03:00
|
|
|
* @param string will specify the key to check 'type' or 'order'
|
2016-02-14 22:38:27 +03:00
|
|
|
* @param array $parsedConfigItem the sorting configuration to analyse
|
|
|
|
* @param bool $safe whether the current config has been deemed safe to use so far
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-03-10 22:33:07 +03:00
|
|
|
private function isSortingSafe($key,$parsedConfigItem, $safe) {
|
|
|
|
if ($safe && array_key_exists($key, $parsedConfigItem)) {
|
|
|
|
$safe = $safe && $this->sortingValidator($key, $parsedConfigItem[ $key ]);
|
2016-02-14 22:38:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $safe;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the background colour found in the config file is safe for web use
|
|
|
|
*
|
|
|
|
* @param array $parsedConfigItem the design configuration to analyse
|
|
|
|
* @param bool $safe whether the current config has been deemed safe to use so far
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isDesignColourSafe($parsedConfigItem, $safe) {
|
|
|
|
if (array_key_exists('background', $parsedConfigItem)) {
|
|
|
|
$background = $parsedConfigItem['background'];
|
2016-02-14 23:12:43 +03:00
|
|
|
$safe = $safe && ctype_xdigit(substr($background, 1));
|
2016-02-14 22:38:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $safe;
|
|
|
|
}
|
|
|
|
|
2016-02-14 23:12:43 +03:00
|
|
|
/**
|
|
|
|
* Validates the parsed sorting values against allowed values
|
|
|
|
*
|
|
|
|
* @param string $section the section in the sorting config to be analysed
|
|
|
|
* @param string $value the value found in that section
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function sortingValidator($section, $value) {
|
|
|
|
if ($section === 'type') {
|
|
|
|
$validValues = ['date', 'name'];
|
|
|
|
} else {
|
|
|
|
$validValues = ['des', 'asc'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return in_array($value, $validValues);
|
|
|
|
}
|
|
|
|
|
2016-02-14 22:38:27 +03:00
|
|
|
}
|