<?

$sys['fullPath'] = gluePath($permissions['path'], $sys['path']);


$_GET['object'] = $str->fromHTML($_GET['object']);

if (!isCleanFilename($_GET['object'])){
die(base64_encode("dirty filename ".$_GET['object']));
}

$share->cleanMines();



if ($permissions['sharing']) {
//saving ...

	if ($_GET['action'] == "clearlist") {
		$db->query("UPDATE `".$settings->config['history_tablename']."` SET usernames='' where uid='".$auth->currentUserInfo['id']."'");
	} elseif ($_GET['action'] == "save") {

if (!$permissions['sharing']) {
$msg = $lang->get("You are not allowed to share folders.");
$statusBarMessage[] = array($msg, 0);
} else {

	$path = gluePath("/", $sys['path'], $_GET['object']);
	$lines = split("\n", $_POST['dump']);
	
	/* remove all existing shares ... */
	$share->removeByPath($path);
	
	
	
	for ($j = 0 ; $j < sizeof($lines) ; $j++) {
		$line = $str->fromHTML($lines[$j]);
		
		if (strlen($line) > 0) {
		
		$userInfo = split(",", $line);
		$to = $userInfo[0];
		$name = $userInfo[1];
		$write = trim($userInfo[2]);
		
		$to_uid = $auth->username2id($to);
		
		if (isCleanFilename($name)) {
		
		if ($to != $auth->currentUserInfo['username']) {
		
		
		if ($auth->getUserInfoById($to_uid)) {
		
			if(!$share->isSharedTo($path, $to_uid)){
				if (sizeof($userInfo) > 1) {
				
				
				$oldUsernames = $db->GetOne("SELECT usernames FROM `".$settings->config['history_tablename']."` WHERE uid='".$auth->currentUserInfo['id']."'");
				$oldUsernames = unserialize($oldUsernames);
				if (!is_array($oldUsernames)) {$oldUsernames = array();}
				$oldUsernames[] = $to;
				
				$db->query("UPDATE `".$settings->config['history_tablename']."` set usernames='".serialize(array_unique($oldUsernames))."' where uid='".$auth->currentUserInfo['id']."'");
					$share->addNewOrUpdateOld($path, $to_uid, $name, $write);
					$log->act("0", "folder share", "\"".gluePath($permissions['path'],$path)."\" WITH \"".$to."\"");
				}
			} else {
				$js .= "alert('".$lang->get("You are already sharing a folder as \'%1\' with user \'%2\'.", array($name, $to))."');";
			}
		
		} else {
			$js .= "alert('".$lang->get("User \'%1\' doesn\'t exist.", array($to))."');";
		}
		
		} else {
			$js .= "alert('".$lang->get("You cannot share folders with yourself..")."');";
		}
		
		} else {
			$js .= "alert('".$lang->get("Sharing folder as \'%1\' with user \'%2\'", array($name,$to )).": ".$lang->get("A file or folder name cannot contain any of the following characters: \ / : * ? < > | &quot")."');";
		}
	}
}
	}
	
	
	}
} else {
	$js .= "alert('".$lang->get("You are not allowed to share folders.")."');";
}


if (is_dir(gluePath($permissions['path'],$sys['path'],$_GET['object']))) {
	$shareIsDir = true;
} else {
	$shareIsDir = false;
}

//share's path
//echo $sys['path']."/".$_GET['object'];

$title = $lang->get("Sharing a folder");
//template header
require_once($sys['root']."/system/templates/popup/header.inc");
?>

<script language="JavaScript1.2" type="text/javascript" src="?file=share.js&sharename=<?echo urlencode($_GET['object'])?>">
	</script>
<script language="JavaScript1.2" type="text/javascript">
<?echo $js?>

<?

if ($_GET['objectID']) {
	$rs = $share->getInfoByFid($_GET['objectID']);
} else {
	$path = gluePath("/",$sys['path'],$str->fromHTML($_GET['object']));
	$rs = $share->getInfoByPath($path);
}

$shareInfos = array();
while (!$rs->EOF) {
	$info = $rs->fields;
	$info['to'] = $auth->id2username($info['to_uid']);
	$shareInfos[] = $info;
	$rs->MoveNext();
}

$to = array();
foreach ($shareInfos as $key => $row) {
    $to[$key]  = $row['to'];
}

//$GLOBALS['vlad']->debug($shareInfos,1,1);
array_multisort($to, SORT_ASC, SORT_STRING, $shareInfos);

foreach ($shareInfos as $shareInfo) {
?>
users['<?echo $str->safeJS($shareInfo['to'])?>'] = new Array;
users['<?echo $str->safeJS($shareInfo['to'])?>']['username'] = '<?echo $str->safeJS($shareInfo['to'])?>';
users['<?echo $str->safeJS($shareInfo['to'])?>']['sharename'] = '<?echo $str->safeJS($shareInfo['as'])?>';
users['<?echo $str->safeJS($shareInfo['to'])?>']['write'] = <?if($shareInfo['write']){echo "1";}else{echo "0";}?>;
<?

}
?>

</script>
<form name="formm">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%;height:100%;background-color:#F2F2F2">
<tr>
    <td background="images/popup/top-bg.gif" height="26">&nbsp;</td>
</tr>

<tr>
    <td style="padding:10px;">
<!-- Popup Contents -->


<table border="0" cellpadding="5" cellspacing="0" width="480" align="center">
<tr style="font-weight: bold" height="20">
	<td height="20"><?$lang->display("Share with..");?></td>
	<td style="padding-right: 5px; padding-left: 7px; padding-bottom: 0px;"><?$lang->display("Options for selected user:");?></td>
</tr>
<tr>
	<td valign="top" width="50%">
<select name="users" size="11" style="width:213px;" onchange="showInfo()" multiple>
</select>
<div style="padding-top:3px;">
<table>
<tr>
	<td>
<?
$tpl->btn(
$lang->get("Add users.."), 
($options['view_others'] == "-")?"x=prompt('".$lang->get("Please enter the username of the user you want to share this folder with:")."','');if(x){addNewUser(x);}":"showAddUser()",
"96px"
)
?>
	</td>
	<td><div style="visibility:hidden" id="discard_button"><?
$tpl->btn(
$lang->get("Discard selected"), 
"deleteSelected()"
)
?></div></td>
</tr>
</table>

</div>
	</td>
	<td width="50%" valign="top">
	
<div id="detailsNoneSelected" style="border:1px solid silver;height:150px;width:220px;padding:10px;">

<?if (is_array($result)) {?>
<li><?$lang->display("Select a user from the list to edit his details.");?></li>
<li></li>
<?} else {?>
<li><?$lang->display("To share the selected folder with users click 'Select users..'.");?></li>
<?}?>
</div>

<table cellpadding="2" cellspacing="0" border="0" width="100%" id="details" style="display:none" height="130">
<tr>
	<td style="padding-top:6px;"><?$lang->display("Share as");?></td>
	<td><input type="text" name="sharename" onkeydown="javascript:updateValue('sharename', this.value)" onkeyup="javascript:updateValue('sharename', this.value)"></td>
</tr>
<tr>
	<td colspan="2"><input type="checkbox" size="3" name="writee"  onclick="javascript:if(this.checked){updateValue('write', 1);}else{updateValue('write', 0);}" <?if (!$shareIsDir) {?>disabled<?}?>> <?$lang->display("User may upload or paste files in this folder.");?>
</td>
</tr>
</table>
	</td>
</tr>
</table>
</form>

<form action="<?echo glueURL(array(
"file"=>"share", 
"path"=>$str->forURL($sys['path']), 
"object"=>$str->forURL($_GET['object']), 
"action"=>"save"
));?>" method="post" name="submitform">
<textarea name="dump" cols="0" rows="0" style="visibility:hidden;position:absolute;">
</textarea>

	</td>
</tr>

<tr>
    <td align="center" height="20">
<table border="0" cellspacing="3" cellpadding="1" style="position:relative;top:18px;z-index:1">
<tr>
	<td><?
$tpl->btn(
$lang->get("Save"), 
"dumpData();if(onSendCheckShareName())document.submitform.submit();"
)
?></td>
	<td><?
$tpl->btn(
$lang->get("Close"), 
"self.close();window.close()"
)
?></td>
</tr>
</table>
	</td>
</tr>
<tr>
    <td background="images/popup/bottom-bg.gif" height="46">&nbsp;</td>
</tr>
</table>
</form>



<script language="JavaScript1.2" type="text/javascript">
preloadStupidMSIEElementsPosition();
drawList();
</script>

<?
//template footer
require_once($sys['root']."/system/templates/popup/footer.inc");
?>