aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorredmatrix <redmatrix@redmatrix.me>2015-09-02 20:12:40 -0700
committerredmatrix <redmatrix@redmatrix.me>2015-09-02 20:12:40 -0700
commitc22da3da1c5d3c5418cb9f142883adb838db9f76 (patch)
tree61c8575d5959826918c9b4fcd42666048ce246fc
parent69fc10d5adfa0640792376ad4f417bd7f28fc7f7 (diff)
downloadvolse-hubzilla-c22da3da1c5d3c5418cb9f142883adb838db9f76.tar.gz
volse-hubzilla-c22da3da1c5d3c5418cb9f142883adb838db9f76.tar.bz2
volse-hubzilla-c22da3da1c5d3c5418cb9f142883adb838db9f76.zip
start to modularise the clone import and sync functions
-rw-r--r--include/AccessList.php17
-rw-r--r--include/import.php312
-rw-r--r--include/zot.php6
-rw-r--r--mod/import.php234
-rw-r--r--mod/thing.php21
5 files changed, 386 insertions, 204 deletions
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 @@
+<?php
+
+
+function import_channel($channel) {
+
+ if(! array_key_exists('channel_system',$channel)) {
+ $channel['channel_system'] = (($channel['channel_pageflags'] & 0x1000) ? 1 : 0);
+ $channel['channel_removed'] = (($channel['channel_pageflags'] & 0x8000) ? 1 : 0);
+ }
+
+ $r = q("select * from channel where (channel_guid = '%s' or channel_hash = '%s' or channel_address = '%s' ) limit 1",
+ dbesc($channel['channel_guid']),
+ dbesc($channel['channel_hash']),
+ dbesc($channel['channel_address'])
+ );
+
+ // We should probably also verify the hash
+
+ if($r) {
+ if($r[0]['channel_guid'] === $channel['channel_guid'] || $r[0]['channel_hash'] === $channel['channel_hash']) {
+ logger('mod_import: duplicate channel. ', print_r($channel,true));
+ notice( t('Cannot create a duplicate channel identifier on this system. Import failed.') . EOL);
+ return false;
+ }
+ else {
+ // try at most ten times to generate a unique address.
+ $x = 0;
+ $found_unique = false;
+ do {
+ $tmp = $channel['channel_address'] . mt_rand(1000,9999);
+ $r = q("select * from channel where channel_address = '%s' limit 1",
+ dbesc($tmp)
+ );
+ if(! $r) {
+ $channel['channel_address'] = $tmp;
+ $found_unique = true;
+ break;
+ }
+ $x ++;
+ } while ($x < 10);
+ if(! $found_unique) {
+ logger('mod_import: duplicate channel. randomisation failed.', print_r($channel,true));
+ notice( t('Unable to create a unique channel address. Import failed.') . EOL);
+ return false;
+ }
+ }
+ }
+
+ unset($channel['channel_id']);
+ $channel['channel_account_id'] = get_account_id();
+ $channel['channel_primary'] = (($seize) ? 1 : 0);
+
+ dbesc_array($channel);
+
+ $r = dbq("INSERT INTO channel (`"
+ . implode("`, `", array_keys($channel))
+ . "`) VALUES ('"
+ . implode("', '", array_values($channel))
+ . "')"
+ );
+
+ if(! $r) {
+ logger('mod_import: channel clone failed. ', print_r($channel,true));
+ notice( t('Channel clone failed. Import failed.') . EOL);
+ return false;
+ }
+
+ $r = q("select * from channel where channel_account_id = %d and channel_guid = '%s' limit 1",
+ intval(get_account_id()),
+ $channel['channel_guid'] // Already dbesc'd
+ );
+ if(! $r) {
+ logger('mod_import: channel not found. ', print_r($channel,true));
+ notice( t('Cloned channel not found. Import failed.') . EOL);
+ return false;
+ }
+ // reset
+ $channel = $r[0];
+
+ set_default_login_identity(get_account_id(),$channel['channel_id'],false);
+ logger('import step 1');
+ $_SESSION['import_step'] = 1;
+ ref_session_write(session_id(), serialize($_SESSION));
+ return $channel;
+
+}
+
+function import_config($channel,$configs) {
+
+ if($channel && $configs) {
+ foreach($configs as $config) {
+ unset($config['id']);
+ $config['uid'] = $channel['channel_id'];
+ dbesc_array($config);
+ $r = dbq("INSERT INTO pconfig (`"
+ . implode("`, `", array_keys($config))
+ . "`) VALUES ('"
+ . implode("', '", array_values($config))
+ . "')" );
+ }
+ load_pconfig($channel['channel_id']);
+ }
+}
+
+
+function import_profiles($channel,$profiles) {
+
+ if($channel && $profiles) {
+ foreach($profiles as $profile) {
+ unset($profile['id']);
+ $profile['aid'] = get_account_id();
+ $profile['uid'] = $channel['channel_id'];
+
+ // we are going to reset all profile photos to the original
+ // somebody will have to fix this later and put all the applicable photos into the export
+
+ $profile['photo'] = z_root() . '/photo/profile/l/' . $channel['channel_id'];
+ $profile['thumb'] = z_root() . '/photo/profile/m/' . $channel['channel_id'];
+
+ dbesc_array($profile);
+ $r = dbq("INSERT INTO profile (`"
+ . implode("`, `", array_keys($profile))
+ . "`) VALUES ('"
+ . implode("', '", array_values($profile))
+ . "')"
+ );
+ }
+ }
+}
+
+
+function import_hublocs($channel,$hublocs,$seize) {
+
+ if($channel && $hublocs) {
+ foreach($hublocs as $hubloc) {
+
+ $hash = make_xchan_hash($hubloc['hubloc_guid'],$hubloc['hubloc_guid_sig']);
+ if($hubloc['hubloc_network'] === 'zot' && $hash !== $hubloc['hubloc_hash']) {
+ logger('forged hubloc: ' . print_r($hubloc,true));
+ continue;
+ }
+
+ if(! array_key_exists('hubloc_primary',$hubloc)) {
+ $hubloc['hubloc_primary'] = (($hubloc['hubloc_flags'] & 0x0001) ? 1 : 0);
+ $hubloc['hubloc_orphancheck'] = (($hubloc['hubloc_flags'] & 0x0004) ? 1 : 0);
+ $hubloc['hubloc_error'] = (($hubloc['hubloc_status'] & 0x0003) ? 1 : 0);
+ $hubloc['hubloc_deleted'] = (($hubloc['hubloc_flags'] & 0x1000) ? 1 : 0);
+ }
+
+ $arr = array(
+ 'guid' => $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);
diff --git a/mod/import.php b/mod/import.php
index 0472abfcd..c2ed82a0a 100644
--- a/mod/import.php
+++ b/mod/import.php
@@ -6,6 +6,8 @@
require_once('include/Contact.php');
require_once('include/zot.php');
require_once('include/identity.php');
+require_once('include/import.php');
+
function import_post(&$a) {
@@ -118,118 +120,41 @@ function import_post(&$a) {
// import channel
if(array_key_exists('channel',$data)) {
- $channel = $data['channel'];
if($completed < 1) {
+ $channel = import_channel($data['channel']);
- if(! array_key_exists('channel_system',$channel)) {
- $channel['channel_system'] = (($channel['channel_pageflags'] & 0x1000) ? 1 : 0);
- $channel['channel_removed'] = (($channel['channel_pageflags'] & 0x8000) ? 1 : 0);
- }
-
- $r = q("select * from channel where (channel_guid = '%s' or channel_hash = '%s' or channel_address = '%s' ) limit 1",
- dbesc($channel['channel_guid']),
- dbesc($channel['channel_hash']),
- dbesc($channel['channel_address'])
- );
-
- // We should probably also verify the hash
-
- if($r) {
- if($r[0]['channel_guid'] === $channel['channel_guid'] || $r[0]['channel_hash'] === $channel['channel_hash']) {
- logger('mod_import: duplicate channel. ', print_r($channel,true));
- notice( t('Cannot create a duplicate channel identifier on this system. Import failed.') . EOL);
- return;
- }
- else {
- // try at most ten times to generate a unique address.
- $x = 0;
- $found_unique = false;
- do {
- $tmp = $channel['channel_address'] . mt_rand(1000,9999);
- $r = q("select * from channel where channel_address = '%s' limit 1",
- dbesc($tmp)
- );
- if(! $r) {
- $channel['channel_address'] = $tmp;
- $found_unique = true;
- break;
- }
- $x ++;
- } while ($x < 10);
- if(! $found_unique) {
- logger('mod_import: duplicate channel. randomisation failed.', print_r($channel,true));
- notice( t('Unable to create a unique channel address. Import failed.') . EOL);
- return;
- }
- }
- }
-
- unset($channel['channel_id']);
- $channel['channel_account_id'] = get_account_id();
- $channel['channel_primary'] = (($seize) ? 1 : 0);
-
- dbesc_array($channel);
-
- $r = dbq("INSERT INTO channel (`"
- . implode("`, `", array_keys($channel))
- . "`) VALUES ('"
- . implode("', '", array_values($channel))
- . "')" );
-
- if(! $r) {
- logger('mod_import: channel clone failed. ', print_r($channel,true));
- notice( t('Channel clone failed. Import failed.') . EOL);
- return;
- }
-
+ }
+ else {
$r = q("select * from channel where channel_account_id = %d and channel_guid = '%s' limit 1",
intval(get_account_id()),
- $channel['channel_guid'] // Already dbesc'd
+ dbesc($channel['channel_guid'])
);
- if(! $r) {
- logger('mod_import: channel not found. ', print_r($channel,true));
- notice( t('Cloned channel not found. Import failed.') . EOL);
- return;
- }
- // reset
- $channel = $r[0];
-
- set_default_login_identity(get_account_id(),$channel['channel_id'],false);
- logger('import step 1');
- $_SESSION['import_step'] = 1;
- ref_session_write(session_id(), serialize($_SESSION));
+ if($r)
+ $channel = $r[0];
}
- }
- else {
- $r = q("select * from channel where channel_account_id = %d and channel_guid = '%s' limit 1",
- intval(get_account_id()),
- dbesc($channel['channel_guid'])
- );
- if($r)
- $channel = $r[0];
- else {
+ if(! $channel) {
logger('mod_import: channel not found. ', print_r($channel,true));
notice( t('Cloned channel not found. Import failed.') . EOL);
return;
}
}
- if($completed < 2) {
+ if(! $channel)
+ $channel = $a->get_channel();
+
+ if(! $channel) {
+ logger('mod_import: channel not found. ', print_r($channel,true));
+ notice( t('No channel. Import failed.') . EOL);
+ return;
+ }
- $configs = $data['config'];
- if($configs) {
- foreach($configs as $config) {
- unset($config['id']);
- $config['uid'] = $channel['channel_id'];
- dbesc_array($config);
- $r = dbq("INSERT INTO pconfig (`"
- . implode("`, `", array_keys($config))
- . "`) VALUES ('"
- . implode("', '", array_values($config))
- . "')" );
- }
+
+ if($completed < 2) {
+ if(is_array($data['config'])) {
+ import_config($channel,$data['config']);
}
+
logger('import step 2');
$_SESSION['import_step'] = 2;
ref_session_write(session_id(), serialize($_SESSION));
@@ -244,27 +169,9 @@ function import_post(&$a) {
import_channel_photo(base64url_decode($data['photo']['data']),$data['photo']['type'],get_account_id(),$channel['channel_id']);
}
- $profiles = $data['profile'];
- if($profiles) {
- foreach($profiles as $profile) {
- unset($profile['id']);
- $profile['aid'] = get_account_id();
- $profile['uid'] = $channel['channel_id'];
-
- // we are going to reset all profile photos to the original
- // somebody will have to fix this later and put all the applicable photos into the export
-
- $profile['photo'] = z_root() . '/photo/profile/l/' . $channel['channel_id'];
- $profile['thumb'] = z_root() . '/photo/profile/m/' . $channel['channel_id'];
+ if(is_array($data['profiles']))
+ import_profiles($channel,$data['profiles']);
- dbesc_array($profile);
- $r = dbq("INSERT INTO profile (`"
- . implode("`, `", array_keys($profile))
- . "`) VALUES ('"
- . implode("', '", array_values($profile))
- . "')" );
- }
- }
logger('import step 3');
$_SESSION['import_step'] = 3;
ref_session_write(session_id(), serialize($_SESSION));
@@ -272,43 +179,10 @@ function import_post(&$a) {
if($completed < 4) {
- $hublocs = $data['hubloc'];
- if($hublocs) {
- foreach($hublocs as $hubloc) {
- $hash = make_xchan_hash($hubloc['hubloc_guid'],$hubloc['hubloc_guid_sig']);
- if($hubloc['hubloc_network'] === 'zot' && $hash !== $hubloc['hubloc_hash']) {
- logger('forged hubloc: ' . print_r($hubloc,true));
- continue;
- }
+ if(is_array($data['hubloc'])) {
+ import_hublocs($channel,$data['hubloc'],$seize);
- if(! array_key_exists('hubloc_primary',$hubloc)) {
- $hubloc['hubloc_primary'] = (($hubloc['hubloc_flags'] & 0x0001) ? 1 : 0);
- $hubloc['hubloc_orphancheck'] = (($hubloc['hubloc_flags'] & 0x0004) ? 1 : 0);
- $hubloc['hubloc_error'] = (($hubloc['hubloc_status'] & 0x0003) ? 1 : 0);
- $hubloc['hubloc_deleted'] = (($hubloc['hubloc_flags'] & 0x1000) ? 1 : 0);
- }
-
- $arr = array(
- 'guid' => $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))
- . "')" );
- }
- }
}
logger('import step 4');
$_SESSION['import_step'] = 4;
@@ -556,59 +430,11 @@ function import_post(&$a) {
ref_session_write(session_id(), serialize($_SESSION));
}
- $objs = $data['obj'];
- if($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'],get_observer_hash(),true);
- $obj['obj_imgurl'] = $x[0];
- }
-
- dbesc_array($obj);
- $r = dbq("INSERT INTO obj (`"
- . implode("`, `", array_keys($obj))
- . "`) VALUES ('"
- . implode("', '", array_values($obj))
- . "')" );
- }
- }
-
- $apps = $data['app'];
- if($app) {
- 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'],get_observer_hash(),true);
- $app['app_photo'] = $x[0];
- }
-
- dbesc_array($app);
- $r = dbq("INSERT INTO app (`"
- . implode("`, `", array_keys($obj))
- . "`) VALUES ('"
- . implode("', '", array_values($obj))
- . "')" );
- }
- }
+ if(is_array($data['obj']))
+ import_objs($channel,$data['obj']);
+ if(is_array($data['app']))
+ import_apps($channel,$data['app']);
$saved_notification_flags = notifications_off($channel['channel_id']);
diff --git a/mod/thing.php b/mod/thing.php
index 86712d9eb..03dc7db5b 100644
--- a/mod/thing.php
+++ b/mod/thing.php
@@ -109,6 +109,15 @@ function thing_init(&$a) {
);
info( t('Thing updated') . EOL);
+
+ $r = q("select * from obj where obj_channel = %d and obj_obj = '%s' limit 1",
+ intval(local_channel()),
+ dbesc($term_hash)
+ );
+ if($r) {
+ build_sync_packet(0, array('obj' => $r));
+ }
+
return;
}
@@ -157,6 +166,14 @@ function thing_init(&$a) {
info( t('Thing added'));
+ $r = q("select * from obj where obj_channel = %d and obj_obj = '%s' limit 1",
+ intval(local_channel()),
+ dbesc($hash)
+ );
+ if($r) {
+ build_sync_packet(0, array('obj' => $r));
+ }
+
if($activity) {
$arr = array();
$links = array(array('rel' => 'alternate','type' => 'text/html', 'href' => $url));
@@ -310,6 +327,10 @@ function thing_content(&$a) {
intval(local_channel())
);
+ $r[0]['obj_deleted'] = 1;
+
+ build_sync_packet(0,array('obj' => $r));
+
return $o;
}