Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
created

apps/files_sharing/lib/external/scanner.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Olivier Paroz <[email protected]>
8
 * @author Robin Appelman <[email protected]>
9
 * @author Vincent Petry <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OCA\Files_Sharing\External;
28
29
use OC\ForbiddenException;
30
use OCP\Files\NotFoundException;
31
use OCP\Files\StorageInvalidException;
32
use OCP\Files\StorageNotAvailableException;
33
34
class Scanner extends \OC\Files\Cache\Scanner {
35
	/** @var \OCA\Files_Sharing\External\Storage */
36
	protected $storage;
37
38
	/** {@inheritDoc} */
39
	public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) {
40
		if(!$this->storage->remoteIsOwnCloud()) {
41
			return parent::scan($path, $recursive, $recursive, $lock);
0 ignored issues
show
$recursive is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
		}
43
44
		$this->scanAll();
45
	}
46
47
	/**
48
	 * Scan a single file and store it in the cache.
49
	 * If an exception happened while accessing the external storage,
50
	 * the storage will be checked for availability and removed
51
	 * if it is not available any more.
52
	 *
53
	 * @param string $file file to scan
54
	 * @param int $reuseExisting
55
	 * @param int $parentId
56
	 * @param array | null $cacheData existing data in the cache for the file to be scanned
57
	 * @param bool $lock set to false to disable getting an additional read lock during scanning
58
	 * @return array an array of metadata of the scanned file
59
	 */
60
	public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) {
61
		try {
62
			return parent::scanFile($file, $reuseExisting);
63
		} catch (ForbiddenException $e) {
64
			$this->storage->checkStorageAvailability();
65
		} catch (NotFoundException $e) {
66
			// if the storage isn't found, the call to
67
			// checkStorageAvailable() will verify it and remove it
68
			// if appropriate
69
			$this->storage->checkStorageAvailability();
70
		} catch (StorageInvalidException $e) {
71
			$this->storage->checkStorageAvailability();
72
		} catch (StorageNotAvailableException $e) {
73
			$this->storage->checkStorageAvailability();
74
		}
75
	}
76
77
	/**
78
	 * Checks the remote share for changes.
79
	 * If changes are available, scan them and update
80
	 * the cache.
81
	 * @throws NotFoundException
82
	 * @throws StorageInvalidException
83
	 * @throws \Exception
84
	 */
85
	public function scanAll() {
86
		try {
87
			$data = $this->storage->getShareInfo();
88
		} catch (\Exception $e) {
89
			$this->storage->checkStorageAvailability();
90
			throw new \Exception(
91
				'Error while scanning remote share: "' .
92
				$this->storage->getRemote() . '" ' .
93
				$e->getMessage()
94
			);
95
		}
96
		if ($data['status'] === 'success') {
97
			$this->addResult($data['data'], '');
98
		} else {
99
			throw new \Exception(
100
				'Error while scanning remote share: "' .
101
				$this->storage->getRemote() . '"'
102
			);
103
		}
104
	}
105
106
	/**
107
	 * @param array $data
108
	 * @param string $path
109
	 */
110
	private function addResult($data, $path) {
111
		$id = $this->cache->put($path, $data);
112
		if (isset($data['children'])) {
113
			$children = [];
114
			foreach ($data['children'] as $child) {
115
				$children[$child['name']] = true;
116
				$this->addResult($child, ltrim($path . '/' . $child['name'], '/'));
117
			}
118
119
			$existingCache = $this->cache->getFolderContentsById($id);
120
			foreach ($existingCache as $existingChild) {
121
				// if an existing child is not in the new data, remove it
122
				if (!isset($children[$existingChild['name']])) {
123
					$this->cache->remove(ltrim($path . '/' . $existingChild['name'], '/'));
124
				}
125
			}
126
		}
127
	}
128
}
129