| 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 |  |  | class External extends \OCA\Files_Antivirus\Scanner { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	// Daemon/socket mode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	private $useSocket; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 2 |  | 	public function __construct($config){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 2 |  | 		$this->appConfig = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 2 |  | 		$this->useSocket = $this->appConfig->getAvMode() === 'socket'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 2 |  | 	public function initScanner(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 2 |  | 		parent::initScanner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 2 |  | 		if ($this->useSocket){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			$avSocket = $this->appConfig->getAvSocket(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			$this->writeHandle = stream_socket_client('unix://' . $avSocket, $errno, $errstr, 5); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			if (!$this->getWriteHandle()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 				throw new \RuntimeException('Cannot connect to "' . $avSocket . '": ' . $errstr . ' (code ' . $errno . ')'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  | 			$avHost = $this->appConfig->getAvHost(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 2 |  | 			$avPort = $this->appConfig->getAvPort(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 2 |  | 			$this->writeHandle = ($avHost && $avPort) ? @fsockopen($avHost, $avPort) : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  | 			if (!$this->getWriteHandle()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  | 				throw new \RuntimeException('The clamav module is not configured for daemon mode.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		// request scan from the daemon | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  | 		@fwrite($this->getWriteHandle(), "nINSTREAM\n"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  | 	protected function shutdownScanner(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  | 		@fwrite($this->getWriteHandle(), pack('N', 0)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  | 		$response = fgets($this->getWriteHandle()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  | 		\OC::$server->getLogger()->debug( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  | 			'Response :: ' . $response, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  | 			['app' => 'files_antivirus'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  | 		@fclose($this->getWriteHandle()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  | 		$this->status->parseResponse($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  | 	protected function prepareChunk($data){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  | 		$chunkLength = pack('N', strlen($data)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 | 1 |  | 		return $chunkLength . $data; | 
            
                                                        
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 60 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  | 
            
                        
If you suppress an error, we recommend checking for the error condition explicitly: