Completed
Push — master ( 8a811f...3eee97 )
by Thomas
07:57 queued 07:23
created

Autoloader::disableGlobalClassPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Andreas Fischer <[email protected]>
4
 * @author Georg Ehrke <[email protected]>
5
 * @author Joas Schilling <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Markus Goetz <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Robin McCorkell <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 * @author Thomas Müller <[email protected]>
13
 * @author Vincent Petry <[email protected]>
14
 *
15
 * @copyright Copyright (c) 2018, ownCloud GmbH
16
 * @license AGPL-3.0
17
 *
18
 * This code is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License, version 3,
20
 * as published by the Free Software Foundation.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License, version 3,
28
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
29
 *
30
 */
31
32
namespace OC;
33
34
use \OCP\AutoloadNotAllowedException;
35
36
class Autoloader {
37
	/** @var array */
38
	private $validRoots = [];
39
40
	/**
41
	 * Optional low-latency memory cache for class to path mapping.
42
	 *
43
	 * @var \OC\Memcache\Cache
44
	 */
45
	protected $memoryCache;
46
47
	/**
48
	 * Autoloader constructor.
49
	 *
50
	 * @param string[] $validRoots
51
	 */
52
	public function __construct(array $validRoots) {
53
		foreach ($validRoots as $root) {
54
			$this->validRoots[$root] = true;
55
		}
56
	}
57
58
	/**
59
	 * Add a path to the list of valid php roots for auto loading
60
	 *
61
	 * @param string $root
62
	 */
63
	public function addValidRoot($root) {
64
		$root = \stream_resolve_include_path($root);
65
		$this->validRoots[$root] = true;
66
	}
67
68
	/**
69
	 * get the possible paths for a class
70
	 *
71
	 * @param string $class
72
	 * @return array|bool an array of possible paths or false if the class is not part of ownCloud
73
	 */
74
	public function findClass($class) {
75
		$class = \trim($class, '\\');
76
77
		$paths = [];
78
		if (\strpos($class, 'OC_') === 0) {
79
			$paths[] = \OC::$SERVERROOT . '/lib/private/legacy/' . \strtolower(\str_replace('_', '/', \substr($class, 3)) . '.php');
80
		} elseif (\strpos($class, 'OCA\\') === 0) {
81
			list(, $app, $rest) = \explode('\\', $class, 3);
82
			$app = \strtolower($app);
83
			$appPath = \OC_App::getAppPath($app);
84
			if ($appPath && \stream_resolve_include_path($appPath)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $appPath of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
85
				$paths[] = $appPath . '/' . \strtolower(\str_replace('\\', '/', $rest) . '.php');
86
				// If not found in the root of the app directory, insert '/lib' after app id and try again.
87
				$paths[] = $appPath . '/lib/' . \strtolower(\str_replace('\\', '/', $rest) . '.php');
88
			}
89
		} elseif ($class === 'Test\\TestCase') {
90
			// This File is considered public API, so we make sure that the class
91
			// can still be loaded, although the PSR-4 paths have not been loaded.
92
			$paths[] = \OC::$SERVERROOT . '/tests/lib/TestCase.php';
93
		}
94
		return $paths;
95
	}
96
97
	/**
98
	 * @param string $fullPath
99
	 * @return bool
100
	 */
101
	protected function isValidPath($fullPath) {
102
		foreach ($this->validRoots as $root => $true) {
103
			if (\substr($fullPath, 0, \strlen($root) + 1) === $root . '/') {
104
				return true;
105
			}
106
		}
107
		throw new AutoloadNotAllowedException($fullPath);
108
	}
109
110
	/**
111
	 * Load the specified class
112
	 *
113
	 * @param string $class
114
	 * @return bool
115
	 */
116
	public function load($class) {
117
		$pathsToRequire = null;
118
		if ($this->memoryCache) {
119
			$pathsToRequire = $this->memoryCache->get($class);
120
		}
121
122
		if (\class_exists($class, false)) {
123
			return false;
124
		}
125
126
		if (!\is_array($pathsToRequire)) {
127
			// No cache or cache miss
128
			$pathsToRequire = [];
129
			foreach ($this->findClass($class) as $path) {
0 ignored issues
show
Bug introduced by
The expression $this->findClass($class) of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
130
				$fullPath = \stream_resolve_include_path($path);
131
				if ($fullPath && $this->isValidPath($fullPath)) {
132
					$pathsToRequire[] = $fullPath;
133
				}
134
			}
135
136
			if ($this->memoryCache) {
137
				$this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
138
			}
139
		}
140
141
		foreach ($pathsToRequire as $fullPath) {
142
			require_once $fullPath;
143
		}
144
145
		return false;
146
	}
147
148
	/**
149
	 * Sets the optional low-latency cache for class to path mapping.
150
	 *
151
	 * @param \OC\Memcache\Cache $memoryCache Instance of memory cache.
152
	 */
153
	public function setMemoryCache(\OC\Memcache\Cache $memoryCache = null) {
154
		$this->memoryCache = $memoryCache;
155
	}
156
}
157