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