Source of file MandrillController.php
Size: 4,784 Bytes - Last Modified: 2021-12-23T10:01:22+00:00
/var/www/docs.ssmods.com/process/src/code/MandrillController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 | <?php namespace LeKoala\Mandrill; use Psr\Log\LoggerInterface; use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Core\Environment; /** * Provide extensions points for handling the webhook * * @author LeKoala <thomas@lekoala.be> */ class MandrillController extends Controller { const EVENT_SEND = 'send'; const EVENT_HARD_BOUNCE = 'hard_bounce'; const EVENT_SOFT_BOUNCE = 'soft_bounce'; const EVENT_OPEN = 'open'; const EVENT_CLICK = 'click'; const EVENT_SPAM = 'spam'; const EVENT_UNSUB = 'unsub'; const EVENT_REJECT = 'reject'; const EVENT_INBOUND = 'inbound'; const EVENT_WHITELIST = 'whitelist'; const EVENT_BLACKLIST = 'blacklist'; protected $eventsCount = 0; protected $skipCount = 0; private static $allowed_actions = [ 'incoming', ]; private static $webhook_auth_enabled = false; /** * Inject public dependencies into the controller * * @var array */ private static $dependencies = [ 'logger' => '%$Psr\Log\LoggerInterface', ]; /** * @var LoggerInterface */ public $logger; /** * Handle incoming webhook * * @link http://help.mandrill.com/entries/21738186-introduction-to-webhooks * @link http://help.mandrill.com/entries/22092308-What-is-the-format-of-inbound-email-webhooks- * @param HTTPRequest $req * @return HTTPResponse */ public function incoming(HTTPRequest $req) { $generatedSignature = $this->generateSignature($req->postVars()); $mandrillSignature = $req->getHeader('X-Mandrill-Signature'); $json = $req->postVar('mandrill_events'); // By default, return a valid response $response = $this->getResponse(); $response->setStatusCode(200); $response->setBody(''); //make sure the generated signature matches the X-Mandrill-Signature header if (self::config()->webhook_auth_enabled && $generatedSignature !== $mandrillSignature) { return $response; } if (!$json) { return $response; } $events = json_decode($json); foreach ($events as $ev) { $this->handleAnyEvent($ev); $event = $ev->event; switch ($event) { // Sync type case self::EVENT_BLACKLIST: case self::EVENT_WHITELIST: $this->handleSyncEvent($ev); break; // Inbound type case self::EVENT_INBOUND: $this->handleInboundEvent($ev); break; // Message type case self::EVENT_CLICK: case self::EVENT_HARD_BOUNCE: case self::EVENT_OPEN: case self::EVENT_REJECT: case self::EVENT_SEND: case self::EVENT_SOFT_BOUNCE: case self::EVENT_SPAM: case self::EVENT_UNSUB: $this->handleMessageEvent($ev); break; } } return $response; } protected function handleAnyEvent($e) { $this->extend('updateHandleAnyEvent', $e); } protected function handleSyncEvent($e) { $this->extend('updateHandleSyncEvent', $e); } protected function handleInboundEvent($e) { $this->extend('updateHandleInboundEvent', $e); } protected function handleMessageEvent($e) { $this->extend('updateHandleMessageEvent', $e); } /** * Get logger * * @return LoggerInterface */ public function getLogger() { return $this->logger; } /** * returns the webhook key * * @return string */ public function getWebHookKey() { $key = Environment::getEnv('MANDRILL_WEBHOOK_KEY'); if (self::config()->webhook_key) { $key = self::config()->webhook_key; } return $key; } /** * generates signature to verify request is from mailchimp. * see https://mailchimp.com/developer/transactional/guides/track-respond-activity-webhooks/#authenticating-webhook-requests * * @param Array $postVars * @return string */ protected function generateSignature(array $postVars) { ksort($postVars); $data = MandrillAdmin::create()->singleton()->WebhookUrl(); $webHookKey = $this->getWebHookKey(); foreach ($postVars as $key => $value) { $data .= $key; $data .= $value; } return base64_encode(hash_hmac('sha1', $data, $webHookKey, true)); } } |