| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Copyright (c) 2015 Victor Dubiniuk <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * later. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * See the COPYING-README file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace OCA\Files_Antivirus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use \OCP\IConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @method string getAvMode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @method string getAvSocket() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @method string getAvHost() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @method int getAvPort() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @method int getAvMaxFileSize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @method string getAvCmdOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @method string getAvPath() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @method string getAvInfectedAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @method null setAvMode(string $avMode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @method null setAvSocket(string $avsocket) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @method null setAvHost(string $avHost) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @method null setAvPort(int $avPort) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @method null setAvMaxFileSize(int $fileSize) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @method null setAvCmdOptions(string $avCmdOptions) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @method null setAvChunkSize(int $chunkSize) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @method null setAvPath(string $avPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @method null setAvInfectedAction(string $avInfectedAction) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @method null setAvStreamMaxLength(int $length) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class AppConfig { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	private $appName = 'files_antivirus'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** @var IConfig  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	private $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	private $defaults = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		'av_mode' => 'executable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		'av_socket' => '/var/run/clamav/clamd.ctl', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		'av_host' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		'av_port' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		'av_cmd_options' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		'av_path' => '/usr/bin/clamscan', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		'av_max_file_size' => -1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		'av_stream_max_length' => '26214400', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		'av_infected_action' => 'only_log', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		'av_background_scan' => 'on', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * AppConfig constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * @param IConfig $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  | 	public function __construct(IConfig $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  | 		$this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	public function getAvChunkSize() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		// See http://php.net/manual/en/function.stream-wrapper-register.php#74765 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		// and \OC_Helper::streamCopy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		return 8192; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 * Get full commandline | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	public function getCmdline() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		$avCmdOptions = $this->getAvCmdOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		$shellArgs = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		if ($avCmdOptions) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			$shellArgs = explode(',', $avCmdOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			$shellArgs = array_map(function ($i) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 				return escapeshellarg($i); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			}, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 				$shellArgs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$preparedArgs = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		if (count($shellArgs)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			$preparedArgs = implode(' ', $shellArgs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		return $preparedArgs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * Get all setting values as an array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	public function getAllValues() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		$keys = array_keys($this->defaults); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$values = array_map([$this, 'getAppValue'], $keys); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$preparedKeys = array_map([$this, 'camelCase'], $keys); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		return array_combine($preparedKeys, $values); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 * Get a value by key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 | 1 |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 108 | 1 |  | 	public function getAppValue($key) { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 | 1 |  | 		$defaultValue = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 110 | 1 |  | 		if (array_key_exists($key, $this->defaults)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 			$defaultValue = $this->defaults[$key]; | 
            
                                                                        
                            
            
                                    
            
            
                | 112 | 1 |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 		return $this->config->getAppValue($this->appName, $key, $defaultValue); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * Set a value by key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	public function setAppValue($key, $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		$this->config->setAppValue($this->appName, $key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 * Set a value with magic __call invocation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 * @param array $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 * @throws \BadFunctionCallException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	protected function setter($key, $args) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		if (array_key_exists($key, $this->defaults)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			$this->setAppValue($key, $args[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			throw new \BadFunctionCallException($key . ' is not a valid key'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	 * Get a value with magic __call invocation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 * @throws \BadFunctionCallException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 1 |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 1 |  | 	protected function getter($key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 1 |  | 		if (array_key_exists($key, $this->defaults)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 			return $this->getAppValue($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		throw new \BadFunctionCallException($key . ' is not a valid key'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * Translates property_name into propertyName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 * @param string $property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	protected function camelCase($property) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		$split = explode('_', $property); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		$ucFirst = implode('', array_map('ucfirst', $split)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		return lcfirst($ucFirst); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	 * Does all the someConfig to some_config magic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	 * @param string $property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 1 |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 1 |  | 	protected function propertyToKey($property) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 1 |  | 		$parts = preg_split('/(?=[A-Z])/', $property); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		$column = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 1 |  | 		foreach ($parts as $part) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 1 |  | 			if ($column === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 				$column = $part; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 1 |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 				$column .= '_' . lcfirst($part); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		return $column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 	 * Get/set an option value by calling getSomeOption method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	 * @param string $methodName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 	 * @param array $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 	 * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 	 * @throws \BadFunctionCallException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 1 |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 1 |  | 	public function __call($methodName, $args) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 1 |  | 		$attr = lcfirst(substr($methodName, 3)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 1 |  | 		$key = $this->propertyToKey($attr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 		if (strpos($methodName, 'set') === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 1 |  | 			$this->setter($key, $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 1 |  | 		} elseif (strpos($methodName, 'get') === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 			return $this->getter($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 			throw new \BadFunctionCallException($methodName . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 					' does not exist'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 203 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |  |