| 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 Local extends \OCA\Files_Antivirus\Scanner{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	protected $avPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * STDIN and STDOUT descriptors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @var array of resources | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	private $pipes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * Process handle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * @var resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	private $process; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 5 |  | 	public function __construct($config){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 5 |  | 		$this->appConfig = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		// get the path to the executable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 5 |  | 		$this->avPath = escapeshellcmd($this->appConfig->getAvPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		// check that the executable is available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 5 |  | 		if (!file_exists($this->avPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  | 			throw new \RuntimeException('The antivirus executable could not be found at ' . $this->avPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 3 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  | 	public function initScanner(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  | 		parent::initScanner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		// using 2>&1 to grab the full command-line output. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  | 		$cmd = $this->avPath . " " . $this->appConfig->getCmdline() ." - 2>&1"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		$descriptorSpec = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 3 |  | 			0 => ["pipe","r"], // STDIN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			1 => ["pipe","w"]  // STDOUT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  | 		$this->process = proc_open($cmd, $descriptorSpec, $this->pipes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  | 		if (!is_resource($this->process)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			throw new \RuntimeException('Error starting process'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 3 |  | 		$this->writeHandle = $this->pipes[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 3 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  | 	protected function shutdownScanner(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  | 		@fclose($this->pipes[0]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  | 		$output = stream_get_contents($this->pipes[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  | 		@fclose($this->pipes[1]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  | 		$result = proc_close($this->process); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  | 		\OC::$server->getLogger()->debug( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 3 |  | 			'Exit code :: ' . $result . ' Response :: ' . $output, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  | 			['app' => 'files_antivirus'] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  | 		); | 
            
                                                        
            
                                    
            
            
                | 69 | 3 |  | 		$this->status->parseResponse($output, $result); | 
            
                                                        
            
                                    
            
            
                | 70 | 3 |  | 	} | 
            
                                                        
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  | 
            
                        
If you suppress an error, we recommend checking for the error condition explicitly: