public function callback(Request $request) { if (config('app.env') != 'production' && $request->message) { // phpunit cannot mock static methods so without making a facade // for SNSMessage we have to pass the json data in $request->message $message = new SNSMessage(json_decode($request->message, true)); } else { $message = SNSMessage::fromRawPostData(); $validator = new SNSMessageValidator(); $validator->validate($message); } switch ($message->offsetGet('Type')) { case 'SubscriptionConfirmation': return $this->confirm_subscription($message); case 'Notification': return $this->process_notification($message); } }
/** * Update record from received bounce mail. */ public function receive() { $message = Message::fromRawPostData(); // Get from SNS notifiate. if (!$message) { $this->log('Error: Failed checkNotificateFromSns().'); return false; } // Check from SNS notificate. if (!$this->checkNotificateFromSns($message)) { $this->log('Error: Failed checkNotificateFromSns().'); return false; } // Check SNS TopicArn. (TopicArn is unique in AWS) if ($message['Type'] == self::TYPE_SUBSCRIPTION) { $topic = $message['TopicArn']; $this->isSubscription = $email = true; } elseif ($message['Type'] == self::TYPE_NOTIFICATION) { $topic = $message['TopicArn']; $detail = json_decode($message['Message'], true); $email = $detail['mail']['source']; $this->isSubscription = false; } else { $this->log('Error: Failed approved message type.'); return false; } if (!$this->checkSnsTopic($topic, $email)) { $this->log('Error: Failed checkSnsTopic().'); return false; } if ($this->isSubscription) { // enable end point $this->SubscriptionEndPoint($message); } else { // Update records. if (!$this->updateRecords($detail['bounce']['bouncedRecipients'][0]['emailAddress'])) { $this->log('Error: Failed updateRecords().'); $this->log(ClassRegistry::init($model)->validationErrors); return false; } } }
/** * Update record from received bounce mail. */ public function receive() { $message = Message::fromRawPostData(); $this->config = Configure::read('AntiBounce'); // Get from SNS notifiate. if (!$message) { $this->log('Error: Failed checkNotificateFromSns().'); exit; } // Check from SNS notificate. if (!$this->checkNotificateFromSns($message)) { $this->log('Error: Failed checkNotificateFromSns().'); exit; } // Check SNS TopicArn. (TopicArn is unique in AWS) if ($message['Type'] == self::TYPE_SUBSCRIPTION) { $topic = $message['TopicArn']; $this->isSubscription = $email = true; } elseif ($message['Type'] == self::TYPE_NOTIFICATION) { $topic = $message['TopicArn']; $detail = json_decode($message['Message'], true); $email = $detail['mail']['source']; $this->isSubscription = false; } else { $this->log('Error: Failed approved message type.'); exit; } if (!$this->checkSnsTopic($topic, $email)) { $this->log('Error: Failed checkSnsTopic().'); exit; } if ($this->isSubscription) { // enable end point $this->SubscriptionEndPoint($message); } else { // Insert bounce log $this->execute($detail['bounce']['bouncedRecipients'][0]['emailAddress'], $message['Message']); } }
public function connect(Application $app) { $controllers = $app['controllers_factory']; $controllers->post('/s3', function (Application $app) { // Instantiate the Message and Validator try { $message = Message::fromRawPostData(); } catch (\Exception $e) { throw new HttpException(404, 'POST data is absent, or not a valid JSON document: ' . $e->getMessage()); } $validator = new MessageValidator(); // Validate the message and log errors if invalid. try { $validator->validate($message); } catch (InvalidSnsMessageException $e) { // Pretend we're not here if the message is invalid. throw new HttpException(404, 'SNS Message Validation Error: ' . $e->getMessage()); } // Check the type of the message and handle the subscription. $type = $message['Type']; if (in_array($type, ['SubscriptionConfirmation', 'UnsubscribeConfirmation'])) { // Confirm the (un)subscription by sending a GET request to the SubscribeURL file_get_contents($message['SubscribeURL']); } elseif ('Notification' === $type) { $json = json_decode($message['Message']); $record = end($json->Records); $bucket = $record->s3->bucket->name; $key = $record->s3->object->key; try { if (isset($app['config']['white']['sync'])) { $white = $app['config']['white']['sync']; if ($white['key'] !== $key || $white['bucket'] != $bucket) { throw new \Exception('Key or Bucket is invalid. Check whitelist config.'); } } } catch (\Exception $e) { throw new HttpException(403, 'Failed sync white-list validation. ' . $e->getMessage()); } try { //get the file from s3 $s3Client = $app['aws']->createS3(); $temp = $app['config.dir'] . '../var/data/' . basename($key); $result = $s3Client->getObject(['Bucket' => $bucket, 'Key' => $key, 'IfMatch' => $record->s3->object->eTag, 'SaveAs' => $temp]); if (file_exists($temp)) { //Validate md5(skip for multi-part) $s3Etag = str_replace('"', '', $result['ETag']); if (md5_file($temp) !== $s3Etag || strpos($s3Etag, '-') !== false) { throw new \Exception('S3 exception. md5 validation failed.'); } //unzip the contents // Open our files (in binary mode) $zip = gzopen($temp, 'rb'); $out = fopen(str_replace('.gz', '', $temp), 'wb'); // Keep repeating until the end of the input file while (!gzeof($zip)) { // Read buffer-size bytes // Both fwrite and gzread and binary-safe fwrite($out, gzread($zip, 4096)); } // Files are done, close files fclose($out); gzclose($zip); unlink($temp); $app['import.dbal'](); } else { throw new Exception("Error retrieving file from S3. {$temp}\n does not exist."); } } catch (S3Exception $e) { // Catch an S3 specific exception. throw new \Exception('S3 exception. ' . $e->getMessage()); } catch (AwsException $e) { // This catches the more generic AwsException. throw new \Exception("AWS exception. {$e->getAwsErrorType()}: {$e->getMessage()}"); } } else { throw new HttpException(404, "Unknown message type: {$type}"); } return new Response('OK', 200); }); return $controllers; }
<?php require "vendor/autoload.php"; if ('POST' !== $_SERVER['REQUEST_METHOD']) { http_response_code(405); die; } $AccountSid = "insert your own sid here"; $AuthToken = "insert your own token here"; $client = new Services_Twilio($AccountSid, $AuthToken); try { $message = \Aws\Sns\Message::fromRawPostData(); $validator = new \Aws\Sns\MessageValidator(); $validator->validate($message); if (in_array($message['Type'], ['SubscriptionConfirmation', 'UnsubscribeConfirmation'])) { file_get_contents($message['SubscribeURL']); } else { $client->account->messages->create(array("From" => "telephone", "To" => "telephone", "Body" => $message['Message'])); } } catch (Services_Twilio_RestException $e) { echo $e->getMessage(); }
<?php require __DIR__ . '/vendor/autoload.php'; use Aws\Sns\Message; use Aws\Sns\MessageValidator; use Aws\Sns\Exception\InvalidSnsMessageException; // Make sure the request is POST if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); die; } try { // Create a message from the post data and validate its signature $message = Message::fromRawPostData(); $validator = new MessageValidator(); $validator->validate($message); } catch (InvalidSnsMessageException $e) { // Invalid message http_response_code(400); error_log('SNS Message Validation Error: ' . $e->getMessage); die; } if ($message['Type'] === 'SubscriptionConfirmation') { // Confirm the subscription by sending a GET request to the SubscribeURL file_get_contents($message['SubscribeURL']); } // Data sent from callback $callbackData = json_decode($message['Message']);