| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Copyright (c) 2014 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 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace OCA\Files_Antivirus\Scanner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use OCA\Files_Antivirus\AppConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use OCA\Files_Antivirus\StatusFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use OCP\ILogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class ExternalClam extends ScannerBase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * Daemon/socket mode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	private $useSocket; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * External constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * @param AppConfig $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * @param ILogger $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * @param StatusFactory $statusFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 2 |  | 	public function __construct(AppConfig $config, ILogger $logger, StatusFactory $statusFactory) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  | 		parent::__construct($config, $logger, $statusFactory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 2 |  | 		$this->useSocket = $this->appConfig->getAvMode() === 'socket'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	public function initScanner(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		parent::initScanner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		if ($this->useSocket){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			$avSocket = $this->appConfig->getAvSocket(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 			$this->writeHandle = stream_socket_client('unix://' . $avSocket, $errno, $errstr, 5); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			if (!$this->getWriteHandle()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 				throw new \RuntimeException('Cannot connect to "' . $avSocket . '": ' . $errstr . ' (code ' . $errno . ')'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			$avHost = $this->appConfig->getAvHost(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			$avPort = $this->appConfig->getAvPort(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			if (!($avHost && $avPort)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 				throw new \RuntimeException('The ClamAV port and host are not set up.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			$this->writeHandle = ($avHost && $avPort) ? @fsockopen($avHost, $avPort) : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 			if (!$this->getWriteHandle()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 				throw new \RuntimeException('The ClamAV module is not in daemon mode.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		// request scan from the daemon | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		@fwrite($this->getWriteHandle(), "nINSTREAM\n"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  | 	 | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 	protected function shutdownScanner(){ | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 		@fwrite($this->getWriteHandle(), pack('N', 0)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 		$response = fgets($this->getWriteHandle()); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 		$this->logger->debug( | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 			'Response :: ' . $response, | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 			['app' => 'files_antivirus'] | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 		@fclose($this->getWriteHandle()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 		$this->status->parseResponse($response); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	protected function prepareChunk($data){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		$chunkLength = pack('N', strlen($data)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		return $chunkLength . $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  | 
            
                        
If you suppress an error, we recommend checking for the error condition explicitly: