File: message.php3

package info (click to toggle)
imp 3%3A2.2.6-5.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,476 kB
  • ctags: 766
  • sloc: php: 4,253; sh: 831; makefile: 98; perl: 50; pascal: 15
file content (407 lines) | stat: -rw-r--r-- 15,267 bytes parent folder | download
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php

/*
  
  File: message.php3
  $Author: bjn $
  $Revision: 2.23.2.69 $
  $Date: 2001/11/20 03:22:36 $
  
  IMP: Copyright 1998, 1999, 2000 Charles J. Hagenbuch <chuck@horde.org>
  
  You should have received a copy of the GNU Public
  License along with this package; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  Boston, MA 02111-1307, USA.
  
*/

/* these need to be up here so we can use the constants we define in functions. strange but true */
require '../lib/horde.lib';
require './lib/imp.lib';           /* IMPlib is the IMP function library */

function messageRange ($arr, $index, &$offset, &$beginIndex) {
	$count = 200; // maybe make this configurable later
 	$half = $count/2;
	
	if ($index < $half)
		$start = 0;
	else if (($index + $count) >= count($arr))
		$start = max(0, count($arr) - $count);
	else
		$start = $index - $half;
	
	$end = min($start + $count, count($arr));
	$new = array();
	for ($i = $start; $i < $end; $i++) {
		$new[] = $arr[$i];
	}
	$offset = $start;
	$beginIndex = $index - $start - 1;
	return $new;
} // messageRange()

function snip ($address) {
	$address = trim($address);
	if ((substr($address, 0, 1) == '<') && (substr($address, -1) == '>')) {
		$address = substr($address, 1, -1);
	}
	return $address;
}

function buildAddressString ($array, $color) {
	global $default, $sess, $lang;
	
	$string = '';
	while (list(,$ob) = each($array)) {
        if (isset($ob->personal)) $ob->personal = decode_mime_string($ob->personal);
		else $ob->personal = '';
		if (!isset($ob->mailbox)) $ob->mailbox = '';
		if (!isset($ob->host)) $ob->host = '';
		
        $addr = snip(imap_rfc822_write_address($ob->mailbox, $ob->host, $ob->personal));
        $inner = snip(imap_rfc822_write_address($ob->mailbox, $ob->host, ''));
		
		if (!empty($string)) $string .= ', ';
		
		if ($default->minimum_popups) {
			$string .= '<a href="' . $sess->url('compose.php3?actionID=' . COMPOSE . '&to=' . urlencode($addr)) . '" ';
		} else {
			$string .= '<a href="" onclick="open_compose_win(\'actionID=' . COMPOSE . '&to=' . urlencode($addr) . '\'); return false;" ';
		}
		$string .= 'onMouseOver="status=\'' . $lang->compose . '\'; return true;" onMouseOut="status=\'\';">' . htmlSpecialChars($addr) . '</a>';
		
		if ($default->user_use_addressbook && $default->use_db) {
			$string .= '<a onMouseOver="status=\'' . addslashes($lang->add_to_contacts) . '\'; return true;" onMouseOut="status=\'\';" href="" onclick="open_add_contact_win(\'actionID=' . ADD_CONTACT_OBJECT . '&object=' . urlencode(serialize((array)$ob)) . '\'); return false;">' . '<img src="' . $default->graphics_url . '/addressbook-' . $color . '.gif" border=0 align="absmiddle" hspace=2 alt="' . addslashes($lang->add_to_contacts) . '"></a>';
		}
    
	} // end while
    
	return $string;
} // buildAddressString()

function buildMessage ($attachments, $msg) {
	global $imp, $index, $default, $lang, $textparts, $images, $atc;
	$j = 0;
	
	reset($attachments);
	while (list($ref, $mime) = each($attachments)) {
		if (!$mime->header) {
			mimeParse($mime);
			if ($mime->type == TYPETEXT && 
				($mime->SUBTYPE == 'plain' || $mime->SUBTYPE == 'enriched')
				&& $mime->bytes > 0
				// display only limited size parts inline
				&& ($default->text_inline_size == 0 || $mime->size <= $default->text_inline_size)
				&& ($mime->encoding == ENC7BIT || $mime->encoding == ENC8BIT || $mime->encoding == ENCQUOTEDPRINTABLE || $mime->encoding == ENCBASE64)
				&& ($textparts == 0 || $default->text_parts_inline)) {
				
				/* Safe to display, it is text */
				/* get and format the main body part */
				$tmsg = @imap_fetchbody($imp->stream, $index, $ref, FT_UID);
	
				/* If it's a quoted-printable document, translate it into the right charset */
				if ($mime->encoding == ENCQUOTEDPRINTABLE) {
					$qmsg = imap_qprint($tmsg);
				        if (!empty($qmsg)) $tmsg = $qmsg;
				} elseif ($mime->encoding == ENCBASE64) {
					$tmsg = imap_base64($tmsg);
				}
	
				if (strlen($tmsg) > 0) {
					$tmsg = str_replace('</A>', '</a>', $tmsg); // make sure that the original message doesn't contain any capital /A tags, so we can assume we generated them
					$tmsg = str_replace('<A', '<a', $tmsg);     // ditto for open <A tags
					$tmsg = preg_replace('|(\w+)://([^\s"<]*)([\w+#?/&=])|', '<A href="\1://\2\3" target="_blank">\1://\2\3</A>', $tmsg);
					$tmsg = preg_replace('|[Mm][Aa][Ii][Ll][Tt][Oo]:(\s?)([\w+-=%&:_.~@]+[#\w+]*)|', 'mailto:\1<A href="" onclick="open_compose_win(\'actionID=33&to=\2\'); return false;">\2</A>', $tmsg);
					$tmsg = htmlspecialchars($tmsg);
					$tmsg = str_replace('&lt;A href=&quot;', '<a href="', $tmsg);
					$tmsg = str_replace('&quot; target=&quot;_blank&quot;&gt;', '" target="_blank">', $tmsg);
					$tmsg = str_replace('&quot;&gt;','">', $tmsg);
                    $tmsg = str_replace('&quot; onclick=&quot;', '" onclick="', $tmsg);
					$tmsg = str_replace('\');&quot;&gt;', '\');">', $tmsg);
					$tmsg = str_replace('&lt;/A&gt;', '</a>', $tmsg); // only reconvert capital /A tags - the ones we generated
				}
				$charset = $mime->charset;
	
				if (strstr($charset, '1251')) {
					$tmsg = convert_cyr_string($tmsg, 'w', 'k');
				}
				elseif (strstr($charset, '866')) {
					$tmsg = convert_cyr_string($tmsg, 'a', 'k');
				}
				elseif (strstr($charset, '8859-5')) {
					$tmsg = convert_cyr_string($tmsg, 'i', 'k');
				}
				elseif (stristr($charset, 'x-mac-cyrrilic')) {
					$tmsg = convert_cyr_string($tmsg, 'm', 'k');
				}
                
				$tmsg = '<pre>' . wrap_message($tmsg) . '</pre>';
                
				if ($textparts > 0) {
					$msg = $msg . '</td></tr><tr><td></td></tr><tr><td bgcolor="' . $default->text_bg . '">' . $tmsg;
				} else {
					$msg = $tmsg;
				}
				if ($default->inline_in_parts_list) {
					$atc .= mimeSummary($mime);
				}
				$textparts++;
	
				/* It may not be text directly, but if its configured to be displayed inline, inline it */
			} elseif (!empty($mime->conf['view']) && !empty($mime->conf['inline']) &&
					  ($mime->encoding == ENC7BIT || $mime->encoding == ENC8BIT || $mime->encoding == ENCQUOTEDPRINTABLE || $mime->encoding == ENCBASE64)
					  && isset($mime->conf['view_function'])
					  && $mime->disposition == 'inline') {
				if ($textparts > 0) {
					/* If this MIME-type is configured to override other things for display, do it */
					if (isset($mime->conf['override_text']) && $mime->conf['override_text']) {
						$msg = '';
					} else {
						$msg = $msg . '</td></tr><tr><td></td></tr><tr><td bgcolor="' . $default->text_bg . '">';
					}
				}
	
				$func = $mime->conf['view_function'];
				if ($mime->SUBTYPE == 'html')
					$msg .= $func($mime);
				else
					$msg .= '<pre>' . $func($mime) . '</pre>';
				$textparts++;
				
				/* add it as an attachment link to download if this type is downloadable */
				if (!empty($mime->conf['download']) && $default->inline_in_parts_list) {
					$atc .= mimeSummary($mime);
				}
			} else {
				if ($mime->ifid) $images[$mime->id] = $ref;
				$atc .= mimeSummary($mime);
			}
		} // end if()
	} // end while()
	return $msg;
}

require './lib/mimetypes.lib';     /* Mime handling routes */
require './config/defaults.php3';  /* Defaults configuration file */
require './config/html.php3';
require './config/mime.php3';
require './config/lang.php3';
$language = select_lang();
require './lib/postconf.php3';
require "./locale/$language/message.lang";
require './locale/local/message.lang';
require './config/lang.php3';

$this_client = new WebClient;
error_reporting($default->error_level);

page_open(array('sess' => 'HordeSession'));
if (!isset($imp) || !is_object($imp)) {
  page_close();
  header('Location: ' . $sess->url('login.php3?reason=logout'));
  exit;
}
$imp->unpickle();
$imp->authenticate();
$sorted = explode(':', $imp->msgl);
if ($array_index == count($sorted)-1 || $array_index == 0 || (isset($actionID) && ($actionID == DELETE_MESSAGES || $actionID == MOVE_MESSAGES || $actionID == COPY_MESSAGES) && $array_index == count($sorted)-2)) {
  $sorted = imap_sort($imp->stream, $imp->sortby, $imp->sortdir, SE_UID);
  if (!isset($array_index)) {
      $array_index = 0;  // ensure we always have a valid array_index
  } else {
      $array_index = intval($array_index);  // prevent abuse
  }
  $num_msgs = count($sorted);
  $sorted = messageRange($sorted, $array_index + $imp->offset + 1, $offset, $array_index);
  $imp->msgl = implode(':', $sorted);
  $imp->offset = $offset;
  $imp->pickle();
}
$msgindex = $array_index + $imp->offset;

if (isset($index)) {
    $index = intval($index);  // prevent abuse
}
if (isset($actionID)) {
    switch ($actionID) {
    case NO_ACTION:
        break;
    
    case DELETE_MESSAGES:
        if ($index && $imp->mailbox) {
            if (!(@imap_delete($imp->stream, imap_msgno($imp->stream, $index)))) {
                $errormsg = $lang->delete_error;
                $array_index++;
                $msgindex++;
                $index = $sorted[$array_index];
            } else {
                if ($imp->servtype == 'pop3') {
                    // expunge the mailbox to actually delete the message, and then recalculate the message list
                    imap_expunge($imp->stream);
		    imap_close($imp->stream);
		    $imp->authenticate();
		    $sorted = imap_sort($imp->stream, $imp->sortby, $imp->sortdir, SE_UID);
		    $num_msgs = count($sorted);
                    $sorted = messageRange($sorted, $array_index + $imp->offset + 1, $offset, $array_index);
                    $imp->msgl = implode(':', $sorted);
                    $imp->offset = $offset;
                    $imp->pickle();
                    $index = $sorted[$array_index];
                    $msgindex = $array_index + $imp->offset;
                } else {
                    $array_index++;
                    $msgindex++;
                    $index = $sorted[$array_index];
                }
            }
        }
        break;
    
    case MOVE_MESSAGES:
        if (isset($index) && isset($targetMbox) && $index && $targetMbox) {
            $targetMbox = imap_utf7_encode($targetMbox);
            if (!(@imap_mail_copy($imp->stream, $index, $targetMbox, CP_UID|CP_MOVE))) {
                $status_string = $lang->move_error;
                $status_color = 'red';
            } else {
                $plural = false;
                $status_string = $lang->success2($imp->label, imap_utf7_decode($targetMbox), 'moved', 1, $plural);
                $status_color = 'green';
                $array_index++;
                $msgindex++;
                $index = $sorted[$array_index];
            }
        }
        if (!isset($status_string)) {
            $plural = false;
            $status_string = $lang->move_error;
            $status_color = 'red';
        }  
        break;
    
    case COPY_MESSAGES:
        if (isset($index) && isset($targetMbox) && $targetMbox && $index) {
            $targetMbox = imap_utf7_encode($targetMbox);
            if (!@imap_mail_copy($imp->stream, $index, $targetMbox, CP_UID)) {
                $status_string = $lang->copy_error;
                $status_color = 'red';
            } else {
                $plural = false;
                $status_string = $lang->success2($imp->label, imap_utf7_decode($targetMbox), 'copied', 1, $plural);
                $status_color = 'green';
                $array_index++;
                $msgindex++;
                $index = $sorted[$array_index];
            }
        }
        if (!isset($status_string)) {
            $plural = false;
            $status_string = $lang->copy_error;
            $status_color = 'red';
        }
        break;
    }
}
page_close();

if ($default->user_use_folders) {
  $options = mailbox_list(true);
}

/* parse MIME header info */
$num_parts = 1;
if ($structure = @imap_fetchstructure($imp->stream, $index, FT_UID)) {
  if (isset($structure->parts) && is_array($structure->parts))
    $num_parts = count($structure->parts);
} else {
  header('Location: ' . $sess->url('mailbox.php3'));
  exit;
}

require "$default->include_dir/doctype.inc";

$bodypart = 1;
$textparts = 0;
$atc = '';
$msg = '&nbsp;';
$foo = false;
$attachments = MimeParseStructure($structure, '', $foo);
$msg = buildMessage($attachments, $msg);
// handle multipart/related images. this should ideally go somewhere else, but it works here
if (!empty($images)) {
  while (list($img, $id) = each($images)) {
    if (strlen($img) > 0) {
      if ($img[0] == '<') $img = substr($img, 1);
      if ($img[strlen($img)-1] == '>') $img = substr($img, 0, strlen($img)-1);
      $msg = str_replace("cid:$img", $sess->url('view.php3?actionID=' . VIEW_ATTACH) . '&index=' . $index . '&mailbox=' . urlencode($imp->mailbox) . '&bodypart=' . $id, $msg);
    }
  }
}

if ($default->view_message_source) {
  if (!isset($atc)) $atc = '';
  $mime = new mime_part_data;
  $mime->set_type_full('message/source');
  $atc .= '<tr><td>' . $mime->img_tag() . '</td><td></td><td><font size="2" face="' . $default->font->face . '">' . "<a href=\"\" onclick=\"view('" . VIEW_SOURCE . "', 0); return false;\">" . $lang->source . '</a></font></td><td colspan="3">&nbsp;</td></tr>';
}

$h = @imap_header($imp->stream, imap_msgno($imp->stream, $index));

if (isset($h->date))     { $dat = htmlspecialchars(chop($h->date)); }
if (isset($h->to))       { $to  = buildAddressString($h->to, 'red'); }
if (isset($h->from))     { $frm = buildAddressString($h->from, 'blue'); }
if (isset($h->cc))       { $cc  = buildAddressString($h->cc, 'yellow'); }
if (isset($h->bcc))      { $bcc = buildAddressString($h->bcc, 'yellow'); }
if (isset($h->reply_to)) { $rep = buildAddressString($h->reply_to, 'blue'); }
if ((isset($h->subject)) && (strlen($h->subject) != 0)) {
  $sub = htmlspecialchars(decode_mime_string(chop($h->subject)));
} else {
  $sub = $lang->no_subject;
}

$msg_index = $array_index + 1;
$check = imap_check($imp->stream);
if (!isset($num_msgs)) {
    $num_msgs = (is_object($check) && isset($check->Nmsgs)) ? $check->Nmsgs : 0;  /* removed call to imap_num_msgs - cjh */
}

$title = "$imp->label: $sub";
$sidebar = false;
require "$default->include_dir/generic-header.inc";

$prev_index = $array_index - 1;
$next_index = $array_index + 1;

if (isset($sorted[$prev_index])) $prev = $sorted[$prev_index];
else $prev = -1;

if (isset($sorted[$next_index])) $next = $sorted[$next_index];
else $next = -1;

if ($msg_index != count($sorted)) {
   $del_target = 'message.php3';
   $del_ident  = 'index';
} else {
   $del_target = 'mailbox.php3';
   $del_ident  = 'indices';
}

if (!$default->minimum_popups && !$this_client->rather_not_popup) {
  include './lib/js/open_compose_win.js';
}
require './lib/js/open_contacts.js';
require "$default->include_dir/message/navbar_aux_top.inc";
require "$default->include_dir/message/navbar.inc";
require "$default->include_dir/message/headers.inc";
require "$default->include_dir/message/message.inc";
require "$default->include_dir/message/navbar.inc";
require "$default->include_dir/message/navbar_aux_bottom.inc";
require "$default->include_dir/message/javascript.inc";

if (isset($status_string)) {
  status($status_string, $status_color);
}

require "$default->include_dir/generic-footer.inc";
?>