1 | <?php |
||
16 | class BackgroundScanner { |
||
17 | |||
18 | /** |
||
19 | * @var ScannerFactory |
||
20 | */ |
||
21 | private $scannerFactory; |
||
22 | |||
23 | /** |
||
24 | * @var IUserManager |
||
25 | */ |
||
26 | private $userManager; |
||
27 | |||
28 | /** |
||
29 | * @var IL10N |
||
30 | */ |
||
31 | private $l10n; |
||
32 | |||
33 | /** |
||
34 | * A constructor |
||
35 | * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory |
||
36 | * @param IUserManager $userManager |
||
37 | * @param IL10N $l10n |
||
38 | */ |
||
39 | 1 | public function __construct(ScannerFactory $scannerFactory, IUserManager $userManager, IL10N $l10n){ |
|
44 | |||
45 | /** |
||
46 | * Background scanner main job |
||
47 | * @return null |
||
48 | */ |
||
49 | 1 | public function run(){ |
|
50 | 1 | if (!$this->initFS()) { |
|
51 | return; |
||
52 | } |
||
53 | // locate files that are not checked yet |
||
54 | 1 | $dirMimetypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory'); |
|
55 | $sql = 'SELECT `*PREFIX*filecache`.`fileid`, `*PREFIX*storages`.*' |
||
56 | .' FROM `*PREFIX*filecache`' |
||
57 | 1 | .' LEFT JOIN `*PREFIX*files_antivirus` ON `*PREFIX*files_antivirus`.`fileid` = `*PREFIX*filecache`.`fileid`' |
|
58 | 1 | .' JOIN `*PREFIX*storages` ON `*PREFIX*storages`.`numeric_id` = `*PREFIX*filecache`.`storage`' |
|
59 | 1 | .' WHERE `mimetype` != ?' |
|
60 | 1 | .' AND (`*PREFIX*storages`.`id` LIKE ? OR `*PREFIX*storages`.`id` LIKE ?)' |
|
61 | 1 | .' AND (`*PREFIX*files_antivirus`.`fileid` IS NULL OR `mtime` > `check_time`)' |
|
62 | 1 | .' AND `path` LIKE ?' |
|
63 | 1 | .' AND `*PREFIX*filecache`.`size` != 0'; |
|
64 | 1 | $stmt = \OCP\DB::prepare($sql, 5); |
|
65 | try { |
||
66 | 1 | $result = $stmt->execute(array($dirMimetypeId, 'local::%', 'home::%', 'files/%')); |
|
67 | 1 | if (\OCP\DB::isError($result)) { |
|
68 | \OCP\Util::writeLog('files_antivirus', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR); |
||
69 | return; |
||
70 | } |
||
71 | 1 | } catch(\Exception $e) { |
|
72 | \OCP\Util::writeLog('files_antivirus', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); |
||
73 | return; |
||
74 | } |
||
75 | |||
76 | 1 | $view = new \OC\Files\View('/'); |
|
77 | 1 | while ($row = $result->fetchRow()) { |
|
78 | 1 | $path = $view->getPath($row['fileid']); |
|
79 | 1 | if (!is_null($path)) { |
|
80 | 1 | $item = new Item($this->l10n, $view, $path, $row['fileid']); |
|
81 | 1 | $scanner = $this->scannerFactory->getScanner(); |
|
82 | 1 | $status = $scanner->scan($item); |
|
83 | 1 | $status->dispatch($item, true); |
|
84 | 1 | } |
|
85 | 1 | } |
|
86 | 1 | \OC_Util::tearDownFS(); |
|
87 | 1 | } |
|
88 | |||
89 | /** |
||
90 | * A hack to access files and views. Better than before. |
||
91 | */ |
||
92 | 1 | protected function initFS(){ |
|
104 | |||
105 | |||
106 | /** |
||
107 | * @deprecated since v8.0.0 |
||
108 | */ |
||
109 | public static function check(){ |
||
111 | } |
||
112 |