aboutsummaryrefslogblamecommitdiffstats
path: root/include/xchan.php
blob: dba0b8b88c6848020b717fd953dd81beba4abc56 (plain) (tree)










































































































































                                                                                                                                                                                 




































































































































































































































                                                                                                                                                        
<?php


function xchan_store_lowlevel($arr) {

	$store = [
		'xchan_hash' => ((array_key_exists('xchan_hash',$arr)) ? $arr['xchan_hash'] : ''),
		'xchan_guid' => ((array_key_exists('xchan_guid',$arr)) ? $arr['xchan_guid'] : ''),
		'xchan_guid_sig' => ((array_key_exists('xchan_guid_sig',$arr)) ? $arr['xchan_guid_sig'] : ''),
		'xchan_pubkey' => ((array_key_exists('xchan_pubkey',$arr)) ? $arr['xchan_pubkey'] : ''),
		'xchan_photo_mimetype'  => ((array_key_exists('xchan_photo_mimetype',$arr)) ? $arr['xchan_photo_mimetype'] : ''),
		'xchan_photo_l'  => ((array_key_exists('xchan_photo_l',$arr)) ? $arr['xchan_photo_l'] : ''),
		'xchan_photo_m' => ((array_key_exists('xchan_photo_m',$arr)) ? $arr['xchan_photo_m'] : ''),
		'xchan_photo_s' => ((array_key_exists('xchan_photo_s',$arr)) ? $arr['xchan_photo_s'] : ''),
		'xchan_addr' => ((array_key_exists('xchan_addr',$arr)) ? $arr['xchan_addr'] : ''),
		'xchan_url' => ((array_key_exists('xchan_url',$arr)) ? $arr['xchan_url'] : ''),
		'xchan_connurl' => ((array_key_exists('xchan_connurl',$arr)) ? $arr['xchan_connurl'] : ''),
		'xchan_follow' => ((array_key_exists('xchan_follow',$arr)) ? $arr['xchan_follow'] : ''),
		'xchan_connpage' => ((array_key_exists('xchan_connpage',$arr)) ? $arr['xchan_connpage'] : ''),
		'xchan_name' => ((array_key_exists('xchan_name',$arr)) ? $arr['xchan_name'] : ''),
		'xchan_network' => ((array_key_exists('xchan_network',$arr)) ? $arr['xchan_network'] : ''),
		'xchan_instance_url' => ((array_key_exists('xchan_instance_url',$arr)) ? $arr['xchan_instance_url'] : ''),
		'xchan_flags' => ((array_key_exists('xchan_flags',$arr)) ? intval($arr['xchan_flags']) : 0),
		'xchan_photo_date' => ((array_key_exists('xchan_photo_date',$arr)) ? datetime_convert('UTC','UTC',$arr['xchan_photo_date']) : NULL_DATE),
		'xchan_name_date' => ((array_key_exists('xchan_name_date',$arr)) ? datetime_convert('UTC','UTC',$arr['xchan_name_date']) : NULL_DATE),
		'xchan_hidden' => ((array_key_exists('xchan_hidden',$arr)) ? intval($arr['xchan_hidden']) : 0),
		'xchan_orphan' => ((array_key_exists('xchan_orphan',$arr)) ? intval($arr['xchan_orphan']) : 0),
		'xchan_censored' => ((array_key_exists('xchan_censored',$arr)) ? intval($arr['xchan_censored']) : 0),
		'xchan_selfcensored' => ((array_key_exists('xchan_selfcensored',$arr)) ? intval($arr['xchan_selfcensored']) : 0),
		'xchan_system' => ((array_key_exists('xchan_system',$arr)) ? intval($arr['xchan_system']) : 0),
		'xchan_pubforum' => ((array_key_exists('xchan_pubforum',$arr)) ? intval($arr['xchan_pubforum']) : 0),
		'xchan_deleted' => ((array_key_exists('xchan_deleted',$arr)) ? intval($arr['xchan_deleted']) : 0)
	];

	return create_table_from_array('xchan',$store);
}



function xchan_store($arr) {

	logger('xchan_store: ' . print_r($arr,true));

	if(! $arr['hash'])
		$arr['hash'] = $arr['guid'];
	if(! $arr['hash'])
		return false;

	$r = q("select * from xchan where xchan_hash = '%s' limit 1",
		dbesc($arr['hash'])
	);
	if($r)
		return true;

	if(! $arr['network'])
		$arr['network'] = 'unknown';
	if(! $arr['name'])
		$arr['name'] = 'unknown';
	if(! $arr['url'])
		$arr['url'] = z_root();
	if(! $arr['photo'])
		$arr['photo'] = z_root() . '/' . get_default_profile_photo();


	if($arr['network'] === 'zot') {
		if((! $arr['key']) || (! rsa_verify($arr['guid'],base64url_decode($arr['guid_sig']),$arr['key']))) {
			logger('Unable to verify signature for ' . $arr['hash']);
			return false;
		}
	}

	$x = [];
	foreach($arr as $k => $v) {
		if($k === 'key') {
			$x['xchan_pubkey'] = $v;
			continue;
		}
		if($k === 'photo') {
			continue;
		}
		
		$x['xchan_' . $k] = $v;
	}

	$x['xchan_name_date'] = datetime_convert();

	$r = xchan_store_lowlevel($x);

	if(! $r)
		return $r;

	$photos = import_xchan_photo($arr['photo'],$arr['hash']);
	$r = q("update xchan set xchan_photo_date = '%s', xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s = '%s', xchan_photo_mimetype = '%s' where xchan_hash = '%s'",
		dbesc(datetime_convert()),
		dbesc($photos[0]),
		dbesc($photos[1]),
		dbesc($photos[2]),
		dbesc($photos[3]),
		dbesc($arr['hash'])
	);
	return $r;

}


function xchan_fetch($arr) {

	$key = '';
	if($arr['hash']) {
		$key = 'xchan_hash';
		$v = $arr['hash'];
	}
	elseif($arr['guid']) {
		$key = 'xchan_guid';
		$v = $arr['guid'];
	}
	elseif($arr['address']) {
		$key = 'xchan_addr';
		$v = $arr['address'];
	}

	if(! $key)
		return false;

	$r = q("select * from xchan where $key = '$v' limit 1");
	if(! $r)
		return false;

	$ret = array();
	foreach($r[0] as $k => $v) {
		if($k === 'xchan_addr')
			$ret['address'] = $v;
		else
			$ret[str_replace('xchan_','',$k)] = $v;
	}
	return $ret;
}


function xchan_change_key($oldx,$newx,$data) {

	$r = q("update abook set abook_xchan = '%s' where abook_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update abconfig set xchan = '%s' where xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update group_member set xchan = '%s' where xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update chat set chat_xchan = '%s' where chat_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update chatpresence set cp_xchan = '%s' where cp_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update event set event_xchan = '%s' where event_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update item set owner_xchan = '%s' where owner_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update item set author_xchan = '%s' where author_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update item set source_xchan = '%s' where source_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update mail set from_xchan = '%s' where from_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update mail set to_xchan = '%s' where to_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update shares set share_xchan = '%s' where share_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update source set src_channel_xchan = '%s' where src_channel_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update source set src_xchan = '%s' where src_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update xchat set xchat_xchan = '%s' where xchat_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update xconfig set xchan = '%s' where xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update xign set xchan = '%s' where xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update xlink set xlink_xchan = '%s' where xlink_xchan = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update xprof set xprof_hash = '%s' where xprof_hash = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("update xtag set xtag_hash = '%s' where xtag_hash = '%s'",
		dbsec($newx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	$r = q("select channel_id, channel_allow_cid, channel_deny_cid from channel where (channel_allow_cid like '%s' or channel_deny_cid like '%s') ",
		dbesc($oldx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	if($r) {
		foreach($r as $rv) {
			$z = q("update channel set channel_allow_cid = '%s', channel_deny_cid = '%s' where channel_id = %d",
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['channel_allow_cid'])),
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['channel_deny_cid'])),
				intval($rv['channel_id'])
			);
		}
	}

	$r = q("select id, allow_cid, deny_cid from attach where (allow_cid like '%s' or deny_cid like '%s') ",
		dbesc($oldx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	if($r) {
		foreach($r as $rv) {
			$z = q("update attach set allow_cid = '%s', deny_cid = '%s' where id = %d",
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['allow_cid'])),
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['deny_cid'])),
				intval($rv['id'])
			);
		}
	}

	$r = q("select cr_id, allow_cid, deny_cid from chatroom where (allow_cid like '%s' or deny_cid like '%s') ",
		dbesc($oldx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	if($r) {
		foreach($r as $rv) {
			$z = q("update chatroom set allow_cid = '%s', deny_cid = '%s' where cr_id = %d",
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['allow_cid'])),
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['deny_cid'])),
				intval($rv['cr_id'])
			);
		}
	}


	$r = q("select id, allow_cid, deny_cid from event where (allow_cid like '%s' or deny_cid like '%s') ",
		dbesc($oldx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	if($r) {
		foreach($r as $rv) {
			$z = q("update event set allow_cid = '%s', deny_cid = '%s' where id = %d",
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['allow_cid'])),
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['deny_cid'])),
				intval($rv['id'])
			);
		}
	}


	$r = q("select id, allow_cid, deny_cid from item where (allow_cid like '%s' or deny_cid like '%s') ",
		dbesc($oldx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	if($r) {
		foreach($r as $rv) {
			$z = q("update item set allow_cid = '%s', deny_cid = '%s' where id = %d",
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['allow_cid'])),
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['deny_cid'])),
				intval($rv['id'])
			);
		}
	}


	$r = q("select mitem_id, allow_cid, deny_cid from menu_item where (allow_cid like '%s' or deny_cid like '%s') ",
		dbesc($oldx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	if($r) {
		foreach($r as $rv) {
			$z = q("update menu_item set allow_cid = '%s', deny_cid = '%s' where mitem_id = %d",
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['allow_cid'])),
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['deny_cid'])),
				intval($rv['mitem_id'])
			);
		}
	}



	$r = q("select obj_id, allow_cid, deny_cid from obj where (allow_cid like '%s' or deny_cid like '%s') ",
		dbesc($oldx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	if($r) {
		foreach($r as $rv) {
			$z = q("update obj set allow_cid = '%s', deny_cid = '%s' where obj_id = %d",
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['allow_cid'])),
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['deny_cid'])),
				intval($rv['obj_id'])
			);
		}
	}

	$r = q("select id, allow_cid, deny_cid from photo where (allow_cid like '%s' or deny_cid like '%s') ",
		dbesc($oldx['xchan_hash']),
		dbesc($oldx['xchan_hash'])
	);

	if($r) {
		foreach($r as $rv) {
			$z = q("update photo set allow_cid = '%s', deny_cid = '%s' where id = %d",
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['allow_cid'])),
				dbesc(str_replace('<' . $oldx['xchan_hash'] . '>', '<' . $newx['xchan_hash'] . '>', $rv['deny_cid'])),
				intval($rv['id'])
			);
		}
	}


}