<?php
/***************************************************************************
* copyright            : (C) 2001-2003 Advanced Internet Designs Inc.
* email                : forum@prohost.org
* $Id: admmime.php,v 1.1.1.1.8.1 2004/11/24 14:12:19 kazuyan Exp $
*
* This program is free software; you can redistribute it and/or modify it 
* under the terms of the GNU General Public License as published by the 
* Free Software Foundation; either version 2 of the License, or 
* (at your option) any later version.
***************************************************************************/

	require('./GLOBALS.php'); fud_phpgw();
	fud_use('adm.inc', true);
	fud_use('widgets.inc', true);

	$tbl = $GLOBALS['DBHOST_TBL_PREFIX'];

	if (isset($_GET['del'])) {
		q('DELETE FROM '.$tbl.'mime WHERE id='.(int)$_GET['del']);
	}

	if (isset($_GET['edit'])) {
		list($mime_descr, $mime_mime_hdr, $mime_fl_ext, $mime_icon) = db_saq('SELECT descr, mime_hdr, fl_ext, icon FROM '.$tbl.'mime WHERE id='.(int)$_GET['edit']);
		$edit = (int)$_GET['edit'];
	} else {
		$mime_icon = $edit = $mime_descr = $mime_mime_hdr = $mime_fl_ext = '';
	}

	if (isset($_FILES['icoul']) && $_FILES['icoul']['size'] && preg_match('!\.(jpg|jpeg|gif|png)$!i', $_FILES['icoul']['name'])) {
		move_uploaded_file($_FILES['icoul']['tmp_name'], $GLOBALS['WWW_ROOT_DISK'] . 'images/mime/' . $_FILES['icoul']['name']);
		if (empty($_POST['mime_icon'])) {
			$_POST['mime_icon'] = $_FILES['icoul']['name'];
		}
	}

	if (isset($_POST['btn_update'], $_POST['edit'])) {
		q('UPDATE '.$tbl.'mime SET descr='.strnull(addslashes($_POST['mime_descr'])).', mime_hdr='.strnull(addslashes($_POST['mime_mime_hdr'])).', fl_ext='.strnull(addslashes($_POST['mime_fl_ext'])).', icon='.strnull(addslashes($_POST['mime_icon'])).' WHERE id='.(int)$_POST['edit']);
	} else if (isset($_POST['btn_submit'])) {
		q('INSERT INTO '.$tbl.'mime (descr, mime_hdr, fl_ext, icon) VALUES ('.strnull(addslashes($_POST['mime_descr'])).', '.strnull(addslashes($_POST['mime_mime_hdr'])).', '.strnull(addslashes($_POST['mime_fl_ext'])).', '.strnull(addslashes($_POST['mime_icon'])).')');
	}

	require($WWW_ROOT_DISK . 'adm/admpanel.php');
?>
<h2><?php echo lang('MIME Management System') ?></h2>
<table border="0" cellspacing="1" cellpadding="3">
<form action="admmime.php" name="frm_mime" method="post" enctype="multipart/form-data">
<?php
	echo _hs;
	if (@is_writeable($GLOBALS['WWW_ROOT_DISK'] . 'images/mime/')) {
?>
<tr bgcolor="#bff8ff">
	<td colspan="2"><b><?php echo lang('MIME Icon Upload (upload mime icons into the system)') ?></td>
</tr>
<tr bgcolor="#bff8ff">
	<td><?php echo lang('MIME Icon Upload') ?>:<br><font size="-1"><?php echo lang('Only (.gif, *.jpg, *.png) files are supported') ?></font></td>
	<td><input type="file" name="icoul"> <input type="submit" name="btn_upload" value="<?php echo lang('Upload') ?>"></td>
</tr>
<?php } else { ?>
<tr bgcolor="#bff8ff">
	<td colspan="2"><font color="#ff0000"><?php echo lang('Web server does not have write permissions to <b>%1images/mime/</b>, mime icon upload disabled', $GLOBALS['WWW_ROOT_DISK']) ?></font></td>
</tr>
<?php } ?>
<tr><td colspan="2">&nbsp;</td></tr>

<tr bgcolor="#bff8ff">
	<td colspan="2"><a name="img"><b><?php echo lang('MIME Management') ?></b></a></td>
</tr>

<tr bgcolor="#bff8ff">
	<td><?php echo lang('MIME Description') ?>:</td>
	<td><input type="text" name="mime_descr" value="<?php echo htmlspecialchars($mime_descr); ?>"></td>
</tr>

<tr bgcolor="#bff8ff">
	<td><?php echo lang('MIME Header') ?>:</td>
	<td><input type="text" name="mime_mime_hdr" value="<?php echo htmlspecialchars($mime_mime_hdr); ?>"></td>
</tr>

<tr bgcolor="#bff8ff">
	<td><?php echo lang('File Extension') ?>:<br><font size="-1"><?php echo lang('Files with this extension (case-insensitive) will be attributed to this MIME.') ?></font></td>
	<td><input type="text" name="mime_fl_ext" value="<?php echo htmlspecialchars($mime_fl_ext); ?>"></td>
</tr>

<tr bgcolor="#bff8ff">
	<td valign="top"><a name="mime_sel"><?php echo lang('MIME Icon') ?>:</a></td>
	<td nowrap><input type="text" name="mime_icon" value="<?php echo htmlspecialchars($mime_icon); ?>" onChange="javascript:
				if (document.frm_sml.mime_icon.value.length) {
					document.prev_icon.src='<?php echo $GLOBALS['WWW_ROOT']; ?>images/mime/' + document.frm_sml.mime_icon.value;
				} else {
					document.prev_icon.src='../blank.gif';
				}"> [<a href="#mime_sel" onClick="javascript:window.open('admmimesel.php?<?php echo _rsidl; ?>', 'admmimesel', 'menubar=false,scrollbars=yes,resizable=yes,height=300,width=500,screenX=100,screenY=100');"><?php echo lang('select MIME icon') ?></a>]</td>
</tr>

<tr bgcolor="#bff8ff">
	<td valign="top"><?php echo lang('Preview Image') ?>:</td>
	<td>
		<table border="1" cellspacing="1" cellpadding="2" bgcolor="#ffffff">
		<tr><td align="center" valign="center"><img src="<?php echo ($mime_icon ? $GLOBALS['WWW_ROOT'] . 'images/mime/' . $mime_icon : '../blank.gif'); ?>" name="prev_icon" border="0"></td></tr>
		</table>
	</td>
</tr>

<tr bgcolor="#bff8ff">
	<td colspan="2" align="right"><input type="submit" name="btn_cancel" value="<?php echo lang('Reset') ?>">
<?php
	if (!$edit) {
		echo '<input type="submit" name="btn_submit" value="'.lang('Add MIME').'">';
	} else {
		echo '<input type="submit" name="btn_update" value="'.lang('Update').'"></td>';
	}
?>
	</td>
</tr>
<input type="hidden" name="edit" value="<?php echo $edit; ?>">
</form>
</table>
<p>
<table border="0" cellspacing="3" cellpadding="2">
<tr bgcolor="#e5ffe7">
	<td><?php echo lang('Icon') ?></td>
	<td><?php echo lang('MIME Header') ?></td>
	<td><?php echo lang('Description') ?></td>
	<td><?php echo lang('Extension') ?></td>
	<td align="center"><?php echo lang('Action') ?></td>
</tr>
<?php
	$c = uq('SELECT id, icon, mime_hdr, fl_ext, descr FROM '.$tbl.'mime');
	$i = 1;
	while ($r = db_rowarr($c)) {
		if ($edit == $r[0]) {
			$bgcolor = ' bgcolor="#ffb5b5"';
		} else {
			$bgcolor = ($i++%2) ? ' bgcolor="#fffee5"' : '';
		}
		echo '<tr'.$bgcolor.' valign="top"><td><img src="'.$GLOBALS['WWW_ROOT'].'images/mime/'.$r[1].'" border="0"></td><td>'.$r[2].'</td><td>'.$r[4].'</td><td>'.$r[3].'</td><td nowrap>[<a href="admmime.php?edit='.$r[0].'&'._rsidl.'#img">'.lang('Edit').'</a>] [<a href="admmime.php?del='.$r[0].'&'._rsidl.'">'.lang('Delete').'</a>]</td></tr>';
	}
?>
</table>
<?php require($WWW_ROOT_DISK . 'adm/admclose.php'); ?>
