ApiController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\NullIO;
  13. use Composer\Factory;
  14. use Composer\Repository\VcsRepository;
  15. use Packagist\WebBundle\Package\Updater;
  16. use Packagist\WebBundle\Entity\Package;
  17. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  23. /**
  24. * @author Jordi Boggiano <j.boggiano@seld.be>
  25. */
  26. class ApiController extends Controller
  27. {
  28. /**
  29. * @Template()
  30. * @Route("/packages.json", name="packages", defaults={"_format" = "json"})
  31. */
  32. public function packagesAction(Request $req)
  33. {
  34. $rootJson = $this->container->getParameter('kernel.root_dir').'/../web/packages_root.json';
  35. if (!$req->query->all() && file_exists($rootJson)) {
  36. return new Response(file_get_contents($rootJson));
  37. }
  38. $em = $this->get('doctrine')->getEntityManager();
  39. $filters = array(
  40. 'type' => $req->query->get('type'),
  41. 'tag' => $req->query->get('tag'),
  42. );
  43. gc_enable();
  44. $packages = $em->getRepository('Packagist\WebBundle\Entity\Package')
  45. ->getFullPackages(null, $filters);
  46. $notifyUrl = $this->generateUrl('track_download', array('name' => 'VND/PKG'));
  47. $data = array(
  48. 'notify' => str_replace('VND/PKG', '%package%', $notifyUrl),
  49. 'packages' => array(),
  50. );
  51. foreach ($packages as $package) {
  52. $versions = array();
  53. foreach ($package->getVersions() as $version) {
  54. $versions[$version->getVersion()] = $version->toArray();
  55. $em->detach($version);
  56. }
  57. $data['packages'][$package->getName()] = $versions;
  58. $em->detach($package);
  59. }
  60. unset($versions, $package, $packages);
  61. $response = new Response(json_encode($data), 200);
  62. $response->setSharedMaxAge(120);
  63. return $response;
  64. }
  65. /**
  66. * @Route("/api/github", name="github_postreceive", defaults={"_format" = "json"})
  67. * @Method({"POST"})
  68. */
  69. public function githubPostReceive(Request $request)
  70. {
  71. $payload = json_decode($request->request->get('payload'), true);
  72. if (!$payload || !isset($payload['repository']['url'])) {
  73. return new Response(json_encode(array('status' => 'error', 'message' => 'Missing or invalid payload',)), 406);
  74. }
  75. $username = $request->request->has('username') ?
  76. $request->request->get('username') :
  77. $request->query->get('username');
  78. $apiToken = $request->request->has('apiToken') ?
  79. $request->request->get('apiToken') :
  80. $request->query->get('apiToken');
  81. $doctrine = $this->get('doctrine');
  82. $user = $doctrine
  83. ->getRepository('PackagistWebBundle:User')
  84. ->findOneBy(array('username' => $username, 'apiToken' => $apiToken));
  85. if (!$user) {
  86. return new Response(json_encode(array('status' => 'error', 'message' => 'Invalid credentials',)), 403);
  87. }
  88. if (!preg_match('{(github.com/[\w.-]+/[\w.-]+?)(\.git)?$}', $payload['repository']['url'], $match)) {
  89. return new Response(json_encode(array('status' => 'error', 'message' => 'Could not parse payload repository URL',)), 406);
  90. }
  91. $payloadRepositoryChunk = $match[1];
  92. foreach ($user->getPackages() as $package) {
  93. if (preg_match('{'.preg_quote($payloadRepositoryChunk).'(\.git)?$}', $package->getRepository())) {
  94. // We found the package that was referenced.
  95. $updater = new Updater($doctrine);
  96. $config = Factory::createConfig();
  97. $repository = new VcsRepository(array('url' => $package->getRepository()), new NullIO, $config);
  98. $package->setAutoUpdated(true);
  99. $doctrine->getEntityManager()->flush();
  100. $updater->update($package, $repository);
  101. return new Response('{"status": "success"}', 202);
  102. }
  103. }
  104. return new Response(json_encode(array('status' => 'error', 'message' => 'Could not find a package that matches this request (does user maintain the package?)',)), 404);
  105. }
  106. /**
  107. * @Route("/downloads/{name}", name="track_download", requirements={"name"="[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+"}, defaults={"_format" = "json"})
  108. * @Method({"POST"})
  109. */
  110. public function trackDownloadAction(Request $request, $name)
  111. {
  112. $result = $this->getDoctrine()->getConnection()->fetchAssoc(
  113. 'SELECT p.id, v.id vid
  114. FROM package p
  115. LEFT JOIN package_version v ON p.id = v.package_id
  116. WHERE p.name = ?
  117. AND v.normalizedVersion = ?
  118. LIMIT 1',
  119. array($name, $request->request->get('version_normalized'))
  120. );
  121. if (!$result) {
  122. return new Response('{"status": "error", "message": "Package not found"}', 200);
  123. }
  124. $redis = $this->get('snc_redis.default');
  125. $id = $result['id'];
  126. $version = $result['vid'];
  127. $throttleKey = 'dl:'.$id.':'.$request->getClientIp().':'.date('Ymd');
  128. $requests = $redis->incr($throttleKey);
  129. if (1 === $requests) {
  130. $redis->expire($throttleKey, 86400);
  131. }
  132. if ($requests <= 10) {
  133. $redis->incr('downloads');
  134. $redis->incr('dl:'.$id);
  135. $redis->incr('dl:'.$id.':'.date('Ym'));
  136. $redis->incr('dl:'.$id.':'.date('Ymd'));
  137. $redis->incr('dl:'.$id.'-'.$version);
  138. $redis->incr('dl:'.$id.'-'.$version.':'.date('Ym'));
  139. $redis->incr('dl:'.$id.'-'.$version.':'.date('Ymd'));
  140. }
  141. return new Response('{"status": "success"}', 201);
  142. }
  143. }