aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorFriendika <info@friendika.com>2011-04-17 23:28:39 -0700
committerFriendika <info@friendika.com>2011-04-17 23:28:39 -0700
commit229eb9c681d62edc5faa63bb1d66046d123c3f35 (patch)
tree0b71e4517975cad460160a1b59666d1e37f107c5 /include
parentc347a5d6c95fd34efd6a11df6df444d6fc7545a2 (diff)
parentab099e91028122dfb6b10cf9510b1b061f6f547f (diff)
downloadvolse-hubzilla-229eb9c681d62edc5faa63bb1d66046d123c3f35.tar.gz
volse-hubzilla-229eb9c681d62edc5faa63bb1d66046d123c3f35.tar.bz2
volse-hubzilla-229eb9c681d62edc5faa63bb1d66046d123c3f35.zip
Merge branch 'imap'
Diffstat (limited to 'include')
-rw-r--r--include/Scrape.php42
-rw-r--r--include/conversation.php10
-rw-r--r--include/email.php136
-rw-r--r--include/group.php3
-rw-r--r--include/notifier.php91
-rw-r--r--include/poller.php92
6 files changed, 331 insertions, 43 deletions
diff --git a/include/Scrape.php b/include/Scrape.php
index e5c10a9be..a6bb5f728 100644
--- a/include/Scrape.php
+++ b/include/Scrape.php
@@ -264,6 +264,7 @@ function scrape_feed($url) {
function probe_url($url) {
+ require_once('include/email.php');
$result = array();
@@ -313,8 +314,43 @@ function probe_url($url) {
}
}
else {
+
+ // Check email
+
+ $orig_url = $url;
if((strpos($orig_url,'@')) && validate_email($orig_url)) {
- $email_conversant = true;
+ $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
+ intval(local_user())
+ );
+ $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
+ intval(local_user())
+ );
+ if(count($x) && count($r)) {
+ $mailbox = construct_mailbox_name($r[0]);
+ $password = '';
+ openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
+ $mbox = email_connect($mailbox,$r[0]['user'],$password);
+ unset($password);
+ }
+ if($mbox) {
+ $msgs = email_poll($mbox,$orig_url);
+ if(count($msgs)) {
+ $addr = $orig_url;
+ $network = NETWORK_MAIL;
+ $name = substr($url,0,strpos($url,'@'));
+ $profile = 'http://' . substr($url,strpos($url,'@')+1);
+ // fix nick character range
+ $vcard = array('fn' => $name, 'nick' => $name, 'photo' => gravatar_img($url));
+ $notify = 'smtp ' . random_string();
+ $poll = 'email ' . random_string();
+ $priority = 0;
+ $x = email_msg_meta($mbox,$msgs[0]);
+ $adr = imap_rfc822_parse_adrlist($x->from,'');
+ if(strlen($adr[0]->personal))
+ $vcard['fn'] = notags($adr[0]->personal);
+ }
+ imap_close($mbox);
+ }
}
}
}
@@ -330,7 +366,7 @@ function probe_url($url) {
}
}
- if($network !== NETWORK_DFRN) {
+ if($network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
$network = NETWORK_OSTATUS;
$priority = 0;
@@ -435,9 +471,11 @@ function probe_url($url) {
$vcard['fn'] = notags($vcard['fn']);
$vcard['nick'] = notags($vcard['nick']);
+
$result['name'] = $vcard['fn'];
$result['nick'] = $vcard['nick'];
$result['url'] = $profile;
+ $result['addr'] = $addr;
$result['notify'] = $notify;
$result['poll'] = $poll;
$result['request'] = $request;
diff --git a/include/conversation.php b/include/conversation.php
index b2d20300f..4c858d818 100644
--- a/include/conversation.php
+++ b/include/conversation.php
@@ -85,6 +85,8 @@ function conversation(&$a, $items, $mode, $update) {
$profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
$profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
$profile_link = ((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
+ if($profile_link === 'mailbox')
+ $profile_link = '';
$redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
@@ -360,6 +362,9 @@ function conversation(&$a, $items, $mode, $update) {
else
$profile_link = $item['url'];
+ if($profile_link === 'mailbox')
+ $profile_link = '';
+
$like = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
$dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
@@ -483,6 +488,9 @@ function item_photo_menu($item){
$profile_link = ((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
$redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
+ if($profile_link === 'mailbox')
+ $profile_link = '';
+
// $item['contact-uid'] is only set on profile page and indicates the uid of the user who owns the profile.
$profile_owner = ((x($item,'contact-uid')) && intval($item['contact-uid']) ? intval($item['contact-uid']) : 0);
@@ -503,6 +511,8 @@ function item_photo_menu($item){
$redir = $a->get_baseurl() . '/redir/' . $a->authors[$item['author-link']]['id'];
$cid = $a->authors[$item['author-link']]['id'];
}
+ if($item['author-link'] === 'mailbox')
+ $cid = $item['cid'];
if((isset($cid)) && (! $item['self'])) {
$contact_url = $a->get_baseurl() . '/contacts/' . $cid;
diff --git a/include/email.php b/include/email.php
index 4d8c25edf..91aafd45e 100644
--- a/include/email.php
+++ b/include/email.php
@@ -1,7 +1,7 @@
<?php
function email_connect($mailbox,$username,$password) {
- if(! (local_user() && function_exists('imap_open')))
+ if(! function_exists('imap_open'))
return false;
$mbox = imap_open($mailbox,$username,$password);
@@ -32,37 +32,63 @@ function email_msg_meta($mbox,$uid) {
return ((count($ret)) ? $ret[0] : array());
}
+function email_msg_headers($mbox,$uid) {
+ $raw_header = (($mbox && $uid) ? imap_fetchheader($mbox,$uid,FT_UID) : '');
+ $raw_header = str_replace("\r",'',$raw_header);
+ $ret = array();
+ $h = split("\n",$raw_header);
+ if(count($h))
+ foreach($h as $line ) {
+ if (preg_match("/^[a-zA-Z]/", $line)) {
+ $key = substr($line,0,strpos($line,':'));
+ $value = substr($line,strpos($line,':')+1);
+
+ $last_entry = strtolower($key);
+ $ret[$last_entry] = trim($value);
+ }
+ else {
+ $ret[$last_entry] .= ' ' . trim($line);
+ }
+ }
+ return $ret;
+}
-function getmsg($mbox,$mid) {
- // input $mbox = IMAP stream, $mid = message id
- // output all the following:
- global $charset,$htmlmsg,$plainmsg,$attachments;
- $htmlmsg = $plainmsg = $charset = '';
- $attachments = array();
+function email_get_msg($mbox,$uid) {
+ $ret = array();
- // HEADER
- $h = imap_header($mbox,$mid);
- // add code here to get date, from, to, cc, subject...
+ $struc = (($mbox && $uid) ? imap_fetchstructure($mbox,$uid,FT_UID) : null);
- // BODY
- $s = imap_fetchstructure($mbox,$mid);
- if (!$s->parts) // simple
- getpart($mbox,$mid,$s,0); // pass 0 as part-number
- else { // multipart: cycle through each part
- foreach ($s->parts as $partno0=>$p)
- getpart($mbox,$mid,$p,$partno0+1);
- }
+ if(! $struc)
+ return $ret;
+
+ if(! $struc->parts) {
+ $ret['body'] = email_get_part($mbox,$uid,$struc,0);
+ }
+ else {
+ foreach($struc->parts as $ptop => $p) {
+ $x = email_get_part($mbox,$uid,$p,$ptop + 1);
+ if($x)
+ $ret['body'] = $x;
+ }
+ }
+ return $ret;
}
-function getpart($mbox,$mid,$p,$partno) {
+// At the moment - only return plain/text.
+// Later we'll repackage inline images as data url's and make the HTML safe
+
+function email_get_part($mbox,$uid,$p,$partno) {
// $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
global $htmlmsg,$plainmsg,$charset,$attachments;
+ echo $partno;
+
// DECODE DATA
- $data = ($partno)?
- imap_fetchbody($mbox,$mid,$partno): // multipart
- imap_body($mbox,$mid); // simple
+ $data = ($partno)
+ ? imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
+ : imap_body($mbox,$uid,FT_UID|FT_PEEK);
+
// Any part may be encoded, even plain text messages, so check everything.
if ($p->encoding==4)
$data = quoted_printable_decode($data);
@@ -82,6 +108,7 @@ function getpart($mbox,$mid,$p,$partno) {
// ATTACHMENT
// Any part with a filename is an attachment,
// so an attached text file (type 0) is not mistaken as the message.
+
if ($params['filename'] || $params['name']) {
// filename may be given as 'Filename' or 'Name' or both
$filename = ($params['filename'])? $params['filename'] : $params['name'];
@@ -90,13 +117,15 @@ function getpart($mbox,$mid,$p,$partno) {
}
// TEXT
- if ($p->type==0 && $data) {
+ if ($p->type == 0 && $data) {
// Messages may be split in different parts because of inline attachments,
// so append parts together with blank row.
if (strtolower($p->subtype)=='plain')
- $plainmsg .= trim($data) ."\n\n";
+ return (trim($data) ."\n\n");
else
- $htmlmsg .= $data ."<br><br>";
+ $data = '';
+
+ // $htmlmsg .= $data ."<br><br>";
$charset = $params['charset']; // assume all parts are same charset
}
@@ -105,13 +134,58 @@ function getpart($mbox,$mid,$p,$partno) {
// but AOL uses type 1 (multipart), which is not handled here.
// There are no PHP functions to parse embedded messages,
// so this just appends the raw source to the main message.
- elseif ($p->type==2 && $data) {
- $plainmsg .= $data."\n\n";
- }
+// elseif ($p->type==2 && $data) {
+// $plainmsg .= $data."\n\n";
+// }
// SUBPART RECURSION
if ($p->parts) {
- foreach ($p->parts as $partno0=>$p2)
- getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1)); // 1.2, 1.2.1, etc.
+ foreach ($p->parts as $partno0=>$p2) {
+ $x = email_get_part($mbox,$uid,$p2,$partno . '.' . ($partno0+1)); // 1.2, 1.2.1, etc.
+ if($x)
+ return $x;
+ }
+ }
+}
+
+
+
+function email_header_encode($in_str, $charset) {
+ $out_str = $in_str;
+ if ($out_str && $charset) {
+
+ // define start delimimter, end delimiter and spacer
+ $end = "?=";
+ $start = "=?" . $charset . "?B?";
+ $spacer = $end . "\r\n " . $start;
+
+ // determine length of encoded text within chunks
+ // and ensure length is even
+ $length = 75 - strlen($start) - strlen($end);
+
+ /*
+ [EDIT BY danbrown AT php DOT net: The following
+ is a bugfix provided by (gardan AT gmx DOT de)
+ on 31-MAR-2005 with the following note:
+ "This means: $length should not be even,
+ but divisible by 4. The reason is that in
+ base64-encoding 3 8-bit-chars are represented
+ by 4 6-bit-chars. These 4 chars must not be
+ split between two encoded words, according
+ to RFC-2047.
+ */
+ $length = $length - ($length % 4);
+
+ // encode the string and split it into chunks
+ // with spacers after each chunk
+ $out_str = base64_encode($out_str);
+ $out_str = chunk_split($out_str, $length, $spacer);
+
+ // remove trailing spacer and
+ // add start and end delimiters
+ $spacer = preg_quote($spacer);
+ $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
+ $out_str = $start . $out_str . $end;
}
-} \ No newline at end of file
+ return $out_str;
+} \ No newline at end of file
diff --git a/include/group.php b/include/group.php
index 065c2241f..5dca08c76 100644
--- a/include/group.php
+++ b/include/group.php
@@ -123,7 +123,8 @@ function group_public_members($gid) {
if(intval($gid)) {
$r = q("SELECT `contact`.`id` AS `contact-id` FROM `group_member`
LEFT JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
- WHERE `gid` = %d AND `group_member`.`uid` = %d AND `contact`.`network` != 'dfrn' ",
+ WHERE `gid` = %d AND `group_member`.`uid` = %d
+ AND `contact`.`network` != 'dfrn' AND `contact`.`network` != 'mail' ",
intval($gid),
intval(local_user())
);
diff --git a/include/notifier.php b/include/notifier.php
index 9d74f8b9b..80f0e67c7 100644
--- a/include/notifier.php
+++ b/include/notifier.php
@@ -72,7 +72,7 @@ function notifier_run($argv, $argc){
else {
// find ancestors
- $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
+ $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
intval($item_id)
);
@@ -80,6 +80,7 @@ function notifier_run($argv, $argc){
return;
}
+ $parent_item = $r[0];
$parent_id = intval($r[0]['parent']);
$uid = $r[0]['uid'];
$updated = $r[0]['edited'];
@@ -267,12 +268,25 @@ function notifier_run($argv, $argc){
logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
+ // If this is a public message and pubmail is set on the parent, include all your email contacts
+
+ if((! strlen($parent_item['allow_cid'])) && (! strlen($parent_item['allow_gid'])) && (! strlen($parent_item['deny_cid'])) && (! strlen($parent_item['deny_gid']))
+ && (intval($parent_item['pubmail']))) {
+ $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
+ intval($uid),
+ dbesc(NETWORK_MAIL)
+ );
+ if(count($r)) {
+ foreach($r as $rr)
+ $recipients[] = $rr['id'];
+ }
+ }
+
if($followup)
$recip_str = $parent['contact-id'];
else
$recip_str = implode(', ', $recipients);
-
$r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
dbesc($recip_str)
);
@@ -352,6 +366,79 @@ function notifier_run($argv, $argc){
}
break;
case 'mail':
+
+ // WARNING: does not currently convert to RFC2047 header encodings, etc.
+
+ $addr = $contact['addr'];
+ if(! strlen($addr))
+ break;
+
+ if($cmd === 'wall-new' || $cmd === 'comment-new') {
+
+ $it = null;
+ if($cmd === 'wall-new')
+ $it = $items[0];
+ else {
+ $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
+ intval($argv[2]),
+ intval($uid)
+ );
+ if(count($r))
+ $it = $r[0];
+ }
+ if(! $it)
+ break;
+
+
+
+ $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
+ intval($uid)
+ );
+ if(! count($local_user))
+ break;
+
+ $reply_to = '';
+ $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
+ intval($uid)
+ );
+ if($r1 && $r1[0]['reply_to'])
+ $reply_to = $r1[0]['reply_to'];
+
+ $subject = (($it['title']) ? $it['title'] : t("\x28no subject\x29")) ;
+ $headers = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
+
+ if($reply_to)
+ $headers .= 'Reply-to: ' . $reply_to . "\n";
+
+ $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
+
+ if($it['uri'] !== $it['parent-uri']) {
+ $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
+ if(! strlen($it['title'])) {
+ $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1",
+ dbesc($it['parent-uri'])
+ );
+ if(count($r)) {
+ $subtitle = $r[0]['title'];
+ if($subtitle) {
+ if(strncasecmp($subtitle,'RE:',3))
+ $subject = $subtitle;
+ else
+ $subject = 'Re: ' . $subtitle;
+ }
+ }
+ }
+ }
+
+ $headers .= 'MIME-Version: 1.0' . "\n";
+ $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
+ $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
+ $html = prepare_body($it);
+ $message = '<html><body>' . $html . '</body></html>';
+ logger('notifier: email delivery to ' . $addr);
+ mail($addr, $subject, $message, $headers);
+ }
+ break;
case 'dspr':
case 'feed':
case 'face':
diff --git a/include/poller.php b/include/poller.php
index 37109f17d..4e1e30ad5 100644
--- a/include/poller.php
+++ b/include/poller.php
@@ -289,24 +289,99 @@ function poller_run($argv, $argc){
}
elseif($contact['network'] === NETWORK_MAIL) {
if(! $mbox) {
- $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = $d LIMIT 1",
+ $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
intval($importer_uid)
);
- $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
+ $mailconf = q("SELECT * FROM `mailacct` WHERE `server` != '' AND `uid` = %d LIMIT 1",
intval($importer_uid)
);
- if(count($x) && count($r)) {
- $mailbox = construct_mailbox_name($r[0]);
+ if(count($x) && count($mailconf)) {
+ $mailbox = construct_mailbox_name($mailconf[0]);
$password = '';
- openssl_private_decrypt($r[0]['pass'],$password,$x[0]['prvkey']);
- $mbox = email_connect($mailbox,$r[0]['user'],$password);
+ openssl_private_decrypt(hex2bin($mailconf[0]['pass']),$password,$x[0]['prvkey']);
+ $mbox = email_connect($mailbox,$mailconf[0]['user'],$password);
unset($password);
+ if($mbox) {
+ q("UPDATE `mailacct` SET `last_check` = '%d' WHERE `id` = %d AND `uid` = %d LIMIT 1",
+ dbesc(datetime_convert()),
+ intval($mailconf[0]['id']),
+ intval($importer_uid)
+ );
+ }
}
}
if($mbox) {
+
$msgs = email_poll($mbox,$contact['addr']);
+
if(count($msgs)) {
- // TODO: loop through, fetch, check duplicates, and import
+ foreach($msgs as $msg_uid) {
+ $datarray = array();
+ $meta = email_msg_meta($mbox,$msg_uid);
+ $headers = email_msg_headers($mbox,$msg_uid);
+
+ // look for a 'references' header and try and match with a parent item we have locally.
+
+ $raw_refs = ((x($headers,'references')) ? str_replace("\t",'',$headers['references']) : '');
+ $datarray['uri'] = trim($meta->message_id,'<>');
+
+ if($raw_refs) {
+ $refs_arr = explode(' ', $raw_refs);
+ if(count($refs_arr)) {
+ for($x = 0; $x < count($refs_arr); $x ++)
+ $refs_arr[$x] = "'" . str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x])) . "'";
+ }
+ $qstr = implode(',',$refs_arr);
+ $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( $qstr ) AND `uid` = %d LIMIT 1",
+ intval($importer_uid)
+ );
+ if(count($r))
+ $datarray['parent-uri'] = $r[0]['uri'];
+ }
+
+
+ if(! x($datarray,'parent-uri'))
+ $datarray['parent-uri'] = $datarray['uri'];
+
+ // Have we seen it before?
+ $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
+ intval($importer_uid),
+ dbesc($datarray['uri'])
+ );
+
+ if(count($r)) {
+ if($meta->deleted && ! $r[0]['deleted']) {
+ q("UPDATE `item` SET `deleted` = `, `changed` = '%s' WHERE `id` = %d LIMIT 1",
+ dbesc(datetime_convert()),
+ intval($r[0]['id'])
+ );
+ }
+ continue;
+ }
+ $datarray['title'] = notags(trim($meta->subject));
+ $datarray['created'] = datetime_convert('UTC','UTC',$meta->date);
+
+ $r = email_get_msg($mbox,$msg_uid);
+ if(! $r)
+ continue;
+ $datarray['body'] = escape_tags($r['body']);
+ $datarray['uid'] = $importer_uid;
+ $datarray['contact-id'] = $contact['id'];
+ if($datarray['parent-uri'] === $datarray['uri'])
+ $datarray['private'] = 1;
+ $datarray['author-name'] = $contact['name'];
+ $datarray['author-link'] = 'mailbox';
+ $datarray['author-avatar'] = $contact['photo'];
+
+ $stored_item = item_store($datarray);
+ q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d",
+ dbesc($datarray['parent-uri']),
+ intval($importer_uid)
+ );
+ q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
+ intval($stored_item)
+ );
+ }
}
}
}
@@ -359,6 +434,9 @@ function poller_run($argv, $argc){
// loop - next contact
}
}
+
+ if($mbox && function_exists('imap_close'))
+ imap_close($mbox);
return;
}