<?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(!isgalleryadmin($userID) OR substr(basename($_SERVER[REQUEST_URI]),0,15) != "admincenter.php") die('Access denied.');

if($_GET['part'] == "groups") {

if($_POST['save']) {
	safe_query("INSERT INTO ".PREFIX."gallery_groups ( name, sort ) values( '".$_POST['name']."', '1' ) ");
}
elseif($_POST['saveedit']) {
	safe_query("UPDATE ".PREFIX."gallery_groups SET name='".$_POST['name']."' WHERE groupID='".$_POST['groupID']."'");
}
elseif($_POST['sort']) {
	if(is_array($_POST['sortlist'])) {
		foreach($_POST['sortlist'] as $sortstring) {
		    $sorter=explode("-", $sortstring);
			safe_query("UPDATE ".PREFIX."gallery_groups SET sort='$sorter[1]' WHERE groupID='$sorter[0]' ");
		}
	}
}
elseif($_GET['delete']) {
	safe_query("DELETE FROM ".PREFIX."gallery_groups WHERE groupID='".$_GET['groupID']."'");
	safe_query("DELETE FROM ".PREFIX."gallery WHERE groupID='".$_GET['groupID']."'");
}

echo'<h2>group-categorys</h2>';
if($_GET['action']=="add") {
    echo'<form method="post" action="admincenter.php?site=gallery&part=groups">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Group-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>&nbsp;</td>
		   <td><input type="submit" name="save" value="add Group"></td>
		 </tr>
		 </table>
		 </form>';
}
elseif($_GET['action']=="edit") {

  $ergebnis=safe_query("SELECT * FROM ".PREFIX."gallery_groups WHERE groupID='".$_GET['groupID']."'");
	$ds=mysql_fetch_array($ergebnis);

	echo'<form method="post" action="admincenter.php?site=gallery&part=groups">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Group-Name:</td>
		   <td><input type="text" name="name" value="'.$ds[name].'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td><input type="hidden" name="groupID" value="'.$ds[groupID].'"></td>
		   <td><input type="submit" name="saveedit" value="update"></td>
		 </tr>
		 </table>
		 </form>';
}
else {
	echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=gallery&part=groups&action=add\');return document.MM_returnValue" value="new Group"><br><br>';

	$ergebnis=safe_query("SELECT * FROM ".PREFIX."gallery_groups ORDER BY sort");
	echo'<form method="post" name="ws_gallery" action="admincenter.php?site=gallery&part=groups"><table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
   		<tr bgcolor="#CCCCCC">
   		<td class="title" align="center">Group-Name:</td>
  		<td class="title" align="center" colspan="2">Actions:</td>
  		<td class="title" align="center">Sort</td>
   		</tr>
		<tr bgcolor="#FFFFFF"><td colspan="4"></td></tr>';
	while($ds=mysql_fetch_array($ergebnis)) {
    	$list = '<select name="sortlist[]">';
      for($i=1;$i<=mysql_num_rows($ergebnis);$i++) {
       $list.='<option value="'.$ds[groupID].'-'.$i.'">'.$i.'</option>';
      }
      $list .= '</select>';
      $list = str_replace('value="'.$ds[groupID].'-'.$ds[sort].'"','value="'.$ds[groupID].'-'.$ds[sort].'" selected',$list);

    	echo'<tr bgcolor="#FFFFFF">
	       		<td align="center"><b>'.$ds[name].'</b></td>
		   		<td align="center"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=gallery&part=groups&action=edit&groupID='.$ds[groupID].'\');return document.MM_returnValue" value="edit"></td>
		   		<td align="center"><input type="button" class="button" onClick="MM_confirm(\'really delete this category?\', \'admincenter.php?site=gallery&part=groups&delete=true&groupID='.$ds[groupID].'\')" value="delete"></td>
		   		<td align="center">'.$list.'</td>
		 	</tr>';
	}
	echo'<tr bgcolor="#CCCCCC"><td colspan="3"></td><td align="center"><input type="submit" name="sort" value="update"></td></table></form>';
}
}



//part: gallerys



elseif($_GET['part'] == "gallerys") {

if($_POST['save']) {
	safe_query("INSERT INTO ".PREFIX."gallery ( name, date, groupID ) values( '".$_POST['name']."', '".time()."', '".$_POST['group']."' ) ");
	$id = mysql_insert_id();
}
elseif($_POST['saveedit']) {
	safe_query("UPDATE ".PREFIX."gallery SET name='".$_POST['name']."', groupID='".$_POST['group']."' WHERE galleryID='".$_POST['galleryID']."'");
}
elseif($_POST['saveftp']) {

  $dir = '../images/gallery/';
  $comment = $_POST['comment'];
  $name = $_POST['name'];
  $i=0;
  foreach($_POST['pictures'] as $picture) {
   $typ = getimagesize($dir.$picture);
   switch ($typ[2]) {
    case 1: $typ = '.gif'; break;
    case 2: $typ = '.jpg'; break;
   }
   
   if($name[$i]) $insertname = $name[$i];
   else $insertname = $picture;
 	 safe_query("INSERT INTO ".PREFIX."gallery_pictures ( galleryID, name, comment, comments) VALUES ('".$_POST['galleryID']."', '".$insertname."', '".$comment[$i]."', '".$_POST['comments']."' )");
 	 $insertid = mysql_insert_id();
 	 copy($dir.$picture, $dir.'large/'.$insertid.$typ);
 	 $galclass->savethumb($dir.'large/'.$insertid.$typ, $dir.'thumb/'.$insertid.'.jpg');
 	 @unlink($dir.$picture);
 	 $i++;
	}
}
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']."' )");
	 $insertid = mysql_insert_id();

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

   move_uploaded_file($picture[tmp_name], $dir.'large/'.$insertid.$typ);
 	 $galclass->savethumb($dir.'large/'.$insertid.$typ, $dir.'thumb/'.$insertid.'.jpg');
	}
}
elseif($_GET['delete']) {
 //SQL
 if(safe_query("DELETE FROM ".PREFIX."gallery WHERE galleryID='".$_GET['galleryID']."'")) {
	
  //FILES

  $ergebnis=safe_query("SELECT picID FROM ".PREFIX."gallery_pictures WHERE galleryID='".$_GET['galleryID']."'");
	while($ds=mysql_fetch_array($ergebnis)) {
   @unlink('../images/gallery/'.$_GET['galleryID'].'/thumb/'.$ds[picID].'.jpg'); //thumbnails

   $path = '../images/gallery/'.$_GET['galleryID'].'/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>Gallerys</h2>';
if($_GET['action']=="add") {

  $ergebnis=safe_query("SELECT * FROM ".PREFIX."gallery_groups");
  $groups = '<select name="group">';
	while($ds=mysql_fetch_array($ergebnis)) {
   $groups.='<option value="'.$ds[groupID].'">'.$ds[name].'</option>';
  }
  $groups.='</select>';
  
    echo'<form method="post" action="admincenter.php?site=gallery&part=gallerys&action=upload">
	     <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>'.$groups.'</td>
		 </tr>
		 <tr>
		   <td>Picture Upload:</td>
		   <td><select name="upload"><option value="ftp">FTP</option><option value="form">Formular</option><select></td><td>
		 </tr>
		 <tr>
		   <td>&nbsp;</td>
		   <td><input type="submit" name="save" value="add Gallery"></td>
		 </tr>
		 </table>
		 </form><br>Upload your images to "http://'.$hp_url.'/images/gallery" via FTP to add files.';
}
elseif($_GET['action']=="edit") {

  $ergebnis=safe_query("SELECT * FROM ".PREFIX."gallery_groups");
  $groups = '<select name="group">';
	while($ds=mysql_fetch_array($ergebnis)) {
   $groups.='<option value="'.$ds[groupID].'">'.$ds[name].'</option>';
  }
  $groups.='</select>';

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

  $groups = str_replace('value="'.$ds[groupID].'"','value="'.$ds[groupID].'" selected',$groups);

	echo'<form method="post" action="admincenter.php?site=gallery&part=gallerys">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Group-Name:</td>
		   <td><input type="text" name="name" value="'.$ds[name].'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Group:</td>
		   <td>'.$groups.'</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") {

 $dir = '../images/gallery/';
 if($_POST['upload'] == "ftp") {
  $ergebnis=safe_query("SELECT * FROM ".PREFIX."gallery_groups WHERE groupID='".$_GET['groupID']."'");
	$ds=mysql_fetch_array($ergebnis);

	echo'<form method="post" action="admincenter.php?site=gallery&part=gallerys">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>';

     $pics = Array();
     $picdir = opendir($dir);
     while (false !== ($file = readdir($picdir))) {
        if ($file != "." && $file != "..") {
         if(is_file($dir.$file)) {
          if($info = getimagesize($dir.$file)) {
           if($info[2]==1 OR $info[2]==2) $pics[] = $file;
          }
         }
        }
     }
     closedir($picdir);
     natcasesort ($pics);
     reset ($pics);
     echo '<table border="0" width="100%"><tr><td></td><td><b>Filename</b></td><td><b>Name</b></td><td><b>Comment</b></td></tr>';
     foreach($pics as $val) {
      if(is_file($dir.$val)) {
       echo '<tr><td><input type="checkbox" value="'.$val.'" name="pictures[]" checked></td><td><a href="'.$dir.$val.'" target="_blank">'.$val.'</a></td><td><input type="text" name="name[]" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'" width="100"></td><td><input type="text" name="comment[]" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'" width="100"></td></tr>';
      }
     }
     echo '</table>';
     echo '</td>
		 </tr>
		 <tr>
		   <td>Visitor comments  <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><input type="hidden" name="galleryID" value="'.$id.'"><input type="submit" name="saveftp" value="Save"></td>
		 </tr>
		 </table>
		 </form>';
 } elseif($_POST['upload'] == "form" OR $_GET['upload'] == "form") {
 
 if($_GET['upload'] == "form") $id=$_GET['galleryID'];
 
    echo'<form method="post" action="admincenter.php?site=gallery&part=gallerys" 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 {
	echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=gallery&part=gallerys&action=add\');return document.MM_returnValue" value="new Gallery"><br><br>';

	$ergebnis=safe_query("SELECT * FROM ".PREFIX."gallery WHERE userID='0'");
	echo'<b>Pagegalleries</b><br><br><form method="post" name="ws_gallery" action="admincenter.php?site=gallery&part=gallerys"><table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
   		<tr bgcolor="#CCCCCC">
   		<td class="title" align="center">Gallery-Name:</td>
  		<td class="title" align="center" colspan="3">Actions:</td>
   		</tr>
		<tr bgcolor="#FFFFFF"><td colspan="4"></td></tr>';
	while($ds=mysql_fetch_array($ergebnis)) {
    	echo'<tr bgcolor="#FFFFFF">
	       		<td align="center"><b>'.$ds[name].'</b></td>
		   		<td align="center"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=gallery&part=gallerys&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\',\'admincenter.php?site=gallery&part=gallerys&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?\', \'admincenter.php?site=gallery&part=gallerys&delete=true&galleryID='.$ds[galleryID].'\')" value="delete"></td>
		 	</tr>';
	}
	echo'</table></form><br><br>';

	$ergebnis=safe_query("SELECT * FROM ".PREFIX."gallery WHERE userID!='0'");
	echo'<b>Usergalleries</b><br><br><form method="post" name="ws_gallery" action="admincenter.php?site=gallery&part=gallerys"><table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
   		<tr bgcolor="#CCCCCC">
   		<td class="title" align="center">Gallery-Name:</td>
   		<td class="title" align="center">User:</td>
  		<td class="title" align="center" colspan="2">Actions:</td>
   		</tr>
		<tr bgcolor="#FFFFFF"><td colspan="4"></td></tr>';
	while($ds=mysql_fetch_array($ergebnis)) {
    	echo'<tr bgcolor="#FFFFFF">
	       	<td align="center"><b>'.$ds[name].'</b></td>
	       	<td align="center">'.getnickname($ds[userID]).'</td>
		   		<td align="center"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=gallery&part=gallerys&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?\', \'admincenter.php?site=gallery&part=gallerys&delete=true&galleryID='.$ds[galleryID].'\')" value="delete"></td>
		 	</tr>';
	}
	echo'</table></form>';

}


}
?>	
