PlatformRepository.php 6.6 KB

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