Update copyright
[friendica.git/.git] / src / Protocol / Email.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Protocol;
23
24 use Friendica\Core\Hook;
25 use Friendica\Core\Logger;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Content\Text\HTML;
28 use Friendica\Model\Item;
29 use Friendica\Util\Strings;
30
31 /**
32  * Email class
33  */
34 class Email
35 {
36         /**
37          * @param string $mailbox  The mailbox name
38          * @param string $username The username
39          * @param string $password The password
40          * @return resource
41          * @throws \Exception
42          */
43         public static function connect($mailbox, $username, $password)
44         {
45                 if (!function_exists('imap_open')) {
46                         return false;
47                 }
48
49                 $mbox = @imap_open($mailbox, $username, $password);
50
51                 $errors = imap_errors();
52                 if (!empty($errors)) {
53                         Logger::log('IMAP Errors occured: ' . json_encode($errors));
54                 }
55
56                 $alerts = imap_alerts();
57                 if (!empty($alerts)) {
58                         Logger::log('IMAP Alerts occured: ' . json_encode($alerts));
59                 }
60
61                 return $mbox;
62         }
63
64         /**
65          * @param resource $mbox       mailbox
66          * @param string   $email_addr email
67          * @return array
68          * @throws \Exception
69          */
70         public static function poll($mbox, $email_addr)
71         {
72                 if (!$mbox || !$email_addr) {
73                         return [];
74                 }
75
76                 $search1 = @imap_search($mbox, 'UNDELETED FROM "' . $email_addr . '"', SE_UID);
77                 if (!$search1) {
78                         $search1 = [];
79                 } else {
80                         Logger::log("Found mails from ".$email_addr, Logger::DEBUG);
81                 }
82
83                 $search2 = @imap_search($mbox, 'UNDELETED TO "' . $email_addr . '"', SE_UID);
84                 if (!$search2) {
85                         $search2 = [];
86                 } else {
87                         Logger::log("Found mails to ".$email_addr, Logger::DEBUG);
88                 }
89
90                 $search3 = @imap_search($mbox, 'UNDELETED CC "' . $email_addr . '"', SE_UID);
91                 if (!$search3) {
92                         $search3 = [];
93                 } else {
94                         Logger::log("Found mails cc ".$email_addr, Logger::DEBUG);
95                 }
96
97                 $res = array_unique(array_merge($search1, $search2, $search3));
98
99                 return $res;
100         }
101
102         /**
103          * @param array   $mailacct mail account
104          * @return string
105          */
106         public static function constructMailboxName($mailacct)
107         {
108                 $ret = '{' . $mailacct['server'] . ((intval($mailacct['port'])) ? ':' . $mailacct['port'] : '');
109                 $ret .= (($mailacct['ssltype']) ?  '/' . $mailacct['ssltype'] . '/novalidate-cert' : '');
110                 $ret .= '}' . $mailacct['mailbox'];
111                 return $ret;
112         }
113
114         /**
115          * @param resource $mbox mailbox
116          * @param integer  $uid  user id
117          * @return mixed
118          */
119         public static function messageMeta($mbox, $uid)
120         {
121                 $ret = (($mbox && $uid) ? @imap_fetch_overview($mbox, $uid, FT_UID) : [[]]); // POSSIBLE CLEANUP --> array(array()) is probably redundant now
122                 return (count($ret)) ? $ret : [];
123         }
124
125         /**
126          * @param resource $mbox  mailbox
127          * @param integer  $uid   user id
128          * @param string   $reply reply
129          * @return array
130          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
131          */
132         public static function getMessage($mbox, $uid, $reply, $item)
133         {
134                 $ret = $item;
135
136                 $struc = (($mbox && $uid) ? @imap_fetchstructure($mbox, $uid, FT_UID) : null);
137
138                 if (!$struc) {
139                         Logger::notice("IMAP structure couldn't be fetched", ['uid' => $uid]);
140                         return $ret;
141                 }
142
143                 if (empty($struc->parts)) {
144                         $html = trim(self::messageGetPart($mbox, $uid, $struc, 0, 'html'));
145
146                         if (!empty($html)) {
147                                 $message = ['text' => '', 'html' => $html, 'item' => $ret];
148                                 Hook::callAll('email_getmessage', $message);
149                                 $ret = $message['item'];
150                                 if (empty($ret['body'])) {
151                                         $ret['body'] = HTML::toBBCode($message['html']);
152                                 }
153                         }
154
155                         if (empty($ret['body'])) {
156                                 $text = self::messageGetPart($mbox, $uid, $struc, 0, 'plain');
157
158                                 $message = ['text' => $text, 'html' => '', 'item' => $ret];
159                                 Hook::callAll('email_getmessage', $message);
160                                 $ret = $message['item'];
161                                 $ret['body'] = $message['text'];
162                         }
163                 } else {
164                         $text = '';
165                         $html = '';
166                         foreach ($struc->parts as $ptop => $p) {
167                                 $x = self::messageGetPart($mbox, $uid, $p, $ptop + 1, 'plain');
168                                 if ($x) {
169                                         $text .= $x;
170                                 }
171
172                                 $x = self::messageGetPart($mbox, $uid, $p, $ptop + 1, 'html');
173                                 if ($x) {
174                                         $html .= $x;
175                                 }
176                         }
177
178                         $message = ['text' => trim($text), 'html' => trim($html), 'item' => $ret];
179                         Hook::callAll('email_getmessage', $message);
180                         $ret = $message['item'];
181
182                         if (empty($ret['body']) && !empty($message['html'])) {
183                                 $ret['body'] = HTML::toBBCode($message['html']);
184                         }
185
186                         if (empty($ret['body'])) {
187                                 $ret['body'] = $message['text'];
188                         }
189                 }
190
191                 $ret['body'] = self::removeGPG($ret['body']);
192                 $msg = self::removeSig($ret['body']);
193                 $ret['body'] = $msg['body'];
194                 $ret['body'] = self::convertQuote($ret['body'], $reply);
195
196                 if (trim($html) != '') {
197                         $ret['body'] = self::removeLinebreak($ret['body']);
198                 }
199
200                 $ret['body'] = self::unifyAttributionLine($ret['body']);
201
202                 $ret['body'] = Strings::escapeHtml($ret['body']);
203                 $ret['body'] = BBCode::limitBodySize($ret['body']);
204
205                 Hook::callAll('email_getmessage_end', $ret);
206
207                 return $ret;
208         }
209
210         /**
211          * fetch the specified message part number with the specified subtype
212          *
213          * @param resource $mbox    mailbox
214          * @param integer  $uid     user id
215          * @param object   $p       parts
216          * @param integer  $partno  part number
217          * @param string   $subtype sub type
218          * @return string
219          */
220         private static function messageGetPart($mbox, $uid, $p, $partno, $subtype)
221         {
222                 // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
223                 global $htmlmsg,$plainmsg,$charset,$attachments;
224
225                 // DECODE DATA
226                 $data = ($partno)
227                         ? @imap_fetchbody($mbox, $uid, $partno, FT_UID|FT_PEEK)
228                 : @imap_body($mbox, $uid, FT_UID|FT_PEEK);
229
230                 // Any part may be encoded, even plain text messages, so check everything.
231                 if ($p->encoding == 4) {
232                         $data = quoted_printable_decode($data);
233                 } elseif ($p->encoding == 3) {
234                         $data = base64_decode($data);
235                 }
236
237                 // PARAMETERS
238                 // get all parameters, like charset, filenames of attachments, etc.
239                 $params = [];
240                 if ($p->parameters) {
241                         foreach ($p->parameters as $x) {
242                                 $params[strtolower($x->attribute)] = $x->value;
243                         }
244                 }
245
246                 if (isset($p->dparameters) && $p->dparameters) {
247                         foreach ($p->dparameters as $x) {
248                                 $params[strtolower($x->attribute)] = $x->value;
249                         }
250                 }
251
252                 // ATTACHMENT
253                 // Any part with a filename is an attachment,
254                 // so an attached text file (type 0) is not mistaken as the message.
255
256                 if ((isset($params['filename']) && $params['filename']) || (isset($params['name']) && $params['name'])) {
257                         // filename may be given as 'Filename' or 'Name' or both
258                         $filename = ($params['filename'])? $params['filename'] : $params['name'];
259                         // filename may be encoded, so see imap_mime_header_decode()
260                         $attachments[$filename] = $data;  // this is a problem if two files have same name
261                 }
262
263                 // TEXT
264                 if ($p->type == 0 && $data) {
265                         // Messages may be split in different parts because of inline attachments,
266                         // so append parts together with blank row.
267                         if (strtolower($p->subtype)==$subtype) {
268                                 $data = iconv($params['charset'], 'UTF-8//IGNORE', $data);
269                                 return (trim($data) ."\n\n");
270                         } else {
271                                 $data = '';
272                         }
273
274                         // $htmlmsg .= $data ."<br><br>";
275                         $charset = $params['charset'];  // assume all parts are same charset
276                 }
277
278                 // EMBEDDED MESSAGE
279                 // Many bounce notifications embed the original message as type 2,
280                 // but AOL uses type 1 (multipart), which is not handled here.
281                 // There are no PHP functions to parse embedded messages,
282                 // so this just appends the raw source to the main message.
283                 //      elseif ($p->type==2 && $data) {
284                 //              $plainmsg .= $data."\n\n";
285                 //      }
286
287                 // SUBPART RECURSION
288                 if (isset($p->parts) && $p->parts) {
289                         $x = "";
290                         foreach ($p->parts as $partno0 => $p2) {
291                                 $x .=  self::messageGetPart($mbox, $uid, $p2, $partno . '.' . ($partno0+1), $subtype);  // 1.2, 1.2.1, etc.
292                         }
293                         return $x;
294                 }
295         }
296
297         /**
298          * @param string $in_str  in string
299          * @param string $charset character set
300          * @return string
301          */
302         public static function encodeHeader($in_str, $charset)
303         {
304                 $out_str = $in_str;
305                 $need_to_convert = false;
306
307                 for ($x = 0; $x < strlen($in_str); $x ++) {
308                         if ((ord($in_str[$x]) == 0) || ((ord($in_str[$x]) > 128))) {
309                                 $need_to_convert = true;
310                         }
311                 }
312
313                 if (!$need_to_convert) {
314                         return $in_str;
315                 }
316
317                 if ($out_str && $charset) {
318                         // define start delimimter, end delimiter and spacer
319                         $end = "?=";
320                         $start = "=?" . $charset . "?B?";
321                         $spacer = $end . "\r\n " . $start;
322
323                         // determine length of encoded text within chunks
324                         // and ensure length is even
325                         $length = 75 - strlen($start) - strlen($end);
326
327                         /*
328                                 [EDIT BY danbrown AT php DOT net: The following
329                                 is a bugfix provided by (gardan AT gmx DOT de)
330                                 on 31-MAR-2005 with the following note:
331                                 "This means: $length should not be even,
332                                 but divisible by 4. The reason is that in
333                                 base64-encoding 3 8-bit-chars are represented
334                                 by 4 6-bit-chars. These 4 chars must not be
335                                 split between two encoded words, according
336                                 to RFC-2047.
337                         */
338                         $length = $length - ($length % 4);
339
340                         // encode the string and split it into chunks
341                         // with spacers after each chunk
342                         $out_str = base64_encode($out_str);
343                         $out_str = chunk_split($out_str, $length, $spacer);
344
345                         // remove trailing spacer and
346                         // add start and end delimiters
347                         $spacer = preg_quote($spacer, '/');
348                         $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
349                         $out_str = $start . $out_str . $end;
350                 }
351                 return $out_str;
352         }
353
354         /**
355          * Function send is used by Protocol::EMAIL code
356          * (not to notify the user, but to send items to email contacts)
357          *
358          * @param string $addr    address
359          * @param string $subject subject
360          * @param string $headers headers
361          * @param array  $item    item
362          *
363          * @return void
364          *
365          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
366          * @throws \ImagickException
367          * @todo This could be changed to use the Emailer class
368          */
369         public static function send($addr, $subject, $headers, $item)
370         {
371                 //$headers .= 'MIME-Version: 1.0' . "\n";
372                 //$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
373                 //$headers .= 'Content-Type: text/plain; charset=UTF-8' . "\n";
374                 //$headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
375
376                 $part = uniqid("", true);
377
378                 $html    = Item::prepareBody($item);
379
380                 $headers .= "Mime-Version: 1.0\n";
381                 $headers .= 'Content-Type: multipart/alternative; boundary="=_'.$part.'"'."\n\n";
382
383                 $body = "\n--=_".$part."\n";
384                 $body .= "Content-Transfer-Encoding: 8bit\n";
385                 $body .= "Content-Type: text/plain; charset=utf-8; format=flowed\n\n";
386
387                 $body .= HTML::toPlaintext($html)."\n";
388
389                 $body .= "--=_".$part."\n";
390                 $body .= "Content-Transfer-Encoding: 8bit\n";
391                 $body .= "Content-Type: text/html; charset=utf-8\n\n";
392
393                 $body .= '<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">'.$html."</body></html>\n";
394
395                 $body .= "--=_".$part."--";
396
397                 //$message = '<html><body>' . $html . '</body></html>';
398                 //$message = html2plain($html);
399                 Logger::log('notifier: email delivery to ' . $addr);
400                 mail($addr, $subject, $body, $headers);
401         }
402
403         /**
404          * @param string $iri string
405          * @return string
406          */
407         public static function iri2msgid($iri)
408         {
409                 if (!strpos($iri, "@")) {
410                         $msgid = preg_replace("/urn:(\S+):(\S+)\.(\S+):(\d+):(\S+)/i", "urn!$1!$4!$5@$2.$3", $iri);
411                 } else {
412                         $msgid = $iri;
413                 }
414
415                 return $msgid;
416         }
417
418         /**
419          * @param string $msgid msgid
420          * @return string
421          */
422         public static function msgid2iri($msgid)
423         {
424                 if (strpos($msgid, "@")) {
425                         $iri = preg_replace("/urn!(\S+)!(\d+)!(\S+)@(\S+)\.(\S+)/i", "urn:$1:$4.$5:$2:$3", $msgid);
426                 } else {
427                         $iri = $msgid;
428                 }
429
430                 return $iri;
431         }
432
433         private static function saveReplace($pattern, $replace, $text)
434         {
435                 $save = $text;
436
437                 $text = preg_replace($pattern, $replace, $text);
438
439                 if ($text == '') {
440                         $text = $save;
441                 }
442                 return $text;
443         }
444
445         private static function unifyAttributionLine($message)
446         {
447                 $quotestr = ['quote', 'spoiler'];
448                 foreach ($quotestr as $quote) {
449                         $message = self::saveReplace('/----- Original Message -----\s.*?From: "([^<"].*?)" <(.*?)>\s.*?To: (.*?)\s*?Cc: (.*?)\s*?Sent: (.*?)\s.*?Subject: ([^\n].*)\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
450                         $message = self::saveReplace('/----- Original Message -----\s.*?From: "([^<"].*?)" <(.*?)>\s.*?To: (.*?)\s*?Sent: (.*?)\s.*?Subject: ([^\n].*)\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
451
452                         $message = self::saveReplace('/-------- Original-Nachricht --------\s*\['.$quote.'\]\nDatum: (.*?)\nVon: (.*?) <(.*?)>\nAn: (.*?)\nBetreff: (.*?)\n/i', "[".$quote."='$2']\n", $message);
453                         $message = self::saveReplace('/-------- Original-Nachricht --------\s*\['.$quote.'\]\sDatum: (.*?)\s.*Von: "([^<"].*?)" <(.*?)>\s.*An: (.*?)\n.*/i', "[".$quote."='$2']\n", $message);
454                         $message = self::saveReplace('/-------- Original-Nachricht --------\s*\['.$quote.'\]\nDatum: (.*?)\nVon: (.*?)\nAn: (.*?)\nBetreff: (.*?)\n/i', "[".$quote."='$2']\n", $message);
455
456                         $message = self::saveReplace('/-----Urspr.*?ngliche Nachricht-----\sVon: "([^<"].*?)" <(.*?)>\s.*Gesendet: (.*?)\s.*An: (.*?)\s.*Betreff: ([^\n].*?).*:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
457                         $message = self::saveReplace('/-----Urspr.*?ngliche Nachricht-----\sVon: "([^<"].*?)" <(.*?)>\s.*Gesendet: (.*?)\s.*An: (.*?)\s.*Betreff: ([^\n].*?)\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
458
459                         $message = self::saveReplace('/Am (.*?), schrieb (.*?):\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
460
461                         $message = self::saveReplace('/Am .*?, \d+ .*? \d+ \d+:\d+:\d+ \+\d+\sschrieb\s(.*?)\s<(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
462
463                         $message = self::saveReplace('/Am (.*?) schrieb (.*?) <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
464                         $message = self::saveReplace('/Am (.*?) schrieb <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
465                         $message = self::saveReplace('/Am (.*?) schrieb (.*?):\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
466                         $message = self::saveReplace('/Am (.*?) schrieb (.*?)\n(.*?):\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
467
468                         $message = self::saveReplace('/(\d+)\/(\d+)\/(\d+) ([^<"].*?) <(.*?)>\s*\['.$quote.'\]/i', "[".$quote."='$4']\n", $message);
469
470                         $message = self::saveReplace('/On .*?, \d+ .*? \d+ \d+:\d+:\d+ \+\d+\s(.*?)\s<(.*?)>\swrote:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
471
472                         $message = self::saveReplace('/On (.*?) at (.*?), (.*?)\s<(.*?)>\swrote:\s*\['.$quote.'\]/i', "[".$quote."='$3']\n", $message);
473                         $message = self::saveReplace('/On (.*?)\n([^<].*?)\s<(.*?)>\swrote:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
474                         $message = self::saveReplace('/On (.*?), (.*?), (.*?)\s<(.*?)>\swrote:\s*\['.$quote.'\]/i', "[".$quote."='$3']\n", $message);
475                         $message = self::saveReplace('/On ([^,].*?), (.*?)\swrote:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
476                         $message = self::saveReplace('/On (.*?), (.*?)\swrote\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
477
478                         // Der loescht manchmal den Body - was eigentlich unmoeglich ist
479                         $message = self::saveReplace('/On (.*?),(.*?),(.*?),(.*?), (.*?) wrote:\s*\['.$quote.'\]/i', "[".$quote."='$5']\n", $message);
480
481                         $message = self::saveReplace('/Zitat von ([^<].*?) <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
482
483                         $message = self::saveReplace('/Quoting ([^<].*?) <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
484
485                         $message = self::saveReplace('/From: "([^<"].*?)" <(.*?)>\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
486                         $message = self::saveReplace('/From: <(.*?)>\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
487
488                         $message = self::saveReplace('/Du \(([^)].*?)\) schreibst:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
489
490                         $message = self::saveReplace('/--- (.*?) <.*?> schrieb am (.*?):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
491                         $message = self::saveReplace('/--- (.*?) schrieb am (.*?):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
492
493                         $message = self::saveReplace('/\* (.*?) <(.*?)> hat geschrieben:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
494
495                         $message = self::saveReplace('/(.*?) <(.*?)> schrieb (.*?)\):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
496                         $message = self::saveReplace('/(.*?) <(.*?)> schrieb am (.*?) um (.*):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
497                         $message = self::saveReplace('/(.*?) schrieb am (.*?) um (.*):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
498                         $message = self::saveReplace('/(.*?) \((.*?)\) schrieb:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
499                         $message = self::saveReplace('/(.*?) schrieb:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
500
501                         $message = self::saveReplace('/(.*?) <(.*?)> writes:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
502                         $message = self::saveReplace('/(.*?) \((.*?)\) writes:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
503                         $message = self::saveReplace('/(.*?) writes:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
504
505                         $message = self::saveReplace('/\* (.*?) wrote:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
506                         $message = self::saveReplace('/(.*?) wrote \(.*?\):\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
507                         $message = self::saveReplace('/(.*?) wrote:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
508
509                         $message = self::saveReplace('/([^<].*?) <.*?> hat am (.*?)\sum\s(.*)\sgeschrieben:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
510
511                         $message = self::saveReplace('/(\d+)\/(\d+)\/(\d+) ([^<"].*?) <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$4']\n", $message);
512                         $message = self::saveReplace('/(\d+)\/(\d+)\/(\d+) (.*?) <(.*?)>\s*\['.$quote.'\]/i', "[".$quote."='$4']\n", $message);
513                         $message = self::saveReplace('/(\d+)\/(\d+)\/(\d+) <(.*?)>:\s*\['.$quote.'\]/i', "[".$quote."='$4']\n", $message);
514                         $message = self::saveReplace('/(\d+)\/(\d+)\/(\d+) <(.*?)>\s*\['.$quote.'\]/i', "[".$quote."='$4']\n", $message);
515
516                         $message = self::saveReplace('/(.*?) <(.*?)> schrubselte:\s*\['.$quote.'\]/i', "[".$quote."='$1']\n", $message);
517                         $message = self::saveReplace('/(.*?) \((.*?)\) schrubselte:\s*\['.$quote.'\]/i', "[".$quote."='$2']\n", $message);
518                 }
519                 return $message;
520         }
521
522         private static function removeGPG($message)
523         {
524                 $pattern = '/(.*)\s*-----BEGIN PGP SIGNED MESSAGE-----\s*[\r\n].*Hash:.*?[\r\n](.*)'.
525                         '[\r\n]\s*-----BEGIN PGP SIGNATURE-----\s*[\r\n].*'.
526                         '[\r\n]\s*-----END PGP SIGNATURE-----(.*)/is';
527
528                 if (preg_match($pattern, $message, $result)) {
529                         $cleaned = trim($result[1].$result[2].$result[3]);
530
531                         $cleaned = str_replace(["\n- --\n", "\n- -"], ["\n-- \n", "\n-"], $cleaned);
532                 } else {
533                         $cleaned = $message;
534                 }
535
536                 return $cleaned;
537         }
538
539         private static function removeSig($message)
540         {
541                 $sigpos = strrpos($message, "\n-- \n");
542                 $quotepos = strrpos($message, "[/quote]");
543
544                 if ($sigpos == 0) {
545                         // Especially for web.de who are using that as a separator
546                         $message = str_replace("\n___________________________________________________________\n", "\n-- \n", $message);
547                         $sigpos = strrpos($message, "\n-- \n");
548                         $quotepos = strrpos($message, "[/quote]");
549                 }
550
551                 // When the signature separator is inside a quote, we don't separate
552                 if (($sigpos < $quotepos) && ($sigpos != 0)) {
553                         return ['body' => $message, 'sig' => ''];
554                 }
555
556                 $pattern = '/(.*)[\r\n]-- [\r\n](.*)/is';
557
558                 preg_match($pattern, $message, $result);
559
560                 if (!empty($result[1]) && !empty($result[2])) {
561                         $cleaned = trim($result[1])."\n";
562                         $sig = trim($result[2]);
563                 } else {
564                         $cleaned = $message;
565                         $sig = '';
566                 }
567
568                 return ['body' => $cleaned, 'sig' => $sig];
569         }
570
571         private static function removeLinebreak($message)
572         {
573                 $arrbody = explode("\n", trim($message));
574
575                 $lines = [];
576                 $lineno = 0;
577
578                 foreach ($arrbody as $i => $line) {
579                         $currquotelevel = 0;
580                         $currline = $line;
581                         while ((strlen($currline)>0) && ((substr($currline, 0, 1) == '>')
582                                 || (substr($currline, 0, 1) == ' '))) {
583                                 if (substr($currline, 0, 1) == '>') {
584                                         $currquotelevel++;
585                                 }
586
587                                 $currline = ltrim(substr($currline, 1));
588                         }
589
590                         $quotelevel = 0;
591                         $nextline = trim($arrbody[$i + 1] ?? '');
592                         while ((strlen($nextline)>0) && ((substr($nextline, 0, 1) == '>')
593                                 || (substr($nextline, 0, 1) == ' '))) {
594                                 if (substr($nextline, 0, 1) == '>') {
595                                         $quotelevel++;
596                                 }
597
598                                 $nextline = ltrim(substr($nextline, 1));
599                         }
600
601                         if (!empty($lines[$lineno])) {
602                                 if (substr($lines[$lineno], -1) != ' ') {
603                                         $lines[$lineno] .= ' ';
604                                 }
605
606                                 while ((strlen($line)>0) && ((substr($line, 0, 1) == '>')
607                                         || (substr($line, 0, 1) == ' '))) {
608
609                                         $line = ltrim(substr($line, 1));
610                                 }
611                         } else {
612                                 $lines[$lineno] = '';
613                         }
614
615                         $lines[$lineno] .= $line;
616                         if (((substr($line, -1, 1) != ' '))
617                                 || ($quotelevel != $currquotelevel)) {
618                                 $lineno++;
619                         }
620                 }
621                 return implode("\n", $lines);
622         }
623
624         private static function convertQuote($body, $reply)
625         {
626                 // Convert Quotes
627                 $arrbody = explode("\n", trim($body));
628                 $arrlevel = [];
629
630                 for ($i = 0; $i < count($arrbody); $i++) {
631                         $quotelevel = 0;
632                         $quoteline = $arrbody[$i];
633
634                         while ((strlen($quoteline)>0) and ((substr($quoteline, 0, 1) == '>')
635                                 || (substr($quoteline, 0, 1) == ' '))) {
636                                 if (substr($quoteline, 0, 1) == '>')
637                                         $quotelevel++;
638
639                                 $quoteline = ltrim(substr($quoteline, 1));
640                         }
641
642                         $arrlevel[$i] = $quotelevel;
643                         $arrbody[$i] = $quoteline;
644                 }
645
646                 $quotelevel = 0;
647                 $arrbodyquoted = [];
648
649                 for ($i = 0; $i < count($arrbody); $i++) {
650                         $previousquote = $quotelevel;
651                         $quotelevel = $arrlevel[$i];
652
653                         while ($previousquote < $quotelevel) {
654                                 $quote = "[quote]";
655                                 $arrbody[$i] = $quote.$arrbody[$i];
656                                 $previousquote++;
657                         }
658
659                         while ($previousquote > $quotelevel) {
660                                 $arrbody[$i] = '[/quote]'.$arrbody[$i];
661                                 $previousquote--;
662                         }
663
664                         $arrbodyquoted[] = $arrbody[$i];
665                 }
666                 while ($quotelevel > 0) {
667                         $arrbodyquoted[] = '[/quote]';
668                         $quotelevel--;
669                 }
670
671                 $body = implode("\n", $arrbodyquoted);
672
673                 if (strlen($body) > 0) {
674                         $body = $body."\n\n";
675                 }
676
677                 if ($reply) {
678                         $body = self::removeToFu($body);
679                 }
680
681                 return $body;
682         }
683
684         private static function removeToFu($message)
685         {
686                 $message = trim($message);
687
688                 do {
689                         $oldmessage = $message;
690                         $message = preg_replace('=\[/quote\][\s](.*?)\[quote\]=i', '$1', $message);
691                         $message = str_replace("[/quote][quote]", "", $message);
692                 } while ($message != $oldmessage);
693
694                 $quotes = [];
695
696                 $startquotes = 0;
697
698                 $start = 0;
699
700                 while (($pos = strpos($message, '[quote', $start)) > 0) {
701                         $quotes[$pos] = -1;
702                         $start = $pos + 7;
703                         $startquotes++;
704                 }
705
706                 $endquotes = 0;
707                 $start = 0;
708
709                 while (($pos = strpos($message, '[/quote]', $start)) > 0) {
710                         $start = $pos + 7;
711                         $endquotes++;
712                 }
713
714                 while ($endquotes < $startquotes) {
715                         $message .= '[/quote]';
716                         ++$endquotes;
717                 }
718
719                 $start = 0;
720
721                 while (($pos = strpos($message, '[/quote]', $start)) > 0) {
722                         $quotes[$pos] = 1;
723                         $start = $pos + 7;
724                 }
725
726                 if (strtolower(substr($message, -8)) != '[/quote]')
727                         return($message);
728
729                 krsort($quotes);
730
731                 $quotelevel = 0;
732                 $quotestart = 0;
733                 foreach ($quotes as $index => $quote) {
734                         $quotelevel += $quote;
735
736                         if (($quotelevel == 0) and ($quotestart == 0))
737                                 $quotestart = $index;
738                 }
739
740                 if ($quotestart != 0) {
741                         $message = trim(substr($message, 0, $quotestart))."\n[spoiler]".substr($message, $quotestart+7, -8).'[/spoiler]';
742                 }
743
744                 return $message;
745         }
746 }