summaryrefslogtreecommitdiff
path: root/src/F2M.php
blob: fe0856554b4df0778c32c22e292af484f7ff65af (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

try {
  send_to_lists($user, $mode, $data, $post_data);
}
catch (Exception $e) {
  trigger_error($e, E_USER_ERROR);
}

# TODO: wrap long lines
# TODO: handle attachments

function send_to_lists($user, $mode, $data, $post_data) {

/*
  print '<p>';
  var_dump($data);
  var_dump($post_data);
  print '</p>';
*/

  require_once(__DIR__ . '/../HTML/BBCodeParser.php');

  $text = nl2br($data['message']);
  $parser = new HTML_BBCodeParser();
  $parser->setText($text);
  $parser->parse();
  $text = $parser->getParsed();

  print $text;
/*
  $text = nl2br($data['message']);
  $bbcode = new bbcode(base64_encode($data['bbcode_bitfield']));         
  $bbcode->bbcode_second_pass($text, $data['bbcode_uid'], $data['bbcode_bitfield']);

  print $text;
*/

  require_once('Mail.php');

  require_once(__DIR__ . '/Bridge.php');
  require_once(__DIR__ . '/PhpBB3.php');
  require_once(__DIR__ . '/Util.php');

  $postId = $data['post_id'];
  $forumId = $data['forum_id'];
 
  $bridge = new Bridge();

  $to = $bridge->getLists($forumId);
  if (count($to) == 0) {
    # No lists to send to, bail out.
    return;    
  }
  $to = implode(', ', $to);

  $userName = $user->data['username'];
  $userEmail = $user->data['user_email'];

  $from = utf8_quote($userName) . ' <' . $userEmail . '>';
  $sender = 'forum@test.nomic.net';
  $subject = utf8_quote($post_data['post_subject']);

  $phpbb = new PhpBB3();

  $time = $phpbb->getPostTime($postId);
  if ($time === false) {
    throw new Exception('no post time: ' . $postId);
  }

  $date = date(DATE_RFC2822, $time);
  $messageId = build_message_id($time, $postId, $_SERVER['SERVER_NAME']);

  $inReplyTo = null;
  $references = null;
  
  if ($mode == 'reply') {
    $firstId = $data['topic_first_post_id']; 
    $firstMessageId = $bridge->getMessageId($firstId);
    if ($firstMessageId === null) {
      throw new Exception('unrecognized post id: ' . $firstId);
    }

# FIXME: try to build better References by matching, maybe?
    $inReplyTo = $references = $firstMessageId;
  }

  $forumURL = 'http://' . $_SERVER['SERVER_NAME'] .
                  dirname($_SERVER['SCRIPT_NAME']);

  $body = $data['message'];

  # Assemble the message headers
  $headers = array(
    'To'                       => $to,
    'From'                     => $from,
    'Subject'                  => $subject,
    'Date'                     => $date,
    'Message-Id'               => $messageId,
    'X-BeenThere'              => $forumURL,
    'Content-Type'             => 'text/plain; charset=UTF-8; format=flowed',
    'MIME-Version'             => '1.0',
    'Conten-Transfer-Encoding' => '8bit'
  );

  if ($inReplyTo !== null) {
    $headers['In-Reply-To'] = $inReplyTo;
  }
  
  if ($references !== null) {
    $headers['References'] = $references;
  }

  $mailer = Mail::factory('sendmail');

  $seen = !$bridge->registerMessage($postId, $messageId,
                                    $inReplyTo, $references);
  if ($seen) {
    throw new Exception('message id already seen: ' . $messageId);
  }

  try {
    # Send the message
    $err = $mailer->send($to, $headers, $body);
    if (PEAR::isError($err)) {
      throw new Exception('Mail::send error: ' . $err->toString());
    }
  }
  catch (Exception $e) {
    # Bridging failed, unregister message.
    $bridge->unregisterMessage($messageId);
    throw $e;
  }
}

?>