summaryrefslogtreecommitdiff
path: root/scripts/forward_emails.php
blob: 92c7563339ac23e5e54d75ebec5263d2f258fe98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/php
<?php

/* Foursquare Community Site
 * 
 * Copyright (C) 2011 Foursquare Church.
 * 
 * Developers: Jesse Morgan <jmorgan@foursquarestaff.com>
 *
 */

require_once __DIR__ . "/../htdocs/src/base.inc.php";

// Read the email
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);

// Parse the email
$headers = "";
$to = "";
$from = "";
$subject = "";
$message = "";

$splitmsg = split("\n", $email);

$inheaders = true;
foreach ($splitmsg as $line) {
    if ($inheaders) {
        // This is a header
        if ($line == '') {
            $inheaders = false;
        
        } else {
            $header = split(':', $line, 2);
        
            switch (strtolower(trim($header[0]))) {
                case 'from':
                    $from = $header[1];
                    break;

                case 'subject':
                    $subject = $header[1];
                    break;

                case 'x-original-to':
                    $to = $header[1];
                    break;

                case 'delivered-to':
                    break;

                default:
                    $headers .= "$line\n";
            }
        }

    } else {
        // Messsage line
        $message .= "$line\n";
    }
}


// Get the post id and post.
preg_match("/posting-(.+)@.+/", $to, $identifiers);

if (!isset($identifiers[1]) or !is_numeric($identifiers[1])) {
    mailFailure("Invalid id. '$to' " . print_r($identifiers, true));
}

$id = $identifiers[1];

$post = Post::getById($id);

if (!$post or $post->getStage() != 'approved') {
    mailFailure('Invalid post');
}

// Valid Post... forward the message.
$newsubject = "[" . $CONFIG['sitetitle'] . "] $subject";

if (mail($post->getEmail(), $newsubject, $message, $headers)) {
    exit(0);

} else {
    exit(2);
}

function mailFailure($message='') {
    echo "5.1.1 $message\n";
    exit(1);
}

?>