api_zot.php 14.5 KB
Newer Older
Zot's avatar
Zot committed
1 2
<?php

Zot's avatar
Zot committed
3
	function zot_api_init() {
Zot's avatar
Zot committed
4 5
        api_register_func('api/red/version','api_zot_version',false);
        api_register_func('api/z/1.0/version','api_zot_version',false);
Zot's avatar
Zot committed
6 7 8
		api_register_func('api/export/basic','api_export_basic', true);
		api_register_func('api/red/channel/export/basic','api_export_basic', true);
		api_register_func('api/z/1.0/channel/export/basic','api_export_basic', true);
9 10
		api_register_func('api/red/channel/list','api_channel_list', true);
		api_register_func('api/z/1.0/channel/list','api_channel_list', true);
Zot's avatar
Zot committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
		api_register_func('api/red/channel/stream','api_channel_stream', true);
		api_register_func('api/z/1.0/channel/stream','api_channel_stream', true);
		api_register_func('api/red/files','api_attach_list', true);
		api_register_func('api/z/1.0/files','api_attach_list', true);
		api_register_func('api/red/filemeta', 'api_file_meta', true);
		api_register_func('api/z/1.0/filemeta', 'api_file_meta', true);
		api_register_func('api/red/filedata', 'api_file_data', true);
		api_register_func('api/z/1.0/filedata', 'api_file_data', true);
		api_register_func('api/red/file/export', 'api_file_export', true);
		api_register_func('api/z/1.0/file/export', 'api_file_export', true);
		api_register_func('api/red/file', 'api_file_detail', true);
		api_register_func('api/z/1.0/file', 'api_file_detail', true);
		api_register_func('api/red/albums','api_albums', true);
		api_register_func('api/z/1.0/albums','api_albums', true);
		api_register_func('api/red/photos','api_photos', true);
		api_register_func('api/z/1.0/photos','api_photos', true);
		api_register_func('api/red/photo', 'api_photo_detail', true);
		api_register_func('api/z/1.0/photo', 'api_photo_detail', true);
		api_register_func('api/red/group_members','api_group_members', true);
		api_register_func('api/z/1.0/group_members','api_group_members', true);
		api_register_func('api/red/group','api_group', true);
		api_register_func('api/z/1.0/group','api_group', true);
		api_register_func('api/red/xchan','api_red_xchan',true);
		api_register_func('api/z/1.0/xchan','api_red_xchan',true);
35 36
		api_register_func('api/red/item/update','zot_item_update', true);
		api_register_func('api/z/1.0/item/update','zot_item_update', true);
Zot's avatar
Zot committed
37 38 39
		api_register_func('api/red/item/full','red_item', true);
		api_register_func('api/z/1.0/item/full','red_item', true);

40
		api_register_func('api/z/1.0/network/stream','api_network_stream', true);
Zot's avatar
Zot committed
41
		api_register_func('api/z/1.0/abook','api_zot_abook_xchan',true);
Zot's avatar
Zot committed
42
		api_register_func('api/z/1.0/abconfig','api_zot_abconfig',true);
43
		api_register_func('api/z/1.0/perm_allowed','api_zot_perm_allowed',true);
Zot's avatar
Zot committed
44

Zot's avatar
Zot committed
45 46 47
		return;
	}

Zot's avatar
Zot committed
48

Zot's avatar
Zot committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	function api_zot_version($type) {

		if($type === 'xml') {
			header('Content-type: application/xml');
			echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<version>' . Zotlabs\Lib\System::get_project_version() . '</version>' . "\r\n";
			killme();
		}
		elseif($type === 'json') {
			header('Content-type: application/json');
			echo '"' . Zotlabs\Lib\System::get_project_version() . '"';
			killme();
		}
	}



Zot's avatar
Zot committed
65 66 67 68
	/*
	 * Red basic channel export
	 */

Zot's avatar
Zot committed
69
	function api_export_basic($type) {
Zot's avatar
Zot committed
70 71 72 73
		if(api_user() === false) {
			logger('api_export_basic: no user');
			return false;
		}
74 75 76 77 78 79 80
		$sections = (($_REQUEST['sections']) ? explode(',',$_REQUEST['sections']) : '');
		if($_REQUEST['posts']) {
			$sections = get_default_export_sections();
			$sections[] = 'items';
		}

		json_return_and_die(identity_basic_export(api_user(),$sections));
Zot's avatar
Zot committed
81 82 83
	}


84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	function api_network_stream($type) {
		if(api_user() === false) {
			logger('api_channel_stream: no user');
			return false;
		}

		$channel = channelx_by_n(api_user());
		if(! $channel)
			return false;


		if($_SERVER['REQUEST_METHOD'] == 'POST') {
			// json_return_and_die(post_activity_item($_REQUEST));
		}
		else {
99 100 101 102
			if(array_key_exists('dbegin',$_REQUEST))
				$_REQUEST['datequery2'] = $_REQUEST['dbegin'];
			if(array_key_exists('dend',$_REQUEST))
				$_REQUEST['datequery'] = $_REQUEST['dend'];
103 104 105 106 107 108 109 110 111 112 113 114 115 116

			$arr = $_REQUEST;
			$ret = [];	
			$i = items_fetch($arr,App::get_channel(),get_observer_hash());
			if($i) {
				foreach($i as $iv) {
					$ret[] = encode_item($iv);
				}
			}

			json_return_and_die($ret);
		}
	}

117 118 119 120 121
	function api_channel_list($type) {
		if(api_user() === false) {
			logger('api_channel_stream: no user');
			return false;
		}
122

123 124 125
		$channel = channelx_by_n(api_user());
		if(! $channel)
			return false;
126

127
		$ret = [];
128

129 130 131 132 133 134 135 136 137 138 139 140 141
		$r = q("select channel_address from channel where channel_account_id = %d",
			intval($channel['channel_account_id'])
		);

		if($r) {
			foreach($r as $rv) {
				$ret[] = $rv['channel_address'];
			}
		}

		json_return_and_die($ret);

	}
142 143


Zot's avatar
Zot committed
144
	function api_channel_stream($type) {
Zot's avatar
Zot committed
145 146 147 148 149
		if(api_user() === false) {
			logger('api_channel_stream: no user');
			return false;
		}

150 151 152 153 154
		$channel = channelx_by_n(api_user());
		if(! $channel)
			return false;


Zot's avatar
Zot committed
155 156 157 158
		if($_SERVER['REQUEST_METHOD'] == 'POST') {
			json_return_and_die(post_activity_item($_REQUEST));
		}
		else {
159 160 161
			$mindate = (($_REQUEST['mindate']) ? datetime_convert('UTC','UTC',$_REQUEST['mindate']) : '');
        	if(! $mindate)
            	$mindate = datetime_convert('UTC','UTC', 'now - 14 days');
Zot's avatar
Zot committed
162

163
			json_return_and_die(zot_feed($channel['channel_id'],$channel['channel_hash'],[ 'mindate' => $mindate ]));
Zot's avatar
Zot committed
164 165 166 167
		}
	}

	function api_attach_list($type) {
Zot's avatar
Zot committed
168 169 170
		if(api_user() === false) 
			return false;

Zot's avatar
Zot committed
171
		logger('api_user: ' . api_user());
172 173 174 175 176 177
		$hash     = ((array_key_exists('filehash',$_REQUEST)) ? $_REQUEST['filehash'] : '');
		$filename = ((array_key_exists('filename',$_REQUEST)) ? $_REQUEST['filename'] : '');
		$filetype = ((array_key_exists('filetype',$_REQUEST)) ? $_REQUEST['filetype'] : '');
		$start    = ((array_key_exists('start',$_REQUEST))    ? intval($_REQUEST['start'])   : 0);
		$records  = ((array_key_exists('records',$_REQUEST))  ? intval($_REQUEST['records']) : 0);

Zot's avatar
Zot committed
178 179 180 181 182
		$since    = ((array_key_exists('since',$_REQUEST))    ? datetime_convert(date_default_timezone_get(),'UTC',$_REQUEST['since'])   : NULL_DATE);
		$until    = ((array_key_exists('until',$_REQUEST))    ? datetime_convert(date_default_timezone_get(),'UTC',$_REQUEST['until'])   : datetime_convert());

		$x = attach_list_files(api_user(),get_observer_hash(),$hash,$filename,$filetype,'created asc',$start,$records, $since, $until);

Zot's avatar
Zot committed
183 184 185 186 187 188
		if($start || $records) {
			$x['start'] = $start;
			$x['records'] = count($x['results']);
		}

		json_return_and_die($x);
Zot's avatar
Zot committed
189 190 191 192
	}


	function api_file_meta($type) {
Zot's avatar
Zot committed
193 194
		if(api_user() === false)
			return false;
Zot's avatar
Zot committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
		if(! $_REQUEST['file_id']) return false;
		$r = q("select * from attach where uid = %d and hash = '%s' limit 1",
			intval(api_user()),
			dbesc($_REQUEST['file_id'])
		);
		if($r) {
			unset($r[0]['content']);				
			$ret = array('attach' => $r[0]);
			json_return_and_die($ret);
		}
		killme();
	}



	function api_file_data($type) {
Zot's avatar
Zot committed
211 212
		if(api_user() === false)
			return false;
Zot's avatar
Zot committed
213 214 215 216
		if(! $_REQUEST['file_id']) return false;
		$start = (($_REQUEST['start']) ? intval($_REQUEST['start']) : 0);
		$length = (($_REQUEST['length']) ? intval($_REQUEST['length']) : 0);

Zot's avatar
Zot committed
217
		$r = q("select * from attach where uid = %d and hash like '%s' limit 1",
Zot's avatar
Zot committed
218
			intval(api_user()),
Zot's avatar
Zot committed
219
			dbesc($_REQUEST['file_id'] . '%')
Zot's avatar
Zot committed
220 221 222 223 224 225 226 227 228 229
		);
		if($r) {
			$ptr = $r[0];
			if($length === 0)
				$length = intval($ptr['filesize']);

			if($ptr['is_dir'])
				$ptr['content'] = '';
			elseif(! intval($r[0]['os_storage'])) {
				$ptr['start'] = $start;
Zot's avatar
Zot committed
230
				$x = substr(dbunescbin($ptr['content']),$start,$length);
Zot's avatar
Zot committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
				$ptr['length'] = strlen($x);
				$ptr['content'] = base64_encode($x);
			}
			else {
				$fp = fopen(dbunescbin($ptr['content']),'r');
				if($fp) {
					$seek = fseek($fp,$start,SEEK_SET);
					$x = fread($fp,$length);
					$ptr['start'] = $start;
					$ptr['length'] = strlen($x);
					$ptr['content'] = base64_encode($x);
				}
			}
				
			$ret = array('attach' => $ptr);
			json_return_and_die($ret);
		}
		killme();
	}


	function api_file_export($type) {
Zot's avatar
Zot committed
253 254 255 256
		if(api_user() === false)
			return false;
		if(! $_REQUEST['file_id'])
			return false;
Zot's avatar
Zot committed
257

Zot's avatar
Zot committed
258 259 260 261
		$channel = channelx_by_n(api_user());

		$ret = attach_export_data($channel,$_REQUEST['file_id']);

Zot's avatar
Zot committed
262 263 264 265 266 267 268 269
		if($ret) {
			json_return_and_die($ret);
		}
		killme();
	}


	function api_file_detail($type) {
Zot's avatar
Zot committed
270 271
		if(api_user() === false)
			return false;
Zot's avatar
Zot committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
		if(! $_REQUEST['file_id']) return false;
		$r = q("select * from attach where uid = %d and hash = '%s' limit 1",
			intval(api_user()),
			dbesc($_REQUEST['file_id'])
		);
		if($r) {
			if($r[0]['is_dir'])
				$r[0]['content'] = '';
			elseif(intval($r[0]['os_storage'])) 
				$r[0]['content'] = base64_encode(file_get_contents(dbunescbin($r[0]['content'])));
			else
				$r[0]['content'] = base64_encode(dbunescbin($r[0]['content']));
				
			$ret = array('attach' => $r[0]);
			json_return_and_die($ret);
		}
		killme();
	}



	function api_albums($type) {
Zot's avatar
Zot committed
294 295
		if(api_user() === false)
			return false;
Zot's avatar
Zot committed
296 297 298 299
		json_return_and_die(photos_albums_list(App::get_channel(),App::get_observer()));
	}

	function api_photos($type) {
Zot's avatar
Zot committed
300 301
		if(api_user() === false)
			return false;
Zot's avatar
Zot committed
302 303 304 305 306
		$album = $_REQUEST['album'];
		json_return_and_die(photos_list_photos(App::get_channel(),App::get_observer(),$album));
	}

	function api_photo_detail($type) {
Zot's avatar
Zot committed
307 308
		if(api_user() === false)
			return false;
Zot's avatar
Zot committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
		if(! $_REQUEST['photo_id']) return false;
		$scale = ((array_key_exists('scale',$_REQUEST)) ? intval($_REQUEST['scale']) : 0);
		$r = q("select * from photo where uid = %d and resource_id = '%s' and imgscale = %d limit 1",
			intval(local_channel()),
			dbesc($_REQUEST['photo_id']),
			intval($scale)
		);
		if($r) {
            $data = dbunescbin($r[0]['content']);
			if(array_key_exists('os_storage',$r[0]) && intval($r[0]['os_storage']))
				$data = file_get_contents($data);
			$r[0]['content'] = base64_encode($data);
			$ret = array('photo' => $r[0]);
			$i = q("select id from item where uid = %d and resource_type = 'photo' and resource_id = '%s' limit 1",
				intval(local_channel()),
				dbesc($_REQUEST['photo_id'])
			);
			if($i) {
				$ii = q("select * from item where parent = %d order by id",
					intval($i[0]['id'])
				);
				if($ii) {
					xchan_query($ii,true,0);
					$ii = fetch_post_tags($ii,true);
					if($ii) {
						$ret['item'] = array();
						foreach($ii as $iii)
							$ret['item'][] = encode_item($iii,true);
					}
				}
			}

			json_return_and_die($ret);
		}
		killme();
	}

	function api_group_members($type) {
		if(api_user() === false)
			return false;

350 351
		$r = null;

Zot's avatar
Zot committed
352
		if($_REQUEST['group_id']) {
Max Kostikov's avatar
Max Kostikov committed
353
			$r = q("select * from pgrp where uid = %d and id = %d limit 1",
Zot's avatar
Zot committed
354 355 356 357
				intval(api_user()),
				intval($_REQUEST['group_id'])
			);
		}
358
		elseif($_REQUEST['group_name']) {
Max Kostikov's avatar
Max Kostikov committed
359
			$r = q("select * from pgrp where uid = %d and gname = '%s' limit 1",
360 361 362 363
				intval(api_user()),
				dbesc($_REQUEST['group_name'])
			);
		}
Zot's avatar
Zot committed
364

365
		if($r) {
Max Kostikov's avatar
Max Kostikov committed
366
			$x = q("select * from pgrp_member left join abook on abook_xchan = xchan and abook_channel = pgrp_member.uid left join xchan on pgrp_member.xchan = xchan.xchan_hash 
Zot's avatar
Zot committed
367
				where gid = %d",
368 369 370 371
				intval($r[0]['id'])
			);
			json_return_and_die($x);
		}
Zot's avatar
Zot committed
372

373
	}
Zot's avatar
Zot committed
374 375 376 377 378

	function api_group($type) {
		if(api_user() === false)
			return false;

Max Kostikov's avatar
Max Kostikov committed
379
		$r = q("select * from pgrp where uid = %d",
Zot's avatar
Zot committed
380 381 382 383 384 385 386 387 388 389 390
			intval(api_user())
		);
		json_return_and_die($r);
	}


	function api_red_xchan($type) {
		if(api_user() === false)
			return false;
		logger('api_xchan');

391 392
		if($_SERVER['REQUEST_METHOD'] === 'POST') {			
			$r = xchan_store($_REQUEST);
Zot's avatar
Zot committed
393 394 395 396 397
		}
		$r = xchan_fetch($_REQUEST);
		json_return_and_die($r);
	};

Zot's avatar
Zot committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	function api_zot_abook_xchan($type) {
		logger('api_abook_xchan');

		if(api_user() === false)
			return false;

		$sql_extra = ((array_key_exists('abook_id',$_REQUEST) && intval($_REQUEST['abook_id'])) ? ' and abook_id = ' . intval($_REQUEST['abook_id']) . ' ' : '');
		if($_SERVER['REQUEST_METHOD'] === 'POST') {
			// update
		}
		$r = q("select * from abook left join xchan on abook_xchan = xchan_hash where abook_channel = %d $sql_extra ",
			intval(api_user())
		);

		json_return_and_die($r);
	};

Zot's avatar
Zot committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
	function api_zot_abconfig($type) {

		if(api_user() === false)
			return false;

		$sql_extra = ((array_key_exists('abook_id',$_REQUEST) && intval($_REQUEST['abook_id'])) ? ' and abook_id = ' . intval($_REQUEST['abook_id']) . ' ' : '');
		if($_SERVER['REQUEST_METHOD'] === 'POST') {
			// update
		}
		$r = q("select abconfig.* from abconfig left join abook on abook_xchan = abconfig.xchan and abook_channel = abconfig.chan where abconfig.chan = %d $sql_extra ",
			intval(api_user())
		);

		json_return_and_die($r);

	}
Zot's avatar
Zot committed
431

432 433 434 435 436 437 438
	function api_zot_perm_allowed($type) {
		if(api_user() === false)
			return false;

		$perm = ((array_key_exists('perm',$_REQUEST)) ? $_REQUEST['perm'] : '');

		if(array_key_exists('abook_id',$_REQUEST) && intval($_REQUEST['abook_id'])) {
439
			$r = q("select abook_xchan as hash from abook where abook_id = %d and abook_channel = %d limit 1",
440 441 442 443 444 445 446 447
				intval($_REQUEST['abook_id']),
				intval(api_user())
			);
		}
		else {
			$r = xchan_fetch($_REQUEST);
		}

448 449 450 451
		$x = false;

		if($r) {
			if($perm)
452
				$x = [ [ 'perm' => $perm, 'allowed' => perm_is_allowed(api_user(), $r[0]['hash'], $perm)] ];
453 454
			else {
				$x = [];
455
				$p = get_all_perms(api_user(),$r[0]['hash']);
456 457 458 459 460 461 462
				if($p) {
					foreach($p as $k => $v)
						$x[] = [ 'perm' => $k, 'allowed' => $v ];
				}
			}
		}
		
463
		json_return_and_die($x);
464

465
	}
Zot's avatar
Zot committed
466

467
	function zot_item_update($type) {
Zot's avatar
Zot committed
468 469

		if (api_user() === false) {
470
			logger('api_red_item_store: no user');
Zot's avatar
Zot committed
471 472 473
			return false;
		}

474 475
		logger('api_red_item_store: REQUEST ' . print_r($_REQUEST,true));
		logger('api_red_item_store: FILES ' . print_r($_FILES,true));
Zot's avatar
Zot committed
476 477 478 479 480 481 482 483 484 485


		// set this so that the item_post() function is quiet and doesn't redirect or emit json

		$_REQUEST['api_source'] = true;
		$_REQUEST['profile_uid'] = api_user();

		if(x($_FILES,'media')) {
			$_FILES['userfile'] = $_FILES['media'];
			// upload the image if we have one
486
			$mod = new Zotlabs\Module\Wall_attach();
Zot's avatar
Zot committed
487
			$media = $mod->post();
488 489
			if($media)
				$_REQUEST['body'] .= "\n\n" . $media;
Zot's avatar
Zot committed
490 491 492 493 494 495 496 497 498
		}

		$mod = new Zotlabs\Module\Item();
		$x = $mod->post();	
		json_return_and_die($x);
	}



Zot's avatar
Zot committed
499
	function red_item($type) {
Zot's avatar
Zot committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

		if (api_user() === false) {
			logger('api_red_item_full: no user');
			return false;
		}

		if($_REQUEST['mid']) {
			$arr = array('mid' => $_REQUEST['mid']);
		}
		elseif($_REQUEST['item_id']) {
			$arr = array('item_id' => $_REQUEST['item_id']);
		}
		else
			json_return_and_die(array());

		$arr['start'] = 0;
		$arr['records'] = 999999;
		$arr['item_type'] = '*';

		$i = items_fetch($arr,App::get_channel(),get_observer_hash());

		if(! $i)
			json_return_and_die(array());

		$ret = array();
		$tmp = array();
		foreach($i as $ii) {
			$tmp[] = encode_item($ii,true);
		}
		$ret['item'] = $tmp;	
					 
		json_return_and_die($ret);
	}