summaryrefslogtreecommitdiff
path: root/src/F2M.php
blob: 510cfa481ac6ba7909bb29d141bf2c05b4a50670 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php

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

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

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

  # Check the mode
  switch ($mode) {
  case 'post':
  case 'reply':
  case 'quote':
    break;  # Mail this post
  case 'edit':  # TODO
  case 'delete': # TODO
    return;
  default:
    throw new Exception('unrecognized mode: ' . $mode);
  }

  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' || $mode == 'quote') {
    $firstId = $data['topic_first_post_id'];
    $firstMessageId = $bridge->getMessageId($firstId);
    if ($firstMessageId === false) {
      throw new Exception('unrecognized post id: ' . $firstId);
    }

# TODO: try to build better References by matching, maybe?
    $inReplyTo = $references = $firstMessageId;
  }
  else if ($mode == 'edit') {
    $inReplyTo = $bridge->getMessageId($postId);
  }

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

  # Assemble the message headers
  $headers = array(
    'To'                       => $to,
    'From'                     => $from,
    'Subject'                  => $subject,
    'Date'                     => $date,
    'Message-Id'               => $messageId,
    'X-BeenThere'              => $forumURL
  );

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

  # Build the message body
  $text = $data['message'];
  strip_bbcode($text, $data['bbcode_uid']);
  $text = htmlspecialchars_decode($text);
  $text = wordwrap($text, 72);

  if ($mode == 'edit') {
    $edit_notice = <<<EOF
[This message has been edited.]

EOF;

    $edit_header = 'Edit: ';

    $text = $edit_notice . $text;
    $headers['Subject'] = $edit_header . $headers['Subject'];
  }

# TODO: BBCode to Markdown (?)

  # Build the bridge footer
  $postURL = "$forumURL/viewtopic.php?p=$postId#p$postId";
  $footer = <<<EOF

_______________________________________________
Read this topic online here:
$postURL
EOF;

  $body = null;

  # Handle attachements, if any
  if (empty($data['attachment_data'])) {
    # No attachments, send a plain email
    $body = $text . "\n" . $footer;
    $headers['Content-Type'] = 'text/plain; charset=UTF-8; format=flowed';
    $headers['Content-Transfer-Encoding'] = '8bit';
  }
  else {
    # Attachments, build a MIME email
    require_once('Mail/mimePart.php');

    $headers['MIME-Version'] = '1.0';

    $params = array('content_type' => 'multipart/mixed');
    $mime = new Mail_mimePart('', $params);

    # Build the main body
    $params = array(
      'content_type' => 'text/plain',
      'charset'      => 'utf-8',
      'encoding'     => '8bit',
      'disposition'  => 'inline'
    );
    $mime->addSubPart($text, $params);

    # Build each attachment
    foreach ($data['attachment_data'] as $a) {
      $attachId = $a['attach_id'];
      $adata = $phpbb->getAttachmentData($attachId);
      if ($adata === false) {
        throw new Exception('unrecognized attachment id: ' . $attachId);
      }

      $afile = $phpbb_root_path . $config['upload_path'] . '/' .
               utf8_basename($adata['physical_filename']); 

      $bytes = file_get_contents($afile);
      if ($bytes === false) {
        throw new Exception('failed to read file: ' . $afile);
      }

      $params = array( 
        'content_type' => $adata['mimetype'],
        'encoding'     => 'base64',
        'disposition'  => 'attachment',
        'dfilename'    => $adata['real_filename'],
        'description'  => $adata['attach_comment']
      );
      $mime->addSubPart($bytes, $params);
    }

    # Build footer
    $params = array(
      'content_type' => 'text/plain',
      'charset'      => 'utf-8',
      'encoding'     => '8bit',
      'disposition'  => 'inline'
    );
    $mime->addSubPart($footer, $params);

    # Encode the message
    $msg = $mime->encode();
    $headers = array_merge($headers, $msg['headers']);
    $body = $msg['body'];
  }

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

# FIXME: edited messages break the bijection between post and message ids!

  # Register the message
  $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;
  }
}

?>