ApiController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  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 Packagist\WebBundle\Controller;
  12. use Composer\Console\HtmlOutputFormatter;
  13. use Composer\Factory;
  14. use Composer\IO\BufferIO;
  15. use Composer\Package\Loader\ArrayLoader;
  16. use Composer\Package\Loader\ValidatingArrayLoader;
  17. use Composer\Repository\InvalidRepositoryException;
  18. use Composer\Repository\VcsRepository;
  19. use Packagist\WebBundle\Entity\Package;
  20. use Packagist\WebBundle\Entity\User;
  21. use Packagist\WebBundle\Entity\Job;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  24. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. use Symfony\Component\HttpFoundation\JsonResponse;
  27. use Symfony\Component\HttpFoundation\NotFoundHttpException;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  31. /**
  32. * @author Jordi Boggiano <j.boggiano@seld.be>
  33. */
  34. class ApiController extends Controller
  35. {
  36. /**
  37. * @Route("/packages.json", name="packages", defaults={"_format" = "json"})
  38. * @Method({"GET"})
  39. */
  40. public function packagesAction()
  41. {
  42. // fallback if any of the dumped files exist
  43. $rootJson = $this->container->getParameter('kernel.root_dir').'/../web/packages_root.json';
  44. if (file_exists($rootJson)) {
  45. return new Response(file_get_contents($rootJson));
  46. }
  47. $rootJson = $this->container->getParameter('kernel.root_dir').'/../web/packages.json';
  48. if (file_exists($rootJson)) {
  49. return new Response(file_get_contents($rootJson));
  50. }
  51. $this->get('logger')->alert('packages.json is missing and the fallback controller is being hit, you need to use app/console packagist:dump');
  52. return new Response('Horrible misconfiguration or the dumper script messed up, you need to use app/console packagist:dump', 404);
  53. }
  54. /**
  55. * @Route("/api/create-package", name="generic_create", defaults={"_format" = "json"})
  56. * @Method({"POST"})
  57. */
  58. public function createPackageAction(Request $request)
  59. {
  60. $payload = json_decode($request->getContent(), true);
  61. if (!$payload) {
  62. return new JsonResponse(array('status' => 'error', 'message' => 'Missing payload parameter'), 406);
  63. }
  64. $url = $payload['repository']['url'];
  65. $package = new Package;
  66. $package->setEntityRepository($this->getDoctrine()->getRepository('PackagistWebBundle:Package'));
  67. $package->setRouter($this->get('router'));
  68. $user = $this->findUser($request);
  69. $package->addMaintainer($user);
  70. $package->setRepository($url);
  71. $errors = $this->get('validator')->validate($package);
  72. if (count($errors) > 0) {
  73. $errorArray = array();
  74. foreach ($errors as $error) {
  75. $errorArray[$error->getPropertyPath()] = $error->getMessage();
  76. }
  77. return new JsonResponse(array('status' => 'error', 'message' => $errorArray), 406);
  78. }
  79. try {
  80. $em = $this->getDoctrine()->getManager();
  81. $em->persist($package);
  82. $em->flush();
  83. } catch (\Exception $e) {
  84. $this->get('logger')->critical($e->getMessage(), array('exception', $e));
  85. return new JsonResponse(array('status' => 'error', 'message' => 'Error saving package'), 500);
  86. }
  87. return new JsonResponse(array('status' => 'success'), 202);
  88. }
  89. /**
  90. * @Route("/api/update-package", name="generic_postreceive", defaults={"_format" = "json"})
  91. * @Route("/api/github", name="github_postreceive", defaults={"_format" = "json"})
  92. * @Route("/api/bitbucket", name="bitbucket_postreceive", defaults={"_format" = "json"})
  93. * @Method({"POST"})
  94. */
  95. public function updatePackageAction(Request $request)
  96. {
  97. // parse the payload
  98. $payload = json_decode($request->request->get('payload'), true);
  99. if (!$payload && $request->headers->get('Content-Type') === 'application/json') {
  100. $payload = json_decode($request->getContent(), true);
  101. }
  102. if (!$payload) {
  103. return new JsonResponse(array('status' => 'error', 'message' => 'Missing payload parameter'), 406);
  104. }
  105. if (isset($payload['project']['git_http_url'])) { // gitlab event payload
  106. $urlRegex = '{^(?:ssh://git@|https?://|git://|git@)?(?P<host>[a-z0-9.-]+)(?::[0-9]+/|[:/])(?P<path>[\w.-]+(?:/[\w.-]+?)+)(?:\.git|/)?$}i';
  107. $url = $payload['project']['git_http_url'];
  108. } elseif (isset($payload['repository']['url'])) { // github/anything hook
  109. $urlRegex = '{^(?:ssh://git@|https?://|git://|git@)?(?P<host>[a-z0-9.-]+)(?::[0-9]+/|[:/])(?P<path>[\w.-]+(?:/[\w.-]+?)+)(?:\.git|/)?$}i';
  110. $url = $payload['repository']['url'];
  111. $url = str_replace('https://api.github.com/repos', 'https://github.com', $url);
  112. } elseif (isset($payload['repository']['links']['html']['href'])) { // bitbucket push event payload
  113. $urlRegex = '{^(?:https?://|git://|git@)?(?:api\.)?(?P<host>bitbucket\.org)[/:](?P<path>[\w.-]+/[\w.-]+?)(\.git)?/?$}i';
  114. $url = $payload['repository']['links']['html']['href'];
  115. } elseif (isset($payload['canon_url']) && isset($payload['repository']['absolute_url'])) { // bitbucket post hook (deprecated)
  116. $urlRegex = '{^(?:https?://|git://|git@)?(?P<host>bitbucket\.org)[/:](?P<path>[\w.-]+/[\w.-]+?)(\.git)?/?$}i';
  117. $url = $payload['canon_url'].$payload['repository']['absolute_url'];
  118. } else {
  119. return new JsonResponse(array('status' => 'error', 'message' => 'Missing or invalid payload'), 406);
  120. }
  121. return $this->receivePost($request, $url, $urlRegex);
  122. }
  123. /**
  124. * @Route(
  125. * "/api/packages/{package}",
  126. * name="api_edit_package",
  127. * requirements={"package"="[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?"},
  128. * defaults={"_format" = "json"}
  129. * )
  130. * @ParamConverter("package", options={"mapping": {"package": "name"}})
  131. * @Method({"PUT"})
  132. */
  133. public function editPackageAction(Request $request, Package $package)
  134. {
  135. $user = $this->findUser($request);
  136. if (!$package->getMaintainers()->contains($user) && !$this->isGranted('ROLE_EDIT_PACKAGES')) {
  137. throw new AccessDeniedException;
  138. }
  139. $payload = json_decode($request->request->get('payload'), true);
  140. if (!$payload && $request->headers->get('Content-Type') === 'application/json') {
  141. $payload = json_decode($request->getContent(), true);
  142. }
  143. $package->setRepository($payload['repository']);
  144. $errors = $this->get('validator')->validate($package, array("Update"));
  145. if (count($errors) > 0) {
  146. $errorArray = array();
  147. foreach ($errors as $error) {
  148. $errorArray[$error->getPropertyPath()] = $error->getMessage();
  149. }
  150. return new JsonResponse(array('status' => 'error', 'message' => $errorArray), 406);
  151. }
  152. $package->setCrawledAt(null);
  153. $em = $this->getDoctrine()->getManager();
  154. $em->persist($package);
  155. $em->flush();
  156. return new JsonResponse(array('status' => 'success'), 200);
  157. }
  158. /**
  159. * @Route("/downloads/{name}", name="track_download", requirements={"name"="[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+"}, defaults={"_format" = "json"})
  160. * @Method({"POST"})
  161. */
  162. public function trackDownloadAction(Request $request, $name)
  163. {
  164. $result = $this->getPackageAndVersionId($name, $request->request->get('version_normalized'));
  165. if (!$result) {
  166. return new JsonResponse(array('status' => 'error', 'message' => 'Package not found'), 200);
  167. }
  168. $this->get('packagist.download_manager')->addDownloads(['id' => $result['id'], 'vid' => $result['vid'], 'ip' => $request->getClientIp()]);
  169. return new JsonResponse(array('status' => 'success'), 201);
  170. }
  171. /**
  172. * @Route("/jobs/{id}", name="get_job", requirements={"id"="[a-f0-9]+"}, defaults={"_format" = "json"})
  173. * @Method({"GET"})
  174. */
  175. public function getJobAction(Request $request, string $id)
  176. {
  177. return new JsonResponse($this->get('scheduler')->getJobStatus($id), 200);
  178. }
  179. /**
  180. * Expects a json like:
  181. *
  182. * {
  183. * "downloads": [
  184. * {"name": "foo/bar", "version": "1.0.0.0"},
  185. * // ...
  186. * ]
  187. * }
  188. *
  189. * The version must be the normalized one
  190. *
  191. * @Route("/downloads/", name="track_download_batch", defaults={"_format" = "json"})
  192. * @Method({"POST"})
  193. */
  194. public function trackDownloadsAction(Request $request)
  195. {
  196. $contents = json_decode($request->getContent(), true);
  197. if (empty($contents['downloads']) || !is_array($contents['downloads'])) {
  198. return new JsonResponse(array('status' => 'error', 'message' => 'Invalid request format, must be a json object containing a downloads key filled with an array of name/version objects'), 200);
  199. }
  200. $failed = array();
  201. $ip = $request->headers->get('X-'.$this->container->getParameter('trusted_ip_header'));
  202. if (!$ip) {
  203. $ip = $request->getClientIp();
  204. }
  205. $jobs = [];
  206. foreach ($contents['downloads'] as $package) {
  207. $result = $this->getPackageAndVersionId($package['name'], $package['version']);
  208. if (!$result) {
  209. $failed[] = $package;
  210. continue;
  211. }
  212. $jobs[] = ['id' => $result['id'], 'vid' => $result['vid'], 'ip' => $ip];
  213. }
  214. $this->get('packagist.download_manager')->addDownloads($jobs);
  215. if ($failed) {
  216. return new JsonResponse(array('status' => 'partial', 'message' => 'Packages '.json_encode($failed).' not found'), 200);
  217. }
  218. return new JsonResponse(array('status' => 'success'), 201);
  219. }
  220. /**
  221. * @param string $name
  222. * @param string $version
  223. * @return array
  224. */
  225. protected function getPackageAndVersionId($name, $version)
  226. {
  227. return $this->get('doctrine.dbal.default_connection')->fetchAssoc(
  228. 'SELECT p.id, v.id vid
  229. FROM package p
  230. LEFT JOIN package_version v ON p.id = v.package_id
  231. WHERE p.name = ?
  232. AND v.normalizedVersion = ?
  233. LIMIT 1',
  234. array($name, $version)
  235. );
  236. }
  237. /**
  238. * Perform the package update
  239. *
  240. * @param Request $request the current request
  241. * @param string $url the repository's URL (deducted from the request)
  242. * @param string $urlRegex the regex used to split the user packages into domain and path
  243. * @return Response
  244. */
  245. protected function receivePost(Request $request, $url, $urlRegex)
  246. {
  247. // try to parse the URL first to avoid the DB lookup on malformed requests
  248. if (!preg_match($urlRegex, $url)) {
  249. return new Response(json_encode(array('status' => 'error', 'message' => 'Could not parse payload repository URL')), 406);
  250. }
  251. // find the user
  252. $user = $this->findUser($request);
  253. if (!$user) {
  254. return new Response(json_encode(array('status' => 'error', 'message' => 'Invalid credentials')), 403);
  255. }
  256. // try to find the user package
  257. $packages = $this->findPackagesByUrl($user, $url, $urlRegex);
  258. if (!$packages) {
  259. return new Response(json_encode(array('status' => 'error', 'message' => 'Could not find a package that matches this request (does user maintain the package?)')), 404);
  260. }
  261. // put both updating the database and scanning the repository in a transaction
  262. $em = $this->get('doctrine.orm.entity_manager');
  263. $jobs = [];
  264. /** @var Package $package */
  265. foreach ($packages as $package) {
  266. $package->setAutoUpdated(true);
  267. $em->flush($package);
  268. $job = $this->get('scheduler')->scheduleUpdate($package, $updateEqualRefs);
  269. $jobs[] = $job->getId();
  270. }
  271. return new JsonResponse(['status' => 'success', 'jobs' => $jobs], 202);
  272. }
  273. /**
  274. * Find a user by his username and API token
  275. *
  276. * @param Request $request
  277. * @return User|null the found user or null otherwise
  278. */
  279. protected function findUser(Request $request)
  280. {
  281. $username = $request->request->has('username') ?
  282. $request->request->get('username') :
  283. $request->query->get('username');
  284. $apiToken = $request->request->has('apiToken') ?
  285. $request->request->get('apiToken') :
  286. $request->query->get('apiToken');
  287. $user = $this->get('packagist.user_repository')
  288. ->findOneBy(array('username' => $username, 'apiToken' => $apiToken));
  289. return $user;
  290. }
  291. /**
  292. * Find a user package given by its full URL
  293. *
  294. * @param User $user
  295. * @param string $url
  296. * @param string $urlRegex
  297. * @return array the packages found
  298. */
  299. protected function findPackagesByUrl(User $user, $url, $urlRegex)
  300. {
  301. if (!preg_match($urlRegex, $url, $matched)) {
  302. return array();
  303. }
  304. $packages = array();
  305. foreach ($user->getPackages() as $package) {
  306. if (preg_match($urlRegex, $package->getRepository(), $candidate)
  307. && strtolower($candidate['host']) === strtolower($matched['host'])
  308. && strtolower($candidate['path']) === strtolower($matched['path'])
  309. ) {
  310. $packages[] = $package;
  311. }
  312. }
  313. return $packages;
  314. }
  315. }