From c22da3da1c5d3c5418cb9f142883adb838db9f76 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 2 Sep 2015 20:12:40 -0700 Subject: start to modularise the clone import and sync functions --- include/AccessList.php | 17 +++ include/import.php | 312 +++++++++++++++++++++++++++++++++++++++++++++++++ include/zot.php | 6 + 3 files changed, 335 insertions(+) create mode 100644 include/import.php (limited to 'include') diff --git a/include/AccessList.php b/include/AccessList.php index 46e66d33d..43f1de111 100644 --- a/include/AccessList.php +++ b/include/AccessList.php @@ -34,6 +34,11 @@ class AccessList { return $this->explicit; } + /** + * Set AccessList from strings such as those in already + * existing stored data items + */ + function set($arr,$explicit = true) { $this->allow_cid = $arr['allow_cid']; $this->allow_gid = $arr['allow_gid']; @@ -43,6 +48,12 @@ class AccessList { $this->explicit = $explicit; } + /** + * return an array consisting of the current + * access list components where the elements + * are directly storable. + */ + function get() { return array( 'allow_cid' => $this->allow_cid, @@ -52,6 +63,12 @@ class AccessList { ); } + /** + * Set AccessList from arrays, such as those provided by + * acl_selector(). For convenience, a string (or non-array) input is + * assumed to be a comma-separated list and auto-converted into an array. + */ + function set_from_array($arr,$explicit = true) { $this->allow_cid = perms2str((is_array($arr['contact_allow'])) ? $arr['contact_allow'] : explode(',',$arr['contact_allow'])); diff --git a/include/import.php b/include/import.php new file mode 100644 index 000000000..cee0d14fd --- /dev/null +++ b/include/import.php @@ -0,0 +1,312 @@ + $hubloc['hubloc_guid'], + 'guid_sig' => $hubloc['hubloc_guid_sig'], + 'url' => $hubloc['hubloc_url'], + 'url_sig' => $hubloc['hubloc_url_sig'] + ); + if(($hubloc['hubloc_hash'] === $channel['channel_hash']) && intval($hubloc['hubloc_primary']) && ($seize)) + $hubloc['hubloc_primary'] = 0; + + if(! zot_gethub($arr)) { + unset($hubloc['hubloc_id']); + dbesc_array($hubloc); + + $r = dbq("INSERT INTO hubloc (`" + . implode("`, `", array_keys($hubloc)) + . "`) VALUES ('" + . implode("', '", array_values($hubloc)) + . "')" + ); + } + } + } +} + + + +function import_objs($channel,$objs) { + + if($channel && $objs) { + foreach($objs as $obj) { + + // if it's the old term format - too hard to support + if(! $obj['obj_created']) + continue; + + $baseurl = $obj['obj_baseurl']; + unset($obj['obj_id']); + unset($obj['obj_baseurl']); + + $obj['obj_channel'] = $channel['channel_id']; + + if($baseurl && (strpos($obj['obj_url'],$baseurl . '/thing/') !== false)) { + $obj['obj_url'] = str_replace($baseurl,z_root(),$obj['obj_url']); + } + + if($obj['obj_imgurl']) { + $x = import_xchan_photo($obj['obj_imgurl'],$channel['channel_hash'],true); + $obj['obj_imgurl'] = $x[0]; + } + + dbesc_array($obj); + + $r = dbq("INSERT INTO obj (`" + . implode("`, `", array_keys($obj)) + . "`) VALUES ('" + . implode("', '", array_values($obj)) + . "')" + ); + } + } +} + +function sync_objs($channel,$objs) { + + if($channel && $objs) { + foreach($objs as $obj) { + + if(array_key_exists('obj_deleted',$obj) && $obj['obj_deleted'] && $obj['obj_obj']) { + q("delete from obj where obj_obj = '%s' and obj_channel = %d limit 1", + dbesc($obj['obj_obj']), + intval($channel['channel_id']) + ); + continue; + } + + // if it's the old term format - too hard to support + if(! $obj['obj_created']) + continue; + + $baseurl = $obj['obj_baseurl']; + unset($obj['obj_id']); + unset($obj['obj_baseurl']); + + $obj['obj_channel'] = $channel['channel_id']; + + if($baseurl && (strpos($obj['obj_url'],$baseurl . '/thing/') !== false)) { + $obj['obj_url'] = str_replace($baseurl,z_root(),$obj['obj_url']); + } + + $exists = false; + + $x = q("select * from obj where obj_obj = '%s' and obj_channel = %d limit 1", + dbesc($obj['obj_obj']), + intval($channel['channel_id']) + ); + if($x) { + if($x[0]['obj_edited'] >= $obj['obj_edited']) + continue; + + $exists = true; + } + + if($obj['obj_imgurl']) { + $x = import_xchan_photo($obj['obj_imgurl'],$channel['channel_hash'],true); + $obj['obj_imgurl'] = $x[0]; + } + + $hash = $obj['obj_obj']; + + if($exists) { + foreach($obj as $k => $v) { + $r = q("UPDATE obj SET `%s` = '%s' WHERE obj_obj = '%s' AND obj_channel = %d", + dbesc($k), + dbesc($v), + dbesc($hash), + intval($channel['channel_id']) + ); + } + } + else { + + dbesc_array($obj); + + $r = dbq("INSERT INTO obj (`" + . implode("`, `", array_keys($obj)) + . "`) VALUES ('" + . implode("', '", array_values($obj)) + . "')" + ); + } + } + } +} + + + + + +function import_apps($channel,$apps) { + + if($channel && $apps) { + foreach($apps as $app) { + + unset($app['id']); + unset($app['app_channel']); + + $app['app_channel'] = $channel['channel_id']; + + if($app['app_photo']) { + $x = import_xchan_photo($app['app_photo'],$channel['channel_hash'],true); + $app['app_photo'] = $x[0]; + } + + dbesc_array($app); + $r = dbq("INSERT INTO app (`" + . implode("`, `", array_keys($app)) + . "`) VALUES ('" + . implode("', '", array_values($app)) + . "')" + ); + } + } +} \ No newline at end of file diff --git a/include/zot.php b/include/zot.php index 8a8b74860..cb70f38a2 100644 --- a/include/zot.php +++ b/include/zot.php @@ -2841,6 +2841,8 @@ function build_sync_packet($uid = 0, $packet = null, $groups_changed = false) { */ function process_channel_sync_delivery($sender, $arr, $deliveries) { + require_once('include/import.php'); + /** @FIXME this will sync red structures (channel, pconfig and abook). Eventually we need to make this application agnostic. */ $result = array(); @@ -2873,6 +2875,10 @@ function process_channel_sync_delivery($sender, $arr, $deliveries) { } } + if(array_key_exists('obj',$arr) && $arr['obj']) + sync_objs($channel,$arr['obj']); + + if(array_key_exists('channel',$arr) && is_array($arr['channel']) && count($arr['channel'])) { if(array_key_exists('channel_page_flags',$arr['channel']) && intval($arr['channel']['channel_pageflags'])) { $arr['channel']['channel_removed'] = (($arr['channel']['channel_pageflags'] & 0x8000) ? 1 : 0); -- cgit v1.2.3