dependencyanalyzer.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. /** @var array */
  32. private $appInfo;
  33. /**
  34. * @param Platform $platform
  35. * @param \OCP\IL10N $l
  36. */
  37. function __construct(Platform $platform, IL10N $l) {
  38. $this->platform = $platform;
  39. $this->l = $l;
  40. }
  41. /**
  42. * @param array $app
  43. * @returns array of missing dependencies
  44. */
  45. public function analyze(array $app) {
  46. $this->appInfo = $app;
  47. if (isset($app['dependencies'])) {
  48. $dependencies = $app['dependencies'];
  49. } else {
  50. $dependencies = [];
  51. }
  52. return array_merge(
  53. $this->analyzePhpVersion($dependencies),
  54. $this->analyzeDatabases($dependencies),
  55. $this->analyzeCommands($dependencies),
  56. $this->analyzeLibraries($dependencies),
  57. $this->analyzeOS($dependencies),
  58. $this->analyzeOC($dependencies, $app)
  59. );
  60. }
  61. /**
  62. * Truncates both versions to the lowest common version, e.g.
  63. * 5.1.2.3 and 5.1 will be turned into 5.1 and 5.1,
  64. * 5.2.6.5 and 5.1 will be turned into 5.2 and 5.1
  65. * @param string $first
  66. * @param string $second
  67. * @return array first element is the first version, second element is the
  68. * second version
  69. */
  70. private function normalizeVersions($first, $second) {
  71. $first = explode('.', $first);
  72. $second = explode('.', $second);
  73. // get both arrays to the same minimum size
  74. $length = min(count($second), count($first));
  75. $first = array_slice($first, 0, $length);
  76. $second = array_slice($second, 0, $length);
  77. return [implode('.', $first), implode('.', $second)];
  78. }
  79. /**
  80. * Parameters will be normalized and then passed into version_compare
  81. * in the same order they are specified in the method header
  82. * @param string $first
  83. * @param string $second
  84. * @param string $operator
  85. * @return bool result similar to version_compare
  86. */
  87. private function compare($first, $second, $operator) {
  88. // we cant normalize versions if one of the given parameters is not a
  89. // version string but null. In case one parameter is null normalization
  90. // will therefore be skipped
  91. if ($first !== null && $second !== null) {
  92. list($first, $second) = $this->normalizeVersions($first, $second);
  93. }
  94. return version_compare($first, $second, $operator);
  95. }
  96. /**
  97. * Checks if a version is bigger than another version
  98. * @param string $first
  99. * @param string $second
  100. * @return bool true if the first version is bigger than the second
  101. */
  102. private function compareBigger($first, $second) {
  103. return $this->compare($first, $second, '>');
  104. }
  105. /**
  106. * Checks if a version is smaller than another version
  107. * @param string $first
  108. * @param string $second
  109. * @return bool true if the first version is smaller than the second
  110. */
  111. private function compareSmaller($first, $second) {
  112. return $this->compare($first, $second, '<');
  113. }
  114. /**
  115. * @param array $dependencies
  116. * @return array
  117. */
  118. private function analyzePhpVersion(array $dependencies) {
  119. $missing = [];
  120. if (isset($dependencies['php']['@attributes']['min-version'])) {
  121. $minVersion = $dependencies['php']['@attributes']['min-version'];
  122. if ($this->compareSmaller($this->platform->getPhpVersion(), $minVersion)) {
  123. $missing[] = (string)$this->l->t('PHP %s or higher is required.', $minVersion);
  124. }
  125. }
  126. if (isset($dependencies['php']['@attributes']['max-version'])) {
  127. $maxVersion = $dependencies['php']['@attributes']['max-version'];
  128. if ($this->compareBigger($this->platform->getPhpVersion(), $maxVersion)) {
  129. $missing[] = (string)$this->l->t('PHP with a version lower than %s is required.', $maxVersion);
  130. }
  131. }
  132. return $missing;
  133. }
  134. /**
  135. * @param array $dependencies
  136. * @return array
  137. */
  138. private function analyzeDatabases(array $dependencies) {
  139. $missing = [];
  140. if (!isset($dependencies['database'])) {
  141. return $missing;
  142. }
  143. $supportedDatabases = $dependencies['database'];
  144. if (empty($supportedDatabases)) {
  145. return $missing;
  146. }
  147. if (!is_array($supportedDatabases)) {
  148. $supportedDatabases = array($supportedDatabases);
  149. }
  150. $supportedDatabases = array_map(function ($db) {
  151. return $this->getValue($db);
  152. }, $supportedDatabases);
  153. $currentDatabase = $this->platform->getDatabase();
  154. if (!in_array($currentDatabase, $supportedDatabases)) {
  155. $missing[] = (string)$this->l->t('Following databases are supported: %s', join(', ', $supportedDatabases));
  156. }
  157. return $missing;
  158. }
  159. /**
  160. * @param array $dependencies
  161. * @return array
  162. */
  163. private function analyzeCommands(array $dependencies) {
  164. $missing = [];
  165. if (!isset($dependencies['command'])) {
  166. return $missing;
  167. }
  168. $commands = $dependencies['command'];
  169. if (!is_array($commands)) {
  170. $commands = array($commands);
  171. }
  172. $os = $this->platform->getOS();
  173. foreach ($commands as $command) {
  174. if (isset($command['@attributes']['os']) && $command['@attributes']['os'] !== $os) {
  175. continue;
  176. }
  177. $commandName = $this->getValue($command);
  178. if (!$this->platform->isCommandKnown($commandName)) {
  179. $missing[] = (string)$this->l->t('The command line tool %s could not be found', $commandName);
  180. }
  181. }
  182. return $missing;
  183. }
  184. /**
  185. * @param array $dependencies
  186. * @return array
  187. */
  188. private function analyzeLibraries(array $dependencies) {
  189. $missing = [];
  190. if (!isset($dependencies['lib'])) {
  191. return $missing;
  192. }
  193. $libs = $dependencies['lib'];
  194. if (!is_array($libs)) {
  195. $libs = array($libs);
  196. }
  197. foreach ($libs as $lib) {
  198. $libName = $this->getValue($lib);
  199. $libVersion = $this->platform->getLibraryVersion($libName);
  200. if (is_null($libVersion)) {
  201. $missing[] = (string)$this->l->t('The library %s is not available.', $libName);
  202. continue;
  203. }
  204. if (is_array($lib)) {
  205. if (isset($lib['@attributes']['min-version'])) {
  206. $minVersion = $lib['@attributes']['min-version'];
  207. if ($this->compareSmaller($libVersion, $minVersion)) {
  208. $missing[] = (string)$this->l->t('Library %s with a version higher than %s is required - available version %s.',
  209. array($libName, $minVersion, $libVersion));
  210. }
  211. }
  212. if (isset($lib['@attributes']['max-version'])) {
  213. $maxVersion = $lib['@attributes']['max-version'];
  214. if ($this->compareBigger($libVersion, $maxVersion)) {
  215. $missing[] = (string)$this->l->t('Library %s with a version lower than %s is required - available version %s.',
  216. array($libName, $maxVersion, $libVersion));
  217. }
  218. }
  219. }
  220. }
  221. return $missing;
  222. }
  223. /**
  224. * @param array $dependencies
  225. * @return array
  226. */
  227. private function analyzeOS(array $dependencies) {
  228. $missing = [];
  229. if (!isset($dependencies['os'])) {
  230. return $missing;
  231. }
  232. $oss = $dependencies['os'];
  233. if (empty($oss)) {
  234. return $missing;
  235. }
  236. if (is_array($oss)) {
  237. $oss = array_map(function ($os) {
  238. return $this->getValue($os);
  239. }, $oss);
  240. } else {
  241. $oss = array($oss);
  242. }
  243. $currentOS = $this->platform->getOS();
  244. if (!in_array($currentOS, $oss)) {
  245. $missing[] = (string)$this->l->t('Following platforms are supported: %s', join(', ', $oss));
  246. }
  247. return $missing;
  248. }
  249. /**
  250. * @param array $dependencies
  251. * @param array $appInfo
  252. * @return array
  253. */
  254. private function analyzeOC(array $dependencies, array $appInfo) {
  255. $missing = [];
  256. $minVersion = null;
  257. if (isset($dependencies['owncloud']['@attributes']['min-version'])) {
  258. $minVersion = $dependencies['owncloud']['@attributes']['min-version'];
  259. } elseif (isset($appInfo['requiremin'])) {
  260. $minVersion = $appInfo['requiremin'];
  261. } elseif (isset($appInfo['require'])) {
  262. $minVersion = $appInfo['require'];
  263. }
  264. $maxVersion = null;
  265. if (isset($dependencies['owncloud']['@attributes']['max-version'])) {
  266. $maxVersion = $dependencies['owncloud']['@attributes']['max-version'];
  267. } elseif (isset($appInfo['requiremax'])) {
  268. $maxVersion = $appInfo['requiremax'];
  269. }
  270. if (!is_null($minVersion)) {
  271. if ($this->compareSmaller($this->platform->getOcVersion(), $minVersion)) {
  272. $missing[] = (string)$this->l->t('ownCloud %s or higher is required.', $minVersion);
  273. }
  274. }
  275. if (!is_null($maxVersion)) {
  276. if ($this->compareBigger($this->platform->getOcVersion(), $maxVersion)) {
  277. $missing[] = (string)$this->l->t('ownCloud %s or lower is required.', $maxVersion);
  278. }
  279. }
  280. return $missing;
  281. }
  282. /**
  283. * @param $element
  284. * @return mixed
  285. */
  286. private function getValue($element) {
  287. if (isset($element['@value']))
  288. return $element['@value'];
  289. return (string)$element;
  290. }
  291. }