ValidatingArrayLoader.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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\Version\VersionParser;
  14. /**
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class ValidatingArrayLoader implements LoaderInterface
  18. {
  19. private $loader;
  20. private $versionParser;
  21. private $ignoreErrors;
  22. private $errors = array();
  23. public function __construct(LoaderInterface $loader, $ignoreErrors = true, VersionParser $parser = null)
  24. {
  25. $this->loader = $loader;
  26. $this->ignoreErrors = $ignoreErrors;
  27. if (!$parser) {
  28. $parser = new VersionParser();
  29. }
  30. $this->versionParser = $parser;
  31. }
  32. public function load(array $config)
  33. {
  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($config['version'])) {
  37. try {
  38. $this->versionParser->normalize($config['version']);
  39. } catch (\Exception $e) {
  40. unset($this->config['version']);
  41. $this->errors[] = 'version : invalid value ('.$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($config['license'])) {
  53. if (is_string($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($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 (isset($author['homepage']) && !$this->filterUrl($author['homepage'])) {
  72. $this->errors[] = 'authors.'.$key.'.homepage : invalid value, must be a valid http/https URL';
  73. unset($this->config['authors'][$key]['homepage']);
  74. }
  75. if (isset($author['email']) && !filter_var($author['email'], FILTER_VALIDATE_EMAIL)) {
  76. $this->errors[] = 'authors.'.$key.'.email : invalid value, must be a valid email address';
  77. unset($this->config['authors'][$key]['email']);
  78. }
  79. if (isset($author['name']) && !is_string($author['name'])) {
  80. $this->errors[] = 'authors.'.$key.'.name : invalid value, must be a string';
  81. unset($this->config['authors'][$key]['name']);
  82. }
  83. if (isset($author['role']) && !is_string($author['role'])) {
  84. $this->errors[] = 'authors.'.$key.'.role : invalid value, must be a string';
  85. unset($this->config['authors'][$key]['role']);
  86. }
  87. }
  88. if (empty($this->config['authors'])) {
  89. unset($this->config['authors']);
  90. }
  91. }
  92. $this->validateArray('support');
  93. if (!empty($this->config['support'])) {
  94. if (isset($this->config['support']['email']) && !filter_var($this->config['support']['email'], FILTER_VALIDATE_EMAIL)) {
  95. $this->errors[] = 'support.email : invalid value, must be a valid email address';
  96. unset($this->config['support']['email']);
  97. }
  98. if (isset($this->config['support']['irc'])
  99. && (!filter_var($this->config['support']['irc'], FILTER_VALIDATE_URL) || !preg_match('{^irc://}iu', $this->config['support']['irc']))
  100. ) {
  101. $this->errors[] = 'support.irc : invalid value, must be ';
  102. unset($this->config['support']['irc']);
  103. }
  104. foreach (array('issues', 'forum', 'wiki', 'source') as $key) {
  105. if (isset($this->config['support'][$key]) && !$this->filterUrl($this->config['support'][$key])) {
  106. $this->errors[] = 'support.'.$key.' : invalid value, must be a valid http/https URL';
  107. unset($this->config['support'][$key]);
  108. }
  109. }
  110. if (empty($this->config['support'])) {
  111. unset($this->config['support']);
  112. }
  113. }
  114. // TODO validate require/require-dev/replace/provide
  115. // TODO validate suggest
  116. // TODO validate autoload
  117. // TODO validate minimum-stability
  118. // TODO validate dist
  119. // TODO validate source
  120. // TODO validate repositories
  121. $this->validateFlatArray('include-path');
  122. // branch alias validation
  123. if (isset($this->config['extra']['branch-alias'])) {
  124. if (!is_array($this->config['extra']['branch-alias'])) {
  125. $this->errors[] = 'extra.branch-alias : must be an array of versions => aliases';
  126. } else {
  127. foreach ($this->config['extra']['branch-alias'] as $sourceBranch => $targetBranch) {
  128. // ensure it is an alias to a -dev package
  129. if ('-dev' !== substr($targetBranch, -4)) {
  130. $this->errors[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') must end in -dev';
  131. unset($this->config['extra']['branch-alias'][$sourceBranch]);
  132. continue;
  133. }
  134. // normalize without -dev and ensure it's a numeric branch that is parseable
  135. $validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4));
  136. if ('-dev' !== substr($validatedTargetBranch, -4)) {
  137. $this->errors[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') must be a parseable number like 2.0-dev';
  138. unset($this->config['extra']['branch-alias'][$sourceBranch]);
  139. }
  140. }
  141. }
  142. }
  143. if ($this->errors && !$this->ignoreErrors) {
  144. throw new \Exception(implode("\n", $this->errors));
  145. }
  146. $package = $this->loader->load($this->config);
  147. $this->errors = array();
  148. unset($this->config);
  149. return $package;
  150. }
  151. private function validateRegex($property, $regex, $mandatory = false)
  152. {
  153. if (!$this->validateString($property, $mandatory)) {
  154. return false;
  155. }
  156. if (!preg_match('{^'.$regex.'$}u', $this->config[$property])) {
  157. $this->errors[] = $property.' : invalid value, must match '.$regex;
  158. unset($this->config[$property]);
  159. return false;
  160. }
  161. return true;
  162. }
  163. private function validateString($property, $mandatory = false)
  164. {
  165. if (isset($this->config[$property]) && !is_string($this->config[$property])) {
  166. $this->errors[] = $property.' : should be a string, '.gettype($this->config[$property]).' given';
  167. unset($this->config[$property]);
  168. return false;
  169. }
  170. if (!isset($this->config[$property]) || trim($this->config[$property]) === '') {
  171. if ($mandatory) {
  172. $this->errors[] = $property.' : must be present';
  173. }
  174. unset($this->config[$property]);
  175. return false;
  176. }
  177. return true;
  178. }
  179. private function validateArray($property, $mandatory = false)
  180. {
  181. if (isset($this->config[$property]) && !is_array($this->config[$property])) {
  182. $this->errors[] = $property.' : should be an array, '.gettype($this->config[$property]).' given';
  183. unset($this->config[$property]);
  184. return false;
  185. }
  186. if (!isset($this->config[$property]) || !count($this->config[$property])) {
  187. if ($mandatory) {
  188. $this->errors[] = $property.' : must be present and contain at least one element';
  189. }
  190. unset($this->config[$property]);
  191. return false;
  192. }
  193. return true;
  194. }
  195. private function validateFlatArray($property, $regex = null, $mandatory = false)
  196. {
  197. if (!$this->validateArray($property, $mandatory)) {
  198. return false;
  199. }
  200. $pass = true;
  201. foreach ($this->config[$property] as $key => $value) {
  202. if (!is_string($value) && !is_numeric($value)) {
  203. $this->errors[] = $property.'.'.$key.' : must be a string or int, '.gettype($value).' given';
  204. unset($this->config[$property][$key]);
  205. $pass = false;
  206. continue;
  207. }
  208. if ($regex && !preg_match('{^'.$regex.'$}u', $value)) {
  209. $this->errors[] = $property.'.'.$key.' : invalid value, must match '.$regex;
  210. unset($this->config[$property][$key]);
  211. $pass = false;
  212. }
  213. }
  214. return $pass;
  215. }
  216. private function validateUrl($property, $mandatory = false)
  217. {
  218. if (!$this->validateString($property, $mandatory)) {
  219. return false;
  220. }
  221. if (!$this->filterUrl($this->config[$property])) {
  222. $this->errors[] = $property.' : invalid value, must be a valid http/https URL';
  223. unset($this->config[$property]);
  224. return false;
  225. }
  226. }
  227. private function filterUrl($value)
  228. {
  229. return filter_var($value, FILTER_VALIDATE_URL) && preg_match('{^https?://}iu', $value);
  230. }
  231. }