- if ($item['title']) {
- return $item['title'];
- }
- $parent = $item['thr-parent'];
- // Don't look more than 100 levels deep for a subject, in case of loops
- for ($i = 0; ($i < 100) && $parent; $i++) {
- $r = q("SELECT `thr-parent`, `title` FROM `item` WHERE `uri` = '%s'", dbesc($parent));
- if (!count($r)) {
- break;
- }
- if ($r[0]['thr-parent'] === $parent) {
- break;
- }
- if ($r[0]['title']) {
- return t('Re:') . ' ' . $r[0]['title'];
- }
- $parent = $r[0]['thr-parent'];
- }
- $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d",
- intval($item['contact-id']), intval($item['uid']));
- $contact = $r[0];
- if ($contact['network'] === 'dfrn') {
- return t("Friendica post");
- }
- if ($contact['network'] === 'dspr') {
- return t("Diaspora post");
- }
- if ($contact['network'] === 'face') {
- $subject = (strlen($item['body']) > 150) ? (substr($item['body'], 0, 140) . '...') : $item['body'];
- return preg_replace('/\\s+/', ' ', $subject);
- }
- if ($contact['network'] === 'feed') {
- return t("Feed item");
- }
- if ($contact['network'] === 'mail') {
- return t("Email");
- }
- return t("Friendica Item");
+ if ($item['title']) {
+ return mailstream_decode_subject($item['title']);
+ }
+ $parent = $item['thr-parent'];
+ // Don't look more than 100 levels deep for a subject, in case of loops
+ for ($i = 0; ($i < 100) && $parent; $i++) {
+ $r = q("SELECT `thr-parent`, `title` FROM `item` WHERE `uri` = '%s'", dbesc($parent));
+ if (!DBM::is_result($r)) {
+ break;
+ }
+ if ($r[0]['thr-parent'] === $parent) {
+ break;
+ }
+ if ($r[0]['title']) {
+ return L10n::t('Re:') . ' ' . mailstream_decode_subject($r[0]['title']);
+ }
+ $parent = $r[0]['thr-parent'];
+ }
+ $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d",
+ intval($item['contact-id']), intval($item['uid']));
+ $contact = $r[0];
+ if ($contact['network'] === 'dfrn') {
+ return L10n::t("Friendica post");
+ }
+ if ($contact['network'] === 'dspr') {
+ return L10n::t("Diaspora post");
+ }
+ if ($contact['network'] === 'face') {
+ $text = mailstream_decode_subject($item['body']);
+ // For some reason these do show up in Facebook
+ $text = preg_replace('/\xA0$/', '', $text);
+ $subject = (strlen($text) > 150) ? (substr($text, 0, 140) . '...') : $text;
+ return preg_replace('/\\s+/', ' ', $subject);
+ }
+ if ($contact['network'] === 'feed') {
+ return L10n::t("Feed item");
+ }
+ if ($contact['network'] === 'mail') {
+ return L10n::t("Email");
+ }
+ return L10n::t("Friendica Item");
+}
+
+function mailstream_send($a, $message_id, $item, $user) {
+ if (!$item['visible']) {
+ return;
+ }
+ if (!$message_id) {
+ return;
+ }
+ require_once(dirname(__file__).'/phpmailer/class.phpmailer.php');
+
+ $attachments = [];
+ mailstream_do_images($a, $item, $attachments);
+ $frommail = Config::get('mailstream', 'frommail');
+ if ($frommail == "") {
+ $frommail = 'friendica@localhost.local';
+ }
+ $address = PConfig::get($item['uid'], 'mailstream', 'address');
+ if (!$address) {
+ $address = $user['email'];
+ }
+ $mail = new PHPmailer;
+ try {
+ $mail->XMailer = 'Friendica Mailstream Addon';
+ $mail->SetFrom($frommail, mailstream_sender($item));
+ $mail->AddAddress($address, $user['username']);
+ $mail->MessageID = $message_id;
+ $mail->Subject = mailstream_subject($item);
+ if ($item['thr-parent'] != $item['uri']) {
+ $mail->addCustomHeader('In-Reply-To: ' . mailstream_generate_id($a, $item['thr-parent']));
+ }
+ $mail->addCustomHeader('X-Friendica-Mailstream-URI: ' . $item['uri']);
+ $mail->addCustomHeader('X-Friendica-Mailstream-Plink: ' . $item['plink']);
+ $encoding = 'base64';
+ foreach ($attachments as $url => $image) {
+ $mail->AddStringEmbeddedImage($image['data'], $image['guid'], $image['filename'], $encoding, $image['type']);
+ }
+ $mail->IsHTML(true);
+ $mail->CharSet = 'utf-8';
+ $template = get_markup_template('mail.tpl', 'addon/mailstream/');
+ $item['body'] = BBCode::convert($item['body']);
+ $item['url'] = $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $item['id'];
+ $mail->Body = replace_macros($template, [
+ '$upstream' => L10n::t('Upstream'),
+ '$local' => L10n::t('Local'),
+ '$item' => $item]);
+ mailstream_html_wrap($mail->Body);
+ if (!$mail->Send()) {
+ throw new Exception($mail->ErrorInfo);
+ }
+ logger('mailstream_send sent message ' . $mail->MessageID . ' ' . $mail->Subject, LOGGER_DEBUG);
+ } catch (phpmailerException $e) {
+ logger('mailstream_send PHPMailer exception sending message ' . $message_id . ': ' . $e->errorMessage(), LOGGER_NORMAL);
+ } catch (Exception $e) {
+ logger('mailstream_send exception sending message ' . $message_id . ': ' . $e->getMessage(), LOGGER_NORMAL);
+ }
+ // In case of failure, still set the item to completed. Otherwise
+ // we'll just try to send it over and over again and it'll fail
+ // every time.
+ q('UPDATE `mailstream_item` SET `completed` = now() WHERE `message-id` = "%s"', dbesc($message_id));