X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=mailstream%2Fmailstream.php;h=a58dfc1cde33af486096a579e4a20b9c752c3174;hb=80ec21c76bc7a0cc5f1e73acf91db16ceaa06b04;hp=4f86e7045bbc84c4d8c741d8b743333df981711a;hpb=37dd8a938bb947d8b8f463dd58d377b40654bba6;p=friendica-addons.git diff --git a/mailstream/mailstream.php b/mailstream/mailstream.php index 4f86e704..a58dfc1c 100644 --- a/mailstream/mailstream.php +++ b/mailstream/mailstream.php @@ -96,25 +96,34 @@ function mailstream_generate_id($a, $uri) { $host = $a->getHostName(); $resource = hash('md5', $uri); $message_id = "<" . $resource . "@" . $host . ">"; - Logger::log('mailstream: Generated message ID ' . $message_id . ' for URI ' . $uri, Logger::DEBUG); + Logger::debug('mailstream: Generated message ID ' . $message_id . ' for URI ' . $uri); return $message_id; } function mailstream_post_hook(&$a, &$item) { if (!PConfig::get($item['uid'], 'mailstream', 'enabled')) { + Logger::debug('mailstream: not enabled for item ' . $item['id']); return; } if (!$item['uid']) { + Logger::debug('mailstream: no uid for item ' . $item['id']); return; } if (!$item['contact-id']) { + Logger::debug('mailstream: no contact-id for item ' . $item['id']); return; } if (!$item['uri']) { + Logger::debug('mailstream: no uri for item ' . $item['id']); + return; + } + if (!$item['plink']) { + Logger::debug('mailstream: no plink for item ' . $item['id']); return; } if (PConfig::get($item['uid'], 'mailstream', 'nolikes')) { if ($item['verb'] == ACTIVITY_LIKE) { + Logger::debug('mailstream: like item ' . $item['id']); return; } } @@ -125,16 +134,14 @@ function mailstream_post_hook(&$a, &$item) { intval($item['contact-id']), DBA::escape($item['uri']), DBA::escape($message_id)); $r = q('SELECT * FROM `mailstream_item` WHERE `uid` = %d AND `contact-id` = %d AND `uri` = "%s"', intval($item['uid']), intval($item['contact-id']), DBA::escape($item['uri'])); if (count($r) != 1) { - Logger::log('mailstream_post_remote_hook: Unexpected number of items returned from mailstream_item', Logger::INFO); + Logger::info('mailstream_post_remote_hook: Unexpected number of items returned from mailstream_item'); return; } $ms_item = $r[0]; - Logger::log('mailstream_post_remote_hook: created mailstream_item ' - . $ms_item['id'] . ' for item ' . $item['uri'] . ' ' - . $item['uid'] . ' ' . $item['contact-id'], Logger::DATA); + Logger::debug('mailstream_post_remote_hook: created mailstream_item ' . $ms_item['id'] . ' for item ' . $item['uri'] . ' ' . $item['uid'] . ' ' . $item['contact-id']); $user = mailstream_get_user($item['uid']); if (!$user) { - Logger::log('mailstream_post_remote_hook: no user ' . $item['uid'], Logger::INFO); + Logger::info('mailstream_post_remote_hook: no user ' . $item['uid']); return; } mailstream_send($a, $ms_item['message-id'], $item, $user); @@ -143,7 +150,7 @@ function mailstream_post_hook(&$a, &$item) { function mailstream_get_user($uid) { $r = q('SELECT * FROM `user` WHERE `uid` = %d', intval($uid)); if (count($r) != 1) { - Logger::log('mailstream_post_remote_hook: Unexpected number of users returned', Logger::INFO); + Logger::info('mailstream_post_remote_hook: Unexpected number of users returned'); return; } return $r[0]; @@ -156,14 +163,18 @@ function mailstream_do_images($a, &$item, &$attachments) { $attachments = []; preg_match_all("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", $item["body"], $matches1); preg_match_all("/\[img\](.*?)\[\/img\]/ism", $item["body"], $matches2); - foreach (array_merge($matches1[3], $matches2[1]) as $url) { - $redirects = 0; + preg_match_all("/\[img\=([^\]]*)\]([^[]*)\[\/img\]/ism", $item["body"], $matches3); + foreach (array_merge($matches1[3], $matches2[1], $matches3[1]) as $url) { + $components = parse_url($url); + if (!$components) { + continue; + } $cookiejar = tempnam(get_temppath(), 'cookiejar-mailstream-'); - $curlResult = Network::fetchUrlFull($url, true, $redirects, 0, null, $cookiejar); + $curlResult = Network::fetchUrlFull($url, true, 0, '', $cookiejar); $attachments[$url] = [ 'data' => $curlResult->getBody(), 'guid' => hash("crc32", $url), - 'filename' => basename($url), + 'filename' => basename($components['path']), 'type' => $curlResult->getContentType() ]; @@ -254,7 +265,7 @@ function mailstream_subject($item) { return L10n::t("Friendica Item"); } -function mailstream_send($a, $message_id, $item, $user) { +function mailstream_send(\Friendica\App $a, $message_id, $item, $user) { if (!$item['visible']) { return; } @@ -292,8 +303,9 @@ function mailstream_send($a, $message_id, $item, $user) { $mail->IsHTML(true); $mail->CharSet = 'utf-8'; $template = Renderer::getMarkupTemplate('mail.tpl', 'addon/mailstream/'); + $mail->AltBody = BBCode::toPlaintext($item['body']); $item['body'] = BBCode::convert($item['body']); - $item['url'] = $a->getBaseURL() . '/display/' . $user['nickname'] . '/' . $item['id']; + $item['url'] = $a->getBaseURL() . '/display/' . $item['guid']; $mail->Body = Renderer::replaceMacros($template, [ '$upstream' => L10n::t('Upstream'), '$local' => L10n::t('Local'), @@ -302,11 +314,11 @@ function mailstream_send($a, $message_id, $item, $user) { if (!$mail->Send()) { throw new Exception($mail->ErrorInfo); } - Logger::log('mailstream_send sent message ' . $mail->MessageID . ' ' . $mail->Subject, Logger::DEBUG); + Logger::debug('mailstream_send sent message ' . $mail->MessageID . ' ' . $mail->Subject); } catch (phpmailerException $e) { - Logger::log('mailstream_send PHPMailer exception sending message ' . $message_id . ': ' . $e->errorMessage(), Logger::INFO); + Logger::debug('mailstream_send PHPMailer exception sending message ' . $message_id . ': ' . $e->errorMessage()); } catch (Exception $e) { - Logger::log('mailstream_send exception sending message ' . $message_id . ': ' . $e->getMessage(), Logger::INFO); + Logger::debug('mailstream_send exception sending message ' . $message_id . ': ' . $e->getMessage()); } // 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 @@ -335,10 +347,10 @@ function mailstream_cron($a, $b) { // mailstream_post_remote_hook fails for some reason will this get // used, and in that case it's worth holding off a bit anyway. $ms_item_ids = q("SELECT `mailstream_item`.`message-id`, `mailstream_item`.`uri`, `item`.`id` FROM `mailstream_item` JOIN `item` ON (`mailstream_item`.`uid` = `item`.`uid` AND `mailstream_item`.`uri` = `item`.`uri` AND `mailstream_item`.`contact-id` = `item`.`contact-id`) WHERE `mailstream_item`.`completed` IS NULL AND `mailstream_item`.`created` < DATE_SUB(NOW(), INTERVAL 1 HOUR) AND `item`.`visible` = 1 ORDER BY `mailstream_item`.`created` LIMIT 100"); - Logger::log('mailstream_cron processing ' . count($ms_item_ids) . ' items', Logger::DEBUG); + Logger::debug('mailstream_cron processing ' . count($ms_item_ids) . ' items'); foreach ($ms_item_ids as $ms_item_id) { if (!$ms_item_id['message-id'] || !strlen($ms_item_id['message-id'])) { - Logger::log('mailstream_cron: Item ' . $ms_item_id['id'] . ' URI ' . $ms_item_id['uri'] . ' has no message-id', Logger::INFO); + Logger::info('mailstream_cron: Item ' . $ms_item_id['id'] . ' URI ' . $ms_item_id['uri'] . ' has no message-id'); } $item = Item::selectFirst([], ['id' => $ms_item_id['id']]); $users = q("SELECT * FROM `user` WHERE `uid` = %d", intval($item['uid'])); @@ -347,7 +359,7 @@ function mailstream_cron($a, $b) { mailstream_send($a, $ms_item_id['message-id'], $item, $user); } else { - Logger::log('mailstream_cron: Unable to find item ' . $ms_item_id['id'], Logger::INFO); + Logger::info('mailstream_cron: Unable to find item ' . $ms_item_id['id']); q("UPDATE `mailstream_item` SET `completed` = now() WHERE `message-id` = %d", intval($ms_item['message-id'])); } } @@ -416,5 +428,5 @@ function mailstream_tidy() { foreach ($r as $rr) { q('DELETE FROM mailstream_item WHERE id = %d', intval($rr['id'])); } - Logger::log('mailstream_tidy: deleted ' . count($r) . ' old items', Logger::DEBUG); + Logger::debug('mailstream_tidy: deleted ' . count($r) . ' old items'); }