123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- /*
- * This file is part of Packagist.
- *
- * (c) Jordi Boggiano <j.boggiano@seld.be>
- * Nils Adermann <naderman@naderman.de>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Packagist\WebBundle\Controller;
- use Composer\IO\NullIO;
- use Composer\Factory;
- use Composer\Repository\VcsRepository;
- use Packagist\WebBundle\Package\Updater;
- use Packagist\WebBundle\Entity\Package;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpFoundation\Request;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
- /**
- * @author Jordi Boggiano <j.boggiano@seld.be>
- */
- class ApiController extends Controller
- {
- /**
- * @Template()
- * @Route("/packages.json", name="packages", defaults={"_format" = "json"})
- */
- public function packagesAction(Request $req)
- {
- if (!$req->query->all()) {
- return new Response(file_get_contents($this->container->getParameter('kernel.root_dir').'/../web/packages_root.json'));
- }
- $em = $this->get('doctrine')->getEntityManager();
- $filters = array(
- 'type' => $req->query->get('type'),
- 'tag' => $req->query->get('tag'),
- );
- gc_enable();
- $packages = $em->getRepository('Packagist\WebBundle\Entity\Package')
- ->getFullPackages(null, $filters);
- $notifyUrl = $this->generateUrl('track_download', array('name' => 'VND/PKG'));
- $data = array(
- 'notify' => str_replace('VND/PKG', '%package%', $notifyUrl),
- 'packages' => array(),
- );
- foreach ($packages as $package) {
- $versions = array();
- foreach ($package->getVersions() as $version) {
- $versions[$version->getVersion()] = $version->toArray();
- $em->detach($version);
- }
- $data['packages'][$package->getName()] = $versions;
- $em->detach($package);
- }
- unset($versions, $package, $packages);
- $response = new Response(json_encode($data), 200);
- $response->setSharedMaxAge(120);
- return $response;
- }
- /**
- * @Route("/api/github", name="github_postreceive", defaults={"_format" = "json"})
- * @Method({"POST"})
- */
- public function githubPostReceive(Request $request)
- {
- $payload = json_decode($request->request->get('payload'), true);
- if (!$payload || !isset($payload['repository']['url'])) {
- return new Response(json_encode(array('status' => 'error', 'message' => 'Missing or invalid payload',)), 406);
- }
- $username = $request->request->has('username') ?
- $request->request->get('username') :
- $request->query->get('username');
- $apiToken = $request->request->has('apiToken') ?
- $request->request->get('apiToken') :
- $request->query->get('apiToken');
- $doctrine = $this->get('doctrine');
- $user = $doctrine
- ->getRepository('PackagistWebBundle:User')
- ->findOneBy(array('username' => $username, 'apiToken' => $apiToken));
- if (!$user) {
- return new Response(json_encode(array('status' => 'error', 'message' => 'Invalid credentials',)), 403);
- }
- if (!preg_match('{(github.com/[\w.-]+/[\w.-]+?)(\.git)?$}', $payload['repository']['url'], $match)) {
- return new Response(json_encode(array('status' => 'error', 'message' => 'Could not parse payload repository URL',)), 406);
- }
- $payloadRepositoryChunk = $match[1];
- foreach ($user->getPackages() as $package) {
- if (false !== strpos($package->getRepository(), $payloadRepositoryChunk)) {
- // We found the package that was referenced.
- $updater = new Updater($doctrine);
- $config = Factory::createConfig();
- $repository = new VcsRepository(array('url' => $package->getRepository()), new NullIO, $config);
- $package->setAutoUpdated(true);
- $doctrine->getEntityManager()->flush();
- $updater->update($package, $repository);
- return new Response('{"status": "success"}', 202);
- }
- }
- return new Response(json_encode(array('status' => 'error', 'message' => 'Could not find a package that matches this request (does user maintain the package?)',)), 404);
- }
- /**
- * @Route("/downloads/{name}", name="track_download", requirements={"name"="[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+"}, defaults={"_format" = "json"})
- * @Method({"POST"})
- */
- public function trackDownloadAction(Request $request, $name)
- {
- $result = $this->getDoctrine()->getConnection()->fetchAssoc(
- 'SELECT p.id, v.id vid
- FROM package p
- LEFT JOIN package_version v ON p.id = v.package_id
- WHERE p.name = ?
- AND v.normalizedVersion = ?
- LIMIT 1',
- array($name, $request->request->get('version_normalized'))
- );
- if (!$result) {
- return new Response('{"status": "error", "message": "Package not found"}', 200);
- }
- $redis = $this->get('snc_redis.default');
- $id = $result['id'];
- $version = $result['vid'];
- $throttleKey = 'dl:'.$id.':'.$request->getClientIp().':'.date('Ymd');
- $requests = $redis->incr($throttleKey);
- if (1 === $requests) {
- $redis->expire($throttleKey, 86400);
- }
- if ($requests <= 10) {
- $redis->incr('downloads');
- $redis->incr('dl:'.$id);
- $redis->incr('dl:'.$id.':'.date('Ym'));
- $redis->incr('dl:'.$id.':'.date('Ymd'));
- $redis->incr('dl:'.$id.'-'.$version);
- $redis->incr('dl:'.$id.'-'.$version.':'.date('Ym'));
- $redis->incr('dl:'.$id.'-'.$version.':'.date('Ymd'));
- }
- return new Response('{"status": "success"}', 201);
- }
- }
|