| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | * ownCloud - files_antivirus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | * @author Manuel Deglado | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | * @copyright 2012 Manuel Deglado [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | * This library is free software; you can redistribute it and/or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | * License as published by the Free Software Foundation; either | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | * version 3 of the License, or any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | * This library is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | * GNU AFFERO GENERAL PUBLIC LICENSE for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | * You should have received a copy of the GNU Affero General Public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | * License along with this library.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | namespace OCA\Files_Antivirus\Scanner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use OCA\Files_Antivirus\IScannable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCA\Files_Antivirus\Status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | abstract class ScannerBase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * Scan result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @var Status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	protected $status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * If scanning was done part by part | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * the first detected infected part is stored here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * @var Status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	protected $infectedStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** @var  int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	protected $byteCount; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** @var  resource */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	protected $writeHandle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	/** @var \OCA\Files_Antivirus\AppConfig */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	protected $appConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	protected $lastChunk; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	/** @var bool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	protected $isLogUsed = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	/** @var bool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	protected $isAborted = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * Close used resources | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	abstract protected function shutdownScanner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  | 	public function getStatus(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  | 		if ($this->infectedStatus instanceof Status){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			return $this->infectedStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  | 		if ($this->status instanceof Status){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  | 			return $this->status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		return new Status(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * Synchronous scan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * @param IScannable $item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * @return Status | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 2 |  | 	public function scan(IScannable $item) { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 2 |  | 		$this->initScanner(); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 2 |  | 		while (false !== ($chunk = $item->fread())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 2 |  | 			$this->writeChunk($chunk); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 2 |  | 		$this->shutdownScanner(); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 2 |  | 		return $this->getStatus(); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * Async scan - new portion of data is available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * @param string $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	public function onAsyncData($data){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$this->writeChunk($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * Async scan - resource is closed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 * @return Status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	public function completeAsyncScan(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		$this->shutdownScanner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		return $this->getStatus(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * Open write handle. etc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  | 	public function initScanner(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  | 		$this->byteCount = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 2 |  | 		if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			$this->infectedStatus = clone $this->status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 2 |  | 		$this->status = new Status(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * @param string $chunk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 2 |  | 	protected function writeChunk($chunk){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 2 |  | 		$this->fwrite( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 2 |  | 			$this->prepareChunk($chunk) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 * @param string $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 2 |  | 	final protected function fwrite($data){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 2 |  | 		if ($this->isAborted){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 2 |  | 		$dataLength = strlen($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 2 |  | 		$streamSizeLimit = (int)$this->appConfig->getAvStreamMaxLength(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 2 |  | 		if ($this->byteCount + $dataLength > $streamSizeLimit){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 2 |  | 			\OC::$server->getLogger()->debug( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 2 |  | 				'reinit scanner', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 2 |  | 				['app' => 'files_antivirus'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 2 |  | 			$this->shutdownScanner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 2 |  | 			$isReopenSuccessful = $this->retry(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 			$isReopenSuccessful = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 2 |  | 		if (!$isReopenSuccessful || !$this->writeRaw($data)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 			if (!$this->isLogUsed) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 				$this->isLogUsed = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 				\OC::$server->getLogger()->warning( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 					'Failed to write a chunk. Check if Stream Length matches StreamMaxLength in ClamAV daemon settings', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 					['app' => 'files_antivirus'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 				); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			// retry on error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 			$isRetrySuccessful = $this->retry() && $this->writeRaw($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 			$this->isAborted = !$isRetrySuccessful; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 2 |  | 	protected function retry(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 2 |  | 		$this->initScanner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 2 |  | 		if (!is_null($this->lastChunk)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 			return $this->writeRaw($this->lastChunk); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 2 |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	 * @param $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 2 |  | 	protected function writeRaw($data){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 2 |  | 		$dataLength = strlen($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 2 |  | 		$bytesWritten = @fwrite($this->getWriteHandle(), $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 2 |  | 		if ($bytesWritten === $dataLength){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 2 |  | 			$this->byteCount += $bytesWritten; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 2 |  | 			$this->lastChunk = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 2 |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 	 * Get a resource to write data into | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 	 * @return resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 2 |  | 	protected function getWriteHandle(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 2 |  | 		return $this->writeHandle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 	 * Prepare chunk (if required) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 	 * @param string $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 2 |  | 	protected function prepareChunk($data){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 2 |  | 		return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 208 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 209 |  |  |  | 
            
                        
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: