ApiController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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\IO\BufferIO;
  13. use Composer\Factory;
  14. use Composer\Repository\VcsRepository;
  15. use Composer\Repository\InvalidRepositoryException;
  16. use Composer\Package\Loader\ValidatingArrayLoader;
  17. use Composer\Package\Loader\ArrayLoader;
  18. use Packagist\WebBundle\Package\Updater;
  19. use Packagist\WebBundle\Entity\Package;
  20. use Packagist\WebBundle\Entity\User;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\HttpFoundation\JsonResponse;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\Console\Output\OutputInterface;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  26. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  27. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  28. /**
  29. * @author Jordi Boggiano <j.boggiano@seld.be>
  30. */
  31. class ApiController extends Controller
  32. {
  33. /**
  34. * @Template()
  35. * @Route("/packages.json", name="packages", defaults={"_format" = "json"})
  36. */
  37. public function packagesAction(Request $req)
  38. {
  39. // fallback if any of the dumped files exist
  40. $rootJson = $this->container->getParameter('kernel.root_dir').'/../web/packages_root.json';
  41. if (file_exists($rootJson)) {
  42. return new Response(file_get_contents($rootJson));
  43. }
  44. $rootJson = $this->container->getParameter('kernel.root_dir').'/../web/packages.json';
  45. if (file_exists($rootJson)) {
  46. return new Response(file_get_contents($rootJson));
  47. }
  48. if ($req->getHost() === 'packagist.org') {
  49. $this->get('logger')->alert('packages.json is missing and the fallback controller is being hit');
  50. return new Response('Horrible misconfiguration or the dumper script messed up', 404);
  51. }
  52. $em = $this->get('doctrine')->getManager();
  53. gc_enable();
  54. $packages = $em->getRepository('Packagist\WebBundle\Entity\Package')
  55. ->getFullPackages();
  56. $notifyUrl = $this->generateUrl('track_download', array('name' => 'VND/PKG'));
  57. $data = array(
  58. 'notify' => str_replace('VND/PKG', '%package%', $notifyUrl),
  59. 'packages' => array(),
  60. );
  61. foreach ($packages as $package) {
  62. $versions = array();
  63. foreach ($package->getVersions() as $version) {
  64. $versions[$version->getVersion()] = $version->toArray();
  65. $em->detach($version);
  66. }
  67. $data['packages'][$package->getName()] = $versions;
  68. $em->detach($package);
  69. }
  70. unset($versions, $package, $packages);
  71. $response = new Response(json_encode($data), 200);
  72. $response->setSharedMaxAge(120);
  73. return $response;
  74. }
  75. /**
  76. * @Route("/api/github", name="github_postreceive", defaults={"_format" = "json"})
  77. * @Method({"POST"})
  78. */
  79. public function githubPostReceive(Request $request)
  80. {
  81. // parse the GitHub payload
  82. $payload = json_decode($request->request->get('payload'), true);
  83. if (!$payload || !isset($payload['repository']['url'])) {
  84. return new Response(json_encode(array('status' => 'error', 'message' => 'Missing or invalid payload',)), 406);
  85. }
  86. $urlRegex = '{^(?:https?://|git://|git@)?(?P<host>github\.com)[:/](?P<path>[\w.-]+/[\w.-]+?)(?:\.git)?$}';
  87. $repoUrl = $payload['repository']['url'];
  88. return $this->receivePost($request, $repoUrl, $urlRegex);
  89. }
  90. /**
  91. * @Route("/api/bitbucket", name="bitbucket_postreceive", defaults={"_format" = "json"})
  92. * @Method({"POST"})
  93. */
  94. public function bitbucketPostReceive(Request $request)
  95. {
  96. // decode Bitbucket's POST payload
  97. $payload = json_decode($request->request->get('payload'), true);
  98. if (!$payload || !isset($payload['canon_url']) || !isset($payload['repository']['absolute_url'])) {
  99. return new Response(json_encode(array('status' => 'error', 'message' => 'Missing or invalid payload',)), 406);
  100. }
  101. $urlRegex = '{^(?:https?://|git://|git@)?(?P<host>bitbucket\.org)[/:](?P<path>[\w.-]+/[\w.-]+?)(\.git)?/?$}';
  102. $repoUrl = $payload['canon_url'].$payload['repository']['absolute_url'];
  103. return $this->receivePost($request, $repoUrl, $urlRegex);
  104. }
  105. /**
  106. * @Route("/downloads/{name}", name="track_download", requirements={"name"="[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+"}, defaults={"_format" = "json"})
  107. * @Method({"POST"})
  108. */
  109. public function trackDownloadAction(Request $request, $name)
  110. {
  111. $result = $this->getPackageAndVersionId($name, $request->request->get('version_normalized'));
  112. if (!$result) {
  113. return new JsonResponse(array('status' => 'error', 'message' => 'Package not found'), 200);
  114. }
  115. $this->trackDownload($result['id'], $result['vid'], $request->getClientIp());
  116. return new JsonResponse(array('status' => 'success'), 201);
  117. }
  118. /**
  119. * Expects a json like:
  120. *
  121. * {
  122. * "downloads": [
  123. * {"name": "foo/bar", "version": "1.0.0.0"},
  124. * // ...
  125. * ]
  126. * }
  127. *
  128. * The version must be the normalized one
  129. *
  130. * @Route("/downloads/", name="track_download_batch", defaults={"_format" = "json"})
  131. * @Method({"POST"})
  132. */
  133. public function trackDownloadsAction(Request $request)
  134. {
  135. $contents = json_decode($request->getContent(), true);
  136. if (empty($contents['downloads']) || !is_array($contents['downloads'])) {
  137. 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);
  138. }
  139. $failed = array();
  140. foreach ($contents['downloads'] as $package) {
  141. $result = $this->getPackageAndVersionId($package['name'], $package['version']);
  142. if (!$result) {
  143. $failed[] = $package;
  144. continue;
  145. }
  146. $this->trackDownload($result['id'], $result['vid'], $request->getClientIp());
  147. }
  148. if ($failed) {
  149. return new JsonResponse(array('status' => 'partial', 'message' => 'Packages '.json_encode($failed).' not found'), 200);
  150. }
  151. return new JsonResponse(array('status' => 'success'), 201);
  152. }
  153. protected function getPackageAndVersionId($name, $version)
  154. {
  155. return $this->get('doctrine.dbal.default_connection')->fetchAssoc(
  156. 'SELECT p.id, v.id vid
  157. FROM package p
  158. LEFT JOIN package_version v ON p.id = v.package_id
  159. WHERE p.name = ?
  160. AND v.normalizedVersion = ?
  161. LIMIT 1',
  162. array($name, $version)
  163. );
  164. }
  165. protected function trackDownload($id, $vid, $ip)
  166. {
  167. $redis = $this->get('snc_redis.default');
  168. $manager = $this->get('packagist.download_manager');
  169. $throttleKey = 'dl:'.$id.':'.$ip.':'.date('Ymd');
  170. $requests = $redis->incr($throttleKey);
  171. if (1 === $requests) {
  172. $redis->expire($throttleKey, 86400);
  173. }
  174. if ($requests <= 10) {
  175. $manager->addDownload($id, $vid);
  176. }
  177. }
  178. /**
  179. * Perform the package update
  180. *
  181. * @param Request $request the current request
  182. * @param string $url the repository's URL (deducted from the request)
  183. * @param string $urlRegex the regex used to split the user packages into domain and path
  184. * @return Response
  185. */
  186. protected function receivePost(Request $request, $url, $urlRegex)
  187. {
  188. // try to parse the URL first to avoid the DB lookup on malformed requests
  189. if (!preg_match($urlRegex, $url)) {
  190. return new Response(json_encode(array('status' => 'error', 'message' => 'Could not parse payload repository URL')), 406);
  191. }
  192. // find the user
  193. $user = $this->findUser($request);
  194. if (!$user) {
  195. return new Response(json_encode(array('status' => 'error', 'message' => 'Invalid credentials')), 403);
  196. }
  197. // try to find the user package
  198. $package = $this->findPackageByUrl($user, $url, $urlRegex);
  199. if (!$package) {
  200. return new Response(json_encode(array('status' => 'error', 'message' => 'Could not find a package that matches this request (does user maintain the package?)')), 404);
  201. }
  202. // don't die if this takes a while
  203. set_time_limit(3600);
  204. // put both updating the database and scanning the repository in a transaction
  205. $em = $this->get('doctrine.orm.entity_manager');
  206. $updater = $this->get('packagist.package_updater');
  207. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE);
  208. try {
  209. $em->transactional(function($em) use ($package, $updater, $io) {
  210. // prepare dependencies
  211. $config = Factory::createConfig();
  212. $io->loadConfiguration($config);
  213. $loader = new ValidatingArrayLoader(new ArrayLoader());
  214. // prepare repository
  215. $repository = new VcsRepository(array('url' => $package->getRepository()), $io, $config);
  216. $repository->setLoader($loader);
  217. // perform the actual update (fetch and re-scan the repository's source)
  218. $updater->update($package, $repository);
  219. // update the package entity
  220. $package->setAutoUpdated(true);
  221. $em->flush();
  222. });
  223. } catch (\Exception $e) {
  224. if ($e instanceof InvalidRepositoryException) {
  225. $this->get('packagist.package_manager')->notifyUpdateFailure($package, $e, $io->getOutput());
  226. }
  227. return new Response(json_encode(array(
  228. 'status' => 'error',
  229. 'message' => '['.get_class($e).'] '.$e->getMessage(),
  230. 'details' => '<pre>'.$io->getOutput().'</pre>'
  231. )), 400);
  232. }
  233. return new JsonResponse(array('status' => 'success'), 202);
  234. }
  235. /**
  236. * Find a user by his username and API token
  237. *
  238. * @param Request $request
  239. * @return User|null the found user or null otherwise
  240. */
  241. protected function findUser(Request $request)
  242. {
  243. $username = $request->request->has('username') ?
  244. $request->request->get('username') :
  245. $request->query->get('username');
  246. $apiToken = $request->request->has('apiToken') ?
  247. $request->request->get('apiToken') :
  248. $request->query->get('apiToken');
  249. $user = $this->get('packagist.user_repository')
  250. ->findOneBy(array('username' => $username, 'apiToken' => $apiToken));
  251. return $user;
  252. }
  253. /**
  254. * Find a user package given by its full URL
  255. *
  256. * @param User $user
  257. * @param string $url
  258. * @param string $urlRegex
  259. * @return Package|null the found package or null otherwise
  260. */
  261. protected function findPackageByUrl(User $user, $url, $urlRegex)
  262. {
  263. if (!preg_match($urlRegex, $url, $matched)) {
  264. return null;
  265. }
  266. foreach ($user->getPackages() as $package) {
  267. if (preg_match($urlRegex, $package->getRepository(), $candidate)
  268. && $candidate['host'] === $matched['host']
  269. && $candidate['path'] === $matched['path']
  270. ) {
  271. return $package;
  272. }
  273. }
  274. return null;
  275. }
  276. }