<?php

/**
 * [UCenter] (C)2001-2099 Discuz! Team
 * This is NOT a freeware, use is subject to license terms
 * https://license.discuz.vip
 */

if(!defined('UC_API')) {
	exit('Access denied');
}

error_reporting(0);

const IN_UC = TRUE;
define('UC_ROOT', substr(__FILE__, 0, -10));
require UC_ROOT.'./release/release.php';
const UC_DATADIR = DISCUZ_DATA;
const UC_DATAURL = '';
define('UC_API_FUNC', ((defined('UC_CONNECT') && UC_CONNECT == 'mysql') || defined('UC_STANDALONE') && UC_STANDALONE) ? 'uc_api_mysql' : 'uc_api_post');
$uc_controls = [];

function uc_addslashes($string, $force = 0, $strip = FALSE) {
	if(is_array($string)) {
		foreach($string as $key => $val) {
			$string[$key] = uc_addslashes($val, $force, $strip);
		}
	} else {
		$string = addslashes($strip ? stripslashes($string) : $string);
	}
	return $string;
}

if(!function_exists('daddslashes')) {
	function daddslashes($string, $force = 0) {
		return uc_addslashes($string, $force);
	}
}


if(!function_exists('dhtmlspecialchars')) {
	function dhtmlspecialchars($string, $flags = null) {
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = dhtmlspecialchars($val, $flags);
			}
		} else {
			if($flags === null) {
				$string = str_replace(['&', '"', '<', '>'], ['&amp;', '&quot;', '&lt;', '&gt;'], $string);
				if(str_contains($string, '&amp;#')) {
					$string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $string);
				}
			} else {
				if(strtolower(CHARSET) == 'utf-8') {
					$charset = 'UTF-8';
				} else {
					$charset = 'ISO-8859-1';
				}
				$string = htmlspecialchars($string, $flags, $charset);
			}
		}
		return $string;
	}
}
if(!function_exists('fsocketopen')) {
	function fsocketopen($hostname, $port = 80, &$errno = null, &$errstr = null, $timeout = 15) {
		$fp = '';
		if(function_exists('fsockopen')) {
			$fp = @fsockopen($hostname, $port, $errno, $errstr, $timeout);
		} elseif(function_exists('pfsockopen')) {
			$fp = @pfsockopen($hostname, $port, $errno, $errstr, $timeout);
		} elseif(function_exists('stream_socket_client')) {
			$fp = @stream_socket_client($hostname.':'.$port, $errno, $errstr, $timeout);
		}
		return $fp;
	}
}

function uc_api_post($module, $action, $arg = []) {
	$s = $sep = '';
	foreach($arg as $k => $v) {
		$k = urlencode($k);
		if(is_array($v)) {
			$s2 = $sep2 = '';
			foreach($v as $k2 => $v2) {
				$k2 = urlencode($k2);
				$s2 .= "$sep2{$k}[$k2]=".urlencode($v2);
				$sep2 = '&';
			}
			$s .= $sep.$s2;
		} else {
			$s .= "$sep$k=".urlencode($v);
		}
		$sep = '&';
	}
	$postdata = uc_api_requestdata($module, $action, $s);
	return uc_fopen2(UC_API.'/index.php', 500000, $postdata, '', TRUE, UC_IP, 20);
}

function uc_api_requestdata($module, $action, $arg = '', $extra = '') {
	$input = uc_api_input($arg, $module, $action);
	$post = "m=$module&a=$action&inajax=2&release=".UC_CLIENT_RELEASE."&input=$input&appid=".UC_APPID.$extra;
	return $post;
}

function uc_api_url($module, $action, $arg = '', $extra = '') {
	$url = UC_API.'/index.php?'.uc_api_requestdata($module, $action, $arg, $extra);
	return $url;
}

function uc_api_input($data, $module, $action) {
	$data = $data."&m=$module&a=$action&appid=".UC_APPID;
	$s = urlencode(uc_authcode($data.'&agent='.md5($_SERVER['HTTP_USER_AGENT']).'&time='.time(), 'ENCODE', UC_KEY));
	return $s;
}

function uc_api_mysql($model, $action, $args = []) {
	global $uc_controls;
	if(empty($uc_controls[$model])) {
		include_once UC_ROOT.'./lib/dbi.class.php';
		include_once UC_ROOT.'./model/base.php';
		include_once UC_ROOT."./control/$model.php";
		$modelname = $model.'control';
		$uc_controls[$model] = new $modelname();
	}
	if($action[0] != '_') {
		$args = uc_addslashes($args, 1, TRUE);
		$action = 'on'.$action;
		$uc_controls[$model]->input = $args;
		return $uc_controls[$model]->$action($args);
	} else {
		return '';
	}
}

function uc_serialize($arr, $htmlon = 0) {
	include_once UC_ROOT.'./lib/xml.class.php';
	return xml_serialize($arr, $htmlon);
}

function uc_unserialize($s) {
	include_once UC_ROOT.'./lib/xml.class.php';
	return xml_unserialize($s);
}

function uc_authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {

	
	$ckey_length = 4;

	$key = md5($key ? $key : UC_KEY);
	
	$keya = md5(substr($key, 0, 16));
	$keyb = md5(substr($key, 16, 16));
	$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';

	
	$cryptkey = $keya.md5($keya.$keyc);
	$key_length = strlen($cryptkey);

	
	
	$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
	$string_length = strlen($string);

	$result = '';
	$box = range(0, 255);

	
	$rndkey = [];
	for($i = 0; $i <= 255; $i++) {
		$rndkey[$i] = ord($cryptkey[$i % $key_length]);
	}

	
	
	for($j = $i = 0; $i < 256; $i++) {
		$j = ($j + $box[$i] + $rndkey[$i]) % 256;
		$tmp = $box[$i];
		$box[$i] = $box[$j];
		$box[$j] = $tmp;
	}

	
	for($a = $j = $i = 0; $i < $string_length; $i++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box[$a]) % 256;
		$tmp = $box[$a];
		$box[$a] = $box[$j];
		$box[$j] = $tmp;
		$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
	}

	if($operation == 'DECODE') {
		
		
		
		if(((int)substr($result, 0, 10) == 0 || (int)substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) === substr(md5(substr($result, 26).$keyb), 0, 16)) {
			return substr($result, 26);
		} else {
			return '';
		}
	} else {
		
		return $keyc.str_replace('=', '', base64_encode($result));
	}
}

function uc_fopen2($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE', $allowcurl = TRUE) {
	$__times__ = isset($_GET['__times__']) ? intval($_GET['__times__']) + 1 : 1;
	if($__times__ > 2) {
		return '';
	}
	$url .= (!str_contains($url, '?') ? '?' : '&')."__times__=$__times__";
	return uc_fopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block, $encodetype, $allowcurl);
}

function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE', $allowcurl = TRUE) {
	$return = '';
	$matches = parse_url($url);
	$scheme = strtolower($matches['scheme']);
	$host = $matches['host'];
	$path = !empty($matches['path']) ? $matches['path'].(!empty($matches['query']) ? '?'.$matches['query'] : '') : '/';
	$port = !empty($matches['port']) ? $matches['port'] : ($scheme == 'https' ? 443 : 80);

	if(function_exists('curl_init') && function_exists('curl_exec') && $allowcurl) {
		$ch = curl_init();
		$ip && curl_setopt($ch, CURLOPT_HTTPHEADER, ['Host: '.$host]);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		
		
		if(!empty($ip) && filter_var($ip, FILTER_VALIDATE_IP) && !filter_var($host, FILTER_VALIDATE_IP)) {
			curl_setopt($ch, CURLOPT_RESOLVE, ["$host:$port:$ip"]);
			curl_setopt($ch, CURLOPT_URL, $scheme.'://'.$host.':'.$port.$path);
		} else {
			curl_setopt($ch, CURLOPT_URL, $scheme.'://'.($ip ? $ip : $host).':'.$port.$path);
		}
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		if($post) {
			curl_setopt($ch, CURLOPT_POST, 1);
			if($encodetype == 'URLENCODE') {
				curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
			} else {
				parse_str($post, $postarray);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $postarray);
			}
		}
		if($cookie) {
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$data = curl_exec($ch);
		$status = curl_getinfo($ch);
		$errno = curl_errno($ch);
		if($errno || $status['http_code'] != 200) {
			return;
		} else {
			return !$limit ? $data : substr($data, 0, $limit);
		}
	}

	if($post) {
		$out = "POST $path HTTP/1.0\r\n";
		$header = "Accept: */*\r\n";
		$header .= "Accept-Language: zh-cn\r\n";
		if($allowcurl) {
			$encodetype = 'URLENCODE';
		}
		$boundary = $encodetype == 'URLENCODE' ? '' : '; boundary='.trim(substr(trim($post), 2, strpos(trim($post), "\n") - 2));
		$header .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data$boundary\r\n";
		$header .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
		$header .= "Host: $host:$port\r\n";
		$header .= 'Content-Length: '.strlen($post)."\r\n";
		$header .= "Connection: Close\r\n";
		$header .= "Cache-Control: no-cache\r\n";
		$header .= "Cookie: $cookie\r\n\r\n";
		$out .= $header.$post;
	} else {
		$out = "GET $path HTTP/1.0\r\n";
		$header = "Accept: */*\r\n";
		$header .= "Accept-Language: zh-cn\r\n";
		$header .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
		$header .= "Host: $host:$port\r\n";
		$header .= "Connection: Close\r\n";
		$header .= "Cookie: $cookie\r\n\r\n";
		$out .= $header;
	}

	$fpflag = 0;
	$context = [];
	if($scheme == 'https') {
		$context['ssl'] = [
			'verify_peer' => false,
			'verify_peer_name' => false,
			'peer_name' => $host
		];
	}
	if(ini_get('allow_url_fopen')) {
		$context['http'] = [
			'method' => $post ? 'POST' : 'GET',
			'header' => $header,
			'timeout' => $timeout
		];
		if($post) {
			$context['http']['content'] = $post;
		}
		$context = stream_context_create($context);
		$fp = @fopen($scheme.'://'.($ip ? $ip : $host).':'.$port.$path, 'b', false, $context);
		$fpflag = 1;
	} elseif(function_exists('stream_socket_client')) {
		$context = stream_context_create($context);
		$fp = @stream_socket_client(($scheme == 'https' ? 'ssl://' : '').($ip ? $ip : $host).':'.$port, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context);
	} else {
		$fp = @fsocketopen(($scheme == 'https' ? 'ssl://' : '').($scheme == 'https' ? $host : ($ip ? $ip : $host)), $port, $errno, $errstr, $timeout);
	}

	if(!$fp) {
		return '';
	} else {
		stream_set_blocking($fp, $block);
		stream_set_timeout($fp, $timeout);
		if(!$fpflag) {
			@fwrite($fp, $out);
		}
		$status = stream_get_meta_data($fp);
		if(!$status['timed_out']) {
			while(!feof($fp) && !$fpflag) {
				if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
					break;
				}
			}

			$stop = false;
			while(!feof($fp) && !$stop) {
				$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
				$return .= $data;
				if($limit) {
					$limit -= strlen($data);
					$stop = $limit <= 0;
				}
			}
		}
		@fclose($fp);
		return $return;
	}
}

function uc_app_ls() {
	$return = call_user_func(UC_API_FUNC, 'app', 'ls', []);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_feed_add($icon, $uid, $username, $title_template = '', $title_data = '', $body_template = '', $body_data = '', $body_general = '', $target_ids = '', $images = []) {
	return call_user_func(UC_API_FUNC, 'feed', 'add',
		['icon' => $icon,
			'appid' => UC_APPID,
			'uid' => $uid,
			'username' => $username,
			'title_template' => $title_template,
			'title_data' => $title_data,
			'body_template' => $body_template,
			'body_data' => $body_data,
			'body_general' => $body_general,
			'target_ids' => $target_ids,
			'image_1' => $images[0]['url'],
			'image_1_link' => $images[0]['link'],
			'image_2' => $images[1]['url'],
			'image_2_link' => $images[1]['link'],
			'image_3' => $images[2]['url'],
			'image_3_link' => $images[2]['link'],
			'image_4' => $images[3]['url'],
			'image_4_link' => $images[3]['link']
		]
	);
}

function uc_feed_get($limit = 100, $delete = TRUE) {
	$return = call_user_func(UC_API_FUNC, 'feed', 'get', ['limit' => $limit, 'delete' => $delete]);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_friend_add($uid, $friendid, $comment = '') {
	return call_user_func(UC_API_FUNC, 'friend', 'add', ['uid' => $uid, 'friendid' => $friendid, 'comment' => $comment]);
}

function uc_friend_delete($uid, $friendids) {
	return call_user_func(UC_API_FUNC, 'friend', 'delete', ['uid' => $uid, 'friendids' => $friendids]);
}

function uc_friend_totalnum($uid, $direction = 0) {
	return call_user_func(UC_API_FUNC, 'friend', 'totalnum', ['uid' => $uid, 'direction' => $direction]);
}

function uc_friend_ls($uid, $page = 1, $pagesize = 10, $totalnum = 10, $direction = 0) {
	$return = call_user_func(UC_API_FUNC, 'friend', 'ls', ['uid' => $uid, 'page' => $page, 'pagesize' => $pagesize, 'totalnum' => $totalnum, 'direction' => $direction]);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_user_register($username, $password, $email = '', $questionid = '', $answer = '', $regip = '', $secmobicc = '', $secmobile = '', $censor = true) {
	if($censor) {
		$ret = censor($username, NULL, TRUE, FALSE);
		if(is_array($ret)) {
			return -1;
		}
	}
	return call_user_func(UC_API_FUNC, 'user', 'register', ['username' => $username, 'password' => $password, 'email' => $email, 'questionid' => $questionid, 'answer' => $answer, 'regip' => $regip, 'secmobicc' => $secmobicc, 'secmobile' => $secmobile]);
}

function uc_user_login($username, $password, $isuid = 0, $checkques = 0, $questionid = '', $answer = '', $ip = '', $nolog = 0) {
	$isuid = intval($isuid);
	$return = call_user_func(UC_API_FUNC, 'user', 'login', ['username' => $username, 'password' => $password, 'isuid' => $isuid, 'checkques' => $checkques, 'questionid' => $questionid, 'answer' => $answer, 'ip' => $ip, 'nolog' => $nolog]);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_user_synlogin($uid) {
	return '';
}

function uc_user_synlogout() {
	return '';
}

function uc_user_edit($username, $oldpw, $newpw, $email = '', $ignoreoldpw = 0, $questionid = '', $answer = '', $secmobicc = '', $secmobile = '') {
	return call_user_func(UC_API_FUNC, 'user', 'edit', ['username' => $username, 'oldpw' => $oldpw, 'newpw' => $newpw, 'email' => $email, 'ignoreoldpw' => $ignoreoldpw, 'questionid' => $questionid, 'answer' => $answer, 'secmobicc' => $secmobicc, 'secmobile' => $secmobile]);
}

function uc_user_delete($uid) {
	return call_user_func(UC_API_FUNC, 'user', 'delete', ['uid' => $uid, 'action' => 'delete']);
}

function uc_user_deleteavatar($uid) {
	if(UC_STANDALONE) {
		@include_once UC_ROOT.'./extend_client.php';
		uc_note_handler::loadavatarpath();
		uc_api_mysql('user', 'deleteavatar', ['uid' => $uid]);
	} else {
		uc_api_post('user', 'deleteavatar', ['uid' => $uid]);
	}

	$ftp = getglobal('setting/ftp');
	$oss = getglobal('setting/oss');
	if(!empty($ftp['on']) && $ftp['on'] == 2 && $oss['oss_avatar']) {
		ftpcmd('delete', 'avatar/'.uc_avatar_path($uid, 'big', 'real'));
		ftpcmd('delete', 'avatar/'.uc_avatar_path($uid, 'middle', 'real'));
		ftpcmd('delete', 'avatar/'.uc_avatar_path($uid, 'small', 'real'));
		ftpcmd('delete', 'avatar/'.uc_avatar_path($uid, 'big'));
		ftpcmd('delete', 'avatar/'.uc_avatar_path($uid, 'middle'));
		ftpcmd('delete', 'avatar/'.uc_avatar_path($uid, 'small'));
	}
}

function uc_avatar_path($uid, $size = 'big', $type = '') {
	$size = in_array($size, ['big', 'middle', 'small']) ? $size : 'big';
	$uid = abs(intval($uid));
	$uid = sprintf('%09d', $uid);
	$dir1 = substr($uid, 0, 3);
	$dir2 = substr($uid, 3, 2);
	$dir3 = substr($uid, 5, 2);
	$typeadd = $type == 'real' ? '_real' : '';
	return $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd."_avatar_$size.jpg";
}

function uc_user_checkname($username, $censor = true) {
	if($censor) {
		$ret = censor($username, NULL, TRUE, FALSE);
		if(is_array($ret)) {
			return -2;
		}
	}
	return call_user_func(UC_API_FUNC, 'user', 'check_username', ['username' => $username]);
}

function uc_user_checkemail($email) {
	return call_user_func(UC_API_FUNC, 'user', 'check_email', ['email' => $email]);
}

function uc_user_checksecmobile($secmobicc, $secmobile) {
	return call_user_func(UC_API_FUNC, 'user', 'check_secmobile', ['secmobicc' => $secmobicc, 'secmobile' => $secmobile]);
}

function uc_user_addprotected($username, $admin = '') {
	return call_user_func(UC_API_FUNC, 'user', 'addprotected', ['username' => $username, 'admin' => $admin]);
}

function uc_user_deleteprotected($username) {
	return call_user_func(UC_API_FUNC, 'user', 'deleteprotected', ['username' => $username]);
}

function uc_user_getprotected() {
	$return = call_user_func(UC_API_FUNC, 'user', 'getprotected', ['1' => 1]);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_get_user($username, $isuid = 0) {
	$return = call_user_func(UC_API_FUNC, 'user', 'get_user', ['username' => $username, 'isuid' => $isuid]);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_user_chgusername($uid, $newusername, $oldusername = '') {
	global $_G;
	$oldusername = $oldusername ? $oldusername : $_G['username'];
	if($newusername == $oldusername) {
		return 1;
	}
	require_once libfile('function/misc');

	$usernamelen = dstrlen($newusername);
	if($usernamelen < 3) {
		acpmsg('profile_username_tooshort', '', 'error');
	} elseif($usernamelen > 50) {
		acpmsg('profile_username_toolong', '', 'error');
	}

	$chguser = getuserbyuid($uid);
	if($newusername != $chguser['loginname']) {
		$ucresult = uc_user_checkname($newusername);
		if($ucresult < 0) {
			if($ucresult == -1) {
				acpmsg('members_chgusername_check_failed', '', 'error');
			} elseif($ucresult == -2) {
				acpmsg('members_chgusername_name_badword', '', 'error');
			} elseif($ucresult == -3) {
				acpmsg('members_chgusername_name_exists', '', 'error');
			} else {
				acpmsg('members_chgusername_change_failed', '', 'error');
			}
		}
	}
	$checkuid = table_common_member::t()->fetch_uid_by_username($newusername);
	if($checkuid && $checkuid != $uid) {
		acpmsg('members_add_username_duplicate', '', 'error');
	}
	$checkuid = table_common_member_archive::t()->fetch_uid_by_username($newusername);
	if($checkuid && $checkuid != $uid) {
		acpmsg('members_add_username_duplicate', '', 'error');
	}
	$his = table_common_member_username_history::t()->fetch($newusername);
	if($his && $his['uid'] != $uid) {
		acpmsg('profile_username_duplicate', '', 'error');
	}
	if(table_common_member_username_history::t()->fetch($oldusername)) {
		table_common_member_username_history::t()->update($oldusername, [
			'uid' => $uid,
			'dateline' => time()
		]);
	} else {
		table_common_member_username_history::t()->insert([
			'username' => $oldusername,
			'uid' => $uid,
			'dateline' => time()
		]);
	}
	table_common_member::t()->update_username($uid, $newusername);

	if($_G['setting']['chgusername']['othertable']) {
		@include_once UC_ROOT.'./extend_client.php';
		uc_note_handler::renameuser(['uid' => $uid, 'newusername' => $newusername], []);
	}
	return 1;
}

function uc_user_merge($oldusername, $newusername, $uid, $password, $email) {
	return call_user_func(UC_API_FUNC, 'user', 'merge', ['oldusername' => $oldusername, 'newusername' => $newusername, 'uid' => $uid, 'password' => $password, 'email' => $email]);
}

function uc_user_merge_remove($username) {
	return call_user_func(UC_API_FUNC, 'user', 'merge_remove', ['username' => $username]);
}

function uc_user_getcredit($appid, $uid, $credit) {
	return uc_api_post('user', 'getcredit', ['appid' => $appid, 'uid' => $uid, 'credit' => $credit]);
}


function uc_user_logincheck($username, $ip) {
	return call_user_func(UC_API_FUNC, 'user', 'logincheck', ['username' => $username, 'ip' => $ip]);
}

function uc_pm_location($uid, $newpm = 0) {
	$apiurl = uc_api_url('pm_client', 'ls', "uid=$uid&frontend=1", ($newpm ? '&folder=newbox' : ''));
	@header('Expires: 0');
	@header('Cache-Control: private, post-check=0, pre-check=0, max-age=0', FALSE);
	@header('Pragma: no-cache');
	@header("location: $apiurl");
}

function uc_pm_checknew($uid, $more = 0) {
	$return = call_user_func(UC_API_FUNC, 'pm', 'check_newpm', ['uid' => $uid, 'more' => $more]);
	return (!$more || UC_CONNECT == 'mysql') ? $return : uc_unserialize($return);
}

function uc_pm_send($fromuid, $msgto, $subject, $message, $instantly = 1, $replypmid = 0, $isusername = 0, $type = 0) {
	if($instantly) {
		$replypmid = @is_numeric($replypmid) ? $replypmid : 0;
		return call_user_func(UC_API_FUNC, 'pm', 'sendpm', ['fromuid' => $fromuid, 'msgto' => $msgto, 'subject' => $subject, 'message' => $message, 'replypmid' => $replypmid, 'isusername' => $isusername, 'type' => $type]);
	} else {
		$fromuid = intval($fromuid);
		$subject = rawurlencode($subject);
		$msgto = rawurlencode($msgto);
		$message = rawurlencode($message);
		$replypmid = @is_numeric($replypmid) ? $replypmid : 0;
		$replyadd = $replypmid ? "&pmid=$replypmid&do=reply" : '';
		$apiurl = uc_api_url('pm_client', 'send', "uid=$fromuid", "&msgto=$msgto&subject=$subject&message=$message$replyadd");
		@header('Expires: 0');
		@header('Cache-Control: private, post-check=0, pre-check=0, max-age=0', FALSE);
		@header('Pragma: no-cache');
		@header('location: '.$apiurl);
	}
}

function uc_pm_delete($uid, $folder, $pmids) {
	return call_user_func(UC_API_FUNC, 'pm', 'delete', ['uid' => $uid, 'pmids' => $pmids]);
}

function uc_pm_deleteuser($uid, $touids) {
	return call_user_func(UC_API_FUNC, 'pm', 'deleteuser', ['uid' => $uid, 'touids' => $touids]);
}

function uc_pm_deletechat($uid, $plids, $type = 0) {
	return call_user_func(UC_API_FUNC, 'pm', 'deletechat', ['uid' => $uid, 'plids' => $plids, 'type' => $type]);
}

function uc_pm_readstatus($uid, $uids, $plids = [], $status = 0) {
	return call_user_func(UC_API_FUNC, 'pm', 'readstatus', ['uid' => $uid, 'uids' => $uids, 'plids' => $plids, 'status' => $status]);
}

function uc_pm_list($uid, $page = 1, $pagesize = 10, $folder = 'inbox', $filter = 'newpm', $msglen = 0) {
	$uid = intval($uid);
	$page = intval($page);
	$pagesize = intval($pagesize);
	$return = call_user_func(UC_API_FUNC, 'pm', 'ls', ['uid' => $uid, 'page' => $page, 'pagesize' => $pagesize, 'filter' => $filter, 'msglen' => $msglen]);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_pm_ignore($uid) {
	$uid = intval($uid);
	return call_user_func(UC_API_FUNC, 'pm', 'ignore', ['uid' => $uid]);
}

function uc_pm_view($uid, $pmid = 0, $touid = 0, $daterange = 1, $page = 0, $pagesize = 10, $type = 0, $isplid = 0) {
	$uid = intval($uid);
	$touid = intval($touid);
	$page = intval($page);
	$pagesize = intval($pagesize);
	$pmid = @is_numeric($pmid) ? $pmid : 0;
	$return = call_user_func(UC_API_FUNC, 'pm', 'view', ['uid' => $uid, 'pmid' => $pmid, 'touid' => $touid, 'daterange' => $daterange, 'page' => $page, 'pagesize' => $pagesize, 'type' => $type, 'isplid' => $isplid]);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_pm_view_num($uid, $touid, $isplid) {
	$uid = intval($uid);
	$touid = intval($touid);
	$isplid = intval($isplid);
	return call_user_func(UC_API_FUNC, 'pm', 'viewnum', ['uid' => $uid, 'touid' => $touid, 'isplid' => $isplid]);
}

function uc_pm_viewnode($uid, $type, $pmid) {
	$uid = intval($uid);
	$type = intval($type);
	$pmid = @is_numeric($pmid) ? $pmid : 0;
	$return = call_user_func(UC_API_FUNC, 'pm', 'viewnode', ['uid' => $uid, 'type' => $type, 'pmid' => $pmid]);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_pm_chatpmmemberlist($uid, $plid = 0) {
	$uid = intval($uid);
	$plid = intval($plid);
	$return = call_user_func(UC_API_FUNC, 'pm', 'chatpmmemberlist', ['uid' => $uid, 'plid' => $plid]);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_pm_kickchatpm($plid, $uid, $touid) {
	$uid = intval($uid);
	$plid = intval($plid);
	$touid = intval($touid);
	return call_user_func(UC_API_FUNC, 'pm', 'kickchatpm', ['uid' => $uid, 'plid' => $plid, 'touid' => $touid]);
}

function uc_pm_appendchatpm($plid, $uid, $touid) {
	$uid = intval($uid);
	$plid = intval($plid);
	$touid = intval($touid);
	return call_user_func(UC_API_FUNC, 'pm', 'appendchatpm', ['uid' => $uid, 'plid' => $plid, 'touid' => $touid]);
}

function uc_pm_blackls_get($uid) {
	$uid = intval($uid);
	return call_user_func(UC_API_FUNC, 'pm', 'blackls_get', ['uid' => $uid]);
}

function uc_pm_blackls_set($uid, $blackls) {
	$uid = intval($uid);
	return call_user_func(UC_API_FUNC, 'pm', 'blackls_set', ['uid' => $uid, 'blackls' => $blackls]);
}

function uc_pm_blackls_add($uid, $username) {
	$uid = intval($uid);
	return call_user_func(UC_API_FUNC, 'pm', 'blackls_add', ['uid' => $uid, 'username' => $username]);
}

function uc_pm_blackls_delete($uid, $username) {
	$uid = intval($uid);
	return call_user_func(UC_API_FUNC, 'pm', 'blackls_delete', ['uid' => $uid, 'username' => $username]);
}

function uc_domain_ls() {
	$return = call_user_func(UC_API_FUNC, 'domain', 'ls', ['1' => 1]);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_credit_exchange_request($uid, $from, $to, $toappid, $amount) {
	$uid = intval($uid);
	$from = intval($from);
	$toappid = intval($toappid);
	$to = intval($to);
	$amount = intval($amount);
	return uc_api_post('credit', 'request', ['uid' => $uid, 'from' => $from, 'to' => $to, 'toappid' => $toappid, 'amount' => $amount]);
}

function uc_tag_get($tagname, $nums = 0) {
	$return = call_user_func(UC_API_FUNC, 'tag', 'gettag', ['tagname' => $tagname, 'nums' => $nums]);
	return UC_CONNECT == 'mysql' ? $return : uc_unserialize($return);
}

function uc_avatar($uid, $type = 'virtual', $returnhtml = 1) {
	$uid = intval($uid);
	$uc_input = uc_api_input("uid=$uid&frontend=1", 'user', 'rectavatar');
	$avatarpath = UC_STANDALONE ? UC_AVTAPI : UC_API;
	$uc_avatarflash = UC_API.'/images/camera.swf?inajax=1&appid='.UC_APPID.'&input='.$uc_input.'&agent='.md5($_SERVER['HTTP_USER_AGENT']).'&ucapi='.urlencode(UC_API).'&avatartype='.$type.'&uploadSize=2048';
	$uc_avatarhtml5 = UC_API.'/index.php?m=user&a=camera&width=450&height=253&appid='.UC_APPID.'&input='.$uc_input.'&agent='.md5($_SERVER['HTTP_USER_AGENT']).'&ucapi='.urlencode(UC_API).'&avatartype='.$type.'&uploadSize=2048';
	$uc_avatarstl = $avatarpath.'/index.php?m=user&inajax=1&a=rectavatar&appid='.UC_APPID.'&input='.$uc_input.'&agent='.md5($_SERVER['HTTP_USER_AGENT']).'&avatartype='.$type.'&base64=yes';
	if($returnhtml) {
		$flash = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="450" height="253" id="mycamera" align="middle"><param name="allowScriptAccess" value="always" /><param name="scale" value="exactfit" /><param name="wmode" value="transparent" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="movie" value="'.$uc_avatarflash.'" /><param name="menu" value="false" /><embed src="'.$uc_avatarflash.'" quality="high" bgcolor="#ffffff" width="450" height="253" name="mycamera" align="middle" allowScriptAccess="always" allowFullScreen="false" scale="exactfit"  wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
		$html5 = '<iframe src="'.$uc_avatarhtml5.'" width="450" marginwidth="0" height="253" marginheight="0" scrolling="no" frameborder="0" id="mycamera" name="mycamera" align="middle"></iframe>';
		return '<script type="text/javascript">document.write(document.createElement("Canvas").getContext ? \''.$html5.'\' : \''.$flash.'\');</script>';
	} else {
		return [
			'width', '450',
			'height', '253',
			'scale', 'exactfit',
			'src', $uc_avatarflash,
			'html5_src', $uc_avatarhtml5,
			'stl_src', $uc_avatarstl,
			'id', 'mycamera',
			'name', 'mycamera',
			'quality', 'high',
			'bgcolor', '#ffffff',
			'menu', 'false',
			'swLiveConnect', 'true',
			'allowScriptAccess', 'always'
		];
	}
}

function uc_rectavatar($uid) {
	return uc_api_mysql('user', 'rectavatar', ['uid' => $uid]);
}

function uc_mail_queue($uids, $emails, $subject, $message, $frommail = '', $charset = 'gbk', $htmlon = FALSE, $level = 1) {
	return call_user_func(UC_API_FUNC, 'mail', 'add', ['uids' => $uids, 'emails' => $emails, 'subject' => $subject, 'message' => $message, 'frommail' => $frommail, 'charset' => $charset, 'htmlon' => $htmlon, 'level' => $level]);
}

function uc_check_avatar($uid, $size = 'middle', $type = 'virtual') {
	if(UC_STANDALONE && @include UC_ROOT.'./extend_client.php') {
		$uc_chk = new uc_note_handler();
		$res = $uc_chk->checkavatar(['uid' => $uid, 'size' => $size, 'type' => $type], []);
	} else {
		$url = UC_API."/avatar.php?uid=$uid&size=$size&type=$type&check_file_exists=1";
		$res = uc_fopen2($url, 500000, '', '', TRUE, UC_IP, 20);
	}
	if($res == 1) {
		return 1;
	} else {
		return 0;
	}
}

function uc_check_version() {
	$return = uc_api_post('version', 'check', []);
	$data = uc_unserialize($return);
	return is_array($data) ? $data : $return;
}

function uc_get_settings($key = '') {
	if(!UC_STANDALONE) {
		return [];
	}
	static $base = null;
	if($base == null) {
		include_once UC_ROOT.'./model/base.php';
		$b = new base();
	}
	return $b->get_setting();
}

function uc_set_settings($data) {
	if(!UC_STANDALONE) {
		return [];
	}
	static $base = null;
	if($base == null) {
		include_once UC_ROOT.'./model/base.php';
		$b = new base();
		$b->load('cache');
	}
	foreach($data as $k => $v) {
		$b->set_setting($k, $v);
	}
	$_ENV['cache']->updatedata('settings');
}