From c2dcc6880302406de6e1abf90c985975e8a4f13a Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 22 Sep 2015 16:19:08 -0700 Subject: [PATCH 1/7] backport item sync --- include/items.php | 8 +++-- include/zot.php | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/include/items.php b/include/items.php index 3669628b4..86da7941f 100755 --- a/include/items.php +++ b/include/items.php @@ -805,10 +805,14 @@ function title_is_body($title, $body) { } -function get_item_elements($x) { +function get_item_elements($x,$allow_code = false) { $arr = array(); - $arr['body'] = (($x['body']) ? htmlspecialchars($x['body'],ENT_COMPAT,'UTF-8',false) : ''); + + if($allow_code) + $arr['body'] = $x['body']; + else + $arr['body'] = (($x['body']) ? htmlspecialchars($x['body'],ENT_COMPAT,'UTF-8',false) : ''); $key = get_config('system','pubkey'); diff --git a/include/zot.php b/include/zot.php index ddcd6b41f..a41295aad 100644 --- a/include/zot.php +++ b/include/zot.php @@ -3273,6 +3273,14 @@ function process_channel_sync_delivery($sender, $arr, $deliveries) { } } + + if(array_key_exists('item',$arr) && $arr['item']) + sync_items($channel,$arr['item']); + + if(array_key_exists('item_id',$arr) && $arr['item_id']) + sync_items($channel,$arr['item_id']); + + $result[] = array($d['hash'],'channel sync updated',$channel['channel_name'],''); } @@ -3431,3 +3439,80 @@ function zot_process_message_request($data) { return $ret; } + + + +function import_items($channel,$items) { + + if($channel && $items) { + $allow_code = false; + $r = q("select account_id, account_roles, channel_pageflags from account left join channel on channel_account_id = account_id + where channel_id = %d limit 1", + intval($channel['channel_id']) + ); + if($r) { + if(($r[0]['account_roles'] & ACCOUNT_ROLE_ALLOWCODE) || ($r[0]['channel_pageflags'] & PAGE_ALLOWCODE)) { + $allow_code = true; + } + } + + foreach($items as $i) { + $item = get_item_elements($i,$allow_code); + if(! $item) + continue; + + $r = q("select id, edited from item where mid = '%s' and uid = %d limit 1", + dbesc($item['mid']), + intval($channel['channel_id']) + ); + if($r) { + if($item['edited'] > $r[0]['edited']) { + $item['id'] = $r[0]['id']; + $item['uid'] = $channel['channel_id']; + item_store_update($item); + continue; + } + } + else { + $item['aid'] = $channel['channel_account_id']; + $item['uid'] = $channel['channel_id']; + $item_result = item_store($item); + } + + } + } +} + + +function sync_items($channel,$items) { + import_items($channel,$items); +} + + + +function import_item_ids($channel,$itemids) { + if($channel && $itemids) { + foreach($itemids as $i) { + $r = q("select id from item where mid = '%s' and uid = %d limit 1", + dbesc($i['mid']), + intval($channel['channel_id']) + ); + if(! $r) + continue; + $z = q("select * from item_id where service = '%s' and sid = '%s' and iid = %d and uid = %d limit 1", + dbesc($i['service']), + dbesc($i['sid']), + intval($r[0]['id']), + intval($channel['channel_id']) + ); + if(! $z) { + q("insert into item_id (iid,uid,sid,service) values(%d,%d,'%s','%s')", + intval($r[0]['id']), + intval($channel['channel_id']), + dbesc($i['sid']), + dbesc($i['service']) + ); + } + } + } +} From 642f460dcad84b8254126f316db4c0225a958958 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 22 Sep 2015 16:43:36 -0700 Subject: [PATCH 2/7] report the channel sync --- include/zot.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/zot.php b/include/zot.php index a41295aad..eb04e421c 100644 --- a/include/zot.php +++ b/include/zot.php @@ -3281,7 +3281,17 @@ function process_channel_sync_delivery($sender, $arr, $deliveries) { sync_items($channel,$arr['item_id']); - $result[] = array($d['hash'],'channel sync updated',$channel['channel_name'],''); + // we should probably do this for all items, but usually we only send one. + + require_once('include/DReport.php'); + + if(array_key_exists('item',$arr) && is_array($arr['item'][0])) + $DR = new DReport(z_root(),$d['hash'],$d['hash'],$arr['item'][0]['message_id'],'channel sync processed'); + else + $DR = new DReport(z_root(),$d['hash'],$d['hash'],'sync packet','channel sync delivered'); + + $result[] = $DR->get(); + } return $result; From 34291d593845c1ec4c2d68dcc4662b020db33f32 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 22 Sep 2015 16:50:08 -0700 Subject: [PATCH 3/7] add names to delivery report for sync delivery --- include/zot.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/zot.php b/include/zot.php index eb04e421c..0b1aaa989 100644 --- a/include/zot.php +++ b/include/zot.php @@ -3285,8 +3285,10 @@ function process_channel_sync_delivery($sender, $arr, $deliveries) { require_once('include/DReport.php'); - if(array_key_exists('item',$arr) && is_array($arr['item'][0])) + if(array_key_exists('item',$arr) && is_array($arr['item'][0])) { $DR = new DReport(z_root(),$d['hash'],$d['hash'],$arr['item'][0]['message_id'],'channel sync processed'); + $DR->addto_recipient($channel['channel_name'] . ' <' . $channel['channel_address'] . '@' . get_app()->get_hostname() . '>'); + } else $DR = new DReport(z_root(),$d['hash'],$d['hash'],'sync packet','channel sync delivered'); From 15feeb7bfe324bbe87e689ee7920a6be20cc204c Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 22 Sep 2015 18:40:03 -0700 Subject: [PATCH 4/7] inverted logic syncing deleted hublocs --- include/zot.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/zot.php b/include/zot.php index 0b1aaa989..236b55522 100644 --- a/include/zot.php +++ b/include/zot.php @@ -2363,7 +2363,7 @@ function sync_locations($sender, $arr, $absolute = false) { foreach($xisting as $x) { if(! array_key_exists('updated',$x)) { logger('sync_locations: deleting unreferenced hub location ' . $x['hubloc_url']); - $r = q("update hubloc set hubloc_flags = (hubloc_flags & ~%d), hubloc_updated = '%s' where hubloc_id = %d", + $r = q("update hubloc set hubloc_flags = (hubloc_flags | %d), hubloc_updated = '%s' where hubloc_id = %d", intval(HUBLOC_FLAGS_DELETED), dbesc(datetime_convert()), intval($x['hubloc_id']) From d16ac62a511ed32e7bcd50ee25148c0771b5e765 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 22 Sep 2015 19:13:10 -0700 Subject: [PATCH 5/7] don't send sync packets to deleted hubloc locations --- include/zot.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/zot.php b/include/zot.php index 236b55522..aae429709 100644 --- a/include/zot.php +++ b/include/zot.php @@ -2802,8 +2802,10 @@ function build_sync_packet($uid = 0, $packet = null, $groups_changed = false) { $channel = $r[0]; - $h = q("select * from hubloc where hubloc_hash = '%s'", - dbesc($channel['channel_hash']) + $h = q("select * from hubloc where hubloc_hash = '%s' and not (hubloc_flags & %d) > 0 and not (hubloc_status & %d) > 0", + dbesc($channel['channel_hash']), + intval(HUBLOC_FLAGS_DELETED), + intval(HUBLOC_OFFLINE) ); if(! $h) From d35c5bcb2f425bf498eae53c5ac268a96c808cb9 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 22 Sep 2015 19:16:13 -0700 Subject: [PATCH 6/7] merge issue --- include/zot.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/include/zot.php b/include/zot.php index 86e9da5f3..288493b07 100644 --- a/include/zot.php +++ b/include/zot.php @@ -2816,10 +2816,8 @@ function build_sync_packet($uid = 0, $packet = null, $groups_changed = false) { $channel = $r[0]; - $h = q("select * from hubloc where hubloc_hash = '%s' and not (hubloc_flags & %d) > 0 and not (hubloc_status & %d) > 0", - dbesc($channel['channel_hash']), - intval(HUBLOC_FLAGS_DELETED), - intval(HUBLOC_OFFLINE) + $h = q("select * from hubloc where hubloc_hash = '%s' and hubloc_deleted = 0", + dbesc($channel['channel_hash']) ); if(! $h) From 7734bbfe4d7730abed39e4e23287a68461d86040 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Tue, 22 Sep 2015 22:30:09 -0700 Subject: [PATCH 7/7] problems with extended item import and signatures --- include/items.php | 65 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/include/items.php b/include/items.php index 86da7941f..eed9dabb2 100755 --- a/include/items.php +++ b/include/items.php @@ -807,6 +807,7 @@ function title_is_body($title, $body) { function get_item_elements($x,$allow_code = false) { + $arr = array(); if($allow_code) @@ -922,7 +923,6 @@ function get_item_elements($x,$allow_code = false) { logger('get_item_elements: message verification failed.'); } - // if it's a private post, encrypt it in the DB. // We have to do that here because we need to cleanse the input and prevent bad stuff from getting in, // and we need plaintext to do that. @@ -949,9 +949,68 @@ function get_item_elements($x,$allow_code = false) { $arr['postopts'] = $x['postopts']; $arr['resource_id'] = $x['resource_id']; $arr['resource_type'] = $x['resource_type']; - $arr['item_restrict'] = $x['item_restrict']; - $arr['item_flags'] = $x['item_flags']; $arr['attach'] = $x['attach']; + + if(! array_key_exists('item_origin',$x)) { + $arr['item_restrict'] = $x['item_restrict']; + $arr['item_flags'] = $x['item_flags']; + } + + if(array_key_exists('item_origin',$x) && intval($x['item_origin'])) + $arr['item_flags'] |= ITEM_ORIGIN; + if(array_key_exists('item_unseen',$x) && intval($x['item_unseen'])) + $arr['item_flags'] |= ITEM_UNSEEN; + if(array_key_exists('item_starred',$x) && intval($x['item_starred'])) + $arr['item_flags'] |= ITEM_STARRED; + if(array_key_exists('item_uplink',$x) && intval($x['item_uplink'])) + $arr['item_flags'] |= ITEM_UPLINK; + if(array_key_exists('item_consensus',$x) && intval($x['item_consensus'])) + $arr['item_flags'] |= ITEM_CONSENSUS; + if(array_key_exists('item_wall',$x) && intval($x['item_wall'])) + $arr['item_flags'] |= ITEM_WALL; + if(array_key_exists('item_thread_top',$x) && intval($x['item_thread_top'])) + $arr['item_flags'] |= ITEM_THREAD_TOP; + if(array_key_exists('item_notshown',$x) && intval($x['item_notshown'])) + $arr['item_flags'] |= ITEM_NOTSHOWN; + if(array_key_exists('item_nsfw',$x) && intval($x['item_nsfw'])) + $arr['item_flags'] |= ITEM_NSFW; + if(array_key_exists('item_mentionsme',$x) && intval($x['item_mentionsme'])) + $arr['item_flags'] |= ITEM_MENTIONSME; + if(array_key_exists('item_nocomment',$x) && intval($x['item_nocomment'])) + $arr['item_flags'] |= ITEM_NOCOMMENT; + if(array_key_exists('item_retained',$x) && intval($x['item_retained'])) + $arr['item_flags'] |= ITEM_RETAINED; + if(array_key_exists('item_rss',$x) && intval($x['item_rss'])) + $arr['item_flags'] |= ITEM_RSS; + + + if(array_key_exists('item_deleted',$x)&& intval($x['item_deleted'])) + $arr['item_restrict'] |= ITEM_DELETED; + if(array_key_exists('item_unpublished',$x)&& intval($x['item_unpublished'])) + $arr['item_restrict'] |= ITEM_UNPUBLISHED; + if(array_key_exists('item_delayed',$x)&& intval($x['item_delayed'])) + $arr['item_restrict'] |= ITEM_DELAYED_PUBLISH; + if(array_key_exists('item_pending_remove',$x)&& intval($x['item_pending_remove'])) + $arr['item_restrict'] |= ITEM_PENDING_REMOVE; + if(array_key_exists('item_type',$x)) { + switch(intval($x['item_type'])) { + case 1: + $arr['item_restrict'] |= ITEM_BUILDBLOCK; + break; + case 2: + $arr['item_restrict'] |= ITEM_PDL; + break; + case 3: + $arr['item_restrict'] |= ITEM_WEBPAGE; + break; + case 4: + $arr['item_restrict'] |= ITEM_BUG; + break; + case 0: + default: + break; + } + } } return $arr;