<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

if(!isanyadmin($userID) OR substr(basename($_SERVER[REQUEST_URI]),0,15) != "admincenter.php") die('Access denied.');
$filepath = "../images/smileys/";

if($_GET["action"]=="add") {
	echo'
		<h2>SMILEYS</h2>
		<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=smileys\');return document.MM_returnValue" value="existing smileys">
		<h2>ADD SMILEY</h2>';
	echo'
		<form method="post" action="admincenter.php?site=smileys" enctype="multipart/form-data">
		<table cellpadding="4" cellspacing="0">
			<tr>
				<td>Icon-Upload:</td>
				<td><input name="icon" type="file" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
			</tr>
			<tr>
				<td>Smiley Name:</td>
				<td><input type="text" name="alt" size="20" maxlength="255" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
			</tr>
			<tr>
				<td>Pattern:<br>(f.e: ":smileyname:")</td>
				<td><input type="text" name="pattern" size="20" maxlength="20" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" name="save" value="add smiley"></td>
			</tr>
		</table>
		</form>';
}
elseif($_GET["action"]=="edit") {
	$ds=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."smileys WHERE smileyID='".$_GET["smileyID"]."'"));
	$pic='<img src="../images/smileys/'.$ds['name'].'" border="0" alt="'.$ds['alt'].'">';
	
	echo'
		<h2>SMILEYS</h2>
		<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=smileys\');return document.MM_returnValue" value="existing smileys">
		<h2>EDIT SMILEY - '.$ds['alt'].'</h2>';
	echo'
		<form method="post" action="admincenter.php?site=smileys" enctype="multipart/form-data">
		<input type="hidden" name="smileyID" value="'.$ds['smileyID'].'">
		<table cellpadding="4" cellspacing="0">
			<tr>
				<td>Present Icon:</td>
				<td>'.$pic.'</td>
			</tr>		
			<tr>
				<td>Icon-Upload:</td>
				<td><input name="icon" type="file"></td>
			</tr>
			<tr>
				<td>Smiley Name:</td>
				<td><input type="text" name="alt" size="20" maxlength="255" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'" value="'.$ds['alt'].'"></td>
			</tr>
			<tr>
				<td>Pattern:<br>(e.g. ":smileyname:")</td>
				<td><input type="text" name="pattern" size="20" maxlength="20" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'" value="'.$ds['pattern'].'"></td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" name="saveedit" value="edit smiley"></td>
			</tr>
		</table>
		</form>';
}
elseif($_POST["save"]) {
	$icon=$_FILES["icon"];
	$alt=$_POST["alt"];
	$pattern=$_POST["pattern"];

	echo'
		<h2>SMILEYS</h2>
		<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=smileys&action=add\');return document.MM_returnValue" value="new smileys">
		<h2>NEW SMILEY</h2>';
	
	if($pattern) {
		$file_ext=strtolower(substr($icon[name], strrpos($icon[name], ".")));
		if($file_ext==".gif") {
			if($icon[name] != "") {
				move_uploaded_file($icon[tmp_name], $filepath.$icon[name]);
				@chmod($filepath.$icon[name], 0755);
				$file=$alt.$file_ext;
				rename($filepath.$icon[name], $filepath.$file);
  			safe_query("INSERT INTO ".PREFIX."smileys (smileyID, name, alt, pattern) values('', '".$file."', '".$alt."', '".$pattern."')");
				redirect('admincenter.php?site=smileys','Successful!');
			}
		} else echo'<b>The format of the icon was incorrect. Please upload only icons in *.gif format.</b><br><br><a href="javascript:history.back()">&laquo; back</a>';
	} else echo'<b>Please fill in the form correctly.</b><br><br><a href="javascript:history.back()">&laquo; back</a>';
}
elseif($_POST["saveedit"]) {
	$icon=$_FILES["icon"];
	$alt=$_POST["alt"];
	$pattern=$_POST['pattern'];

	echo'
		<h2>SMILEYS</h2>
		<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=smileys\');return document.MM_returnValue" value="existing smileys">
		<h2>EDIT SMILEY</h2>';
	
	if($pattern) {
		if($icon[name]=="") {
			if(safe_query("UPDATE ".PREFIX."smileys SET alt='".$alt."', pattern='".$pattern."' WHERE smileyID='".$_POST["smileyID"]."'"))
				redirect('admincenter.php?site=smileys','Successful!');
		} else {
			$file_ext=strtolower(substr($icon[name], strrpos($icon[name], ".")));
			if($file_ext==".gif") {
				move_uploaded_file($icon[tmp_name], $filepath.$icon[name]);
				@chmod($filepath.$icon[name], 0755);
				$file=$short.$file_ext;
				rename($filepath.$icon[name], $filepath.$file);

				if(safe_query("UPDATE ".PREFIX."smiley SET name='".$icon[name]."', alt='".$alt."', pattern='".$pattern."' WHERE smileyID='".$_POST["smileyID"]."'")) {
									redirect('admincenter.php?site=smileys','Successful!');
				}
			} else echo'<b>The format of the icon was incorrect. Please upload only icons in *.gif format.</b><br><br><a href="javascript:history.back()">&laquo; back</a>';
		}
	} else echo'<b>Please fill in the form correctly.</b><br><br><a href="javascript:history.back()">&laquo; back</a>';
}
elseif($_GET["delete"]) {
	safe_query("DELETE FROM ".PREFIX."smileys WHERE smileyID='".$_GET["smileyID"]."'");
	redirect('admincenter.php?site=smileys','Successful!');
}
else {
	echo'
		<h2>SMILEYS</h2>
		<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=smileys&action=add\');return document.MM_returnValue" value="new smiley">
		<h2>existing smileys</h2>
		<form method="post" action="admincenter.php?site=smileys">
		<table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
			<tr bgcolor="#CCCCCC">
				<td class="title">Icon:</td>
				<td class="title">Name:</td>
				<td class="title">Prefix:</td>
				<td class="title">Actions:</td>
			</tr>
			<tr bgcolor="#EEEEEE">
				<td colspan="4"></td>
			</tr>';
	$ds=safe_query("SELECT * FROM ".PREFIX."smileys");
	$anz=mysql_num_rows($ds);
	if($anz) {
		while($smileys = mysql_fetch_array($ds)) {
			$pic='<img src="../images/smileys/'.$smileys['name'].'" border="0" alt="'.$smileys['alt'].'">';
			if($smileys['alt']=="") $smileys['alt']=$smileys['name'];			
			echo'
				<tr bgcolor="#FFFFFF">
					<td>'.$pic.'</td>
					<td>'.$smileys['alt'].'</td>
					<td>'.$smileys['pattern'].'</td>
					<td>
				   <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=smileys&action=edit&smileyID='.$smileys[smileyID].'\');return document.MM_returnValue" value="edit">
				   <input type="button" class="button" onClick="MM_confirm(\'really delete this smiley?\', \'admincenter.php?site=smileys&delete=true&smileyID='.$smileys[smileyID].'\')" value="delete"></td>
					</td>
				</tr>
			';
		}
	} else echo'<tr><td>no entries</td></tr>';
	echo '</table>';
}
?>
