3 * @file src/Protocol/Email.php
5 namespace Friendica\Protocol;
7 use Friendica\Core\Hook;
8 use Friendica\Core\Logger;
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Content\Text\HTML;
11 use Friendica\Model\Item;
12 use Friendica\Util\Strings;
20 * @param string $mailbox The mailbox name
21 * @param string $username The username
22 * @param string $password The password
26 public static function connect($mailbox, $username, $password)
28 if (!function_exists('imap_open')) {
32 $mbox = @imap_open($mailbox, $username, $password);
34 $errors = imap_errors();
35 if (!empty($errors)) {
36 Logger::log('IMAP Errors occured: ' . json_encode($errors));
39 $alerts = imap_alerts();
40 if (!empty($alerts)) {
41 Logger::log('IMAP Alerts occured: ' . json_encode($alerts));
48 * @param resource $mbox mailbox
49 * @param string $email_addr email
53 public static function poll($mbox, $email_addr)
55 if (!$mbox || !$email_addr) {
59 $search1 = @imap_search($mbox, 'FROM "' . $email_addr . '"', SE_UID);
63 Logger::log("Found mails from ".$email_addr, Logger::DEBUG);
66 $search2 = @imap_search($mbox, 'TO "' . $email_addr . '"', SE_UID);
70 Logger::log("Found mails to ".$email_addr, Logger::DEBUG);
73 $search3 = @imap_search($mbox, 'CC "' . $email_addr . '"', SE_UID);
77 Logger::log("Found mails cc ".$email_addr, Logger::DEBUG);
80 $res = array_unique(array_merge($search1, $search2, $search3));
86 * @param array $mailacct mail account
89 public static function constructMailboxName($mailacct)
91 $ret = '{' . $mailacct['server'] . ((intval($mailacct['port'])) ? ':' . $mailacct['port'] : '');
92 $ret .= (($mailacct['ssltype']) ? '/' . $mailacct['ssltype'] . '/novalidate-cert' : '');
93 $ret .= '}' . $mailacct['mailbox'];
98 * @param resource $mbox mailbox
99 * @param integer $uid user id
102 public static function messageMeta($mbox, $uid)
104 $ret = (($mbox && $uid) ? @imap_fetch_overview($mbox, $uid, FT_UID) : [[]]); // POSSIBLE CLEANUP --> array(array()) is probably redundant now
105 return (count($ret)) ? $ret : [];
109 * @param resource $mbox mailbox
110 * @param integer $uid user id
111 * @param string $reply reply
113 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
115 public static function getMessage($mbox, $uid, $reply, $item)
119 $struc = (($mbox && $uid) ? @imap_fetchstructure($mbox, $uid, FT_UID) : null);
125 if (empty($struc->parts)) {
126 $ret['body'] = self::messageGetPart($mbox, $uid, $struc, 0, 'html');
127 $html = $ret['body'];
129 if (trim($ret['body']) == '') {
130 $ret['body'] = self::messageGetPart($mbox, $uid, $struc, 0, 'plain');
132 $message = ['text' => $ret['body'], 'html' => ''];
133 Hook::callAll('email_getmessage', $message, $ret);
134 $ret['body'] = $message['text'];
136 $message = ['text' => '', 'html' => $ret['body']];
137 Hook::callAll('email_getmessage', $message, $ret);
138 $ret['body'] = $message['html'];
140 $ret['body'] = HTML::toBBCode($ret['body']);
145 foreach ($struc->parts as $ptop => $p) {
146 $x = self::messageGetPart($mbox, $uid, $p, $ptop + 1, 'plain');
151 $x = self::messageGetPart($mbox, $uid, $p, $ptop + 1, 'html');
157 $message = ['text' => trim($text), 'html' => trim($html)];
158 Hook::callAll('email_getmessage', $message, $ret);
159 $html = $message['html'];
160 $text = $message['text'];
163 $ret['body'] = HTML::toBBCode($html);
165 $ret['body'] = $text;
169 $ret['body'] = self::removeGPG($ret['body']);
170 $msg = self::removeSig($ret['body']);
171 $ret['body'] = $msg['body'];
172 $ret['body'] = self::convertQuote($ret['body'], $reply);
174 if (trim($html) != '') {
175 $ret['body'] = self::removeLinebreak($ret['body']);
178 $ret['body'] = self::unifyAttributionLine($ret['body']);
180 $ret['body'] = Strings::escapeHtml($ret['body']);
181 $ret['body'] = BBCode::limitBodySize($ret['body']);
183 Hook::callAll('email_getmessage_end', $ret);
189 * fetch the specified message part number with the specified subtype
191 * @param resource $mbox mailbox
192 * @param integer $uid user id
193 * @param object $p parts
194 * @param integer $partno part number
195 * @param string $subtype sub type
198 private static function messageGetPart($mbox, $uid, $p, $partno, $subtype)
200 // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
201 global $htmlmsg,$plainmsg,$charset,$attachments;
205 ? @imap_fetchbody($mbox, $uid, $partno, FT_UID|FT_PEEK)
206 : @imap_body($mbox, $uid, FT_UID|FT_PEEK);
208 // Any part may be encoded, even plain text messages, so check everything.
209 if ($p->encoding == 4) {
210 $data = quoted_printable_decode($data);
211 } elseif ($p->encoding == 3) {
212 $data = base64_decode($data);
216 // get all parameters, like charset, filenames of attachments, etc.
218 if ($p->parameters) {
219 foreach ($p->parameters as $x) {
220 $params[strtolower($x->attribute)] = $x->value;
224 if (isset($p->dparameters) && $p->dparameters) {
225 foreach ($p->dparameters as $x) {
226 $params[strtolower($x->attribute)] = $x->value;
231 // Any part with a filename is an attachment,
232 // so an attached text file (type 0) is not mistaken as the message.
234 if ((isset($params['filename']) && $params['filename']) || (isset($params['name']) && $params['name'])) {
235 // filename may be given as 'Filename' or 'Name' or both
236 $filename = ($params['filename'])? $params['filename'] : $params['name'];
237 // filename may be encoded, so see imap_mime_header_decode()
238 $attachments[$filename] = $data; // this is a problem if two files have same name
242 if ($p->type == 0 && $data) {
243 // Messages may be split in different parts because of inline attachments,
244 // so append parts together with blank row.
245 if (strtolower($p->subtype)==$subtype) {
246 $data = iconv($params['charset'], 'UTF-8//IGNORE', $data);
247 return (trim($data) ."\n\n");
252 // $htmlmsg .= $data ."<br><br>";
253 $charset = $params['charset']; // assume all parts are same charset
257 // Many bounce notifications embed the original message as type 2,
258 // but AOL uses type 1 (multipart), which is not handled here.
259 // There are no PHP functions to parse embedded messages,
260 // so this just appends the raw source to the main message.
261 // elseif ($p->type==2 && $data) {
262 // $plainmsg .= $data."\n\n";
266 if (isset($p->parts) && $p->parts) {
268 foreach ($p->parts as $partno0 => $p2) {
269 $x .= self::messageGetPart($mbox, $uid, $p2, $partno . '.' . ($partno0+1), $subtype); // 1.2, 1.2.1, etc.
276 * @param string $in_str in string
277 * @param string $charset character set
280 public static function encodeHeader($in_str, $charset)
283 $need_to_convert = false;
285 for ($x = 0; $x < strlen($in_str); $x ++) {
286 if ((ord($in_str[$x]) == 0) || ((ord($in_str[$x]) > 128))) {
287 $need_to_convert = true;
291 if (!$need_to_convert) {
295 if ($out_str && $charset) {
296 // define start delimimter, end delimiter and spacer
298 $start = "=?" . $charset . "?B?";
299 $spacer = $end . "\r\n " . $start;
301 // determine length of encoded text within chunks
302 // and ensure length is even
303 $length = 75 - strlen($start) - strlen($end);
306 [EDIT BY danbrown AT php DOT net: The following
307 is a bugfix provided by (gardan AT gmx DOT de)
308 on 31-MAR-2005 with the following note:
309 "This means: $length should not be even,
310 but divisible by 4. The reason is that in
311 base64-encoding 3 8-bit-chars are represented
312 by 4 6-bit-chars. These 4 chars must not be
313 split between two encoded words, according
316 $length = $length - ($length % 4);
318 // encode the string and split it into chunks
319 // with spacers after each chunk
320 $out_str = base64_encode($out_str);
321 $out_str = chunk_split($out_str, $length, $spacer);
323 // remove trailing spacer and
324 // add start and end delimiters
325 $spacer = preg_quote($spacer, '/');
326 $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
327 $out_str = $start . $out_str . $end;
333 * Function send is used by Protocol::EMAIL code
334 * (not to notify the user, but to send items to email contacts)
336 * @param string $addr address
337 * @param string $subject subject
338 * @param string $headers headers
339 * @param array $item item
343 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
344 * @throws \ImagickException
345 * @todo This could be changed to use the Emailer class
347 public static function send($addr, $subject, $headers, $item)
349 //$headers .= 'MIME-Version: 1.0' . "\n";
350 //$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
351 //$headers .= 'Content-Type: text/plain; charset=UTF-8' . "\n";
352 //$headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
354 $part = uniqid("", true);
356 $html = Item::prepareBody($item);
358 $headers .= "Mime-Version: 1.0\n";
359 $headers .= 'Content-Type: multipart/alternative; boundary="=_'.$part.'"'."\n\n";
361 $body = "\n--=_".$part."\n";
362 $body .= "Content-Transfer-Encoding: 8bit\n";
363 $body .= "Content-Type: text/plain; charset=utf-8; format=flowed\n\n";
365 $body .= HTML::toPlaintext($html)."\n";
367 $body .= "--=_".$part."\n";
368 $body .= "Content-Transfer-Encoding: 8bit\n";
369 $body .= "Content-Type: text/html; charset=utf-8\n\n";
371 $body .= '<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">'.$html."</body></html>\n";
373 $body .= "--=_".$part."--";
375 //$message = '<html><body>' . $html . '</body></html>';
376 //$message = html2plain($html);
377 Logger::log('notifier: email delivery to ' . $addr);
378 mail($addr, $subject, $body, $headers);
382 * @param string $iri string
385 public static function iri2msgid($iri)
387 if (!strpos($iri, "@")) {
388 $msgid = preg_replace("/urn:(\S+):(\S+)\.(\S+):(\d+):(\S+)/i", "urn!$1!$4!$5@$2.$3", $iri);
397 * @param string $msgid msgid
400 public static function msgid2iri($msgid)
402 if (strpos($msgid, "@")) {
403 $iri = preg_replace("/urn!(\S+)!(\d+)!(\S+)@(\S+)\.(\S+)/i", "urn:$1:$4.$5:$2:$3", $msgid);
411 private static function saveReplace($pattern, $replace, $text)
415 $text = preg_replace($pattern, $replace, $text);
423 private static function unifyAttributionLine($message)
425 $quotestr = ['quote', 'spoiler'];
426 foreach ($quotestr as $quote) {
427 $message = self::saveReplace('/----- Original Message -----\s.*?From: "([^<"].*?)" <(.*?)>\s.*?To: (.*?)\s*?Cc: (.*?)\s*?Sent: (.*?)\s.*?Subject: ([^\n].*)\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
428 $message = self::saveReplace('/----- Original Message -----\s.*?From: "([^<"].*?)" <(.*?)>\s.*?To: (.*?)\s*?Sent: (.*?)\s.*?Subject: ([^\n].*)\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
430 $message = self::saveReplace('/-------- Original-Nachricht --------\s*\['.$quote.'\]\nDatum: (.*?)\nVon: (.*?) <(.*?)>\nAn: (.*?)\nBetreff: (.*?)\n/i', "[".$quote."='$2']\n", $message);
431 $message = self::saveReplace('/-------- Original-Nachricht --------\s*\['.$quote.'\]\sDatum: (.*?)\s.*Von: "([^<"].*?)" <(.*?)>\s.*An: (.*?)\n.*/i', "[".$quote."='$2']\n", $message);
432 $message = self::saveReplace('/-------- Original-Nachricht --------\s*\['.$quote.'\]\nDatum: (.*?)\nVon: (.*?)\nAn: (.*?)\nBetreff: (.*?)\n/i', "[".$quote."='$2']\n", $message);
434 $message = self::saveReplace('/-----Urspr.*?ngliche Nachricht-----\sVon: "([^<"].*?)" <(.*?)>\s.*Gesendet: (.*?)\s.*An: (.*?)\s.*Betreff: ([^\n].*?).*:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
435 $message = self::saveReplace('/-----Urspr.*?ngliche Nachricht-----\sVon: "([^<"].*?)" <(.*?)>\s.*Gesendet: (.*?)\s.*An: (.*?)\s.*Betreff: ([^\n].*?)\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
437 $message = self::saveReplace('/Am (.*?), schrieb (.*?):\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
439 $message = self::saveReplace('/Am .*?, \d+ .*? \d+ \d+:\d+:\d+ \+\d+\sschrieb\s(.*?)\s<(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
441 $message = self::saveReplace('/Am (.*?) schrieb (.*?) <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
442 $message = self::saveReplace('/Am (.*?) schrieb <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
443 $message = self::saveReplace('/Am (.*?) schrieb (.*?):\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
444 $message = self::saveReplace('/Am (.*?) schrieb (.*?)\n(.*?):\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
446 $message = self::saveReplace('/(\d+)\/(\d+)\/(\d+) ([^<"].*?) <(.*?)>\s*\['.$quote.'\]/i', "[".$quote."='$4']\n", $message);
448 $message = self::saveReplace('/On .*?, \d+ .*? \d+ \d+:\d+:\d+ \+\d+\s(.*?)\s<(.*?)>\swrote:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
450 $message = self::saveReplace('/On (.*?) at (.*?), (.*?)\s<(.*?)>\swrote:\s*\['.$quote.'\]/i', "[".$quote."='$3']\n", $message);
451 $message = self::saveReplace('/On (.*?)\n([^<].*?)\s<(.*?)>\swrote:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
452 $message = self::saveReplace('/On (.*?), (.*?), (.*?)\s<(.*?)>\swrote:\s*\['.$quote.'\]/i', "[".$quote."='$3']\n", $message);
453 $message = self::saveReplace('/On ([^,].*?), (.*?)\swrote:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
454 $message = self::saveReplace('/On (.*?), (.*?)\swrote\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
456 // Der loescht manchmal den Body - was eigentlich unmoeglich ist
457 $message = self::saveReplace('/On (.*?),(.*?),(.*?),(.*?), (.*?) wrote:\s*\['.$quote.'\]/i', "[".$quote."='$5']\n", $message);
459 $message = self::saveReplace('/Zitat von ([^<].*?) <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
461 $message = self::saveReplace('/Quoting ([^<].*?) <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
463 $message = self::saveReplace('/From: "([^<"].*?)" <(.*?)>\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
464 $message = self::saveReplace('/From: <(.*?)>\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
466 $message = self::saveReplace('/Du \(([^)].*?)\) schreibst:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
468 $message = self::saveReplace('/--- (.*?) <.*?> schrieb am (.*?):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
469 $message = self::saveReplace('/--- (.*?) schrieb am (.*?):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
471 $message = self::saveReplace('/\* (.*?) <(.*?)> hat geschrieben:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
473 $message = self::saveReplace('/(.*?) <(.*?)> schrieb (.*?)\):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
474 $message = self::saveReplace('/(.*?) <(.*?)> schrieb am (.*?) um (.*):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
475 $message = self::saveReplace('/(.*?) schrieb am (.*?) um (.*):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
476 $message = self::saveReplace('/(.*?) \((.*?)\) schrieb:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
477 $message = self::saveReplace('/(.*?) schrieb:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
479 $message = self::saveReplace('/(.*?) <(.*?)> writes:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
480 $message = self::saveReplace('/(.*?) \((.*?)\) writes:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
481 $message = self::saveReplace('/(.*?) writes:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
483 $message = self::saveReplace('/\* (.*?) wrote:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
484 $message = self::saveReplace('/(.*?) wrote \(.*?\):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
485 $message = self::saveReplace('/(.*?) wrote:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
487 $message = self::saveReplace('/([^<].*?) <.*?> hat am (.*?)\sum\s(.*)\sgeschrieben:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
489 $message = self::saveReplace('/(\d+)\/(\d+)\/(\d+) ([^<"].*?) <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$4']\n", $message);
490 $message = self::saveReplace('/(\d+)\/(\d+)\/(\d+) (.*?) <(.*?)>\s*\['.$quote.'\]/i', "[".$quote."='$4']\n", $message);
491 $message = self::saveReplace('/(\d+)\/(\d+)\/(\d+) <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$4']\n", $message);
492 $message = self::saveReplace('/(\d+)\/(\d+)\/(\d+) <(.*?)>\s*\['.$quote.'\]/i', "[".$quote."='$4']\n", $message);
494 $message = self::saveReplace('/(.*?) <(.*?)> schrubselte:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
495 $message = self::saveReplace('/(.*?) \((.*?)\) schrubselte:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
500 private static function removeGPG($message)
502 $pattern = '/(.*)\s*-----BEGIN PGP SIGNED MESSAGE-----\s*[\r\n].*Hash:.*?[\r\n](.*)'.
503 '[\r\n]\s*-----BEGIN PGP SIGNATURE-----\s*[\r\n].*'.
504 '[\r\n]\s*-----END PGP SIGNATURE-----(.*)/is';
506 if (preg_match($pattern, $message, $result)) {
507 $cleaned = trim($result[1].$result[2].$result[3]);
509 $cleaned = str_replace(["\n- --\n", "\n- -"], ["\n-- \n", "\n-"], $cleaned);
517 private static function removeSig($message)
519 $sigpos = strrpos($message, "\n-- \n");
520 $quotepos = strrpos($message, "[/quote]");
523 // Especially for web.de who are using that as a separator
524 $message = str_replace("\n___________________________________________________________\n", "\n-- \n", $message);
525 $sigpos = strrpos($message, "\n-- \n");
526 $quotepos = strrpos($message, "[/quote]");
529 // When the signature separator is inside a quote, we don't separate
530 if (($sigpos < $quotepos) && ($sigpos != 0)) {
531 return ['body' => $message, 'sig' => ''];
534 $pattern = '/(.*)[\r\n]-- [\r\n](.*)/is';
536 preg_match($pattern, $message, $result);
538 if (!empty($result[1]) && !empty($result[2])) {
539 $cleaned = trim($result[1])."\n";
540 $sig = trim($result[2]);
546 return ['body' => $cleaned, 'sig' => $sig];
549 private static function removeLinebreak($message)
551 $arrbody = explode("\n", trim($message));
556 foreach ($arrbody as $i => $line) {
559 while ((strlen($currline)>0) && ((substr($currline, 0, 1) == '>')
560 || (substr($currline, 0, 1) == ' '))) {
561 if (substr($currline, 0, 1) == '>') {
565 $currline = ltrim(substr($currline, 1));
569 $nextline = trim($arrbody[$i + 1] ?? '');
570 while ((strlen($nextline)>0) && ((substr($nextline, 0, 1) == '>')
571 || (substr($nextline, 0, 1) == ' '))) {
572 if (substr($nextline, 0, 1) == '>') {
576 $nextline = ltrim(substr($nextline, 1));
579 if (!empty($lines[$lineno])) {
580 if (substr($lines[$lineno], -1) != ' ') {
581 $lines[$lineno] .= ' ';
584 while ((strlen($line)>0) && ((substr($line, 0, 1) == '>')
585 || (substr($line, 0, 1) == ' '))) {
587 $line = ltrim(substr($line, 1));
590 $lines[$lineno] = '';
593 $lines[$lineno] .= $line;
594 if (((substr($line, -1, 1) != ' '))
595 || ($quotelevel != $currquotelevel)) {
599 return implode("\n", $lines);
602 private static function convertQuote($body, $reply)
605 $arrbody = explode("\n", trim($body));
608 for ($i = 0; $i < count($arrbody); $i++) {
610 $quoteline = $arrbody[$i];
612 while ((strlen($quoteline)>0) and ((substr($quoteline, 0, 1) == '>')
613 || (substr($quoteline, 0, 1) == ' '))) {
614 if (substr($quoteline, 0, 1) == '>')
617 $quoteline = ltrim(substr($quoteline, 1));
620 $arrlevel[$i] = $quotelevel;
621 $arrbody[$i] = $quoteline;
627 for ($i = 0; $i < count($arrbody); $i++) {
628 $previousquote = $quotelevel;
629 $quotelevel = $arrlevel[$i];
631 while ($previousquote < $quotelevel) {
633 $arrbody[$i] = $quote.$arrbody[$i];
637 while ($previousquote > $quotelevel) {
638 $arrbody[$i] = '[/quote]'.$arrbody[$i];
642 $arrbodyquoted[] = $arrbody[$i];
644 while ($quotelevel > 0) {
645 $arrbodyquoted[] = '[/quote]';
649 $body = implode("\n", $arrbodyquoted);
651 if (strlen($body) > 0) {
652 $body = $body."\n\n";
656 $body = self::removeToFu($body);
662 private static function removeToFu($message)
664 $message = trim($message);
667 $oldmessage = $message;
668 $message = preg_replace('=\[/quote\][\s](.*?)\[quote\]=i', '$1', $message);
669 $message = str_replace("[/quote][quote]", "", $message);
670 } while ($message != $oldmessage);
678 while (($pos = strpos($message, '[quote', $start)) > 0) {
687 while (($pos = strpos($message, '[/quote]', $start)) > 0) {
692 while ($endquotes < $startquotes) {
693 $message .= '[/quote]';
699 while (($pos = strpos($message, '[/quote]', $start)) > 0) {
704 if (strtolower(substr($message, -8)) != '[/quote]')
711 foreach ($quotes as $index => $quote) {
712 $quotelevel += $quote;
714 if (($quotelevel == 0) and ($quotestart == 0))
715 $quotestart = $index;
718 if ($quotestart != 0) {
719 $message = trim(substr($message, 0, $quotestart))."\n[spoiler]".substr($message, $quotestart+7, -8).'[/spoiler]';