<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[35]!=1)
  jump("jump.php?id=25&jump=main.php");
$phpcolor->table="album";
if(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "editalbum")
{
  if(!isset($_GET['page']))
    $_GET['page']=0;
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $album=$phpcolor->exist(1);
  $phpcolor->table="picture";
  $phpcolor->field="*";
  $phpcolor->where="belong = '".$_GET['id']."'";
  $count=$phpcolor->exist(2);
  $phpcolor->where="belong = '".$_GET['id']."' limit ".($_GET['page']*8).",8";
  $picture=$phpcolor->assignment();
  for ($i=0;$i<count($picture);$i++)
  {
    $picture[$i]['name']=$phpcolor->mysubstr($picture[$i]['name'],0,30); 
    $picture[$i]['filename']="upload/album/".$picture[$i]['uid']."/".$picture[$i]['belong']."/".$picture[$i]['filename'];
  }
  $page=pages($count,8,$_GET['page'],'album','action=editalbum&id=',$_GET['id'],'0');
  $tpl -> assign("picture", $picture); 
  $tpl -> assign("page", $page);
  $tpl -> assign("album", $album); 
  $tpl -> display("album_edit.htm");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "del")
{
  $phpcolor->where="id = '".$_GET['id']."'";
  $phpcolor->delete();
  $phpcolor->table="picture";
  $phpcolor->where="belong = '".$_GET['id']."'";
  $phpcolor->delete();
  if(is_dir("../upload/album/".$userv["id"]."/".$_GET['id']))
  {
    $handle = openDir("../upload/album/".$userv["id"]."/".$_GET['id']);
    while(($file_name = readdir($handle))!==false)
    {
      if($file_name!="." && $file_name!="..")
	  {
        $file_path = "../upload/album/".$userv["id"]."/".$_GET['id']."/".$file_name;
        unlink($file_path);
      }
    }
	closedir($handle);
    rmdir("upload/album/".$userv["id"]."/".$_GET['id']);
  }
  jump("jump.php?id=24&jump=album.php");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "delpic")
{
  $phpcolor->table="picture";
  $phpcolor->where="id = '".$_GET['id']."'";
  $rs = $phpcolor->exist(1);
  $phpcolor->delete();
  if(is_dir("upload/album/".$userv["id"]."/".$rs['belong']))
  {
    $file_path = "upload/album/".$userv["id"]."/".$rs['belong']."/".$file_name;
    unlink($file_path);
  }
  jump("jump.php?id=24&jump=album.php?id=".$rs['belong']."=and=action=editalbum");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "editdo")
{
  if($_POST['name']=="")
    jump("jump.php?id=2&last=album.php");
  $phpcolor->where="id = '".$_GET['id']."'";
  $phpcolor->field="name = '".$_POST['name']."',introduction = '".$_POST['introduction']."',type = '".$_POST['type']."',passwd = '".$_POST['passwd']."'";
  $phpcolor->upgrade();
  jump("jump.php?id=24&jump=album.php");
}
else
{
  if(!isset($_GET['page']))
    $_GET['page']=0;
  $phpcolor->field="*";
  if(isset($_GET['search']) and isset($_GET['theme']))
  {
    if($_GET['search']==1)
    {
	  $phpcolor->table="user";
	  $phpcolor->field="id";
      $phpcolor->where="username = '".$_GET['theme']."'";
      $rs=$phpcolor->exist(1);
	  $phpcolor->where="belong = '".$rs['id']."'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="belong = '".$rs['id']."' limit ".($_GET['page']*20).",20";
	  $phpcolor->table="album";
	  $phpcolor->field="*";
    }
    else
    {
      $phpcolor->where="name like '%".$_GET['theme']."%'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="name like '%".$_GET['theme']."%' limit ".($_GET['page']*20).",20";
	}
    $page=pages($count,20,$_GET['page'],'album','&theme='.$_GET['theme'].'&search=',$_GET['search'],'0');
  }
  else
  {
    $phpcolor->where="1 = 1";
    $count=$phpcolor->exist(2);
    $phpcolor->where="1=1 limit ".($_GET['page']*20).",20";
    $album=$phpcolor->assignment();
	$page=pages($count,20,$_GET['page'],'album','','','0');
  }
  $phpcolor->table="user";
  $phpcolor->field="username";
  for ($i=0;$i<count($album);$i++)
  {
    $phpcolor->where="id = '".$album[$i]["belong"]."'";
    $rs=$phpcolor->exist(1);
    $album[$i]['usern']=$rs['username'];
  }
  $tpl -> assign("album", $album); 
  $tpl -> assign("page", $page);
  $tpl -> display("album.htm");
}
?>