ValidatingArrayLoader.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Package\Loader;
  12. use Composer\Package;
  13. use Composer\Package\BasePackage;
  14. use Composer\Package\LinkConstraint\VersionConstraint;
  15. use Composer\Package\Version\VersionParser;
  16. use Composer\Repository\PlatformRepository;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class ValidatingArrayLoader implements LoaderInterface
  21. {
  22. const CHECK_ALL = 1;
  23. const CHECK_UNBOUND_CONSTRAINTS = 1;
  24. private $loader;
  25. private $versionParser;
  26. private $errors;
  27. private $warnings;
  28. private $config;
  29. private $strictName;
  30. private $flags;
  31. public function __construct(LoaderInterface $loader, $strictName = true, VersionParser $parser = null, $flags = 0)
  32. {
  33. $this->loader = $loader;
  34. $this->versionParser = $parser ?: new VersionParser();
  35. $this->strictName = $strictName;
  36. $this->flags = $flags;
  37. }
  38. public function load(array $config, $class = 'Composer\Package\CompletePackage')
  39. {
  40. $this->errors = array();
  41. $this->warnings = array();
  42. $this->config = $config;
  43. if ($this->strictName) {
  44. $this->validateRegex('name', '[A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*', true);
  45. } else {
  46. $this->validateString('name', true);
  47. }
  48. if (!empty($this->config['version'])) {
  49. try {
  50. $this->versionParser->normalize($this->config['version']);
  51. } catch (\Exception $e) {
  52. unset($this->config['version']);
  53. $this->errors[] = 'version : invalid value ('.$this->config['version'].'): '.$e->getMessage();
  54. }
  55. }
  56. $this->validateRegex('type', '[A-Za-z0-9-]+');
  57. $this->validateString('target-dir');
  58. $this->validateArray('extra');
  59. $this->validateFlatArray('bin');
  60. $this->validateArray('scripts'); // TODO validate event names & listener syntax
  61. $this->validateString('description');
  62. $this->validateUrl('homepage');
  63. $this->validateFlatArray('keywords', '[A-Za-z0-9 ._-]+');
  64. if (isset($this->config['license'])) {
  65. if (is_string($this->config['license'])) {
  66. $this->validateRegex('license', '[A-Za-z0-9+. ()-]+');
  67. } else {
  68. $this->validateFlatArray('license', '[A-Za-z0-9+. ()-]+');
  69. }
  70. }
  71. $this->validateString('time');
  72. if (!empty($this->config['time'])) {
  73. try {
  74. $date = new \DateTime($this->config['time'], new \DateTimeZone('UTC'));
  75. } catch (\Exception $e) {
  76. $this->errors[] = 'time : invalid value ('.$this->config['time'].'): '.$e->getMessage();
  77. unset($this->config['time']);
  78. }
  79. }
  80. if ($this->validateArray('authors') && !empty($this->config['authors'])) {
  81. foreach ($this->config['authors'] as $key => $author) {
  82. if (!is_array($author)) {
  83. $this->errors[] = 'authors.'.$key.' : should be an array, '.gettype($author).' given';
  84. unset($this->config['authors'][$key]);
  85. continue;
  86. }
  87. foreach (array('homepage', 'email', 'name', 'role') as $authorData) {
  88. if (isset($author[$authorData]) && !is_string($author[$authorData])) {
  89. $this->errors[] = 'authors.'.$key.'.'.$authorData.' : invalid value, must be a string';
  90. unset($this->config['authors'][$key][$authorData]);
  91. }
  92. }
  93. if (isset($author['homepage']) && !$this->filterUrl($author['homepage'])) {
  94. $this->warnings[] = 'authors.'.$key.'.homepage : invalid value ('.$author['homepage'].'), must be an http/https URL';
  95. unset($this->config['authors'][$key]['homepage']);
  96. }
  97. if (isset($author['email']) && !filter_var($author['email'], FILTER_VALIDATE_EMAIL)) {
  98. $this->warnings[] = 'authors.'.$key.'.email : invalid value ('.$author['email'].'), must be a valid email address';
  99. unset($this->config['authors'][$key]['email']);
  100. }
  101. if (empty($this->config['authors'][$key])) {
  102. unset($this->config['authors'][$key]);
  103. }
  104. }
  105. if (empty($this->config['authors'])) {
  106. unset($this->config['authors']);
  107. }
  108. }
  109. if ($this->validateArray('support') && !empty($this->config['support'])) {
  110. foreach (array('issues', 'forum', 'wiki', 'source', 'email', 'irc') as $key) {
  111. if (isset($this->config['support'][$key]) && !is_string($this->config['support'][$key])) {
  112. $this->errors[] = 'support.'.$key.' : invalid value, must be a string';
  113. unset($this->config['support'][$key]);
  114. }
  115. }
  116. if (isset($this->config['support']['email']) && !filter_var($this->config['support']['email'], FILTER_VALIDATE_EMAIL)) {
  117. $this->warnings[] = 'support.email : invalid value ('.$this->config['support']['email'].'), must be a valid email address';
  118. unset($this->config['support']['email']);
  119. }
  120. if (isset($this->config['support']['irc']) && !$this->filterUrl($this->config['support']['irc'], array('irc'))) {
  121. $this->warnings[] = 'support.irc : invalid value ('.$this->config['support']['irc'].'), must be a irc://<server>/<channel> URL';
  122. unset($this->config['support']['irc']);
  123. }
  124. foreach (array('issues', 'forum', 'wiki', 'source') as $key) {
  125. if (isset($this->config['support'][$key]) && !$this->filterUrl($this->config['support'][$key])) {
  126. $this->warnings[] = 'support.'.$key.' : invalid value ('.$this->config['support'][$key].'), must be an http/https URL';
  127. unset($this->config['support'][$key]);
  128. }
  129. }
  130. if (empty($this->config['support'])) {
  131. unset($this->config['support']);
  132. }
  133. }
  134. $unboundConstraint = new VersionConstraint('=', $this->versionParser->normalize('dev-master'));
  135. foreach (array_keys(BasePackage::$supportedLinkTypes) as $linkType) {
  136. if ($this->validateArray($linkType) && isset($this->config[$linkType])) {
  137. foreach ($this->config[$linkType] as $package => $constraint) {
  138. if (!preg_match('{^[A-Za-z0-9_./-]+$}', $package)) {
  139. $this->warnings[] = $linkType.'.'.$package.' : invalid key, package names must be strings containing only [A-Za-z0-9_./-]';
  140. }
  141. if (!is_string($constraint)) {
  142. $this->errors[] = $linkType.'.'.$package.' : invalid value, must be a string containing a version constraint';
  143. unset($this->config[$linkType][$package]);
  144. } elseif ('self.version' !== $constraint) {
  145. try {
  146. $linkConstraint = $this->versionParser->parseConstraints($constraint);
  147. } catch (\Exception $e) {
  148. $this->errors[] = $linkType.'.'.$package.' : invalid version constraint ('.$e->getMessage().')';
  149. unset($this->config[$linkType][$package]);
  150. continue;
  151. }
  152. // check requires for unbound constraints on non-platform packages
  153. if (
  154. ($this->flags & self::CHECK_UNBOUND_CONSTRAINTS)
  155. && 'require' === $linkType
  156. && $linkConstraint->matches($unboundConstraint)
  157. && !preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $package)
  158. ) {
  159. $this->warnings[] = $linkType.'.'.$package.' : unbound version constraints ('.$constraint.') should be avoided';
  160. }
  161. }
  162. }
  163. }
  164. }
  165. if ($this->validateArray('suggest') && !empty($this->config['suggest'])) {
  166. foreach ($this->config['suggest'] as $package => $description) {
  167. if (!is_string($description)) {
  168. $this->errors[] = 'suggest.'.$package.' : invalid value, must be a string describing why the package is suggested';
  169. unset($this->config['suggest'][$package]);
  170. }
  171. }
  172. }
  173. if ($this->validateString('minimum-stability') && !empty($this->config['minimum-stability'])) {
  174. if (!isset(BasePackage::$stabilities[$this->config['minimum-stability']])) {
  175. $this->errors[] = 'minimum-stability : invalid value ('.$this->config['minimum-stability'].'), must be one of '.implode(', ', array_keys(BasePackage::$stabilities));
  176. unset($this->config['minimum-stability']);
  177. }
  178. }
  179. if ($this->validateArray('autoload') && !empty($this->config['autoload'])) {
  180. $types = array('psr-0', 'psr-4', 'classmap', 'files');
  181. foreach ($this->config['autoload'] as $type => $typeConfig) {
  182. if (!in_array($type, $types)) {
  183. $this->errors[] = 'autoload : invalid value ('.$type.'), must be one of '.implode(', ', $types);
  184. unset($this->config['autoload'][$type]);
  185. }
  186. if ($type === 'psr-4') {
  187. foreach ($typeConfig as $namespace => $dirs) {
  188. if ($namespace !== '' && '\\' !== substr($namespace, -1)) {
  189. $this->errors[] = 'autoload.psr-4 : invalid value ('.$namespace.'), namespaces must end with a namespace separator, should be '.$namespace.'\\';
  190. }
  191. }
  192. }
  193. }
  194. }
  195. if (!empty($this->config['autoload']['psr-4']) && !empty($this->config['target-dir'])) {
  196. $this->errors[] = 'target-dir : this can not be used together with the autoload.psr-4 setting, remove target-dir to upgrade to psr-4';
  197. // Unset the psr-4 setting, since unsetting target-dir might
  198. // interfere with other settings.
  199. unset($this->config['autoload']['psr-4']);
  200. }
  201. // TODO validate dist
  202. // TODO validate source
  203. // TODO validate repositories
  204. // TODO validate package repositories' packages using this recursively
  205. $this->validateFlatArray('include-path');
  206. $this->validateArray('transport-options');
  207. // branch alias validation
  208. if (isset($this->config['extra']['branch-alias'])) {
  209. if (!is_array($this->config['extra']['branch-alias'])) {
  210. $this->errors[] = 'extra.branch-alias : must be an array of versions => aliases';
  211. } else {
  212. foreach ($this->config['extra']['branch-alias'] as $sourceBranch => $targetBranch) {
  213. // ensure it is an alias to a -dev package
  214. if ('-dev' !== substr($targetBranch, -4)) {
  215. $this->warnings[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') must end in -dev';
  216. unset($this->config['extra']['branch-alias'][$sourceBranch]);
  217. continue;
  218. }
  219. // normalize without -dev and ensure it's a numeric branch that is parseable
  220. $validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4));
  221. if ('-dev' !== substr($validatedTargetBranch, -4)) {
  222. $this->warnings[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') must be a parseable number like 2.0-dev';
  223. unset($this->config['extra']['branch-alias'][$sourceBranch]);
  224. }
  225. }
  226. }
  227. }
  228. if ($this->errors) {
  229. throw new InvalidPackageException($this->errors, $this->warnings, $config);
  230. }
  231. $package = $this->loader->load($this->config, $class);
  232. $this->config = null;
  233. return $package;
  234. }
  235. public function getWarnings()
  236. {
  237. return $this->warnings;
  238. }
  239. public function getErrors()
  240. {
  241. return $this->errors;
  242. }
  243. private function validateRegex($property, $regex, $mandatory = false)
  244. {
  245. if (!$this->validateString($property, $mandatory)) {
  246. return false;
  247. }
  248. if (!preg_match('{^'.$regex.'$}u', $this->config[$property])) {
  249. $message = $property.' : invalid value ('.$this->config[$property].'), must match '.$regex;
  250. if ($mandatory) {
  251. $this->errors[] = $message;
  252. } else {
  253. $this->warnings[] = $message;
  254. }
  255. unset($this->config[$property]);
  256. return false;
  257. }
  258. return true;
  259. }
  260. private function validateString($property, $mandatory = false)
  261. {
  262. if (isset($this->config[$property]) && !is_string($this->config[$property])) {
  263. $this->errors[] = $property.' : should be a string, '.gettype($this->config[$property]).' given';
  264. unset($this->config[$property]);
  265. return false;
  266. }
  267. if (!isset($this->config[$property]) || trim($this->config[$property]) === '') {
  268. if ($mandatory) {
  269. $this->errors[] = $property.' : must be present';
  270. }
  271. unset($this->config[$property]);
  272. return false;
  273. }
  274. return true;
  275. }
  276. private function validateArray($property, $mandatory = false)
  277. {
  278. if (isset($this->config[$property]) && !is_array($this->config[$property])) {
  279. $this->errors[] = $property.' : should be an array, '.gettype($this->config[$property]).' given';
  280. unset($this->config[$property]);
  281. return false;
  282. }
  283. if (!isset($this->config[$property]) || !count($this->config[$property])) {
  284. if ($mandatory) {
  285. $this->errors[] = $property.' : must be present and contain at least one element';
  286. }
  287. unset($this->config[$property]);
  288. return false;
  289. }
  290. return true;
  291. }
  292. private function validateFlatArray($property, $regex = null, $mandatory = false)
  293. {
  294. if (!$this->validateArray($property, $mandatory)) {
  295. return false;
  296. }
  297. $pass = true;
  298. foreach ($this->config[$property] as $key => $value) {
  299. if (!is_string($value) && !is_numeric($value)) {
  300. $this->errors[] = $property.'.'.$key.' : must be a string or int, '.gettype($value).' given';
  301. unset($this->config[$property][$key]);
  302. $pass = false;
  303. continue;
  304. }
  305. if ($regex && !preg_match('{^'.$regex.'$}u', $value)) {
  306. $this->warnings[] = $property.'.'.$key.' : invalid value ('.$value.'), must match '.$regex;
  307. unset($this->config[$property][$key]);
  308. $pass = false;
  309. }
  310. }
  311. return $pass;
  312. }
  313. private function validateUrl($property, $mandatory = false)
  314. {
  315. if (!$this->validateString($property, $mandatory)) {
  316. return false;
  317. }
  318. if (!$this->filterUrl($this->config[$property])) {
  319. $this->warnings[] = $property.' : invalid value ('.$this->config[$property].'), must be an http/https URL';
  320. unset($this->config[$property]);
  321. return false;
  322. }
  323. return true;
  324. }
  325. private function filterUrl($value, array $schemes = array('http', 'https'))
  326. {
  327. if ($value === '') {
  328. return true;
  329. }
  330. $bits = parse_url($value);
  331. if (empty($bits['scheme']) || empty($bits['host'])) {
  332. return false;
  333. }
  334. if (!in_array($bits['scheme'], $schemes, true)) {
  335. return false;
  336. }
  337. return true;
  338. }
  339. }