ValidatingArrayLoader.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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\Version\VersionParser;
  15. /**
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class ValidatingArrayLoader implements LoaderInterface
  19. {
  20. private $loader;
  21. private $versionParser;
  22. private $ignoreErrors;
  23. private $errors;
  24. private $config;
  25. public function __construct(LoaderInterface $loader, $ignoreErrors = true, VersionParser $parser = null)
  26. {
  27. $this->loader = $loader;
  28. $this->ignoreErrors = $ignoreErrors;
  29. $this->versionParser = $parser ?: new VersionParser();
  30. }
  31. public function load(array $config, $class = 'Composer\Package\CompletePackage')
  32. {
  33. $this->errors = array();
  34. $this->config = $config;
  35. $this->validateRegex('name', '[A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*', true);
  36. if (!empty($this->config['version'])) {
  37. try {
  38. $this->versionParser->normalize($this->config['version']);
  39. } catch (\Exception $e) {
  40. unset($this->config['version']);
  41. $this->errors[] = 'version : invalid value ('.$this->config['version'].'): '.$e->getMessage();
  42. }
  43. }
  44. $this->validateRegex('type', '[a-z0-9-]+');
  45. $this->validateString('target-dir');
  46. $this->validateArray('extra');
  47. $this->validateFlatArray('bin');
  48. $this->validateArray('scripts'); // TODO validate event names & listener syntax
  49. $this->validateString('description');
  50. $this->validateUrl('homepage');
  51. $this->validateFlatArray('keywords', '[A-Za-z0-9 -]+');
  52. if (isset($this->config['license'])) {
  53. if (is_string($this->config['license'])) {
  54. $this->validateRegex('license', '[A-Za-z0-9+. ()-]+');
  55. } else {
  56. $this->validateFlatArray('license', '[A-Za-z0-9+. ()-]+');
  57. }
  58. }
  59. $this->validateString('time');
  60. if (!empty($this->config['time'])) {
  61. try {
  62. $date = new \DateTime($this->config['time']);
  63. } catch (\Exception $e) {
  64. $this->errors[] = 'time : invalid value ('.$this->config['time'].'): '.$e->getMessage();
  65. unset($this->config['time']);
  66. }
  67. }
  68. $this->validateArray('authors');
  69. if (!empty($this->config['authors'])) {
  70. foreach ($this->config['authors'] as $key => $author) {
  71. if (!is_array($author)) {
  72. $this->errors[] = 'authors.'.$key.' : should be an array, '.gettype($author).' given';
  73. unset($this->config['authors'][$key]);
  74. continue;
  75. }
  76. if (isset($author['homepage']) && !$this->filterUrl($author['homepage'])) {
  77. $this->errors[] = 'authors.'.$key.'.homepage : invalid value, must be a valid http/https URL';
  78. unset($this->config['authors'][$key]['homepage']);
  79. }
  80. if (isset($author['email']) && !filter_var($author['email'], FILTER_VALIDATE_EMAIL)) {
  81. $this->errors[] = 'authors.'.$key.'.email : invalid value, must be a valid email address';
  82. unset($this->config['authors'][$key]['email']);
  83. }
  84. if (isset($author['name']) && !is_string($author['name'])) {
  85. $this->errors[] = 'authors.'.$key.'.name : invalid value, must be a string';
  86. unset($this->config['authors'][$key]['name']);
  87. }
  88. if (isset($author['role']) && !is_string($author['role'])) {
  89. $this->errors[] = 'authors.'.$key.'.role : invalid value, must be a string';
  90. unset($this->config['authors'][$key]['role']);
  91. }
  92. if (empty($this->config['authors'][$key])) {
  93. unset($this->config['authors'][$key]);
  94. }
  95. }
  96. if (empty($this->config['authors'])) {
  97. unset($this->config['authors']);
  98. }
  99. }
  100. $this->validateArray('support');
  101. if (!empty($this->config['support'])) {
  102. if (isset($this->config['support']['email']) && !filter_var($this->config['support']['email'], FILTER_VALIDATE_EMAIL)) {
  103. $this->errors[] = 'support.email : invalid value, must be a valid email address';
  104. unset($this->config['support']['email']);
  105. }
  106. if (isset($this->config['support']['irc'])
  107. && (!filter_var($this->config['support']['irc'], FILTER_VALIDATE_URL) || !preg_match('{^irc://}iu', $this->config['support']['irc']))
  108. ) {
  109. $this->errors[] = 'support.irc : invalid value, must be ';
  110. unset($this->config['support']['irc']);
  111. }
  112. foreach (array('issues', 'forum', 'wiki', 'source') as $key) {
  113. if (isset($this->config['support'][$key]) && !$this->filterUrl($this->config['support'][$key])) {
  114. $this->errors[] = 'support.'.$key.' : invalid value, must be a valid http/https URL';
  115. unset($this->config['support'][$key]);
  116. }
  117. }
  118. if (empty($this->config['support'])) {
  119. unset($this->config['support']);
  120. }
  121. }
  122. foreach (array_keys(BasePackage::$supportedLinkTypes) as $linkType) {
  123. if (isset($this->config[$linkType])) {
  124. foreach ($this->config[$linkType] as $package => $constraint) {
  125. if (!is_string($constraint)) {
  126. $this->errors[] = $linkType.'.'.$package.' : invalid value, must be a string containing a version constraint';
  127. unset($this->config[$linkType][$package]);
  128. } elseif ('self.version' !== $constraint) {
  129. try {
  130. $this->versionParser->parseConstraints($constraint);
  131. } catch (\Exception $e) {
  132. $this->errors[] = $linkType.'.'.$package.' : invalid version constraint ('.$e->getMessage().')';
  133. unset($this->config[$linkType][$package]);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. $this->validateArray('suggest');
  140. if (!empty($this->config['suggest'])) {
  141. foreach ($this->config['suggest'] as $package => $description) {
  142. if (!is_string($description)) {
  143. $this->errors[] = 'suggest.'.$package.' : invalid value, must be a string describing why the package is suggested';
  144. unset($this->config['suggest'][$package]);
  145. }
  146. }
  147. }
  148. $this->validateString('minimum-stability');
  149. if (!empty($this->config['minimum-stability'])) {
  150. if (!isset(BasePackage::$stabilities[$this->config['minimum-stability']])) {
  151. $this->errors[] = 'minimum-stability : invalid value, must be one of '.implode(', ', array_keys(BasePackage::$stabilities));
  152. unset($this->config['minimum-stability']);
  153. }
  154. }
  155. // TODO validate autoload
  156. // TODO validate dist
  157. // TODO validate source
  158. // TODO validate repositories
  159. // TODO validate package repositories' packages using this recursively
  160. $this->validateFlatArray('include-path');
  161. // branch alias validation
  162. if (isset($this->config['extra']['branch-alias'])) {
  163. if (!is_array($this->config['extra']['branch-alias'])) {
  164. $this->errors[] = 'extra.branch-alias : must be an array of versions => aliases';
  165. } else {
  166. foreach ($this->config['extra']['branch-alias'] as $sourceBranch => $targetBranch) {
  167. // ensure it is an alias to a -dev package
  168. if ('-dev' !== substr($targetBranch, -4)) {
  169. $this->errors[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') must end in -dev';
  170. unset($this->config['extra']['branch-alias'][$sourceBranch]);
  171. continue;
  172. }
  173. // normalize without -dev and ensure it's a numeric branch that is parseable
  174. $validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4));
  175. if ('-dev' !== substr($validatedTargetBranch, -4)) {
  176. $this->errors[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') must be a parseable number like 2.0-dev';
  177. unset($this->config['extra']['branch-alias'][$sourceBranch]);
  178. }
  179. }
  180. }
  181. }
  182. if ($this->errors && !$this->ignoreErrors) {
  183. throw new InvalidPackageException($this->errors, $config);
  184. }
  185. $package = $this->loader->load($this->config, $class);
  186. $this->errors = array();
  187. $this->config = null;
  188. return $package;
  189. }
  190. private function validateRegex($property, $regex, $mandatory = false)
  191. {
  192. if (!$this->validateString($property, $mandatory)) {
  193. return false;
  194. }
  195. if (!preg_match('{^'.$regex.'$}u', $this->config[$property])) {
  196. $this->errors[] = $property.' : invalid value, must match '.$regex;
  197. unset($this->config[$property]);
  198. return false;
  199. }
  200. return true;
  201. }
  202. private function validateString($property, $mandatory = false)
  203. {
  204. if (isset($this->config[$property]) && !is_string($this->config[$property])) {
  205. $this->errors[] = $property.' : should be a string, '.gettype($this->config[$property]).' given';
  206. unset($this->config[$property]);
  207. return false;
  208. }
  209. if (!isset($this->config[$property]) || trim($this->config[$property]) === '') {
  210. if ($mandatory) {
  211. $this->errors[] = $property.' : must be present';
  212. }
  213. unset($this->config[$property]);
  214. return false;
  215. }
  216. return true;
  217. }
  218. private function validateArray($property, $mandatory = false)
  219. {
  220. if (isset($this->config[$property]) && !is_array($this->config[$property])) {
  221. $this->errors[] = $property.' : should be an array, '.gettype($this->config[$property]).' given';
  222. unset($this->config[$property]);
  223. return false;
  224. }
  225. if (!isset($this->config[$property]) || !count($this->config[$property])) {
  226. if ($mandatory) {
  227. $this->errors[] = $property.' : must be present and contain at least one element';
  228. }
  229. unset($this->config[$property]);
  230. return false;
  231. }
  232. return true;
  233. }
  234. private function validateFlatArray($property, $regex = null, $mandatory = false)
  235. {
  236. if (!$this->validateArray($property, $mandatory)) {
  237. return false;
  238. }
  239. $pass = true;
  240. foreach ($this->config[$property] as $key => $value) {
  241. if (!is_string($value) && !is_numeric($value)) {
  242. $this->errors[] = $property.'.'.$key.' : must be a string or int, '.gettype($value).' given';
  243. unset($this->config[$property][$key]);
  244. $pass = false;
  245. continue;
  246. }
  247. if ($regex && !preg_match('{^'.$regex.'$}u', $value)) {
  248. $this->errors[] = $property.'.'.$key.' : invalid value, must match '.$regex;
  249. unset($this->config[$property][$key]);
  250. $pass = false;
  251. }
  252. }
  253. return $pass;
  254. }
  255. private function validateUrl($property, $mandatory = false)
  256. {
  257. if (!$this->validateString($property, $mandatory)) {
  258. return false;
  259. }
  260. if (!$this->filterUrl($this->config[$property])) {
  261. $this->errors[] = $property.' : invalid value, must be a valid http/https URL';
  262. unset($this->config[$property]);
  263. return false;
  264. }
  265. }
  266. private function filterUrl($value)
  267. {
  268. return filter_var($value, FILTER_VALIDATE_URL) && preg_match('{^https?://}iu', $value);
  269. }
  270. }