| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * ownCloud - galleryplus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * later. See the COPYING file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Olivier Paroz <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright Olivier Paroz 2015 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace OCA\GalleryPlus\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Validates parsed configuration elements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @package OCA\GalleryPlus\Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class ConfigValidator { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 * Determines if the content of that sub-section is safe for web use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * @param string $key the configuration sub-section identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * @param array $parsedConfigItem the configuration for a sub-section | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 21 |  | 	public function isConfigSafe($key, $parsedConfigItem) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 21 |  | 		$safe = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		switch ($key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 21 |  | 			case 'sorting': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 13 |  | 				$safe = $this->isSortingSafe('type', $parsedConfigItem, $safe); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 13 |  | 				$safe = $this->isSortingSafe('order', $parsedConfigItem, $safe); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 13 |  | 				break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 14 |  | 			case 'design': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 9 |  | 				$safe = $this->isDesignColourSafe($parsedConfigItem, $safe); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 9 |  | 				break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 21 |  | 		return $safe; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * Determines if the sorting type found in the config file is safe for web use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param string $key will specify the key to check 'type' or 'order' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @param array $parsedConfigItem the sorting configuration to analyse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * @param bool $safe whether the current config has been deemed safe to use so far | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 13 |  | 	private function isSortingSafe($key, $parsedConfigItem, $safe) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 13 |  | 		if ($safe && array_key_exists($key, $parsedConfigItem)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 12 |  | 			$safe = $safe && $this->sortingValidator($key, $parsedConfigItem[$key]); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 13 |  | 		return $safe; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * Determines if the background colour found in the config file is safe for web use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * @param array $parsedConfigItem the design configuration to analyse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * @param bool $safe whether the current config has been deemed safe to use so far | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 9 |  | 	private function isDesignColourSafe($parsedConfigItem, $safe) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 9 |  | 		if (array_key_exists('background', $parsedConfigItem)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 8 |  | 			$background = $parsedConfigItem['background']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 8 |  | 			$safe = $safe && ctype_xdigit(substr($background, 1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 9 |  | 		return $safe; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * Validates the parsed sorting values against allowed values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 * @param string $section the section in the sorting config to be analysed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * @param string $value the value found in that section | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 12 |  | 	private function sortingValidator($section, $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 12 |  | 		if ($section === 'type') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 11 |  | 			$validValues = ['date', 'name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 10 |  | 			$validValues = ['des', 'asc']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 12 |  | 		return in_array($value, $validValues); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  |