<?php
//admin section redirections
if (!isset($afbAccount) || $afbAccount->account['account_admin'] !== 'yes'){
	//jump to home
	header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL);
	exit();
}






//------------------------------
$thispage['title'] = getLangTerm('shares',false);
//------------------------------







//create
//-------------------------------------------------------------------------------------
if (isset($_GET['create'])) {
	
	if (isset($_POST['submit'])) {
		
		//look for errors
		//name
		if (strlen(trim($_POST['share_name'])) == 0) {
			$errors[] = getLangTerm('share-reqd',false);
		}
		//directory
		if (strlen(trim($_POST['share_dir'])) == 0) {
			$errors[] = getLangTerm('dir-reqd',false);
		} else {
			//check if directory exists
			$dir = TieString(str_replace('\\','/',stripslashes($_POST['share_dir'])),'','/');
			
			if (!is_dir($dir)) {
				$errors[] = getLangTerm('dir-no-exist',false).' <strong>'.$dir.'</strong>';
			}
		}
		
		if (count($errors) == 0) {
			//ok to create
			$sql = "INSERT INTO `".TABLE_PREFIX."shares` (
				`share_name`,
				`share_dir`
			) VALUES (
				'".addslashes(stripslashes($_POST['share_name']))."',
				'".addslashes($dir)."'
			)";
			if ($db->Execute($sql) === false) { 
				$errors[] = 'Error: '.$db->ErrorMsg();
			} else {
				//get insert id
				$sql = "SELECT `share_id` FROM `".TABLE_PREFIX."shares` WHERE `share_name`='".addslashes(stripslashes($_POST['share_name']))."' ORDER BY `share_id` DESC LIMIT 0,1";
				$result = $db->Execute($sql);
				$insert_id = $result->fields['share_id'];
				
				//redirect
				header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL.'index.php?admin&section=shares&perms='.$insert_id);
				exit();
			}
		}
	} //end form submit check
	
	
	//-----------------------------
	include(ADMINROOT.'_head.php');
	//-----------------------------
	
	
	if (count($errors) > 0) {
		?>
		<p class="errors"><strong><?php echo getLangTerm('form-val-found',false); ?></strong></p>
		<ul class="errors">
			<?php
				for ($x=0;$x<count($errors);$x++) {
					?><li><?php echo $errors[$x]; ?></li><?php
				}
			?>
		</ul>
		<?php
	}
	?>
	<form action="index.php?admin&section=shares&create" name="create" method="post">
		<table border="0" cellspacing="0">
			<caption><?php echo getLangTerm('create-share',false); ?></caption>
			<tbody>
				<tr>
					<td width="200">
						<?php echo getLangTerm('share-name',false); ?>
					</td>
					<td>
						<input type="text" name="share_name" value="<?php echo stripslashes($_POST['share_name']); ?>" style="width:200px;" class="required" title="<?php echo getLangTerm('share-reqd',false); ?>" />
					</td>
				</tr>
				<tr>
					<td width="200">
						<?php echo getLangTerm('directory',false); ?>
					</td>
					<td>
						<input type="text" name="share_dir" value="<?php echo stripslashes($_POST['share_dir']); ?>" style="width:200px;" class="required" title="<?php echo getLangTerm('dir-reqd',false); ?>" />
					</td>
				</tr>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="2">
						<a href="index.php?admin&section=shares" class="button"><?php echo getLangTerm('cancel',false); ?></a>
						<input type="submit" class="button" value="<?php echo getLangTerm('save',false); ?>" name="submit" />
					</td>
				</tr>
			</tfoot>
		</table>
		
		<p align="center">
			
		</p>
	</form>
	
	<p>&nbsp;</p>
	
	<table border="0">
		<thead>
			<tr>
				<td>
					<?php echo getLangTerm('notes-dir',false); ?>
				</td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<?php echo getLangTerm('notes-dir-1',false); ?>
				</td>
			</tr>
			<tr>
				<td>
					<?php
					if (strpos(strtolower($_SERVER['SERVER_SOFTWARE']),'win') !== false) {
						echo getLangTerm('notes-dir-win',false);
					} else {
						echo getLangTerm('notes-dir-linux-1',false);
						?>
						</td></tr><tr><td>
						<?php
						echo getLangTerm('notes-dir-linux-2',false);
					}
					?>
				</td>
			</tr>
		</tbody>
	</table>	
	<?php
	//-----------------------------
	include(ADMINROOT.'_foot.php');
	//-----------------------------

//edit
//-------------------------------------------------------------------------------------
} elseif (isset($_GET['edit'])) {
	
	//get share details
	$sql = "SELECT * FROM `".TABLE_PREFIX."shares` WHERE `share_id`='".addslashes(stripslashes($_GET['edit']))."' LIMIT 0,1";
	$result = $db->Execute($sql);
	$share_id = $result->fields['share_id'];
	
	
	
	if (isset($_POST['submit'])) {
		
		
		//look for errors
		//name
		if (strlen(trim($_POST['share_name'])) == 0) {
			$errors[] = getLangTerm('share-reqd',false);
		}
		//directory
		if (strlen(trim($_POST['share_dir'])) == 0) {
			$errors[] = getLangTerm('dir-reqd',false);
		} else {
			//check if directory exists
			$dir = TieString(str_replace('\\','/',stripslashes($_POST['share_dir'])),'','/');
			
			if (!is_dir($dir)) {
				$errors[] = getLangTerm('dir-no-exist',false).' <strong>'.$dir.'</strong>';
			}
		}
			
		if (count($errors) == 0) {
			//ok to create
			
			$sql = "UPDATE `".TABLE_PREFIX."shares` SET
				`share_name`='".addslashes(stripslashes($_POST['share_name']))."',
				`share_dir`='".addslashes($dir)."'
			WHERE `share_id`='".$share_id."' LIMIT 1
			";
			if ($db->Execute($sql) === false) { 
				$errors[] = 'Error: '.$db->ErrorMsg();
			} else {
				//redirect
				header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL.'index.php?admin&section=shares');
				exit();
			}
			
		}
		
	} //end submit check
	
	
	
	
	
	//-----------------------------
	include(ADMINROOT.'_head.php');
	//-----------------------------
	
	
	if (count($errors) > 0) {
		?>
		<p class="errors"><strong><?php echo getLangTerm('form-val-found',false); ?></strong></p>
		<ul class="errors">
			<?php
				for ($x=0;$x<count($errors);$x++) {
					?><li><?php echo $errors[$x]; ?></li><?php
				}
			?>
		</ul>
		<?php
	}
	
	
	?>
	<form action="index.php?admin&section=shares&edit=<?php echo $share_id; ?>" name="edit" method="post">
	<table border="0" cellspacing="0">
		<thead>
			<tr>
				<td colspan="2">
					<?php echo getLangTerm('edit-share',false); ?>
				</td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td width="200">
					<?php echo getLangTerm('share-name',false); ?>
				</td>
				<td>
					<input type="text" name="share_name" value="<?php if (isset($_POST['share_name'])) { echo stripslashes($_POST['share_name']); } else { echo stripslashes($result->fields['share_name']); } ?>" style="width:200px;" class="required" title="<?php echo getLangTerm('share-reqd',false); ?>" />
				</td>
			</tr>
			<tr>
				<td width="200">
					<?php echo getLangTerm('directory',false); ?>
				</td>
				<td>
					<input type="text" name="share_dir" value="<?php if (isset($_POST['share_dir'])) { echo stripslashes($_POST['share_dir']); } else { echo stripslashes($result->fields['share_dir']); } ?>" style="width:200px;" class="required" title="<?php echo getLangTerm('dir-reqd',false); ?>" />
				</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="2">
					<a href="index.php?admin&section=shares" class="button"><?php echo getLangTerm('cancel',false); ?></a>
					<input type="submit" class="button" value="<?php echo getLangTerm('save',false); ?>" name="submit" />
				</td>
			</tr>
		</tfoot>
	</table>
	</form>
	
	<p>&nbsp;</p>
	
	<table border="0">
		<thead>
			<tr>
				<td>
					<?php echo getLangTerm('notes-dir',false); ?>
				</td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<?php echo getLangTerm('notes-dir-1',false); ?>
				</td>
			</tr>
			<tr>
				<td>
					<?php
					if (strpos(strtolower($_SERVER['SERVER_SOFTWARE']),'win') !== false) {
						echo getLangTerm('notes-dir-win',false);
					} else {
						echo getLangTerm('notes-dir-linux-1',false);
						?>
						</td></tr><tr><td>
						<?php
						echo getLangTerm('notes-dir-linux-2',false);
					}
					?>
				</td>
			</tr>
		</tbody>
	</table>	
	<?php
	
	//-----------------------------
	include(ADMINROOT.'_foot.php');
	//-----------------------------
	
//perms
//-------------------------------------------------------------------------------------
} elseif (isset($_GET['perms'])) {
	
	//get share details
	$sql = "SELECT * FROM `".TABLE_PREFIX."shares` WHERE `share_id`='".$_GET['perms']."' LIMIT 0,1";
	$result = $db->Execute($sql);
	$share_name = stripslashes($result->fields['share_name']);
	$share_id = $result->fields['share_id'];
	
	
	//check for submission
	if (isset($_POST['submit'])){
		//delete all permissions prior
		$sql = "DELETE FROM `".TABLE_PREFIX."accounts_has_perms` WHERE `share_id`='".$share_id."'";
		if ($db->Execute($sql) === false) { 
			$errors[] = 'Error: '.$db->ErrorMsg();
		}
					
		//re-insert
		//iterate through accounts
		$sql = "SELECT `account_id` FROM `".TABLE_PREFIX."accounts`";
		$result = $db->Execute($sql);
		while (!$result->EOF){
			$list = 'no';
			$download = 'no';
			$write = 'no';
			$modify = 'no';
			
			if ($_POST['list']['a_'.$result->fields['account_id']] == 'yes') {
				$list = 'yes';
			}
			if ($_POST['download']['a_'.$result->fields['account_id']] == 'yes') {
				$download = 'yes';
			}
			if ($_POST['write']['a_'.$result->fields['account_id']] == 'yes') {
				$write = 'yes';
			}
			if ($_POST['modify']['a_'.$result->fields['account_id']] == 'yes') {
				$modify = 'yes';
			}
			
			//insert
			$sql = "INSERT INTO `".TABLE_PREFIX."accounts_has_perms` (
				`account_id`,
				`share_id`,
				`perm_list`,
				`perm_download`,
				`perm_write`,
				`perm_modify`
			) VALUES (
				'".$result->fields['account_id']."',
				'".$share_id."',
				'".$list."',
				'".$download."',
				'".$write."',
				'".$modify."'
			)";
			if ($db->Execute($sql) === false) { 
				$errors[] = 'Error: '.$db->ErrorMsg();
			}
			$result->MoveNext();
		} //end accounts
		
		//add anonymous account
		$list = 'no';
		$download = 'no';
		$write = 'no';
		$modify = 'no';
		
		if ($_POST['list']['a_0'] == 'yes') {
			$list = 'yes';
		}
		if ($_POST['download']['a_0'] == 'yes') {
			$download = 'yes';
		}
		if ($_POST['write']['a_0'] == 'yes') {
			$write = 'yes';
		}
		if ($_POST['modify']['a_0'] == 'yes') {
			$modify = 'yes';
		}
		
		//insert
		$sql = "INSERT INTO `".TABLE_PREFIX."accounts_has_perms` (
			`account_id`,
			`share_id`,
			`perm_list`,
			`perm_download`,
			`perm_write`,
			`perm_modify`
		) VALUES (
			'0',
			'".$share_id."',
			'".$list."',
			'".$download."',
			'".$write."',
			'".$modify."'
		)";
		if ($db->Execute($sql) === false) { 
			$errors[] = 'Error: '.$db->ErrorMsg();
		}
		
		
		
		if (count($errors) == 0){
			//redirect
			header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL.'index.php?admin&section=shares');
			exit();
		}
		
		
	} //end submit check
	
	
	//-----------------------------
	include(ADMINROOT.'_head.php');
	//-----------------------------
	
	
	if (count($errors) > 0) {
		?>
		<p class="errors"><strong><?php echo getLangTerm('form-val-found',false); ?></strong></p>
		<ul class="errors">
			<?php
				for ($x=0;$x<count($errors);$x++) {
					?><li><?php echo $errors[$x]; ?></li><?php
				}
			?>
		</ul>
		<?php
	}
	
	
	?>
	<form action="index.php?admin&section=shares&perms=<?php echo $share_id; ?>" name="perms" method="post">	
	
	<table border="0" cellspacing="0">
		<thead>
			<tr>
				<td>
					<?php echo getLangTerm('change-ac-perms-for',false); ?> <?php echo $share_name; ?>
				</td>
				<td width="100">
					<?php echo getLangTerm('list',false); ?>
				</td>
				<td width="100">
					<?php echo getLangTerm('download',false); ?>
				</td>
				<td width="100">
					<?php echo getLangTerm('write',false); ?>
				</td>
				<td width="100">
					<?php echo getLangTerm('modify',false); ?>
				</td>
			</tr>
		</thead>
		<tbody>
			<?php
			//get anon settings
			$sql = "SELECT
				*
			FROM
				`".TABLE_PREFIX."accounts_has_perms`
			WHERE
				`account_id`='0' AND
				`share_id`='".$share_id."'
			";
			$result = $db->Execute($sql);
			$recordcount = $result->RecordCount();
			?>
			<tr>
				<td>
					<?php echo getLangTerm('anon-users',false); ?>
				</td>
				<td width="100">
					<input type="checkbox" name="list[a_0]" value="yes"<?php if ($result->fields['perm_list'] == 'yes') { echo ' checked="checked"'; } ?> />
				</td>
				<td width="100">
					<input type="checkbox" name="download[a_0]" value="yes"<?php if ($result->fields['perm_download'] == 'yes') { echo ' checked="checked"'; } ?> />
				</td>
				<td width="100">
					<input type="checkbox" name="write[a_0]" value="yes"<?php if ($result->fields['perm_write'] == 'yes') { echo ' checked="checked"'; } ?> />
				</td>
				<td width="100">
					<input type="checkbox" name="modify[a_0]" value="yes"<?php if ($result->fields['perm_modify'] == 'yes') { echo ' checked="checked"'; } ?> />
				</td>
			</tr>
			
			<?php			
			$sql = "SELECT
				a.*,
				h.`perm_list`,
				h.`perm_download`,
				h.`perm_write`,
				h.`perm_modify`
			FROM
				`".TABLE_PREFIX."accounts` a
			LEFT JOIN
				`".TABLE_PREFIX."accounts_has_perms` h
			ON
				h.`account_id`=a.`account_id` AND
				h.`share_id`='".$share_id."'
			GROUP BY
				a.`account_id`
			ORDER BY
				a.`account_name`";
			$result = $db->Execute($sql);
			$recordcount = $result->RecordCount();
			if ($recordcount > 0) {
				while (!$result->EOF){
					?>
					<tr>
						<td>
							<?php echo stripslashes($result->fields['account_name']); ?>
						</td>
						<td width="100">
							<input type="checkbox" name="list[a_<?php echo $result->fields['account_id']; ?>]" value="yes"<?php if ($result->fields['perm_list'] == 'yes') { echo ' checked="checked"'; } ?> />
						</td>
						<td width="100">
							<input type="checkbox" name="download[a_<?php echo $result->fields['account_id']; ?>]" value="yes"<?php if ($result->fields['perm_download'] == 'yes') { echo ' checked="checked"'; } ?> />
						</td>
						<td width="100">
							<input type="checkbox" name="write[a_<?php echo $result->fields['account_id']; ?>]" value="yes"<?php if ($result->fields['perm_write'] == 'yes') { echo ' checked="checked"'; } ?> />
						</td>
						<td width="100">
							<input type="checkbox" name="modify[a_<?php echo $result->fields['account_id']; ?>]" value="yes"<?php if ($result->fields['perm_modify'] == 'yes') { echo ' checked="checked"'; } ?> />
						</td>
					</tr>
					<?php
					$result->MoveNext();
				}
			}
			?>
			
		</tbody>
		<tfoot>
			<tr>
				<td colspan="5">
					<a href="index.php?admin&section=shares" class="button"><?php echo getLangTerm('cancel',false); ?></a>
					<input type="submit" class="button" value="<?php echo getLangTerm('save',false); ?>" name="submit" />
				</td>
			</tr>
		</tfoot>
	</table>
	</form>
	

	<table border="0" cellspacing="0">
		<thead>
			<tr>
				<td width="50%">
					<?php echo getLangTerm('notes',false); ?>
				</td>
				<td width="50%">
					<?php echo getLangTerm('notes-perm',false); ?>
				</td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td valign="top" width="50%">
					
					<ul>
						<li><?php echo getLangTerm('notes-perm-1',false); ?></li>
						<li><?php echo getLangTerm('notes-perm-2',false); ?></li>
						<li><?php echo getLangTerm('notes-perm-3',false); ?></li>
					</ul>
				
				</td>
				<td valign="top" width="50%">
					
					<ul>
						<li><strong><?php echo getLangTerm('list-perms',false); ?></strong> - <?php echo getLangTerm('list-perms-note',false); ?></li>
						<li><strong><?php echo getLangTerm('download-perms',false); ?></strong> - <?php echo getLangTerm('download-perms-note',false); ?></li>
						<li><strong><?php echo getLangTerm('write-perms',false); ?></strong> - <?php echo getLangTerm('write-perms-note',false); ?></li>
						<li><strong><?php echo getLangTerm('modify-perms',false); ?></strong> - <?php echo getLangTerm('modify-perms-note',false); ?></li>
					</ul>
					
				</td>
			</tr>
		</tbody>
	</table>
	<?php
	
	//-----------------------------
	include(ADMINROOT.'_foot.php');
	//-----------------------------

//remove
//-------------------------------------------------------------------------------------
} elseif (isset($_GET['remove'])){
	$sql = "DELETE FROM `".TABLE_PREFIX."shares` WHERE `share_id`='" . addslashes(stripslashes($_GET['remove'])) . "'";
	if ($db->Execute($sql) === false) { 
		//$negative = 'Error: '.$db->ErrorMsg();
	} else {
		if ($db->Affected_Rows()) {
			
			$sql = "DELETE FROM `".TALBE_PREFIX."accounts_has_perms` WHERE `share_id`='" . addslashes(stripslashes($_GET['remove'])) . "'";
			if ($db->Execute($sql) === false) { 
				//$negative = 'Error: '.$db->ErrorMsg();
			}
		}
	}
	
	header("Location: http://".trim($_SERVER['HTTP_HOST'],'/').BASEURL.'index.php?admin&section=shares');
	exit();
	
//default
//-------------------------------------------------------------------------------------
} else {
	
	//-----------------------------
	include(ADMINROOT.'_head.php');
	//-----------------------------
	
	?>
	<table border="0" cellspacing="0">
		<thead>
			<tr>
				<td>
					<?php echo getLangTerm('name',false); ?>
				</td>
				<td>
					<?php echo getLangTerm('location',false); ?>
				</td>
				<td>
					<?php echo getLangTerm('exists',false); ?>
				</td>
				<td colspan="3">&nbsp;
					
				</td>
			</tr>
		</thead>
		<tbody>
			<?php
			$sql = "SELECT * FROM `".TABLE_PREFIX."shares` ORDER BY `share_name`";
			$result = $db->Execute($sql);
			$recordcount = $result->RecordCount();
			if ($recordcount > 0){
				while (!$result->EOF){
					?>
					<tr<?php if (!is_dir(stripslashes($result->fields['share_dir']))) { ?> style="background-color:#fdd;"<?php } ?>>
						<td>
							<strong><?php echo htmlspecialchars(stripslashes($result->fields['share_name'])); ?></strong>
						</td>
						<td>
							<?php echo htmlspecialchars(stripslashes($result->fields['share_dir'])); ?>
						</td>
						<td>
							<?php
							if (is_dir(stripslashes($result->fields['share_dir']))) {
								echo getLangTerm('yes',false);
							} else {
								echo getLangTerm('no',false);
							}
							?>
						</td>
						<td width="16">
							<a href="index.php?admin&section=shares&perms=<?php echo $result->fields['share_id']; ?>" title="<?php echo getLangTerm('change-perms-desc',false); ?>"><img src="<?php echo BASEURL; ?>_includes/admin/theme/images/key.gif" border="0" width="16" height="16" alt="<?php echo getLangTerm('change-perms',false); ?>" /></a>
						</td>
						<td width="16">
							<a href="index.php?admin&section=shares&edit=<?php echo $result->fields['share_id']; ?>" title="<?php echo getLangTerm('edit-share',false); ?>"><img src="<?php echo BASEURL; ?>_includes/admin/theme/images/edit.gif" border="0" width="16" height="16" alt="<?php echo getLangTerm('edit-share',false); ?>" /></a>
						</td>
						<td width="16">
							<a href="javascript:void(AskQuestion('<?php echo getLangTerm('rem-share-ques',false); ?>','index.php?admin&section=shares&remove=<?php echo $result->fields['share_id']; ?>'));" title="<?php echo getLangTerm('remove-share',false); ?>"><img src="<?php echo BASEURL; ?>_includes/admin/theme/images/remove.gif" border="0" width="16" height="16" alt="<?php echo getLangTerm('remove-share',false); ?>" /></a>
						</td>
					</tr>
					<?php
					$result->MoveNext();
				}
			} else {
				?>
				<tr>
					<td colspan="6" align="center">
						<strong><?php echo getLangTerm('no-shares-defined',false); ?></strong>
					</td>
				</tr>
				<?php
			}
			?>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="6">
					<a href="index.php?admin&section=shares&create" title="<?php echo getLangTerm('create-share',false); ?>"><?php echo getLangTerm('create-share',false); ?></a>
				</td>
			</tr>
		</tfoot>
	</table>
	
	<?php
	//-----------------------------
	include(ADMINROOT.'_foot.php');
	//-----------------------------
}

?>