ValidatingArrayLoader.php 10 KB

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