<?php
$changesection="main";
if (isset($_POST['insert']))
{
$insert=$_POST['insert'];
}
if (isset($_POST['update']))
{
$update=$_POST['update'];
}
if (isset($_GET['delete']))
{
$delete=$_GET['delete'];
}
if (isset($_GET['edit']))
{
$edit=$_GET['edit'];
}
if (isset($insert))
{
if (isset($_POST['galleryname']))
{
$galleryname=$_POST['galleryname'];
}
if (!empty($galleryname))
{
$galleries=mysql_query("select * from galleries where gallery='$galleryname'",$db);
$gallery=mysql_fetch_assoc($galleries);
if (empty($gallery))
{
include("connectftp.php");
ftp_mkdir($ftp,"thumbs/".$galleryname);
ftp_mkdir($ftp,"images/".$galleryname);
ftp_put($ftp,"thumbs/".$galleryname."/index.php","thumbs/index.php",FTP_BINARY);
ftp_put($ftp,"images/".$galleryname."/index.php","images/index.php",FTP_BINARY);
$ftp=ftp_close($ftp);
mysql_query("insert into galleries (gallery) values ('$galleryname')",$db);
$changes[$changesection].='Gallery <b>'.$galleryname.'</b> has been inserted into database<br>';
}
else
{
$changes[$changesection].='Gallery could not be inserted into database<br>';
$changes[$changesection].='Gallery <b>'.$galleryname.'</b> already exists<br>';
$changes[$changesection].='<hr>';
}
}
else
{
$changes[$changesection].='Gallery could not be inserted into database<br>';
$changes[$changesection].='Gallery missing<br>';
$changes[$changesection].='<hr>';
}
}
if (isset($update))
{
if (isset($_POST['galleryid']))
{
$galleryid=$_POST['galleryid'];
}
if (isset($_POST['galleryname']))
{
$galleryname=$_POST['galleryname'];
}
$galleries=mysql_query("select * from galleries where id='$galleryid'",$db);
$gallery=mysql_fetch_assoc($galleries);
if ((!empty($galleryname)) && ($galleryname!=$gallery['gallery']))
{
$galleries=mysql_query("select * from galleries where gallery='$galleryname'",$db);
$gallery=mysql_fetch_assoc($galleries);
if (empty($gallery))
{
$galleries=mysql_query("select * from galleries where id='$galleryid'",$db);
$gallery=mysql_fetch_assoc($galleries);
include("connectftp.php");
ftp_rename($ftp,"thumbs/".$gallery['gallery'],"thumbs/".$galleryname);
ftp_rename($ftp,"images/".$gallery['gallery'],"images/".$galleryname);
$ftp=ftp_close($ftp);
mysql_query("update galleries set gallery='$galleryname' where id='$galleryid'",$db);
$changes[$changesection].='Gallery has been changed to <b>'.$galleryname.'</b><br>';
}
else
{
$changes[$changesection].='Gallery could not be updated, gallery <b>'.$galleryname.'</b> already exists<br>';
}
}
}
if (isset($delete))
{
$galleries=mysql_query("select * from galleries where id='$delete'",$db);
$gallery=mysql_fetch_assoc($galleries);
$images=mysql_query("select * from images where gallery='$delete'",$db);
include("connectftp.php");
while ($image=mysql_fetch_assoc($images))
{
ftp_delete($ftp,"thumbs/".$gallery['gallery']."/".$image['filename']);
ftp_delete($ftp,"images/".$gallery['gallery']."/".$image['filename']);
}
ftp_delete($ftp,"thumbs/".$gallery['gallery']."/index.php");
ftp_delete($ftp,"images/".$gallery['gallery']."/index.php");
ftp_rmdir($ftp,"thumbs/".$gallery['gallery']);
ftp_rmdir($ftp,"images/".$gallery['gallery']);
$ftp=ftp_close($ftp);
mysql_query("delete from images where gallery='$delete'",$db);
mysql_query("delete from galleries where id='$delete'",$db);
$changes[$changesection].='Gallery <b>'.$gallery['gallery'].'</b> has been deleted from database<br>';
}
$changes[$changesection].='<form method="post" action="index.php?subsite=galleries-admin.php'.$sessionparm_link.'">';
$changes[$changesection].='<table class="vistable">';
if (isset($edit))
{
$galleries=mysql_query("select * from galleries where id='$edit'",$db);
$gallery=mysql_fetch_assoc($galleries);
$changes[$changesection].='<tr><td>Gallery:</td><td><input type="text" name="galleryname" value="'.$gallery['gallery'].'"></td></tr>';
$changes[$changesection].='</table>';
$changes[$changesection].='<input type="hidden" name="galleryid" value="'.$gallery['id'].'">';
$changes[$changesection].='<input type="submit" name="update" value="Update Gallery">';
$changes[$changesection].='<input type="submit" name="cancel" value="Cancel">';
}
else
{
$changes[$changesection].='<tr><td>Gallery:</td><td><input type="text" name="galleryname"></td></tr>';
$changes[$changesection].='</table>';
$changes[$changesection].='<input type="submit" name="insert" value="Add Gallery">';
$changes[$changesection].='</form>';
$changes[$changesection].='<table class="vistable" border="1">';
$changes[$changesection].='<tr><th>Gallery</th><th>Images</th><th colspan="3">Action</th></tr>';
$galleries=mysql_query("select * from galleries order by gallery",$db);
while ($gallery=mysql_fetch_assoc($galleries))
{
$images=mysql_query("select * from images where gallery='".$gallery['id']."'",$db);
$num_images=mysql_num_rows($images);
$changes[$changesection].='<tr><td>'.$gallery['gallery'].'</td><td>'.$num_images.'</td>';
$changes[$changesection].='<td><a href="index.php?subsite=galleries-admin.php&edit='.$gallery['id'].$sessionparm_link.'">Edit</a></td>';
$changes[$changesection].='<td><a href="index.php?subsite=galleries-admin.php&delete='.$gallery['id'].$sessionparm_link.'">Delete</a></td>';
$changes[$changesection].='<td><a href="index.php?subsite=images-admin.php&gallery='.$gallery['id'].$sessionparm_link.'">Edit Images</a></td>';
$changes[$changesection].='</tr>';
}
$changes[$changesection].='</table>';
}
?>