dependencyanalyzer.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\App;
  25. use OCP\IL10N;
  26. class DependencyAnalyzer {
  27. /** @var Platform */
  28. private $platform;
  29. /** @var \OCP\IL10N */
  30. private $l;
  31. /**
  32. * @param Platform $platform
  33. * @param \OCP\IL10N $l
  34. */
  35. function __construct(Platform $platform, IL10N $l) {
  36. $this->platform = $platform;
  37. $this->l = $l;
  38. }
  39. /**
  40. * @param array $app
  41. * @returns array of missing dependencies
  42. */
  43. public function analyze($app) {
  44. $this->appInfo = $app;
  45. if (isset($app['dependencies'])) {
  46. $dependencies = $app['dependencies'];
  47. } else {
  48. $dependencies = [];
  49. }
  50. return array_merge(
  51. $this->analyzePhpVersion($dependencies),
  52. $this->analyzeDatabases($dependencies),
  53. $this->analyzeCommands($dependencies),
  54. $this->analyzeLibraries($dependencies),
  55. $this->analyzeOS($dependencies),
  56. $this->analyzeOC($dependencies, $app));
  57. }
  58. /**
  59. * Truncates both verions to the lowest common version, e.g.
  60. * 5.1.2.3 and 5.1 will be turned into 5.1 and 5.1,
  61. * 5.2.6.5 and 5.1 will be turned into 5.2 and 5.1
  62. * @param string $first
  63. * @param string $second
  64. * @return array first element is the first version, second element is the
  65. * second version
  66. */
  67. private function normalizeVersions($first, $second) {
  68. $first = explode('.', $first);
  69. $second = explode('.', $second);
  70. // get both arrays to the same minimum size
  71. $length = min(count($second), count($first));
  72. $first = array_slice($first, 0, $length);
  73. $second = array_slice($second, 0, $length);
  74. return [implode('.', $first), implode('.', $second)];
  75. }
  76. /**
  77. * Parameters will be normalized and then passed into version_compare
  78. * in the same order they are specified in the method header
  79. * @param string $first
  80. * @param string $second
  81. * @param string $operator
  82. * @return bool result similar to version_compare
  83. */
  84. private function compare($first, $second, $operator) {
  85. // we cant normalize versions if one of the given parameters is not a
  86. // version string but null. In case one parameter is null normalization
  87. // will therefore be skipped
  88. if ($first !== null && $second !== null) {
  89. list($first, $second) = $this->normalizeVersions($first, $second);
  90. }
  91. return version_compare($first, $second, $operator);
  92. }
  93. /**
  94. * Checks if a version is bigger than another version
  95. * @param string $first
  96. * @param string $second
  97. * @return bool true if the first version is bigger than the second
  98. */
  99. private function compareBigger($first, $second) {
  100. return $this->compare($first, $second, '>');
  101. }
  102. /**
  103. * Checks if a version is smaller than another version
  104. * @param string $first
  105. * @param string $second
  106. * @return bool true if the first version is smaller than the second
  107. */
  108. private function compareSmaller($first, $second) {
  109. return $this->compare($first, $second, '<');
  110. }
  111. private function analyzePhpVersion($dependencies) {
  112. $missing = [];
  113. if (isset($dependencies['php']['@attributes']['min-version'])) {
  114. $minVersion = $dependencies['php']['@attributes']['min-version'];
  115. if ($this->compareSmaller($this->platform->getPhpVersion(), $minVersion)) {
  116. $missing[] = (string)$this->l->t('PHP %s or higher is required.', $minVersion);
  117. }
  118. }
  119. if (isset($dependencies['php']['@attributes']['max-version'])) {
  120. $maxVersion = $dependencies['php']['@attributes']['max-version'];
  121. if ($this->compareBigger($this->platform->getPhpVersion(), $maxVersion)) {
  122. $missing[] = (string)$this->l->t('PHP with a version lower than %s is required.', $maxVersion);
  123. }
  124. }
  125. return $missing;
  126. }
  127. private function analyzeDatabases($dependencies) {
  128. $missing = [];
  129. if (!isset($dependencies['database'])) {
  130. return $missing;
  131. }
  132. $supportedDatabases = $dependencies['database'];
  133. if (empty($supportedDatabases)) {
  134. return $missing;
  135. }
  136. if (!is_array($supportedDatabases)) {
  137. $supportedDatabases = array($supportedDatabases);
  138. }
  139. $supportedDatabases = array_map(function ($db) {
  140. return $this->getValue($db);
  141. }, $supportedDatabases);
  142. $currentDatabase = $this->platform->getDatabase();
  143. if (!in_array($currentDatabase, $supportedDatabases)) {
  144. $missing[] = (string)$this->l->t('Following databases are supported: %s', join(', ', $supportedDatabases));
  145. }
  146. return $missing;
  147. }
  148. private function analyzeCommands($dependencies) {
  149. $missing = [];
  150. if (!isset($dependencies['command'])) {
  151. return $missing;
  152. }
  153. $commands = $dependencies['command'];
  154. if (!is_array($commands)) {
  155. $commands = array($commands);
  156. }
  157. $os = $this->platform->getOS();
  158. foreach ($commands as $command) {
  159. if (isset($command['@attributes']['os']) && $command['@attributes']['os'] !== $os) {
  160. continue;
  161. }
  162. $commandName = $this->getValue($command);
  163. if (!$this->platform->isCommandKnown($commandName)) {
  164. $missing[] = (string)$this->l->t('The command line tool %s could not be found', $commandName);
  165. }
  166. }
  167. return $missing;
  168. }
  169. private function analyzeLibraries($dependencies) {
  170. $missing = [];
  171. if (!isset($dependencies['lib'])) {
  172. return $missing;
  173. }
  174. $libs = $dependencies['lib'];
  175. if (!is_array($libs)) {
  176. $libs = array($libs);
  177. }
  178. foreach ($libs as $lib) {
  179. $libName = $this->getValue($lib);
  180. $libVersion = $this->platform->getLibraryVersion($libName);
  181. if (is_null($libVersion)) {
  182. $missing[] = (string)$this->l->t('The library %s is not available.', $libName);
  183. continue;
  184. }
  185. if (is_array($lib)) {
  186. if (isset($lib['@attributes']['min-version'])) {
  187. $minVersion = $lib['@attributes']['min-version'];
  188. if ($this->compareSmaller($libVersion, $minVersion)) {
  189. $missing[] = (string)$this->l->t('Library %s with a version higher than %s is required - available version %s.',
  190. array($libName, $minVersion, $libVersion));
  191. }
  192. }
  193. if (isset($lib['@attributes']['max-version'])) {
  194. $maxVersion = $lib['@attributes']['max-version'];
  195. if ($this->compareBigger($libVersion, $maxVersion)) {
  196. $missing[] = (string)$this->l->t('Library %s with a version lower than %s is required - available version %s.',
  197. array($libName, $maxVersion, $libVersion));
  198. }
  199. }
  200. }
  201. }
  202. return $missing;
  203. }
  204. private function analyzeOS($dependencies) {
  205. $missing = [];
  206. if (!isset($dependencies['os'])) {
  207. return $missing;
  208. }
  209. $oss = $dependencies['os'];
  210. if (empty($oss)) {
  211. return $missing;
  212. }
  213. if (is_array($oss)) {
  214. $oss = array_map(function ($os) {
  215. return $this->getValue($os);
  216. }, $oss);
  217. } else {
  218. $oss = array($oss);
  219. }
  220. $currentOS = $this->platform->getOS();
  221. if (!in_array($currentOS, $oss)) {
  222. $missing[] = (string)$this->l->t('Following platforms are supported: %s', join(', ', $oss));
  223. }
  224. return $missing;
  225. }
  226. private function analyzeOC($dependencies, $appInfo) {
  227. $missing = [];
  228. $minVersion = null;
  229. if (isset($dependencies['owncloud']['@attributes']['min-version'])) {
  230. $minVersion = $dependencies['owncloud']['@attributes']['min-version'];
  231. } elseif (isset($appInfo['requiremin'])) {
  232. $minVersion = $appInfo['requiremin'];
  233. } elseif (isset($appInfo['require'])) {
  234. $minVersion = $appInfo['require'];
  235. }
  236. $maxVersion = null;
  237. if (isset($dependencies['owncloud']['@attributes']['max-version'])) {
  238. $maxVersion = $dependencies['owncloud']['@attributes']['max-version'];
  239. } elseif (isset($appInfo['requiremax'])) {
  240. $maxVersion = $appInfo['requiremax'];
  241. }
  242. if (!is_null($minVersion)) {
  243. if ($this->compareSmaller($this->platform->getOcVersion(), $minVersion)) {
  244. $missing[] = (string)$this->l->t('ownCloud %s or higher is required.', $minVersion);
  245. }
  246. }
  247. if (!is_null($maxVersion)) {
  248. if ($this->compareBigger($this->platform->getOcVersion(), $maxVersion)) {
  249. $missing[] = (string)$this->l->t('ownCloud with a version lower than %s is required.', $maxVersion);
  250. }
  251. }
  252. return $missing;
  253. }
  254. /**
  255. * @param $element
  256. * @return mixed
  257. */
  258. private function getValue($element) {
  259. if (isset($element['@value']))
  260. return $element['@value'];
  261. return (string)$element;
  262. }
  263. }