<?php
define('IN_PHPBB', 1);

if( !empty($setmodules) )
{
	$file = basename(__FILE__);
	$module['face']['face_mod'] = $file."?action=modify";
	$module['face']['face_add'] = $file."?action=add";
	$module['face']['face_up'] = $file."?action=upload";
	return;
}

//
// Let's set the root dir for phpBB
//
$phpbb_root_path = "./../";
require($phpbb_root_path . 'extension.inc');
require('./pagestart.' . $phpEx);
require($phpbb_root_path."includes/functions_face.php");


if (!empty($HTTP_POST_VARS['action']) || !empty($HTTP_GET_VARS['action']))
{
	$action = (!empty($HTTP_POST_VARS['action'])) ? $HTTP_POST_VARS['action'] : $HTTP_GET_VARS['action'];
}
else
{
	$action="modify";
}
if (!empty($HTTP_POST_VARS['sort']) || !empty($HTTP_GET_VARS['sort']))
{
	$sort=(!empty($HTTP_POST_VARS['sort'])) ? $HTTP_POST_VARS['sort'] : $HTTP_GET_VARS['sort'];
}
if (!empty($HTTP_POST_VARS['sp_fitherd']) || !empty($HTTP_GET_VARS['sp_fitherd']))
{
	$sp_fitherd=(!empty($HTTP_POST_VARS['sp_fitherd'])) ? $HTTP_POST_VARS['sp_fitherd'] : $HTTP_GET_VARS['sp_fitherd'];
}
if (!empty($HTTP_POST_VARS['startpage']) || !empty($HTTP_GET_VARS['startpage']))
{
	$startpage=(!empty($HTTP_POST_VARS['startpage'])) ? $HTTP_POST_VARS['startpage'] : $HTTP_GET_VARS['startpage'];
}
if (!empty($HTTP_POST_VARS['perpage']) || !empty($HTTP_GET_VARS['perpage']))
{
	$perpage=(!empty($HTTP_POST_VARS['perpage'])) ? $HTTP_POST_VARS['perpage'] : $HTTP_GET_VARS['perpage'];
}
if (!empty($HTTP_POST_VARS['faceid']) || !empty($HTTP_GET_VARS['faceid']))
{
	$faceid=(!empty($HTTP_POST_VARS['faceid'])) ? $HTTP_POST_VARS['faceid'] : $HTTP_GET_VARS['faceid'];
}

/*
if (isset($action)==0) {
	$action="modify";
}
*/
// ###################### Start Upload #######################
if ($action=="upload") {
	$leibie1 = $db->sql_query("SELECT * FROM week9_class where have = '1'");

	while ($facesort = $db->sql_fetchrow($leibie1))
	{	
		$faceleibie .= " <option value=\"$facesort[id]\">$facesort[name1]</option>";
	}
  echo "<p><b>使用這項功能要求你的伺服器有PHP寫入檔的許可權。如果許可權不夠，操作將無法完成。</b></p>";

  doformheader("admin_faceset","doupload",1);
  maketableheader("從電腦中上傳新的形象圖片");
  echo  "
  </tr>
	<tr class=secondalt valign=top>
	<td><p>上傳形象分類</p></td>
	<td><p>
	<select name=\"sp_sort\">
	$faceleibie
	</select>
	</p></td>
 </tr>
";
  makeuploadcode("圖示檔（大圖示）","sp_graphic");
  makeuploadcode("圖示檔（小圖示）","sp_sxgraphic");
  makeinputcode("形象名稱","sp_name");
  makeinputcode("形象介紹","sp_description");
  makeinputcode("所售價錢","sp_money");
  makeinputcode("商品耐用度<br>（以10為基本單位每10可以使用1天）","sp_wear");
  echo "
	<tr class=firstalt valign=top>
	<td><p>適合人群</p></td>
	<td><p>
	<select name=\"sp_fitherd\">
	<option value=\"t\" selected>所有人</option>
	<option value=\"m\">帥哥</option>
	<option value=\"f\">美女</option>
	</select>
	</tr>
";
  doformfooter("上傳");

}

// ###################### Start DoUpload #######################
if ($HTTP_POST_VARS['action']=="doupload") {
	$sp_sort=$HTTP_POST_VARS['sp_sort'];
	$sp_name=$HTTP_POST_VARS['sp_name'];
	$sp_description=$HTTP_POST_VARS['sp_description'];
	$sp_money=$HTTP_POST_VARS['sp_money'];
	$sp_wear=$HTTP_POST_VARS['sp_wear'];
	$sp_fitherd=$HTTP_POST_VARS['sp_fitherd'];
	$name = $_FILES["sp_graphic"]["name"];
	$namex = $_FILES["sp_sxgraphic"]["name"];
  	if("" == trim($name))
	{
		echo "上傳檔案名不能為空！<br> <a href=javascript:window.history.back()>點這裏返回修改</a>";
		exit;
	}

	if("" == trim($namex))
		{
		echo "上傳檔案名不能為空！<br> <a href=javascript:window.history.back()>點這裏返回修改</a>";
		exit;
	}
		 
	if("" == trim($sp_name))
	{
		echo "<p align=center>名稱不能為空。<a href=javascript:history.back()>點這裏返回修改</a></p>";	
		exit;
	} 

	if("" == trim($sp_description))
	{
		echo "<p align=center>介紹不能為空。<a href=javascript:history.back()>點這裏返回修改</a></p>";	
		exit;
	}

	if("" == trim($sp_money))
	{
		echo "<p align=center>售價不能為空。<a href=javascript:history.back()>點這裏返回修改</a></p>";	
		exit;
	}

	if("" == trim($sp_wear))
	{
		echo "<p align=center>使用時間不能為空。<a href=javascript:history.back()>點這裏返回修改</a></p>";	
		exit;
	}
	$sp_sortpath = trim($sp_sort);
	$image_basepath = "../images/face/$sp_sortpath/";	

    if("" != $namex)
	{
		if(!preg_match("/.gif$/i",$namex))
		{
			echo  "<p align=center>只允許上傳gif格式的檔。<a href=javascript:window.history.back()>點這裏返回修改</a></p>";	
			exit;
		}
		$sp_sxgraphic = preg_replace("'[\r\n\s]'", "", $namex);
		
		if("" == trim($sp_sxgraphic))
		{
			echo "上傳檔案名不能為空！<br> <a href=javascript:window.history.back()>點這裏返回修改</a>";
		}
		
		$result = $db->sql_query("SELECT COUNT(*) AS nr FROM week9_data WHERE sp_sxgraphic = '$sp_sxgraphic' and sort='$sp_sort'"); 
		$faceidrows =$db->sql_fetchrow($result);
		$nr = $faceidrows[nr];
		if ($nr != '0')
		{
			echo "這個檔已經存在在資料庫裏！<br> <a href=javascript:window.history.back()>點這裏返回修改</a>";
			exit;
		}	
		$targetFileName = $image_basepath.$sp_sxgraphic;
		if (is_uploaded_file($_FILES["sp_sxgraphic"]["tmp_name"]))
		{
			move_uploaded_file($_FILES["sp_sxgraphic"]["tmp_name"], "$targetFileName");
		}
		else
		{
			echo "上傳" . $_FILES["sp_sxgraphic"]["name"] . "失敗！！<br>";
			echo "<a href=javascript:window.history.back()>點這裏返回修改</a>";
			exit;
		}
	}
	if("" != $name)
	{
		if(!preg_match("/.gif$/i", $name))
		{
			echo  "<p align=center>只允許上傳gif格式的檔。<a href=javascript:window.history.back()>點這裏返回修改</a></p>";	
			exit;
		}
		$sp_graphic = preg_replace("'[\r\n\s]'", "", $name);		
		if("" == trim($sp_graphic))
		{
			echo "上傳檔案名不能為空！<br> <a href=javascript:window.history.back()>點這裏返回修改</a>";
		}
		
		$result = $db->sql_query("SELECT COUNT(*) AS nr FROM week9_data WHERE sp_graphic = '$sp_graphic'  and sort='$sp_sort'"); 
		$faceidrows =$db->sql_fetchrow($result);
		$nr = $faceidrows[nr];
		if ($nr != '0')
		{
			echo "這個檔已經存在在資料庫裏！<br> <a href=javascript:window.history.back()>點這裏返回修改</a>";
			exit;
		}
		$targetFileName = $image_basepath.$sp_graphic;
		if (is_uploaded_file($_FILES["sp_graphic"]["tmp_name"]))
		{
			move_uploaded_file($_FILES["sp_graphic"]["tmp_name"], "$targetFileName");
		}
		else
		{
			echo "上傳" . $_FILES["sp_graphic"]["name"] . "失敗！！<br>";
			echo "<a href=javascript:window.history.back()>點這裏返回修改</a>";
			exit;
		}
    }
	$db->sql_query("INSERT INTO week9_data (sort,sp_name,sp_money,sp_description,sp_wear,sp_fitherd,sp_graphic,sp_sxgraphic) VALUES ('$sp_sort', '".addslashes($sp_name)."', '".trim($sp_money)."', '".addslashes($sp_description)."', '".trim($sp_wear)."', '$sp_fitherd','".addslashes($sp_graphic)."','".addslashes($sp_sxgraphic)."')");
	echo "記錄添加成功！<br><a href=\"javascript:history.back()\">返回</a>";
}

// ###################### Start Add #######################
if ($action=="add") {
	$leibie1 = $db->sql_query("SELECT * FROM week9_class where have = '1'");
	while ($facesort = $db->sql_fetchrow($leibie1))
	{	
		$faceleibie .= " <option value=\"$facesort[id]\">$facesort[name1]</option>";
	}
  doformheader("admin_faceset","insert",0,1,"addone");
  maketableheader("添加單個形象圖片");
  echo  "
  </tr>
	<tr class=secondalt valign=top>
	<td><p>添加到形象分類</p></td>
	<td><p>
	<select name=\"sp_sort\" onchange=\"facepath1.innerHTML=facepath2.innerHTML='images/face/'+document.addone.sp_sort.options[document.addone.sp_sort.selectedIndex].value+'/'\">
	$faceleibie
	</select>
	</p></td>
 </tr>
";
  makeinputcode("大圖片名(請確認此圖片位於：<font id='facepath1' color=red>images/face/2/</font>)","sp_graphic","name.gif");
  makeinputcode("小圖片名(請確認此圖片位於：<font id='facepath2' color=red>images/face/2/</font>)","sp_sxgraphic","namex.gif");
  makeinputcode("形象名稱","sp_name");
  makeinputcode("形象介紹","sp_description");
  makeinputcode("所售價錢","sp_money");
  makeinputcode("商品耐用度<br>（以10為基本單位每10可以使用1天）","sp_wear");
  echo "
	<tr class=firstalt valign=top>
	<td><p>適合人群</p></td>
	<td><p>
	<select name=\"sp_fitherd\">
	<option value=\"t\" selected>所有人</option>
	<option value=\"m\">帥哥</option>
	<option value=\"f\">美女</option>
	</select>
	</tr>
";
  doformfooter("添加形象圖片");

  // Add multiple
  doformheader("admin_faceset","insertmultiple",0,1,"addmore");
  maketableheader("添加一組形象圖片");
  echo  "
  </tr>
	<tr class=secondalt valign=top>
	<td><p>添加到形象分類</p></td>
	<td><p>
	<select name=\"facesort\" onchange=\"document.addmore.facepath.value='images/face/'+document.addmore.facesort.options[document.addmore.facesort.selectedIndex].value\">
	$faceleibie
	</select>
	</p></td>
 </tr>
";
  makeinputcode("圖片路徑(要相對於vB論壇的主目錄)","facepath","images/face/2");
  makeinputcode("每頁顯示的形象圖片數量？","facesperpage",20);
  doformfooter("添加形象圖片");
}

// ###################### Start Insert #######################
if ($HTTP_POST_VARS['action']=="insert") {
	$sp_sort=$HTTP_POST_VARS['sp_sort'];
	$sp_name=$HTTP_POST_VARS['sp_name'];
	$sp_description=$HTTP_POST_VARS['sp_description'];
	$sp_money=$HTTP_POST_VARS['sp_money'];
	$sp_wear=$HTTP_POST_VARS['sp_wear'];
	$sp_fitherd=$HTTP_POST_VARS['sp_fitherd'];
	$sp_graphic=$HTTP_POST_VARS['sp_graphic'];
	$sp_sxgraphic=$HTTP_POST_VARS['sp_sxgraphic'];
	$db->sql_query("INSERT INTO week9_data (sort,sp_name,sp_money,sp_description,sp_wear,sp_fitherd,sp_graphic,sp_sxgraphic) VALUES ('$sp_sort', '".addslashes($sp_name)."', '".trim($sp_money)."', '".addslashes($sp_description)."', '".trim($sp_wear)."', '$sp_fitherd','".addslashes($sp_graphic)."','".addslashes($sp_sxgraphic)."')");
	echo "記錄添加成功！<br><a href=\"javascript:history.back()\">返回</a>";
	exit;
}
;
// ###################### Start Multi Prompt  #######################
if ($action=="insertmultiple") {
?>
<script language="JavaScript">
<!--
function CheckAll()
{
	for (var i=0;i<document.form.elements.length;i++)
	{
		var e = document.form.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox'))
		e.checked = document.form.allbox.checked;
	}
}
function CheckCheckAll()
{
	var TotalBoxes = 0;
	var TotalOn = 0;
	for (var i=0;i<document.form.elements.length;i++)
	{
		var e = document.form.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox'))
		{
			TotalBoxes++;
		if (e.checked)
		{
			TotalOn++;
		}
		}
	}
	if (TotalBoxes==TotalOn)
	{document.form.allbox.checked=true;}
	else
	{document.form.allbox.checked=false;}
}
//-->
</script>
<?php

	if (!empty($HTTP_POST_VARS['facesort']) || !empty($HTTP_GET_VARS['facesort']))
	{
		$facesort=(!empty($HTTP_POST_VARS['facesort'])) ? $HTTP_POST_VARS['facesort'] : $HTTP_GET_VARS['facesort'];
	}
	if (!empty($HTTP_POST_VARS['facepath']) || !empty($HTTP_GET_VARS['facepath']))
	{
		$facepath=(!empty($HTTP_POST_VARS['facepath'])) ? $HTTP_POST_VARS['facepath'] : $HTTP_GET_VARS['facepath'];
	}
	if (!empty($HTTP_POST_VARS['facesperpage']) || !empty($HTTP_GET_VARS['facesperpage']))
	{
		$facesperpage=(!empty($HTTP_POST_VARS['facesperpage'])) ? $HTTP_POST_VARS['facesperpage'] : $HTTP_GET_VARS['facesperpage'];
	}
	if (!empty($HTTP_POST_VARS['startpage']) || !empty($HTTP_GET_VARS['startpage']))
	{
		$startpage=(!empty($HTTP_POST_VARS['startpage'])) ? $HTTP_POST_VARS['startpage'] : $HTTP_GET_VARS['startpage'];
	}

  if (intval($startpage)<1)
    $startpage = 1;
  if (intval($facesperpage)<1)
    $facesperpage = 10;
  $facepath = preg_replace("/\/$/s","",$facepath);
  if($dirhandle=@opendir('../'.$facepath)) {
     chdir('../'.$facepath);
     $ok_exit='no';

     doformheader("admin_faceset","",0,0,"form");
     echo "<input type='hidden' value='$facepath' name='facepath'><input type='hidden' value='$facesort' name='facesort'><input type='hidden' value='$facesperpage' name='facesperpage'>";
     echo "<table align='center' border='0' cellspacing='1' cellpadding='5' class='tblborder'>";
     echo "<tr class='secondalt'><td align='center' colspan='8'>添加多個形象圖片從<b>$bburl/$facepath</b>到 分類 $facesort</td></tr>";
     echo "<tr class='tblhead'><td align='center'><b><span class='tblhead'>大圖片</span></b></td><td align='center'><b><span class='tblhead'>小圖片</span></b></td><td align='center'><b><span class='tblhead'>名稱</span></b></td><td align='center'><b><span class='tblhead'>介紹</span></b></td><td align='center'><b><span class='tblhead'>價錢</span></b></td><td align='center'><b><span class='tblhead'>耐用度</b></span></td><td align='center'><b><span class='tblhead'>適合人群</span></b></td>";
     echo "<td align='center'><input name='allbox' type='checkbox' value='Check All' onClick='CheckAll();'></td>";
    $faces=$db->sql_query("SELECT sp_graphic FROM week9_data WHERE sort='$facesort'");
     while ($face=$db->sql_fetchrow($faces)) {
       $faceArray[$face[sp_graphic]] = 'yes';
     }
     while ($filename=readdir($dirhandle)) {
       $facename = $facepath . '/' . $filename;
       if ($filename!='.' and $filename!='..'  and $filename!='init.gif'  and $filename!='initf.gif' and @is_file($filename) and !$faceArray[$filename] and (($filelen=strlen($filename))>=5)) {
           $fileext1 = strtolower(substr($filename,$filelen-4,$filelen-1));
           $fileext2 = strtolower(substr($filename,$filelen-5,$filelen-1));
		   $fileext3=substr($filename,0,strpos($filename,"."));
           if ($fileext1==".gif"&&$fileext2!="x.gif") {
             $FileArray[count($FileArray)] = $fileext3;
           }
       }
     }
     $facecount = count($FileArray);
     if ((($facecount / $facesperpage) - ((int) ($facecount / $facesperpage))) == 0)
       $numpages = $facecount / $facesperpage;
     else
       $numpages = (int) ($facecount / $facesperpage) + 1;
     if ($startpage == 1) {
       $startface = 0;
       $endface = $facesperpage - 1;
     } else {
       $startface = ($startpage - 1) * $facesperpage;
       $endface = ( $facesperpage * $startpage ) - 1 ;
     }
     for ($x = 0; $x < $facecount; $x++) {
       if ($x >= $startface && $x <= $endface) {
//		   $imgname=substr($FileArray[$x],0,strpos($FileArray[$x],"."));
         echo "<tr class='".getrowbg()."'>";
         echo "<td align='center'><img src='../$facepath/$FileArray[$x].gif' border=0 align='center'></td>";
         echo "<td align='center'><img src='../$facepath/$FileArray[$x]x.gif' border=0 align='center'></td>";
         echo "<td align='center'><input type='text' name='facename[$FileArray[$x]]' size=10 value='$FileArray[$x]'></td>";
         echo "<td align='center'><input type='text' name='faceinfo[$FileArray[$x]]' size=10 value='$FileArray[$x]'></td>";
         echo "<td align='center'><input type='text' name='facemoney[$FileArray[$x]]' size=5 value='100'></td>";
         echo "<td align='center'><input type='text' name='facetime[$FileArray[$x]]' size=5 value='300'></td>";
         echo "<td><select name=\"faceuser[$FileArray[$x]]\"><option value=\"t\" selected>所有人</option><option value=\"m\">帥哥</option><option value=\"f\">美女</option></select>";
         echo "<td align='center'><input type='checkbox' name='faceuse[$FileArray[$x]]' value='yes'><input type='hidden' name='facepic[$FileArray[$x]]' value='$FileArray[$x].gif'><input type='hidden' name='facepicx[$FileArray[$x]]' value='$FileArray[$x]x.gif'><input type='hidden' value='$facesort' name='facesort[$FileArray[$x]]'></td></tr>";
       }
     }
     if ($numpages > 1) {
       for ($x = 1; $x <= $numpages; $x++) {
         if ($x == $startpage)
            $pagelinks .= "<b> $x </b>";
         else
            $pagelinks .= " <a href=\"".append_sid("admin_faceset.php?startpage=$x&facesperpage=$facesperpage&action=insertmultiple&facepath=$facepath&facesort=$facesort")."\">$x</a> ";
       }
       if ($startpage != $numpages) {
         $nextstart = $startpage + 1;
         $nextpage = " <a href=".append_sid("admin_faceset.php?startpage=$nextstart&facesperpage=$facesperpage&action=insertmultiple&facepath=$facepath&facesort=$facesort").">></a>";
         $eface = $endface + 1;
       } else
         $eface = $facecount;
       if ($startpage!=1) {
         $prevstart = $startpage - 1;
         $prevpage = "<a href=".append_sid("admin_faceset.php?startpage=$prevstart&facesperpage=$facesperpage&action=insertmultiple&facepath=$facepath&facesort=$facesort")."><</a> ";
       }
       $sface = $startface +  1;
       echo "<tr class='".getrowbg()."'><td align='center' colspan=8>顯示共 $facecount 個形象圖片中的 $sface 到 $eface 個<br><br>$prevpage $pagelinks $nextpage</td></tr>";
     }
     echo '</table>';
     echo "<center><input type='hidden' name='facecount' value='$facecount'>
	 添加形象<input type='radio' name='action' value='domultiple' checked>
	 刪除圖片<input type='radio' name='action' value='del'></center>";
     doformfooter("確定","重置",2,"返回");
     closedir($dirhandle);
  } else {
     echo "<p><b>$facepath</b> 路徑沒有找到<br><input type=\"button\" value=\"   返回   \" onclick=\"history.back(1)\">";
  }
}

// ###################### Start Insert Multiple #######################
if ($HTTP_POST_VARS['action']=="del") {
	$faceuse=$HTTP_POST_VARS['faceuse'];
	$facepic=$HTTP_POST_VARS['facepic'];
	$facepicx=$HTTP_POST_VARS['facepicx'];
	$facesort=$HTTP_POST_VARS['facesort'];
	if (!empty($HTTP_POST_VARS['facesort']) || !empty($HTTP_GET_VARS['facesort']))
	{
		$facesort=(!empty($HTTP_POST_VARS['facesort'])) ? $HTTP_POST_VARS['facesort'] : $HTTP_GET_VARS['facesort'];
	}
	if (!empty($HTTP_POST_VARS['facepath']) || !empty($HTTP_GET_VARS['facepath']))
	{
		$facepath=(!empty($HTTP_POST_VARS['facepath'])) ? $HTTP_POST_VARS['facepath'] : $HTTP_GET_VARS['facepath'];
	}
	if (!empty($HTTP_POST_VARS['facesperpage']) || !empty($HTTP_GET_VARS['facesperpage']))
	{
		$facesperpage=(!empty($HTTP_POST_VARS['facesperpage'])) ? $HTTP_POST_VARS['facesperpage'] : $HTTP_GET_VARS['facesperpage'];
	}
	if (!empty($HTTP_POST_VARS['startpage']) || !empty($HTTP_GET_VARS['startpage']))
	{
		$startpage=(!empty($HTTP_POST_VARS['startpage'])) ? $HTTP_POST_VARS['startpage'] : $HTTP_GET_VARS['startpage'];
	}
	if (!empty($HTTP_POST_VARS['facecount']) || !empty($HTTP_GET_VARS['facecount']))
	{
		$facecount=(!empty($HTTP_POST_VARS['facecount'])) ? $HTTP_POST_VARS['facecount'] : $HTTP_GET_VARS['facecount'];
	}

  if (is_array($faceuse)) {
    $count=0;
    while(list($key,$val)=each($faceuse)) {
      $sp_graphic = $facepic[$key];
      $sp_sxgraphic = $facepicx[$key];
      $sp_sort = $facesort[$key];
	  echo "../images/face/$sp_sort/$sp_graphic";
      if(@unlink("../images/face/$sp_sort/$sp_graphic")) echo "刪除 $sp_graphic 成功！<br>";else echo "刪除 $sp_graphic 失敗！<br>";
      if(@unlink("../images/face/$sp_sort/$sp_sxgraphic")) echo "刪除 $sp_sxgraphic 成功！<br>";else echo "刪除 $sp_sxgraphic 失敗！<br>";
      $count++;
    } //end while
    if ($count==$facecount) { // We inserted all of our available faces
      $action="modify";
    } else {
      echo "<p><a href=\"".append_sid("admin_faceset.php?action=insertmultiple&facepath=$facepath&facesperpage=$facesperpage&facesort=$sp_sort")."\">如果無法自動跳轉點擊這裏繼續</a></p>";
      exit;
    }
  } else {
    echo "<p>沒有選擇形象圖片<p><input type=button value=\"   返回   \" onclick=\"history.back(1)\">";
  }
}
// ###################### Start Insert Multiple #######################
if ($HTTP_POST_VARS['action']=="domultiple") {
	if (!empty($HTTP_POST_VARS['facesort']) || !empty($HTTP_GET_VARS['facesort']))
	{
		$facesort=(!empty($HTTP_POST_VARS['facesort'])) ? $HTTP_POST_VARS['facesort'] : $HTTP_GET_VARS['facesort'];
	}
	if (!empty($HTTP_POST_VARS['facepath']) || !empty($HTTP_GET_VARS['facepath']))
	{
		$facepath=(!empty($HTTP_POST_VARS['facepath'])) ? $HTTP_POST_VARS['facepath'] : $HTTP_GET_VARS['facepath'];
	}
	if (!empty($HTTP_POST_VARS['facesperpage']) || !empty($HTTP_GET_VARS['facesperpage']))
	{
		$facesperpage=(!empty($HTTP_POST_VARS['facesperpage'])) ? $HTTP_POST_VARS['facesperpage'] : $HTTP_GET_VARS['facesperpage'];
	}
	if (!empty($HTTP_POST_VARS['startpage']) || !empty($HTTP_GET_VARS['startpage']))
	{
		$startpage=(!empty($HTTP_POST_VARS['startpage'])) ? $HTTP_POST_VARS['startpage'] : $HTTP_GET_VARS['startpage'];
	}
	if (!empty($HTTP_POST_VARS['facecount']) || !empty($HTTP_GET_VARS['facecount']))
	{
		$facecount=(!empty($HTTP_POST_VARS['facecount'])) ? $HTTP_POST_VARS['facecount'] : $HTTP_GET_VARS['facecount'];
	}
	$faceuse=$HTTP_POST_VARS['faceuse'];
	$facepic=$HTTP_POST_VARS['facepic'];
	$facepicx=$HTTP_POST_VARS['facepicx'];
	$facesort=$HTTP_POST_VARS['facesort'];

	$facename=$HTTP_POST_VARS['facename'];
	$facemoney=$HTTP_POST_VARS['facemoney'];
	$faceinfo=$HTTP_POST_VARS['faceinfo'];
	$facetime=$HTTP_POST_VARS['facetime'];
	$faceuser=$HTTP_POST_VARS['faceuser'];
	
  if (is_array($faceuse)) {
    $count=0;
    while(list($key,$val)=each($faceuse)) {
      $sp_name = $facename[$key];
      $sp_money = $facemoney[$key];
      $sp_description = $faceinfo[$key];
      $sp_wear = $facetime[$key];
      $sp_fitherd = $faceuser[$key];
      $sp_graphic = $facepic[$key];
      $sp_sxgraphic = $facepicx[$key];
      $sp_sort = $facesort[$key];
      $db->sql_query("INSERT INTO week9_data (sort,sp_name,sp_money,sp_description,sp_wear,sp_fitherd,sp_graphic,sp_sxgraphic) VALUES ('$sp_sort', '".addslashes($sp_name)."', '".trim($sp_money)."', '".addslashes($sp_description)."', '".trim($sp_wear)."', '$sp_fitherd','".addslashes($sp_graphic)."','".addslashes($sp_sxgraphic)."')");
      $count++;
    } //end while
    if ($count==$facecount) { // We inserted all of our available faces
      $action="modify";
    } else {
      echo "<script language=\"javascript\">window.location=\"admin_faceset.php?action=insertmultiple&facepath=$facepath&facesperpage=$facesperpage&facesort=$sp_sort\";</script>";
      echo "<p><a href=\"admin_faceset.php?action=insertmultiple&facepath=$facepath&facesperpage=$facesperpage&facesort=$sp_sort\">如果無法自動跳轉點擊這裏繼續</a></p>";
      exit;
    }
  } else {
    echo "<p>沒有選擇形象圖片<p><input type=button value=\"   返回   \" onclick=\"history.back(1)\">";
  }
}

// ###################### Start Edit #######################
if ($action=="edit") {
	$result = $db->sql_query("SELECT id,sort,sp_name,sp_description,sp_money,sp_wear,sp_graphic,sp_sxgraphic,sp_fitherd FROM week9_data WHERE id=$faceid");
	$face=$db->sql_fetchrow($result);
	$leibie1 = $db->sql_query("SELECT * FROM week9_class where have = '1'");
	while ($facesort = $db->sql_fetchrow($leibie1))
	{	
		if("$facesort[id]"=="$face[sort]") $faceleibie .= " <option value=\"$facesort[id]\" selected>$facesort[name1]</option>";
		else $faceleibie .= " <option value=\"$facesort[id]\">$facesort[name1]</option>";
	}
	doformheader("admin_faceset","doupdate");
	maketableheader("編輯形象圖片 $face[sp_name]");
	makehiddencode("faceid","$faceid");
	makehiddencode("sort","$sort");
	makehiddencode("startpage","$startpage");
	makehiddencode("perpage","$perpage");
	echo "</td></tr><tr class='firstalt' ><td ><img src=\"../images/face/$face[sort]/$face[sp_graphic]\" alt='大圖片'>";
	echo "<img src=\"../images/face/$face[sort]/$face[sp_sxgraphic]\" alt='小圖片'></td><td><table>";
	makeinputcode("名稱","sp_name",$face[sp_name]);
	makeinputcode("介紹","sp_description",$face[sp_description]);
	makeinputcode("價格","sp_money",$face[sp_money]);
	makeinputcode("耐用度","sp_wear",$face[sp_wear]);
	echo  "
  </tr>
	<tr class=secondalt valign=top>
	<td><p>所屬分類</p></td>
	<td><p>
	<select name=\"sp_sort\" disabled>
	$faceleibie
	</select>分類無法更改，請將圖片添加到另一個分類，再把此分類中的刪除。
	</p></td>
 </tr>
";
  $sex=($face[sp_fitherd]=="m"?"sexm":($face[sp_fitherd]=="f"?"sexf":"sext"));
  $$sex="selected";
  echo "
	<tr class=firstalt valign=top>
	<td><p>適合人群</p></td>
	<td><p>
	<select name=\"sp_fitherd\">
	<option value=\"t\" $sext>所有人</option>
	<option value=\"m\" $sexm>帥哥</option>
	<option value=\"f\" $sexf>美女</option>
	</select>
	</tr>
";
echo "</table></td></tr>";
  doformfooter("保存更改");
}

// ###################### Start Update #######################
if ($HTTP_POST_VARS['action']=="doupdate") {

  $db->sql_query("UPDATE week9_data SET sp_name='".addslashes($sp_name)."',sp_description='".addslashes($sp_description)."',sp_money='".addslashes($sp_money)."',sp_wear='".addslashes($sp_wear)."',sp_fitherd='".addslashes($sp_fitherd)."' WHERE id='$faceid'");

  echo "<p>形象圖片已更新！</p>";
  $action="modify";

}
// ###################### Start Remove #######################

if ($action=="remove") {

	doformheader("admin_faceset","kill");
	makehiddencode("faceid",$faceid);
	makehiddencode("sort",$sort);
	makehiddencode("sp_fitherd",$sp_fitherd);
	makehiddencode("perpage",$perpage);
	makehiddencode("startpage",$startpage);
	maketableheader("確認刪除");
	makedescription("你確實要刪除這個形象圖片嗎？");
	doformfooter("是","",2,"否");

}

// ###################### Start Kill #######################

if ($HTTP_POST_VARS['action']=="kill") {
	$db->sql_query("DELETE FROM week9_data WHERE id=$faceid");
	echo "<p>已刪除！</p>";
	$action="modify";
}

// ###################### Start Modify #######################
if ($action=="modify") {
$where=" where ";
if(!empty($sort)&&$sort<=25&&$sort>0) $where.="sort=$sort ";else $where.="1 ";
if(!empty($sp_fitherd)&&$sp_fitherd!="n") $where.="and sp_fitherd='$sp_fitherd' ";else $where.="and 1 ";

  if (intval($startpage)<1)
    $startpage = 1;
  if (intval($perpage)<1)
    $perpage = 15;
	$result = $db->sql_query("SELECT count(*) AS count FROM week9_data".$where);
	$facecount=$db->sql_fetchrow($result);
  $totalfaces = $facecount[count];
  if (($startpage-1)*$perpage > $totalfaces) {
     if ((($totalfaces / $perpage) - ((int) ($totalfaces / $perpage))) == 0)
       $startpage = $totalfaces / $perpage;
     else
       $startpage = (int) ($totalfaces / $perpage) + 1;
  }
  $limitlower=($startpage-1)*$perpage+1;
  $limitupper=($startpage)*$perpage;
    if ($limitupper>$totalfaces) {
      $limitupper=$totalfaces;
      if ($limitlower>$totalfaces) {
        $limitlower=$totalfaces-$perpage;
      }
    }
    if ($limitlower<=0) {
      $limitlower=1;
  }

  $faces=$db->sql_query("SELECT id,sort,sp_name,sp_description,sp_money,sp_wear,sp_sxgraphic,sp_fitherd FROM week9_data ".$where."ORDER BY id LIMIT ".($limitlower-1).",$perpage");
  	$leibie1 = $db->sql_query("SELECT * FROM week9_class where have = '1'");
	while ($facesort = $db->sql_fetchrow($leibie1))
	{	
		if($facesort[id]==$sort) $faceleibie .= " <option value=\"$facesort[id]\" selected>$facesort[name1]</option>";
		else $faceleibie .= " <option value=\"$facesort[id]\">$facesort[name1]</option>";
	}

$liebie="<select name=\"facesort\" onchange=\"location.href='".append_sid("admin_faceset.php?startpage=$prevstart&perpage=$perpage&action=modify&sp_fitherd=$sp_fitherd")."&sort='+document.mod.facesort.options[document.mod.facesort.selectedIndex].value\"> <option value=\"26\">所有分類</option> $faceleibie	</select>";

switch($sp_fitherd){
	case "m":
		$sex="sexm";break;
	case "f":
		$sex="sexf";break;
	case "t":
		$sex="sext";break;
	default:$sex="sexn";
}

$$sex="selected";
$sex="<select name=\"sp_fitherd\" onchange=\"location.href='".append_sid("admin_faceset.php?startpage=$prevstart&perpage=$perpage&action=modify&sort=$sort")."&sp_fitherd='+document.mod.sp_fitherd.options[document.mod.sp_fitherd.selectedIndex].value\"><option value=\"n\" $sexn>列出全部</option><option value=\"t\" $sext>通用</option><option value=\"m\" $sexm>男用</option><option value=\"f\" $sexf>女用</option>	</select>";

  doformheader("admin_faceset","modify",0,1,"mod");
  maketableheader("當前的形象圖片-------------形象類別：$liebie-------適用人群：$sex","",0,5);
  $count=0;
  while ($face=$db->sql_fetchrow($faces)) {
	  $sex=($face[sp_fitherd]=="m"?"男用":($face[sp_fitherd]=="f"?"女用":"通用"));
	  $facesort = $db->sql_query("SELECT name1 FROM week9_class where id = '$face[sort]'");
	  $facesort=$db->sql_fetchrow($facesort);
$facesort=$facesort[name1];
    if ($count==0)
      echo "<tr class='".getrowbg()."'>";
    echo "<td valign='bottom'><img src=\"../images/face/$face[sort]/$face[sp_sxgraphic]\"><br>名稱/介紹：$face[sp_name]/$face[sp_description]<br>價格/耐用度：$face[sp_money]/$face[sp_wear]<br>適用/類別：$sex/$facesort<br>";
 	echo makelinkcode("編輯","admin_faceset.php?s=$session[sessionhash]&action=edit&faceid=$face[id]&perpage=$perpage&startpage=$startpage&sp_fitherd=$sp_fitherd&sort=$sort");
	echo makelinkcode("刪除","admin_faceset.php?s=$session[sessionhash]&action=remove&faceid=$face[id]&perpage=$perpage&startpage=$startpage&sp_fitherd=$sp_fitherd&sort=$sort");
    echo '</td>';
    $count++;
    if ($count==5) {
      echo '</tr>';
      $count = 0;
    }
  }
  if ($count!=0) {
    while ($count !=5) {
      echo '<td>&nbsp;</td>';
      $count++;
    }
    echo '</tr>';
  }
  if ((($totalfaces / $perpage) - ((int) ($totalfaces / $perpage))) == 0)
    $numpages = $totalfaces / $perpage;
  else
    $numpages = (int) ($totalfaces / $perpage) + 1;
  if ($startpage == 1) {
    $startface = 0;
    $endface = $perpage - 1;
  } else {
    $startface = ($startpage - 1) * $perpage;
    $endface = ( $perpage * $startpage ) - 1 ;
  }
  if ($numpages > 1) {
    for ($x = 1; $x <= $numpages; $x++) {
      if ($x == $startpage)
        $pagelinks .= "<b> <font size=+1>$x</font> </b>";
      else
        $pagelinks .= " <a href=\"".append_sid("admin_faceset.php?startpage=$x&perpage=$perpage&action=modify&sp_fitherd=$sp_fitherd&sort=$sort")."\">$x</a> ";
    }
  }
  if ($startpage != $numpages) {
    $nextstart = $startpage + 1;
    $nextpage = " <a href='".append_sid("admin_faceset.php?startpage=$nextstart&perpage=$perpage&action=modify&sp_fitherd=$sp_fitherd&sort=$sort")."'>&gt;</a>";
    $eface = $endface + 1;
  } else
    $eface = $totalfaces;
  if ($startpage!=1) {
    $prevstart = $startpage - 1;
    $prevpage = "<a href='".append_sid("admin_faceset.php?startpage=$prevstart&perpage=$perpage&action=modify")."'>&lt;</a> ";
  }
  $sface = $startface +  1;
 echo "<tr class='".getrowbg()."'><td align=\"center\" colspan=5><table border=0><tr><td width=\"100%\" valign=\"top\" align=\"center\">";
  echo "<b>顯示共 $totalfaces 個形象圖片中的 $sface 到 $eface 個</b></td><td nowrap><form action=\"face.php\" method=\"post\">";
  echo "<b>每頁顯示:</b> <input type=\"text\" name=\"perpage\" value=\"$perpage\" size=5> <input type=\"submit\" value=\"確定\">";
  echo "</td></tr></table>$prevpage $pagelinks $nextpage</td></tr>";
  echo "</table></td></tr></table></form>";
}


//cpfooter();
?>
