]> git.mxchange.org Git - friendica-addons.git/blobdiff - mailstream/mailstream.php
Merge pull request #140 from repat/master
[friendica-addons.git] / mailstream / mailstream.php
index a0cd9f1e23426f05937435b3551edbd13409a1e9..047603852df9b7142ed26a641683cafccb0375ba 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * Name: Mail Stream
  * Description: Mail all items coming into your network feed to an email address
- * Version: 0.1
+ * Version: 0.2
  * Author: Matthew Exon <http://mat.exon.name>
  */
 
@@ -31,7 +31,10 @@ function mailstream_install() {
         q('ALTER TABLE `mailstream_item` CHANGE `created` `created` timestamp NOT NULL DEFAULT now()');
         q('ALTER TABLE `mailstream_item` CHANGE `completed` `completed` timestamp NULL DEFAULT NULL');
     }
-    set_config('mailstream', 'dbversion', '0.4');
+    if (get_config('mailstream', 'dbversion') == '0.4') {
+        q('ALTER TABLE `mailstream_item` CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin');
+    }
+    set_config('mailstream', 'dbversion', '0.5');
 }
 
 function mailstream_uninstall() {
@@ -47,12 +50,14 @@ function mailstream_module() {}
 
 function mailstream_plugin_admin(&$a,&$o) {
     $frommail = get_config('mailstream', 'frommail');
-    $template = file_get_contents(dirname(__file__).'/admin.tpl');
+    $template = get_markup_template('admin.tpl', 'addon/mailstream/');
     $config = array('frommail',
                     t('From Address'),
                     $frommail,
                     t('Email address that stream items will appear to be from.'));
-    $o .= replace_macros($template, array('$frommail' => $config));
+    $o .= replace_macros($template, array(
+                             '$frommail' => $config,
+                             '$submit' => t('Submit')));
 }
 
 function mailstream_plugin_admin_post ($a) {
@@ -69,29 +74,46 @@ function mailstream_generate_id($a, $uri) {
 }
 
 function mailstream_post_remote_hook(&$a, &$item) {
-    if (get_pconfig($item['uid'], 'mailstream', 'enabled') === 'on') {
-        if ($item['uid'] && $item['contact-id'] && $item['uri']) {
-            q("INSERT INTO `mailstream_item` (`uid`, `contact-id`, `uri`, `message-id`) " .
-              "VALUES (%d, '%s', '%s', '%s')", intval($item['uid']),
-              intval($item['contact-id']), dbesc($item['uri']), dbesc(mailstream_generate_id($a, $item['uri'])));
-            $r = q('SELECT * FROM `mailstream_item` WHERE `uid` = %d AND `contact-id` = %d AND `uri` = "%s"', intval($item['uid']), intval($item['contact-id']), dbesc($item['uri']));
-            if (count($r) != 1) {
-                logger('mailstream_post_remote_hook: Unexpected number of items returned from mailstream_item', LOGGER_ERROR);
-                return;
-            }
-            $ms_item = $r[0];
-            logger('mailstream_post_remote_hook: created mailstream_item '
-                   . $ms_item['id'] . ' for item ' . $item['uri'] . ' '
-                   . $item['uid'] . ' ' . $item['contact-id'], LOGGER_DATA);
-            $r = q('SELECT * FROM `user` WHERE `uid` = %d', intval($item['uid']));
-            if (count($r) != 1) {
-                logger('mailstream_post_remote_hook: Unexpected number of users returned', LOGGER_ERROR);
-                return;
-            }
-            $user = $r[0];
-            mailstream_send($a, $ms_item, $item, $user);
-        }
+    if (!get_pconfig($item['uid'], 'mailstream', 'enabled')) {
+        return;
+    }
+    if (!$item['uid']) {
+        return;
+    }
+    if (!$item['contact-id']) {
+        return;
+    }
+    if (!$item['uri']) {
+        return;
+    }
+
+    q("INSERT INTO `mailstream_item` (`uid`, `contact-id`, `uri`, `message-id`) " .
+      "VALUES (%d, '%s', '%s', '%s')", intval($item['uid']),
+      intval($item['contact-id']), dbesc($item['uri']), dbesc(mailstream_generate_id($a, $item['uri'])));
+    $r = q('SELECT * FROM `mailstream_item` WHERE `uid` = %d AND `contact-id` = %d AND `uri` = "%s"', intval($item['uid']), intval($item['contact-id']), dbesc($item['uri']));
+    if (count($r) != 1) {
+        logger('mailstream_post_remote_hook: Unexpected number of items returned from mailstream_item', LOGGER_NORMAL);
+        return;
+    }
+    $ms_item = $r[0];
+    logger('mailstream_post_remote_hook: created mailstream_item '
+           . $ms_item['id'] . ' for item ' . $item['uri'] . ' '
+           . $item['uid'] . ' ' . $item['contact-id'], LOGGER_DATA);
+    $user = mailstream_get_user($item['uid']);
+    if (!$user) {
+        logger('mailstream_post_remote_hook: no user ' . $item['uid'], LOGGER_NORMAL);
+        return;
     }
+    mailstream_send($a, $ms_item, $item, $user);
+}
+
+function mailstream_get_user($uid) {
+    $r = q('SELECT * FROM `user` WHERE `uid` = %d', intval($uid));
+    if (count($r) != 1) {
+        logger('mailstream_post_remote_hook: Unexpected number of users returned', LOGGER_NORMAL);
+        return;
+    }
+    return $r[0];
 }
 
 function mailstream_do_images($a, &$item, &$attachments) {
@@ -178,12 +200,15 @@ function mailstream_send($a, $ms_item, $item, $user) {
     if ($frommail == "") {
         $frommail = 'friendica@localhost.local';
     }
-    $email = get_pconfig($item['uid'], 'mailstream', 'address');
+    $address = get_pconfig($item['uid'], 'mailstream', 'address');
+    if (!$address) {
+        $address = $user['email'];
+    }
     $mail = new PHPmailer;
     try {
         $mail->XMailer = 'Friendica Mailstream Plugin';
         $mail->SetFrom($frommail, $item['author-name']);
-        $mail->AddAddress($email, $user['username']);
+        $mail->AddAddress($address, $user['username']);
         $mail->MessageID = $ms_item['message-id'];
         $mail->Subject = mailstream_subject($item);
         if ($item['thr-parent'] != $item['uri']) {
@@ -197,17 +222,21 @@ function mailstream_send($a, $ms_item, $item, $user) {
         }
         $mail->IsHTML(true);
         $mail->CharSet = 'utf-8';
-        $template = file_get_contents(dirname(__file__).'/mail.tpl');
+        $template = get_markup_template('mail.tpl', 'addon/mailstream/');
         $item['body'] = bbcode($item['body']);
-        $mail->Body = replace_macros($template, array('$item' => $item));
+        $item['url'] = $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $item['id'];
+        $mail->Body = replace_macros($template, array(
+                                         '$upstream' => t('Upstream'),
+                                         '$local' => t('Local'),
+                                         '$item' => $item));
         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 ' . $ms_item['message-id'] . ': ' . $e->errorMessage(), LOGGER_ERROR);
+        logger('mailstream_send PHPMailer exception sending message ' . $ms_item['message-id'] . ': ' . $e->errorMessage(), LOGGER_NORMAL);
     } catch (Exception $e) {
-        logger('mailstream_send exception sending message ' . $ms_item['message-id'] . ': ' . $e->getMessage(), LOGGER_ERROR);
+        logger('mailstream_send exception sending message ' . $ms_item['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
@@ -228,7 +257,7 @@ function mailstream_cron($a, $b) {
             mailstream_send($a, $ms_item, $item, $user);
         }
         else {
-            logger('mailstream_cron: Unable to find item ' . $ms_item['uri'], LOGGER_ERROR);
+            logger('mailstream_cron: Unable to find item ' . $ms_item['uri'], LOGGER_NORMAL);
             q("UPDATE `mailstream_item` SET `completed` = now() WHERE `id` = %d", intval($ms_item['id']));
         }
     }
@@ -237,22 +266,31 @@ function mailstream_cron($a, $b) {
 
 function mailstream_plugin_settings(&$a,&$s) {
     $enabled = get_pconfig(local_user(), 'mailstream', 'enabled');
-    $enabled_mu = ($enabled === 'on') ? ' checked="true"' : '';
     $address = get_pconfig(local_user(), 'mailstream', 'address');
-    $address_mu = $address ? (' value="' . $address . '"') : '';
-    $template = file_get_contents(dirname(__file__).'/settings.tpl');
-    $s .= replace_macros($template, array('$address' => $address_mu,
-                                          '$address_caption' => t('Address:'),
-                                          '$enabled' => $enabled_mu,
-                                          '$enabled_caption' => t('Enabled:')));
+    $template = get_markup_template('settings.tpl', 'addon/mailstream/');
+    $s .= replace_macros($template, array(
+                             '$address' => array(
+                                 'mailstream_address',
+                                 t('Email Address'),
+                                 $address,
+                                 t("Leave blank to use your account email address")),
+                             '$enabled' => array(
+                                 'mailstream_enabled',
+                                 t('Enabled'),
+                                 $enabled),
+                             '$title' => t('Mail Stream Settings'),
+                             '$submit' => t('Submit')));
 }
 
 function mailstream_plugin_settings_post($a,$post) {
-    if ($_POST['address'] != "") {
-        set_pconfig(local_user(), 'mailstream', 'address', $_POST['address']);
+    if ($_POST['mailstream_address'] != "") {
+        set_pconfig(local_user(), 'mailstream', 'address', $_POST['mailstream_address']);
+    }
+    else {
+        del_pconfig(local_user(), 'mailstream', 'address');
     }
-    if ($_POST['enabled']) {
-        set_pconfig(local_user(), 'mailstream', 'enabled', $_POST['enabled']);
+    if ($_POST['mailstream_enabled']) {
+        set_pconfig(local_user(), 'mailstream', 'enabled', $_POST['mailstream_enabled']);
     }
     else {
         del_pconfig(local_user(), 'mailstream', 'enabled');