ApiController.php 13 KB

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