Completed
Push — master ( 884aac...3cb94c )
by Patrick
05:58
created

run.php ➔ getRawMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
require '/var/www/common/Autoload.php';
3
require '/var/www/common/libs/aws/aws-autoloader.php';
4
use Aws\S3\S3Client;
5
6
function on_new_socket_cb(ZMQSocket $socket, $persistent_id = null)
7
{
8
    if ($persistent_id === 'server')
9
    {
10
        $socket->bind("tcp://*:55555");
11
    } 
12
    else 
13
    {
14
        $socket->connect("tcp://127.0.0.1:55555");
15
    }
16
}
17
18
$context = new ZMQContext();
19
$socket = $context->getSocket(ZMQ::SOCKET_REP, 'server', 'on_new_socket_cb');
20
while(true)
21
{
22
    $function = $socket->recv();
23
    $message = unserialize(base64_decode($socket->recv()));
24
    $socket->send('ACK');
25
    switch($function)
26
    {
27
        case 'testX:':
28
            testX($message);
29
            break;
30
        case 'processListServMessage:':
31
            processListServMessage($message);
32
            break;
33
        default:
34
            echo "$function\n";
35
            print_r($message);
36
    }
37
}
38
39
function testX($message)
40
{
41
    $router = \Email\EmailRouter::getInstance();
42
    $ret = $router->routeSingle($message['dest'], $message['msg']);
43
    echo json_encode($ret);
44
}
45
46 View Code Duplication
function endswith($string, $test)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
{
48
    $strlen = strlen($string);
49
    $testlen = strlen($test);
50
    if ($testlen > $strlen) return false;
51
    return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
52
}
53
54 View Code Duplication
function getDestinationsForID($id)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
{
56
    if(strcasecmp($id,'pboyd') === 0)
57
    {
58
        return array('[email protected]');
59
    }
60
    else
61
    {
62
        return false;
63
    }
64
}
65
66 View Code Duplication
function getActualDestinations($originals)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
{
68
    $ret = array();
69
    $count = count($originals);
70
    for($i = 0; $i < $count; $i++)
71
    {
72
        $dest = $originals[$i];
73
        if(endswith($dest, 'burningflipside.com'))
74
        {
75
            $parts = explode('@', $dest);
76
            if(count($parts) === 2)
77
            {
78
                $dests = getDestinationsForID($parts[0]);
79
                if($dests === false)
80
                {
81
                    file_put_contents('/var/www/profiles/tmp/log.log', "getActualDestinations: Invalid destination id $parts[0]\n", FILE_APPEND);
82
                }
83
                else
84
                {
85
                    $ret = array_merge($ret, $dests);
86
                }
87
            }
88
            else
89
            {
90
                file_put_contents('/var/www/profiles/tmp/log.log', "getActualDestinations: Invalid destination format $dest\n", FILE_APPEND);
91
            }
92
        }
93
    }
94
    return $ret;
95
}
96
97
function getRawMessage($action)
98
{
99
    $credentials = \Aws\Common\Credentials\Credentials::fromIni('default', '/var/www/secure_settings/aws.ini');
100
101
    $s3Client = S3Client::factory([
102
            'version' => 'latest',
103
            'region'  => 'us-west-2',
104
            'credentials' => $credentials
105
         ]);
106
    $object = $s3Client->getObject(array('Bucket'=>$action->bucketName, 'Key'=>$action->objectKey));
107
    return $object['Body']->__toString();
108
}
109
110
function encodeRecipients($recipient)
111
{
112
    if(is_array($recipient))
113
    {
114
        return join(', ', array_map(array($this, 'encodeRecipients'), $recipient));
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
115
    }
116
    if(preg_match("/(.*)<(.*)>/", $recipient, $regs))
117
    {
118
        $recipient = '=?UTF-8?B?'.base64_encode($regs[1]).'?= <'.$regs[2].'>';
119
    }
120
    return $recipient;
121
}
122
123
function fixMessage($message)
124
{
125
    $output = array();
126
    $array = explode("\n",$message);
127
    foreach($array as $arr)
128
    {
129
        if((preg_match('/^Return-Path:/',$arr)))
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
130
        {
131
            //Strip...
132
        }
133
        else if((preg_match('/^From:/',$arr)))
134
        {
135
            $from = trim(substr($arr, 5));
136
            if(preg_match("/(.*)<(.*)>/", $from, $regs))
137
            {
138
                $output[] = 'From: '.encodeRecipients('BurningFlipside Mailer On Behalf Of '.$regs[1].' <[email protected]>');
139
            }
140
            else
141
            {
142
                $output[] = 'From: '.encodeRecipients('BurningFlipside Mailer On Behalf Of '.$from.' <[email protected]>');
143
            }
144
            $output[] = 'Reply-To: '.$from;
145
        }
146
        else
147
        {
148
            $output[] = $arr;
149
        }
150
    }
151
    return implode("\n", $output);
152
}
153
154
function processListServMessage($message)
155
{
156
    $message = json_decode($message);
157
    $dests = getActualDestinations($message->mail->destination);
158
    if($dests === false)
159
    {
160
        return;
161
    }
162
    //print_r($dests);
163
    //print_r($message);
164
    $rawMessage = getRawMessage($message->receipt->action);
165
    //print_r($rawMessage);
166
    $rawMessage = fixMessage($rawMessage);
167
    $credentials = \Aws\Common\Credentials\Credentials::fromIni('default', '/var/www/secure_settings/aws.ini');
0 ignored issues
show
Unused Code introduced by
$credentials 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...
168
    $smtp = \Email\SMTPServer::getInstance();
169
    $ret = $smtp->connect('email-smtp.us-west-2.amazonaws.com');
170
    if(!$ret)
171
    {
172
        echo "Error connecting to SMTP server!\n";
173
        return;
174
    }
175
    $ret = $smtp->authenticate('AKIAIZT5BYBMPJVWJH3A', 'AhUgHVGrfyi0XErOwMGBDwwJzoVlop77py8AUNHYJScJ');
176
    if(!$ret)
177
    {
178
        echo "Error authenticating!\n";
179
        return;
180
    }
181
    foreach($dests as $dest)
182
    {
183
        $tmpRawMessage = "X-Original-To: $dest\nDelivered-To: [email protected]\nReturn-Path: [email protected]\n".$rawMessage;
184
        $ret = $smtp->sendOne('[email protected]', $dest, $tmpRawMessage);
185
        echo "Send = $ret\n";
186
    }
187
    $smtp->disconnect();
188
    echo "processListServMessage: Exited\n";
189
}
190