<?php 
##
#	Project: PHPDISK File Storage Solution
#	This is NOT a freeware, use is subject to license terms.
#
#	Site: http://www.phpdisk.com
#
#	$Id: functions.inc.php 160 2015-10-03 07:03:08Z along $
#
#	Copyright (C) 2008-2016 PHPDisk Team. All Rights Reserved.
#
##
if(!defined('IN_PHPDISK')) {
	exit('[PHPDisk] Access Denied');
}

function open_phpdisk_sso_plugin($val){
	echo (int)$val;
}
function sso_login($username,$password){
	global $db,$tpf,$settings;
	$js = '';
	if($settings[is_sso_main]){
		$q = $db->query("select * from {$tpf}sso");
		while($rs = $db->fetch_array($q)){
			$sign = md5(md5('login'.$username.$password.$rs[site_key].$settings[phpdisk_url]));
			$param = rawurlencode(pd_encode("action=login&username=$username&password=$password&sign=$sign",'ENCODE',$rs[site_key]));
			$url = $rs[site_url]."plugins/phpdisk_sso/sso_api.php?param=$param";
			$js .= '<script type="text/javascript" src="'.$url.'"></script>';
		}
		unset($rs);
		$db->free($q);
		return $js;
	}
}
function sso_register($username,$password,$email,$gid){
	global $db,$tpf,$settings;
	$js = '';
	if($settings[is_sso_main]){
		$q = $db->query("select * from {$tpf}sso");
		while($rs = $db->fetch_array($q)){
			$sign = md5(md5('register'.$username.$password.$email.$gid.$rs[site_key].$settings[phpdisk_url]));
			$param = rawurlencode(pd_encode("action=register&username=$username&password=$password&email=$email&gid=$gid&sign=$sign",'ENCODE',$rs[site_key]));
			$url = $rs[site_url]."plugins/phpdisk_sso/sso_api.php?param=$param";
			$js .= '<script type="text/javascript" src="'.$url.'"></script>';
		}
		unset($rs);
		$db->free($q);
	}else{
		$sign = md5(md5('register'.$username.$password.$email.$gid.$settings[sso_main_site_key].$settings[phpdisk_url]));
		$param = rawurlencode(pd_encode("action=register&username=$username&password=$password&email=$email&gid=$gid&sign=$sign",'ENCODE',$rs[site_key]));
		$url = $settings[sso_main_site_url]."plugins/phpdisk_sso/sso_api.php?param=$param";
		$js = '<script type="text/javascript" src="'.$url.'"></script>';
	}
	return $js;
}
function sso_edit_user($username,$password,$email,$gid){
	global $db,$tpf,$settings;
	$js = '';
	if($settings[is_sso_main]){
		$q = $db->query("select * from {$tpf}sso");
		while($rs = $db->fetch_array($q)){
			$sign = md5(md5('edit_user'.$username.$password.$email.$gid.$rs[site_key].$settings[phpdisk_url]));
			$param = rawurlencode(pd_encode("action=edit_user&username=$username&password=$password&email=$email&gid=$gid&sign=$sign",'ENCODE',$rs[site_key]));
			$url = $rs[site_url]."plugins/phpdisk_sso/sso_api.php?param=$param";
			$js .= '<script type="text/javascript" src="'.$url.'"></script>';
		}
		unset($rs);
		$db->free($q);
	}else{
		$sign = md5(md5('edit_user'.$username.$password.$email.$gid.$settings[sso_main_site_key].$settings[phpdisk_url]));
		$param = rawurlencode(pd_encode("action=edit_user&username=$username&password=$password&email=$email&gid=$gid&sign=$sign",'ENCODE',$rs[site_key]));
		$url = $settings[sso_main_site_url]."plugins/phpdisk_sso/sso_api.php?param=$param";
		$js = '<script type="text/javascript" src="'.$url.'"></script>';
	}
	return $js;
}
function sso_del_user($username){
	global $db,$tpf,$settings;
	$js = '';
	if($settings[is_sso_main]){
		$q = $db->query("select * from {$tpf}sso");
		while($rs = $db->fetch_array($q)){
			$sign = md5(md5('del_user'.$username.$rs[site_key].$settings[phpdisk_url]));
			$param = rawurlencode(pd_encode("action=del_user&username=$username&sign=$sign",'ENCODE',$rs[site_key]));
			$url = $rs[site_url]."plugins/phpdisk_sso/sso_api.php?param=$param";
			$js .= '<script type="text/javascript" src="'.$url.'"></script>';
		}
		unset($rs);
		$db->free($q);
	}else{
		$sign = md5(md5('del_user'.$username.$settings[sso_main_site_key].$settings[phpdisk_url]));
		$param = rawurlencode(pd_encode("action=del_user&username=$username&sign=$sign",'ENCODE',$rs[site_key]));
		$url = $settings[sso_main_site_url]."plugins/phpdisk_sso/sso_api.php?param=$param";
		$js = '<script type="text/javascript" src="'.$url.'"></script>';
	}
	return $js;
}
function sso_logout(){
	global $db,$tpf,$settings;
	$q = $db->query("select * from {$tpf}sso");
	while($rs = $db->fetch_array($q)){
		$sign = md5(md5('logout'.$rs[site_key].$settings[phpdisk_url]));
		$param = rawurlencode(pd_encode("action=logout&sign=$sign",'ENCODE',$rs[site_key]));
		$url = $rs[site_url]."plugins/phpdisk_sso/sso_api.php?param=$param";
		$js .= '<script type="text/javascript" src="'.$url.'"></script>';
	}
	unset($rs);
	$db->free($q);
	return $js;
}
function sso_sub($act){
	global $db,$tpf,$settings;
	if(!$settings[is_sso_main]){
		switch($act){
			case 'login':
				header('Location: '.$settings[sso_main_site_url].urr("account","action=login").'&ref='.$_SERVER['HTTP_REFERER']);
				break;
			case 'register':
				header('Location: '.$settings[sso_main_site_url].urr("account","action=register"));
				break;
			case 'forget_pwd':
				header('Location: '.$settings[sso_main_site_url].urr("account","action=forget_pwd"));
				break;
			case 'logout':
				header('Location: '.$settings[sso_main_site_url].urr("account","action=logout").'&ref='.$_SERVER['HTTP_REFERER']);
				break;
		}
		exit;
	}
}

?>