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);
     }
 }
 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;
 }
 /**
  * Check notificate from sns.
  *
  * @param object $message
  * @return boolean
  */
 private function checkNotificateFromSns($message)
 {
     $messageValidator = new MessageValidator();
     return $messageValidator->isValid($message);
 }
Example #4
0
<?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']);