xchan.php 7.83 KB
Newer Older
1 2
<?php

3 4
use Zotlabs\Zot6\HTTPSig;

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

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) {

44 45 46 47 48 49 50
	$update_photo = false;
	$update_name = false;

	if(! ($arr['guid'] || $arr['hash'])) {
		$arr = json_decode(file_get_contents('php://input'),true);
	}

51 52 53 54 55 56 57 58 59 60
	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'])
	);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	if(! $r) {

		$update_photo = 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'] === 'zot6') {
			if((! $arr['key']) || (! Libzot::verify($arr['id'],$arr['id_sig'],$arr['key']))) {
				logger('Unable to verify signature for ' . $arr['hash']);
				return false;
			}
79 80
		}

81 82 83 84 85
		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;
			}
86 87
		}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		$columns = db_columns('xchan');

		$x = [];
		foreach($arr as $k => $v) {
			if($k === 'key') {
				$x['xchan_pubkey'] = HTTPSig::convertKey(escape_tags($v));;
				continue;
			}
			if($k === 'photo') {
				continue;
			}
		
			if(in_array($columns,'xchan_' . $k))
				$x['xchan_' . $k] = escape_tags($v);
		}
103

104 105 106
		$x['xchan_name_date'] = datetime_convert();
		$x['xchan_photo_date'] = datetime_convert();
		$x['xchan_system']     = false;
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		$result = xchan_store_lowlevel($x);
	
		if(! $result)
			return $result;
	}
	else {
		if($r[0]['network'] === 'zot6') {
			return true;
		}
		if($r[0]['xchan_photo_date'] < datetime_convert('UTC','UTC',$arr['photo_date'])) {
			$update_photo = true;
		}
		if($r[0]['xchan_name_date'] < datetime_convert('UTC','UTC',$arr['name_date'])) {
			$update_name = true;
		}
    }

	if($update_photo && $arr['photo']) {
		$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'])
		);
	}
	if($update_name && $arr['name']) {
		$x = q("update xchan set xchan_name = '%s', xchan_name_date = '%s' where xchan_hash = '%s'",
			dbesc(escape_tags($arr['name'])),
			dbesc(datetime_convert()),
			dbesc($arr['hash'])
		);
	}
143

144
	return true;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
}


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;
}


Zot's avatar
Zot committed
182 183 184 185
function xchan_keychange_table($table,$column,$oldxchan,$newxchan) {
	$r = q("update $table set $column = '%s' where $column = '%s'",
		dbesc($newxchan['xchan_hash']),
		dbesc($oldxchan['xchan_hash'])
186
	);
Zot's avatar
Zot committed
187 188
	return $r;
}
189

Zot's avatar
Zot committed
190
function xchan_keychange_acl($table,$column,$oldxchan,$newxchan) {
191

Zot's avatar
Zot committed
192 193
	$allow = (($table === 'channel') ? 'channel_allow_cid' : 'allow_cid');
	$deny  = (($table === 'channel') ? 'channel_deny_cid'  : 'deny_cid');
194 195


Zot's avatar
Zot committed
196 197 198
	$r = q("select $column, $allow, $deny from $table where ($allow like '%s' or $deny like '%s') ",
		dbesc('<' . $oldxchan['xchan_hash'] . '>'),
		dbesc('<' . $oldxchan['xchan_hash'] . '>')
199 200 201 202
	);

	if($r) {
		foreach($r as $rv) {
Zot's avatar
Zot committed
203 204 205 206 207 208
			$z = q("update $table set $allow = '%s', $deny = '%s' where $column = %d",
				dbesc(str_replace('<' . $oldxchan['xchan_hash'] . '>', '<' . $newxchan['xchan_hash'] . '>', 
					$rv[$allow])),
				dbesc(str_replace('<' . $oldxchan['xchan_hash'] . '>', '<' . $newxchan['xchan_hash'] . '>', 
					$rv[$deny])),
				intval($rv[$column])
209 210 211
			);
		}
	}
Zot's avatar
Zot committed
212 213
	return $z;
}
214 215


Zot's avatar
Zot committed
216
function xchan_change_key($oldx,$newx,$data) {
217

Zot's avatar
Zot committed
218 219 220
	$tables = [
		'abook'        => 'abook_xchan',
		'abconfig'     => 'xchan',
Max Kostikov's avatar
Max Kostikov committed
221
		'pgrp_member' => 'xchan',
Zot's avatar
Zot committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		'chat'         => 'chat_xchan',
		'chatpresence' => 'cp_xchan',
		'event'        => 'event_xchan',
		'item'         => 'owner_xchan',
		'item'         => 'author_xchan',
		'item'         => 'source_xchan',
		'mail'         => 'from_xchan',
		'mail'         => 'to_xchan',
		'shares'       => 'share_xchan',
		'source'       => 'src_channel_xchan',
		'source'       => 'src_xchan',
		'xchat'        => 'xchat_xchan',
		'xconfig'      => 'xchan',
		'xign'         => 'xchan',
		'xlink'        => 'xlink_xchan',
		'xprof'        => 'xprof_hash',
		'xtag'         => 'xtag_hash'
	];
	

	$acls = [
		'channel'   => 'channel_id',
		'attach'    => 'id',
		'chatroom'  => 'cr_id',
		'event'     => 'id',
		'item'      => 'id',
		'menu_item' => 'mitem_id',
		'obj'       => 'obj_id',
		'photo'     => 'id'
	];
252 253


Zot's avatar
Zot committed
254 255
	foreach($tables as $k => $v) {
		xchan_keychange_table($k,$v,$oldx,$newx);
256 257
	}

Zot's avatar
Zot committed
258 259
	foreach($acls as $k => $v) {
		xchan_keychange_acl($k,$v,$oldx,$newx);
260 261
	}
}