<?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                                                   #
#                                                                        #
 ########################################################################
*/

$galclass = new Gallery;

if($userID) {

if($_POST['save']) {
	if($_POST['name']) safe_query("INSERT INTO ".PREFIX."gallery ( name, date, userID ) values( '".$_POST['name']."', '".time()."', '".$userID."' ) ");
	else redirect('index.php?site=usergallery&action=add','Please enter a name!');
}
elseif($_POST['saveedit']) {
	safe_query("UPDATE ".PREFIX."gallery SET name='".$_POST['name']."' WHERE galleryID='".$_POST['galleryID']."' AND userID='".$userID."'");
}
elseif($_POST['saveform']) {
	
  $dir = 'images/gallery/';
  $picture = $_FILES[picture];

	if($picture[name] != "") {

   if($_POST['name']) $insertname = $_POST['name'];
   else $insertname = $picture[name];
 	 safe_query("INSERT INTO ".PREFIX."gallery_pictures ( galleryID, name, comment, comments) VALUES ('".$_POST['galleryID']."', '".$insertname."', '".$_POST['comment']."', '".$_POST['comments']."' )");

   $typ = getimagesize($picture[tmp_name]);
   switch ($typ[2]) {
    case 1: $endung = '.gif'; break;
    case 2: $endung = '.jpg'; break;
   }

 	 $insertid = mysql_insert_id();
   move_uploaded_file($picture[tmp_name], $dir.'large/'.$insertid.$endung);
 	 $galclass->savethumb($dir.'large/'.$insertid.$endung, $dir.'thumb/'.$insertid.'.jpg'); 	 
   
   if( ($galclass->getuserspace($userID)+filesize($dir.'large/'.$insertid.$endung) + filesize($dir.'thumb/'.$insertid.'.jpg')) > $maxusergalleries ) {
   	@unlink($dir.'large/'.$insertid.$endung);
   	@unlink($dir.'thumb/'.$insertid.'.jpg');		
		safe_query("DELETE FROM ".PREFIX."gallery_pictures WHERE picID='".$insertid."'");
   	echo '<p style="color:'.$loosecolor.'">There is no space left: Canceled.</p>';
	 }
	}
}
elseif($_GET['delete']) {
  //SQL
	if(safe_query("DELETE FROM ".PREFIX."gallery WHERE galleryID='".$_GET['galleryID']."' AND userID='".$userID."'")) {
   //FILES
   $ergebnis=safe_query("SELECT picID FROM ".PREFIX."gallery_pictures WHERE galleryID='".$_GET['galleryID']."'");
 	 while($ds=mysql_fetch_array($ergebnis)) {
    @unlink('images/gallery/thumb/'.$ds[picID].'.jpg'); //thumbnails
    $path = 'images/gallery/large/';
    if(file_exists($path.$ds[picID].'.jpg')) $path = $path.$ds[picID].'.jpg';
    else $path = $path.$ds[picID].'.gif';
    @unlink($path); //large
  	safe_query("DELETE FROM ".PREFIX."comments WHERE parentID='".$ds[picID]."' AND type='ga'");
   }
 	 safe_query("DELETE FROM ".PREFIX."gallery_pictures WHERE galleryID='".$_GET['galleryID']."'");
	}
}

echo'<h2>Usergallerys</h2>';
if($_GET['action']=="add") {

    echo'<form method="post" action="index.php?site=usergallery">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Gallery-Name:</td>
		   <td><input type="text" name="name" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Group:</td>
		   <td>User-Gallery</td>
		 </tr>
		 <tr>
		   <td>&nbsp;</td>
		   <td><input type="submit" name="save" value="add Gallery"></td>
		 </tr>
		 </table>
		 </form>';
}
elseif($_GET['action']=="edit") {

  $ergebnis=safe_query("SELECT * FROM ".PREFIX."gallery WHERE galleryID='".$_GET['galleryID']."' AND userID='".$userID."'");
	$ds=mysql_fetch_array($ergebnis);

	echo'<form method="post" action="index.php?site=usergallery">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Group-Name:</td>
		   <td><input type="text" name="name" value="'.getinput($ds['name']).'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Group:</td>
		   <td>User-Gallery</td>
		 </tr>
		 <tr>
		   <td><input type="hidden" name="galleryID" value="'.$ds['galleryID'].'"></td>
		   <td><input type="submit" name="saveedit" value="update"></td>
		 </tr>
		 </table>
		 </form>';
}
elseif($_GET['action']=="upload") {

  $id=$_GET['galleryID'];

    echo'<form method="post" action="index.php?site=usergallery" enctype="multipart/form-data">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Name:</td>
		   <td><input type="text" name="name" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		 <tr>
		   <td>Comment:</td>
		   <td><input type="text" name="comment" size="50" maxlength="255" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Visitor comments:</td>
       <td><select name="comments"><option value="0">disable comments</option><option value="1">enable user comments</option><option value="2" selected>enable visitor comments</option></select></td>
		 </tr>

		 <tr>
		   <td>Picture:</td>
 		   <td><input name="picture" type="file" class="form_off"></td>
		 </tr>
		 <tr>
     <tr>
		   <td><input type="hidden" name="galleryID" value="'.$id.'"></td>
		   <td><input type="submit" name="saveform" value="add Picture"></td>
		 </tr>
		 </table>
		 </form>';



}
else {
	
	$size = $galclass->getuserspace($userID);
	$percent = percent($size, $maxusergalleries, 0);
	
	if($percent>95) $color = $loosecolor;
	else $color = $wincolor;
	
	$bg1=BG_1;
	$bg2=BG_2;
	$pagebg=PAGEBG;
	$border=BORDER;
	$bghead=BGHEAD;
	$bgcat=BGCAT;

	
	echo'<br><table border="0" width="100%"><tr><td width="50%"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=usergallery&action=add\');return document.MM_returnValue" value="new Gallery"></td>
	<td><table width="104" cellspacing="1" cellpadding="1" bgcolor="'.$border.'">
									  <tr bgcolor="'.$bg1.'">
									    <td background="images/icons/poll_bg.gif"><img src="images/icons/poll.gif" width="'.$percent.'" height="5"></td>
									  </tr>
									</table></td><td><span style="color:'.$color.'">&nbsp;'.round($size/(1024*1024),2).' MB</span> of '.round($maxusergalleries/(1024*1024),2).' MB are in use.</td></tr></table></span>';

	$ergebnis=safe_query("SELECT * FROM ".PREFIX."gallery WHERE userID='".$userID."'");
	echo'<form method="post" name="ws_gallery" action="index.php?site=usergallery"><table width="100%" cellpadding="4" cellspacing="1" bgcolor="'.$border.'">
   		<tr>
   		<td class="title" align="center">Gallery-Name:</td>
  		<td class="title" align="center" colspan="3">Actions:</td>
   		</tr>
		<tr bgcolor="'.$pagebg.'"><td colspan="4"></td></tr>';
	for($i=1;$ds=mysql_fetch_array($ergebnis);$i++) {
		if($i%2) $bg=$bg1;
		else $bg=$bg2;
    	echo'<tr bgcolor="'.$bg.'">
	       		<td align="center"><a href="index.php?site=gallery&galleryID='.$ds[galleryID].'"><b>'.cleartext($ds['name']).'</b></a></td>
		   		<td align="center"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=usergallery&action=upload&upload=form&galleryID='.$ds[galleryID].'\');return document.MM_returnValue" value="add img"></td>
		   		<td align="center"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=usergallery&action=edit&galleryID='.$ds[galleryID].'\');return document.MM_returnValue" value="edit"></td>
		   		<td align="center"><input type="button" class="button" onClick="MM_confirm(\'really delete this category?\', \'index.php?site=usergallery&delete=true&galleryID='.$ds[galleryID].'\')" value="delete"></td>
		 	</tr>';
	}
	if($i==1) echo '<tr bgcolor="'.$bg1.'"><td colspan="4">No galleries available.</td></tr>';
	echo '</table></form>';
}

} else redirect('index.php?site=login','',0);
?>
