Completed
Push — master ( 38520e...e9cdd7 )
by Patrick
02:15
created

aws.php ➔ getDestinationsForID()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
require '/var/www/common/libs/aws/aws-autoloader.php';
3
use Aws\Sns\MessageValidator\Message;
4
use Aws\Sns\MessageValidator\MessageValidator;
5
use Aws\S3\S3Client;
6
use Guzzle\Http\Client;
7
8
function aws()
9
{
10
    global $app;
11
    $app->post('/snsEndpoint', 'snsEndpoint');
12
    $app->post('/test', 'testX');
13
}
14
15
function sendToBackend($function, $payload)
16
{
17
    $context = new ZMQContext();
18
    $queue = new ZMQSocket($context, ZMQ::SOCKET_REQ);
19
20
    $queue->connect('tcp://127.0.0.1:55555');
21
    $message = base64_encode(serialize($payload));
22
    $queue->send($function.':', ZMQ::MODE_SNDMORE);
23
    $queue->send($message);
24
    return $queue->recv();
25
}
26
27
function testX()
28
{
29
    global $app;
30
    $array = $app->getJSONBody(true);
31
32
    echo sendToBackend('testX', $array);
33
    die();
34
}
35
36
function snsEndpoint()
37
{
38
    global $app;
39
    $array = $app->getJSONBody(true);
40
41
    try
42
    {
43
        $message = Message::fromArray($array);
44
45
        // Validate the message
46
        $validator = new MessageValidator();
47
        $validator->validate($message);
48
    }
49
    catch(\Exception $e)
50
    {
51
        $app->notFound();
52
    }
53
54
    $type = $message->get('Type');
55
    switch($type)
56
    {
57
        case 'SubscriptionConfirmation':
58
            (new Client)->get($message->get('SubscribeURL'))->send();
59
            break;
60
        case 'Notification':
61
            $arn = $message->get('TopicArn');
62
            if($arn !== false)
63
            {
64
                $pos = strpos($arn, 'Listserv');
65
                if($pos !== false)
66
                {
67
                    sendToBackend('processListServMessage', $message->get('Message'));
68
                    echo 'true';
69
                    return;
70
                }
71
            }
72
        default:
73
            file_put_contents('/var/www/profiles/tmp/log.log', print_r($message, true), FILE_APPEND);
74
            break;
75
    }
76
}
77
78
function endswith($string, $test)
79
{
80
    $strlen = strlen($string);
81
    $testlen = strlen($test);
82
    if($testlen > $strlen)
83
    {
84
        return false;
85
    }
86
    return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
87
}
88
89
function getDestinationsForID($id)
90
{
91
    if(strcasecmp($id, 'pboyd') === 0)
92
    {
93
        return array('[email protected]');
94
    }
95
    else
96
    {
97
        return false;
98
    }
99
}
100
101
function getActualDestinations($originals)
102
{
103
    $ret = array();
104
    $count = count($originals);
105
    for($i = 0; $i < $count; $i++)
106
    {
107
        $dest = $originals[$i];
108
        if(endswith($dest, 'burningflipside.com'))
109
        {
110
            $parts = explode('@', $dest);
111
            if(count($parts) === 2)
112
            {
113
                $dests = getDestinationsForID($parts[0]);
114
                if($dests === false)
115
                {
116
                    file_put_contents('/var/www/profiles/tmp/log.log', "getActualDestinations: Invalid destination id $parts[0]\n", FILE_APPEND);
117
                }
118
                else
119
                {
120
                    $ret = array_merge($ret, $dests);
121
                }
122
            }
123
            else
124
            {
125
                file_put_contents('/var/www/profiles/tmp/log.log', "getActualDestinations: Invalid destination format $dest\n", FILE_APPEND);
126
            }
127
        }
128
    }
129
    return $ret;
130
}
131
132
function getRawMessage($action)
133
{
134
    $s3Client = S3Client::factory();
135
    $object = $s3Client->getObject(array('Bucket'=>$action['bucketName'], 'Key'=>$action['objectKey']));
136
    file_put_contents('/var/www/profiles/tmp/log.log', print_r($object, true), FILE_APPEND);
137
    return $object;
138
}
139
140
function processListServMessage($message)
141
{
142
    $message = json_decode($message);
143
    $dests = getActualDestinations($message->mail->destination);
144
    file_put_contents('/var/www/profiles/tmp/log.log', print_r($dests, true), FILE_APPEND);
145
    $rawMessage = getRawMessage($message['action']);
0 ignored issues
show
Unused Code introduced by
$rawMessage is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
146
    //file_put_contents('/var/www/profiles/tmp/log.log', print_r($message, true), FILE_APPEND);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
147
    file_put_contents('/var/www/profiles/tmp/log.log', "processListServMessage: Exited\n", FILE_APPEND);
148
}
149
150
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
151