| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Copyright (c) 2012 Bart Visscher <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * later. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * See the COPYING-README file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace OCA\Files_Antivirus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use OC\Files\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use OCP\IL10N; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use OCP\Files\IRootFolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use OCP\IUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use OCP\IUserSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class BackgroundScanner { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	const BATCH_SIZE = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	/** @var IRootFolder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	protected $rootFolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	/** @var \OCP\Files\Folder[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	protected $userFolders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * @var ScannerFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	private $scannerFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * @var IL10N | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	private $l10n; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	protected $currentFilesystemUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	/** @var \OCP\IUserSession */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	protected $userSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * A constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @param IL10N $l10n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param IRootFolder $rootFolder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @param IUserSession $userSession | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	public function __construct(ScannerFactory $scannerFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 								IL10N $l10n, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 								IRootFolder $rootFolder, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 								IUserSession $userSession | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$this->rootFolder = $rootFolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$this->scannerFactory = $scannerFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$this->l10n = $l10n; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$this->userSession = $userSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * Background scanner main job | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * @return null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	public function run(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		// locate files that are not checked yet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			$qb->select(['fc.fileid']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 				->from('filecache', 'fc') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 				->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 				->innerJoin( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 					'fc', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 					'storages', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 					'ss', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 					$qb->expr()->andX( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 						$qb->expr()->eq('fc.storage', 'ss.numeric_id'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 						$qb->expr()->orX( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 							$qb->expr()->like('ss.id', $qb->expr()->literal('local::%')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 							$qb->expr()->like('ss.id', $qb->expr()->literal('home::%')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 						) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 					) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 				) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 				->where( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 					$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 				) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 				->andWhere( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 					$qb->expr()->orX( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 						$qb->expr()->isNull('fa.fileid'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 						$qb->expr()->gt('fc.mtime', 'fa.check_time') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 					) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 				) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 				->andWhere( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 					$qb->expr()->like('fc.path', $qb->expr()->literal('files/%')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 				) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 				->andWhere( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 					$qb->expr()->neq('fc.size', $qb->expr()->literal('0')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 				) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			$result = $qb->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		} catch(\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		$cnt = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		while ($row = $result->fetch() && $cnt < self::BATCH_SIZE) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 				$fileId = $row['fileid']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 				$owner = $this->getOwner($fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 				/** @var IUser $owner */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 				if (!$owner instanceof IUser){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 					continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 				$this->initFilesystemForUser($owner); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 				$view = Filesystem::getView(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 				$path = $view->getPath($fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 				if (!is_null($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 					$item = new Item($this->l10n, $view, $path, $fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 					$scanner = $this->scannerFactory->getScanner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 					$status = $scanner->scan($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 					$status->dispatch($item, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 				// increased only for successfully scanned files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 				$cnt = $cnt + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 			} catch (\Exception $e){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 				\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		$this->tearDownFilesystem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	 * @param int $fileId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	 * @return IUser|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	protected function getOwner($fileId){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		$mountProviderCollection = \OC::$server->getMountProviderCollection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		$mountCache = $mountProviderCollection->getMountCache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		$mounts = $mountCache->getMountsForFileId($fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		if (!empty($mounts)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			$user = $mounts[0]->getUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 			if ($user instanceof IUser) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 				return $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * @param \OCP\IUser $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 * @return \OCP\Files\Folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	protected function getUserFolder(IUser $user) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		if (!isset($this->userFolders[$user->getUID()])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 			$userFolder = $this->rootFolder->getUserFolder($user->getUID()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			$this->userFolders[$user->getUID()] = $userFolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		return $this->userFolders[$user->getUID()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	 * @param IUser $user | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  | 	protected function initFilesystemForUser(IUser $user) { | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  | 		if ($this->currentFilesystemUser !== $user->getUID()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  | 			if ($this->currentFilesystemUser !== '') { | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  | 				$this->tearDownFilesystem(); | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  | 			Filesystem::init($user->getUID(), '/' . $user->getUID() . '/files'); | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  | 			$this->userSession->setUser($user); | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  | 			$this->currentFilesystemUser = $user->getUID(); | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  | 			Filesystem::initMountPoints($user->getUID()); | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	protected function tearDownFilesystem(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 		$this->userSession->setUser(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 		\OC_Util::tearDownFS(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 	 * @deprecated since  v8.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 	public static function check(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 193 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 194 |  |  |  |