aboutsummaryrefslogtreecommitdiffstats
path: root/include/message.php
diff options
context:
space:
mode:
Diffstat (limited to 'include/message.php')
-rw-r--r--include/message.php121
1 files changed, 81 insertions, 40 deletions
diff --git a/include/message.php b/include/message.php
index bfc92cd6d..87975ef6a 100644
--- a/include/message.php
+++ b/include/message.php
@@ -49,14 +49,17 @@ function send_message($uid = 0, $recipient='', $body='', $subject='', $replyto='
// look for any existing conversation structure
+ $conv_guid = '';
+
if(strlen($replyto)) {
$r = q("select convid from mail where channel_id = %d and ( mid = '%s' or parent_mid = '%s' ) limit 1",
intval(local_channel()),
dbesc($replyto),
dbesc($replyto)
);
- if($r)
+ if($r) {
$convid = $r[0]['convid'];
+ }
}
if(! $convid) {
@@ -75,22 +78,28 @@ function send_message($uid = 0, $recipient='', $body='', $subject='', $replyto='
$handles = $recip_handle . ';' . $sender_handle;
+ if($subject)
+ $nsubject = str_rot47(base64url_encode($subject));
+
$r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
intval(local_channel()),
dbesc($conv_guid),
dbesc($sender_handle),
dbesc(datetime_convert()),
dbesc(datetime_convert()),
- dbesc($subject),
+ dbesc($nsubject),
dbesc($handles)
);
+
$r = q("select * from conv where guid = '%s' and uid = %d limit 1",
dbesc($conv_guid),
intval(local_channel())
);
- if($r)
+ if($r) {
$convid = $r[0]['id'];
+ $retconv = $r[0];
+ }
}
if(! $convid) {
@@ -98,6 +107,16 @@ function send_message($uid = 0, $recipient='', $body='', $subject='', $replyto='
return $ret;
}
+ if(! $conv_guid) {
+ $r = q("select * from conv where id = %d and uid = %d limit 1",
+ intval($convid),
+ intval(local_channel())
+ );
+ if($r) {
+ $conv_guid = $r[0]['guid'];
+ $retconv = $r[0];
+ }
+ }
// generate a unique message_id
@@ -163,19 +182,18 @@ function send_message($uid = 0, $recipient='', $body='', $subject='', $replyto='
$jattach = (($attachments) ? json_encode($attachments) : '');
- $key = get_config('system','pubkey');
if($subject)
- $subject = json_encode(crypto_encapsulate($subject,$key));
+ $subject = str_rot47(base64url_encode($subject));
if($body)
- $body = json_encode(crypto_encapsulate($body,$key));
+ $body = str_rot47(base64url_encode($body));
- $r = q("INSERT INTO mail ( account_id, convid, mail_flags, channel_id, from_xchan, to_xchan, title, body, attach, mid, parent_mid, created, expires )
+ $r = q("INSERT INTO mail ( account_id, convid, mail_obscured, channel_id, from_xchan, to_xchan, title, body, attach, mid, parent_mid, created, expires )
VALUES ( %d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
intval($channel['channel_account_id']),
intval($convid),
- intval(MAIL_OBSCURED),
+ intval(1),
intval($channel['channel_id']),
dbesc($channel['channel_hash']),
dbesc($recipient),
@@ -194,8 +212,11 @@ function send_message($uid = 0, $recipient='', $body='', $subject='', $replyto='
dbesc($mid),
intval($channel['channel_id'])
);
- if($r)
+ if($r) {
$post_id = $r[0]['id'];
+ $retmail = $r;
+ $retmail['conv_guid'] = $conv_guid;
+ }
else {
$ret['message'] = t('Stored post could not be verified.');
return $ret;
@@ -213,6 +234,12 @@ function send_message($uid = 0, $recipient='', $body='', $subject='', $replyto='
intval($channel['channel_id']),
dbesc('<' . $channel['channel_hash'] . '>')
);
+ $r = q("UPDATE attach SET allow_cid = '%s' WHERE hash = '%s' AND is_photo = 1 and uid = %d and allow_cid = '%s'",
+ dbesc('<' . $recipient . '>'),
+ dbesc($image_uri),
+ intval($channel['channel_id']),
+ dbesc('<' . $channel['channel_hash'] . '>')
+ );
}
}
@@ -233,6 +260,10 @@ function send_message($uid = 0, $recipient='', $body='', $subject='', $replyto='
$ret['success'] = true;
$ret['message_item'] = intval($post_id);
+ if($retconv)
+ $ret['conv'] = $retconv;
+ if($retmail)
+ $ret['mail'] = $retmail;
return $ret;
}
@@ -242,6 +273,8 @@ function private_messages_list($uid, $mailbox = '', $start = 0, $numitems = 0) {
$where = '';
$limit = '';
+ $t0 = dba_timer();
+
if($numitems)
$limit = " LIMIT " . intval($numitems) . " OFFSET " . intval($start);
@@ -251,18 +284,30 @@ function private_messages_list($uid, $mailbox = '', $start = 0, $numitems = 0) {
);
if(! $x)
return array();
- if($mailbox === 'inbox')
- $where = " and sender_xchan != '" . dbesc($x[0]['channel_hash']) . "' ";
- elseif($mailbox === 'outbox')
- $where = " and sender_xchan = '" . dbesc($x[0]['channel_hash']) . "' ";
+
+ $channel_hash = dbesc($x[0]['channel_hash']);
+ $local_channel = intval(local_channel());
+
+ switch($mailbox) {
+
+ case 'inbox':
+ $sql = "SELECT * FROM mail WHERE channel_id = $local_channel AND from_xchan != '$channel_hash' ORDER BY created DESC $limit";
+ break;
+
+ case 'outbox':
+ $sql = "SELECT * FROM mail WHERE channel_id = $local_channel AND from_xchan = '$channel_hash' ORDER BY created DESC $limit";
+ break;
+
+ case 'combined':
+ $sql = "SELECT * FROM ( SELECT * FROM mail WHERE channel_id = $local_channel ORDER BY created DESC $limit ) AS temp_table GROUP BY parent_mid ORDER BY created DESC";
+ break;
+
+ }
+
}
- // For different orderings, consider applying usort on the results. We thought of doing that
- // inside this function or having some preset sorts, but don't wish to limit app developers.
-
- $r = q("SELECT * from mail WHERE channel_id = %d $where order by created desc $limit",
- intval(local_channel())
- );
+ $r = q($sql);
+
if(! $r) {
return array();
}
@@ -282,13 +327,12 @@ function private_messages_list($uid, $mailbox = '', $start = 0, $numitems = 0) {
foreach($r as $k => $rr) {
$r[$k]['from'] = find_xchan_in_array($rr['from_xchan'],$c);
$r[$k]['to'] = find_xchan_in_array($rr['to_xchan'],$c);
- $r[$k]['seen'] = (($rr['mail_flags'] & MAIL_SEEN) ? 1 : 0);
- if($r[$k]['mail_flags'] & MAIL_OBSCURED) {
- $key = get_config('system','prvkey');
+ $r[$k]['seen'] = intval($rr['mail_seen']);
+ if(intval($r[$k]['mail_obscured'])) {
if($r[$k]['title'])
- $r[$k]['title'] = crypto_unencapsulate(json_decode_plus($r[$k]['title']),$key);
+ $r[$k]['title'] = base64url_decode(str_rot47($r[$k]['title']));
if($r[$k]['body'])
- $r[$k]['body'] = crypto_unencapsulate(json_decode_plus($r[$k]['body']),$key);
+ $r[$k]['body'] = base64url_decode(str_rot47($r[$k]['body']));
}
}
@@ -322,19 +366,17 @@ function private_messages_fetch_message($channel_id, $messageitem_id, $updatesee
foreach($messages as $k => $message) {
$messages[$k]['from'] = find_xchan_in_array($message['from_xchan'],$c);
$messages[$k]['to'] = find_xchan_in_array($message['to_xchan'],$c);
- if($messages[$k]['mail_flags'] & MAIL_OBSCURED) {
- $key = get_config('system','prvkey');
+ if(intval($messages[$k]['mail_obscured'])) {
if($messages[$k]['title'])
- $messages[$k]['title'] = crypto_unencapsulate(json_decode_plus($messages[$k]['title']),$key);
+ $messages[$k]['title'] = base64url_decode(str_rot47($messages[$k]['title']));
if($messages[$k]['body'])
- $messages[$k]['body'] = crypto_unencapsulate(json_decode_plus($messages[$k]['body']),$key);
+ $messages[$k]['body'] = base64url_decode(str_rot47($messages[$k]['body']));
}
}
+
if($updateseen) {
- $r = q("UPDATE `mail` SET mail_flags = (mail_flags | %d) where not (mail_flags & %d)>0 and id = %d AND channel_id = %d",
- intval(MAIL_SEEN),
- intval(MAIL_SEEN),
+ $r = q("UPDATE `mail` SET mail_seen = 1 where mail_seen = 0 and id = %d AND channel_id = %d",
dbesc($messageitem_id),
intval($channel_id)
);
@@ -349,11 +391,12 @@ function private_messages_drop($channel_id, $messageitem_id, $drop_conversation
if($drop_conversation) {
// find the parent_id
- $p = q("SELECT parent_mid FROM mail WHERE id = %d AND channel_id = %d LIMIT 1",
+ $p = q("SELECT parent_mid, convid FROM mail WHERE id = %d AND channel_id = %d LIMIT 1",
intval($messageitem_id),
intval($channel_id)
);
if($p) {
+
$r = q("DELETE FROM mail WHERE parent_mid = '%s' AND channel_id = %d ",
dbesc($p[0]['parent_mid']),
intval($channel_id)
@@ -410,20 +453,18 @@ function private_messages_fetch_conversation($channel_id, $messageitem_id, $upda
foreach($messages as $k => $message) {
$messages[$k]['from'] = find_xchan_in_array($message['from_xchan'],$c);
$messages[$k]['to'] = find_xchan_in_array($message['to_xchan'],$c);
- if($messages[$k]['mail_flags'] & MAIL_OBSCURED) {
- $key = get_config('system','prvkey');
+ if(intval($messages[$k]['mail_obscured'])) {
if($messages[$k]['title'])
- $messages[$k]['title'] = crypto_unencapsulate(json_decode_plus($messages[$k]['title']),$key);
+ $messages[$k]['title'] = base64url_decode(str_rot47($messages[$k]['title']));
if($messages[$k]['body'])
- $messages[$k]['body'] = crypto_unencapsulate(json_decode_plus($messages[$k]['body']),$key);
+ $messages[$k]['body'] = base64url_decode(str_rot47($messages[$k]['body']));
}
}
+
if($updateseen) {
- $r = q("UPDATE `mail` SET mail_flags = (mail_flags | %d) where not (mail_flags & %d)>0 and parent_mid = '%s' AND channel_id = %d",
- intval(MAIL_SEEN),
- intval(MAIL_SEEN),
+ $r = q("UPDATE `mail` SET mail_seen = 1 where mail_seen = 0 and parent_mid = '%s' AND channel_id = %d",
dbesc($r[0]['parent_mid']),
intval($channel_id)
);
@@ -431,4 +472,4 @@ function private_messages_fetch_conversation($channel_id, $messageitem_id, $upda
return $messages;
-} \ No newline at end of file
+}