<?

class share {

var $error;

function share() {
	//$this->countShares = $this->countMines();
}

function isSharedTo($path, $to_uid) {
	global $db, $flags, $settings;
		$fid = $flags->getFidByPathAndFlag($path, "shared");
		if ($fid) {
			$rs = $db->GetOne("SELECT * FROM ".$settings->config['share_info_tablename']." WHERE fid='".$fid."' and to_uid='".$to_uid."'",false);
			if ($rs) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
}

function isShared($path) {
	global $flags;
		if ($flags->hasFlag($path, "shared")) {
			return true;
		} else {
			return false;
		}
}



function getInfoByPath($path) {
	global $db, $flags, $settings;
	$rs = $db->query("SELECT * FROM ".$settings->config['share_info_tablename']." WHERE fid='".$flags->getFidByPath($path)."'");
	return $rs;
}

function getInfoByFid($fid) {
	global $db, $settings;
	$rs = $db->query("SELECT * FROM ".$settings->config['share_info_tablename']." WHERE fid='".$fid."'");
	return $rs;
}



function countMines() {
	global $db, $auth, $settings;
	$count = $db->GetOne("SELECT count(id) AS total FROM ".$settings->config['flags_tablename']." WHERE uid='".$auth->currentUserInfo['id']."' and shared='1'",false);
//echo $count;
	return $count;
}

/*
If this user have shared this path with this user than update.
If not, add a new record.
*/
function addNewOrUpdateOld($path, $to_uid, $as, $write) {
	global $flags;
	if (!$this->isShared($path)) {
		/* add flag */
		$flags->setFlag($path, "shared", "1");
	}
	$fid = $flags->getFidByPath($path);

	$this->addUser2Share($fid, $to_uid, $as, $write);

}

function addUser2Share($fid, $to_uid, $as, $write) {
	global $db, $settings;
	$data['fid'] = $fid;
	$data['to_uid'] = $to_uid;
	$data['as'] = $as;
	$data['write'] = $write?"1":"0";
	$data['date'] = time();
	$db->query("INSERT INTO ".$settings->config['share_info_tablename']." ( `fid`, `to_uid`, `as`, `write`, `date` ) VALUES ( ".$data['fid'].", ".$data['to_uid'].", '".$data['as']."', ".$data['write'].", '".$data['date']."')");
	if ($db->Affected_Rows() > 0) {
		return true;
	} else {
		return false;
	}
}

function removeByPath($path) {
	global $db, $flags, $settings;
	$flagInfo = $flags->getRecordByPath($path);
	$db->query("DELETE FROM ".$settings->config['share_info_tablename']." WHERE fid='".$flagInfo['id']."'");
	/* update flag */
	
	if ($flagInfo) {
		$flags->setFlag($path, "shared", "0");
	}
}

function unshare($fid) {
	global $db, $flags, $settings;
	$info = $flags->getRecordByFid($fid);
	$db->query("DELETE FROM ".$settings->config['share_info_tablename']." WHERE fid='".$fid."'");
	if ($flags->setFlag($info['path'], "shared", "0")) {
		return true;
	} else {
		return false;
	}
}


function cleanMines() {
	global $db, $flags, $auth, $settings;
	$rs = $db->query("SELECT id,path FROM ".$settings->config['flags_tablename']." WHERE uid='".$auth->currentUserInfo['id']."' and shared='1'");

	if ($rs->RecordCount() > 0) {
	while (!$rs->EOF) {
		if ($auth->currentUserInfo['path'] == "") {
			$groupInfo = $auth->getGroupInfo($auth->currentUserInfo['gid']);
			$userpath = $groupInfo['path'];
		} else {
			$userpath = $auth->currentUserInfo['path'];
		}
		if (!file_exists(gluePath($userpath,$rs->fields['path']))) {
			$db->query("DELETE FROM ".$settings->config['share_info_tablename']." WHERE fid='".$rs->fields['id']."'");
			$flags->setFlag($rs->fields['path'], "shared", "0");
		}
		$rs->MoveNext();
	}
	}
}

/* 
Sterge share-urile oamenilor catre userul logat, 
share-uri care nu mai au foldere valide. Functia asta
este chemata la listare userilor in tree.
 */

function cleanOthers() {
	global $db, $flags, $auth, $settings;
	$rs = $db->query("SELECT * FROM ".$settings->config['flags_tablename'].",".$settings->config['share_info_tablename']." WHERE ".$settings->config['share_info_tablename'].".to_uid='".$auth->currentUserInfo['id']."' AND ".$settings->config['flags_tablename'].".shared='1' AND ".$settings->config['flags_tablename'].".id=".$settings->config['share_info_tablename'].".fid");
	if ($rs->RecordCount() > 0) {
	while (!$rs->EOF) {
		$userinfo = $auth->getUserInfoById($rs->fields['uid']);
		if ($userinfo['path'] == "") {
				$groupInfo = $auth->getGroupInfo($userinfo['gid']);
				$userpath = $groupInfo['path'];
		} else {
				$userpath = $userinfo['path'];
		}
		if (!file_exists(gluePath($userpath,$rs->fields['path']))) {
			$db->query("DELETE FROM ".$settings->config['share_info_tablename']." WHERE fid='".$rs->fields['id']."'");
			//$flags->setFlag($rs->fields['path'], "shared", "0");
		}
		$rs->MoveNext();
	}
	}
}



function getInfo($specialpath = "") {
	global $auth, $db, $settings;
	$tmp = split("::",$specialpath);
	$uid = $tmp[0];
	$sharename_slash_path = $tmp[1];
	$sharename_slash_path = substr(strstr($sharename_slash_path, '/'), 1); 
	$tmp  = split("/", $sharename_slash_path);
	$sharename = $tmp[0];
	
	
	$subpath = substr($sharename_slash_path, strlen($sharename)); 
	
	//debug(array("specialpath"=>$specialpath,"sharename_slash_path"=>$sharename_slash_path,"sharename"=>$sharename,"subpath"=>$subpath));
	
	$shareInfo = $db->GetRow("SELECT * FROM ".$settings->config['share_info_tablename'].", ".$settings->config['flags_tablename']." WHERE ".$settings->config['share_info_tablename'].".fid=".$settings->config['flags_tablename'].".id AND ".$settings->config['share_info_tablename'].".to_uid='".$auth->currentUserInfo['id']."' AND ".$settings->config['share_info_tablename'].".as='".$sharename."'");

	$userinfo = $auth->getUserInfoById($uid);
	if ($userinfo['path'] == "") {
			$groupInfo = $auth->getGroupInfo($userinfo['gid']);
			$userpath = $groupInfo['path'];
	} else {
			$userpath = $userinfo['path'];
	}
	
	//debug($shareInfo['path']);
	
	if ($subpath) {
		$fullPath = gluePath($userpath, $shareInfo['path'], $subpath);
	} else {
		$fullPath = gluePath($userpath, $shareInfo['path']);
	}
	return array("path"=>$fullPath, "sharename"=>$sharename, "subpath"=>$subpath, "write"=>$shareInfo['write']);
}



}
?>