SubscriptionController.php in Feeds 8.3
File
src/Controller/SubscriptionController.php
View source
<?php
namespace Drupal\feeds\Controller;
use Drupal\Component\Utility\Html;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface;
use Drupal\feeds\SubscriptionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class SubscriptionController extends ControllerBase implements ContainerInjectionInterface {
protected $keyValueExpireFactory;
public function __construct(KeyValueExpirableFactoryInterface $key_value_expire_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->keyValueExpireFactory = $key_value_expire_factory;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('keyvalue.expirable'), $container
->get('entity_type.manager'));
}
public function subscribe($feeds_subscription_id, $feeds_push_token, Request $request) {
if ($request->query
->get('hub_challenge') === NULL || $request->query
->get('hub_topic') === NULL) {
throw new NotFoundHttpException();
}
if ($request->query
->get('hub_mode') === 'subscribe') {
return $this
->handleSubscribe((int) $feeds_subscription_id, $feeds_push_token, $request);
}
if ($request->query
->get('hub_mode') === 'unsubscribe') {
return $this
->handleUnsubscribe((int) $feeds_subscription_id, $feeds_push_token, $request);
}
throw new NotFoundHttpException();
}
protected function handleSubscribe($subscription_id, $token, Request $request) {
if (!($subscription = $this
->entityTypeManager()
->getStorage('feeds_subscription')
->load($subscription_id))) {
throw new NotFoundHttpException();
}
if ($subscription
->getToken() !== $token || $subscription
->getTopic() !== $request->query
->get('hub_topic')) {
throw new NotFoundHttpException();
}
if ($subscription
->getState() !== 'subscribing' && $subscription
->getState() !== 'subscribed') {
throw new NotFoundHttpException();
}
if ($lease_time = $request->query
->get('hub_lease_seconds')) {
$subscription
->setLease($lease_time);
}
$subscription
->setState('subscribed');
$subscription
->save();
return new Response(Html::escape($request->query
->get('hub_challenge')), 200);
}
protected function handleUnsubscribe($subscription_id, $token, Request $request) {
$id = $token . ':' . $subscription_id;
$subscription = $this->keyValueExpireFactory
->get('feeds_push_unsubscribe')
->get($id);
if (!$subscription) {
throw new NotFoundHttpException();
}
$this->keyValueExpireFactory
->get('feeds_push_unsubscribe')
->delete($id);
return new Response(Html::escape($request->query
->get('hub_challenge')), 200);
}
public function receive(SubscriptionInterface $feeds_subscription, $feeds_push_token, Request $request) {
if ($feeds_subscription
->getToken() !== $feeds_push_token) {
throw new NotFoundHttpException();
}
parse_str($request->headers
->get('X-Hub-Signature'), $result);
if (empty($result['sha1']) || !$feeds_subscription
->checkSignature($result['sha1'], $request
->getContent())) {
throw new NotFoundHttpException();
}
$feed = $this
->entityTypeManager()
->getStorage('feeds_feed')
->load($feeds_subscription
->id());
try {
$feed
->pushImport($request
->getContent());
} catch (\Exception $e) {
return new Response('', 500);
}
return new Response('', 200);
}
}