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.

PlatformRepository.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\App;
  24. /**
  25. * Class PlatformRepository
  26. *
  27. * Inspired by the composer project - licensed under MIT
  28. * https://github.com/composer/composer/blob/master/src/Composer/Repository/PlatformRepository.php#L82
  29. *
  30. * @package OC\App
  31. */
  32. class PlatformRepository {
  33. public function __construct() {
  34. $this->packages = $this->initialize();
  35. }
  36. protected function initialize() {
  37. $loadedExtensions = get_loaded_extensions();
  38. $packages = [];
  39. // Extensions scanning
  40. foreach ($loadedExtensions as $name) {
  41. if (in_array($name, ['standard', 'Core'])) {
  42. continue;
  43. }
  44. $ext = new \ReflectionExtension($name);
  45. try {
  46. $prettyVersion = $ext->getVersion();
  47. $prettyVersion = $this->normalizeVersion($prettyVersion);
  48. } catch (\UnexpectedValueException $e) {
  49. $prettyVersion = '0';
  50. $prettyVersion = $this->normalizeVersion($prettyVersion);
  51. }
  52. $packages[$this->buildPackageName($name)] = $prettyVersion;
  53. }
  54. foreach ($loadedExtensions as $name) {
  55. $prettyVersion = null;
  56. switch ($name) {
  57. case 'curl':
  58. $curlVersion = curl_version();
  59. $prettyVersion = $curlVersion['version'];
  60. break;
  61. case 'intl':
  62. $name = 'ICU';
  63. if (defined('INTL_ICU_VERSION')) {
  64. $prettyVersion = INTL_ICU_VERSION;
  65. } else {
  66. $reflector = new \ReflectionExtension('intl');
  67. ob_start();
  68. $reflector->info();
  69. $output = ob_get_clean();
  70. preg_match('/^ICU version => (.*)$/m', $output, $matches);
  71. $prettyVersion = $matches[1];
  72. }
  73. break;
  74. case 'libxml':
  75. $prettyVersion = LIBXML_DOTTED_VERSION;
  76. break;
  77. case 'openssl':
  78. $prettyVersion = preg_replace_callback('{^(?:OpenSSL\s*)?([0-9.]+)([a-z]?).*}', function ($match) {
  79. return $match[1] . (empty($match[2]) ? '' : '.' . (ord($match[2]) - 96));
  80. }, OPENSSL_VERSION_TEXT);
  81. break;
  82. case 'pcre':
  83. $prettyVersion = preg_replace('{^(\S+).*}', '$1', PCRE_VERSION);
  84. break;
  85. case 'uuid':
  86. $prettyVersion = phpversion('uuid');
  87. break;
  88. case 'xsl':
  89. $prettyVersion = LIBXSLT_DOTTED_VERSION;
  90. break;
  91. default:
  92. // None handled extensions have no special cases, skip
  93. continue 2;
  94. }
  95. try {
  96. $prettyVersion = $this->normalizeVersion($prettyVersion);
  97. } catch (\UnexpectedValueException $e) {
  98. continue;
  99. }
  100. $packages[$this->buildPackageName($name)] = $prettyVersion;
  101. }
  102. return $packages;
  103. }
  104. private function buildPackageName($name) {
  105. return str_replace(' ', '-', $name);
  106. }
  107. /**
  108. * @param $name
  109. * @return string
  110. */
  111. public function findLibrary($name) {
  112. $extName = $this->buildPackageName($name);
  113. if (isset($this->packages[$extName])) {
  114. return $this->packages[$extName];
  115. }
  116. return null;
  117. }
  118. private static $modifierRegex = '[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?';
  119. /**
  120. * Normalizes a version string to be able to perform comparisons on it
  121. *
  122. * https://github.com/composer/composer/blob/master/src/Composer/Package/Version/VersionParser.php#L94
  123. *
  124. * @param string $version
  125. * @param string $fullVersion optional complete version string to give more context
  126. * @throws \UnexpectedValueException
  127. * @return string
  128. */
  129. public function normalizeVersion($version, $fullVersion = null) {
  130. $version = trim($version);
  131. if (null === $fullVersion) {
  132. $fullVersion = $version;
  133. }
  134. // ignore aliases and just assume the alias is required instead of the source
  135. if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $version, $match)) {
  136. $version = $match[1];
  137. }
  138. // match master-like branches
  139. if (preg_match('{^(?:dev-)?(?:master|trunk|default)$}i', $version)) {
  140. return '9999999-dev';
  141. }
  142. if ('dev-' === strtolower(substr($version, 0, 4))) {
  143. return 'dev-' . substr($version, 4);
  144. }
  145. // match classical versioning
  146. if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?' . self::$modifierRegex . '$}i', $version, $matches)) {
  147. $version = $matches[1]
  148. . (!empty($matches[2]) ? $matches[2] : '.0')
  149. . (!empty($matches[3]) ? $matches[3] : '.0')
  150. . (!empty($matches[4]) ? $matches[4] : '.0');
  151. $index = 5;
  152. } elseif (preg_match('{^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)' . self::$modifierRegex . '$}i', $version, $matches)) { // match date-based versioning
  153. $version = preg_replace('{\D}', '-', $matches[1]);
  154. $index = 2;
  155. } elseif (preg_match('{^v?(\d{4,})(\.\d+)?(\.\d+)?(\.\d+)?' . self::$modifierRegex . '$}i', $version, $matches)) {
  156. $version = $matches[1]
  157. . (!empty($matches[2]) ? $matches[2] : '.0')
  158. . (!empty($matches[3]) ? $matches[3] : '.0')
  159. . (!empty($matches[4]) ? $matches[4] : '.0');
  160. $index = 5;
  161. }
  162. // add version modifiers if a version was matched
  163. if (isset($index)) {
  164. if (!empty($matches[$index])) {
  165. if ('stable' === $matches[$index]) {
  166. return $version;
  167. }
  168. $version .= '-' . $this->expandStability($matches[$index]) . (!empty($matches[$index + 1]) ? $matches[$index + 1] : '');
  169. }
  170. if (!empty($matches[$index + 2])) {
  171. $version .= '-dev';
  172. }
  173. return $version;
  174. }
  175. $extraMessage = '';
  176. if (preg_match('{ +as +' . preg_quote($version) . '$}', $fullVersion)) {
  177. $extraMessage = ' in "' . $fullVersion . '", the alias must be an exact version';
  178. } elseif (preg_match('{^' . preg_quote($version) . ' +as +}', $fullVersion)) {
  179. $extraMessage = ' in "' . $fullVersion . '", the alias source must be an exact version, if it is a branch name you should prefix it with dev-';
  180. }
  181. throw new \UnexpectedValueException('Invalid version string "' . $version . '"' . $extraMessage);
  182. }
  183. /**
  184. * @param string $stability
  185. */
  186. private function expandStability($stability) {
  187. $stability = strtolower($stability);
  188. switch ($stability) {
  189. case 'a':
  190. return 'alpha';
  191. case 'b':
  192. return 'beta';
  193. case 'p':
  194. case 'pl':
  195. return 'patch';
  196. case 'rc':
  197. return 'RC';
  198. default:
  199. return $stability;
  200. }
  201. }
  202. }