You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.4 KiB

11 years ago
12 years ago
12 years ago
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Georg Ehrke <georg@owncloud.com>
  5. * @author Markus Goetz <markus@woboq.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC;
  28. class Autoloader {
  29. private $useGlobalClassPath = true;
  30. private $prefixPaths = array();
  31. private $classPaths = array();
  32. private $validRoots = [];
  33. /**
  34. * Optional low-latency memory cache for class to path mapping.
  35. *
  36. * @var \OC\Memcache\Cache
  37. */
  38. protected $memoryCache;
  39. /**
  40. * Autoloader constructor.
  41. *
  42. * @param string[] $validRoots
  43. */
  44. public function __construct(array $validRoots) {
  45. $this->validRoots = $validRoots;
  46. }
  47. /**
  48. * Add a path to the list of valid php roots for auto loading
  49. *
  50. * @param string $root
  51. */
  52. public function addValidRoot($root) {
  53. $this->validRoots[] = $root;
  54. }
  55. /**
  56. * disable the usage of the global classpath \OC::$CLASSPATH
  57. */
  58. public function disableGlobalClassPath() {
  59. $this->useGlobalClassPath = false;
  60. }
  61. /**
  62. * enable the usage of the global classpath \OC::$CLASSPATH
  63. */
  64. public function enableGlobalClassPath() {
  65. $this->useGlobalClassPath = true;
  66. }
  67. /**
  68. * get the possible paths for a class
  69. *
  70. * @param string $class
  71. * @return array|bool an array of possible paths or false if the class is not part of ownCloud
  72. */
  73. public function findClass($class) {
  74. $class = trim($class, '\\');
  75. $paths = array();
  76. if (array_key_exists($class, $this->classPaths)) {
  77. $paths[] = $this->classPaths[$class];
  78. } else if ($this->useGlobalClassPath and array_key_exists($class, \OC::$CLASSPATH)) {
  79. $paths[] = \OC::$CLASSPATH[$class];
  80. /**
  81. * @TODO: Remove this when necessary
  82. * Remove "apps/" from inclusion path for smooth migration to mutli app dir
  83. */
  84. if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
  85. \OCP\Util::writeLog('core', 'include path for class "' . $class . '" starts with "apps/"', \OCP\Util::DEBUG);
  86. $paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
  87. }
  88. } elseif (strpos($class, 'OC_') === 0) {
  89. // first check for legacy classes if underscores are used
  90. $paths[] = 'private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  91. $paths[] = 'private/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
  92. } elseif (strpos($class, 'OC\\') === 0) {
  93. $paths[] = 'private/' . strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
  94. $paths[] = strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
  95. } elseif (strpos($class, 'OCP\\') === 0) {
  96. $paths[] = 'public/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php');
  97. } elseif (strpos($class, 'OCA\\') === 0) {
  98. list(, $app, $rest) = explode('\\', $class, 3);
  99. $app = strtolower($app);
  100. $appPath = \OC_App::getAppPath($app);
  101. if ($appPath && stream_resolve_include_path($appPath)) {
  102. $paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  103. // If not found in the root of the app directory, insert '/lib' after app id and try again.
  104. $paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
  105. }
  106. } elseif (strpos($class, 'Test_') === 0) {
  107. $paths[] = 'tests/lib/' . strtolower(str_replace('_', '/', substr($class, 5)) . '.php');
  108. } elseif (strpos($class, 'Test\\') === 0) {
  109. $paths[] = 'tests/lib/' . strtolower(str_replace('\\', '/', substr($class, 5)) . '.php');
  110. }
  111. return $paths;
  112. }
  113. protected function isValidPath($fullPath) {
  114. foreach ($this->validRoots as $root) {
  115. if (substr($fullPath, 0, strlen($root) + 1) === $root . '/') {
  116. return true;
  117. }
  118. }
  119. throw new \Exception('Path not allowed: '. $fullPath);
  120. }
  121. /**
  122. * Load the specified class
  123. *
  124. * @param string $class
  125. * @return bool
  126. */
  127. public function load($class) {
  128. $pathsToRequire = null;
  129. if ($this->memoryCache) {
  130. $pathsToRequire = $this->memoryCache->get($class);
  131. }
  132. if (!is_array($pathsToRequire)) {
  133. // No cache or cache miss
  134. $pathsToRequire = array();
  135. foreach ($this->findClass($class) as $path) {
  136. $fullPath = stream_resolve_include_path($path);
  137. if ($fullPath && $this->isValidPath($fullPath)) {
  138. $pathsToRequire[] = $fullPath;
  139. }
  140. }
  141. if ($this->memoryCache) {
  142. $this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
  143. }
  144. }
  145. foreach ($pathsToRequire as $fullPath) {
  146. require_once $fullPath;
  147. }
  148. return false;
  149. }
  150. /**
  151. * Sets the optional low-latency cache for class to path mapping.
  152. *
  153. * @param \OC\Memcache\Cache $memoryCache Instance of memory cache.
  154. */
  155. public function setMemoryCache(\OC\Memcache\Cache $memoryCache = null) {
  156. $this->memoryCache = $memoryCache;
  157. }
  158. }