<?php
session_start();
if (empty($_SESSION['namauser']) AND empty($_SESSION['passuser'])){
	header('location:../../404.php');
}else{
include_once '../../../po-library/po-database.php';
include_once '../../../po-library/po-function.php';
include_once '../../../po-library/po-pclzip.lib.php';

$val = new Povalidasi;
$mod=$_POST[mod];
$act=$_POST[act];

// Hapus Theme
if ($mod=='theme' AND $act=='delete'){
	$id = $val->validasi($_POST['id'],'sql');
	$tabledel = new PoTable('theme');
	$currentSearch = $tabledel->findBy(id_theme, $id);
	$currentSearch = $currentSearch->current();
	$folder = $currentSearch->folder;
		$dirPath = "../../../po-content/$folder";
		$deletef = deleteDir($dirPath);
		$tabledel->deleteBy('id_theme', $id);
}

// Input Theme
elseif ($mod=='theme' AND $act=='input'){
$extensionList = array("zip");
$fileName = $_FILES['fupload']['name'];
$tmpName  = $_FILES['fupload']['tmp_name'];
$fileType = $_FILES['fupload']['type'];
$fileSize = $_FILES['fupload']['size'];
$pecah = explode(".", $fileName);
$ekstensi = $pecah[1];
$title = $pecah[0];
$seotitle = seo_title($title);
$acak = rand(000000,999999);
$nama_file = "-popoji.";
$nama_file_unik = $seotitle.'-'.$acak.$nama_file.$ekstensi;
$namaDir = '../../../po-content/po-upload/';
$pathFile = $namaDir . $nama_file_unik;
$title = $val->validasi($_POST['title'],'xss');
$author = $val->validasi($_POST['author'],'xss');
$folder = $val->validasi($_POST['folder'],'xss');

if (!empty($tmpName)){
	if (in_array($ekstensi, $extensionList)){
		move_uploaded_file($tmpName, $pathFile);
		$destination_dir = "../../../po-content/$folder";
		if (file_exists($destination_dir)){ 
			unlink("../../../po-content/po-upload/$nama_file_unik");
			header('location:../../404.php');
		}else{
			$file = "../../../po-content/po-upload/$nama_file_unik";
			$archive = new PclZip($file);
			if ($archive->extract(PCLZIP_OPT_PATH, $destination_dir) == 0){
				unlink("../../../po-content/po-upload/$nama_file_unik");
				header('location:../../404.php');
			}
			$table = new PoTable('theme');
			$table->save(array(
				'title' => $title,
				'author' => $author,
				'folder' => $folder
			));
			unlink("../../../po-content/po-upload/$nama_file_unik");
			header('location:../../admin.php?mod='.$mod);
		}
	}else{
		header('location:../../404.php');
	}
}else{
	header('location:../../404.php');
}
}

// Active Theme
elseif ($mod=='theme' AND $act=='active'){
$id = $val->validasi($_POST['id'],'sql');
$active =  $val->validasi($_POST['active'],'xss');

	$tableS = new PoTable('theme');
	$currentSearch = $tableS->findBy(active, 'Y');
	$currentSearch = $currentSearch->current();
	$id_theme = $currentSearch->id_theme;
	$actives = 'N';
	
	$datas = array(
		'active' => $actives
	);
	$table = new PoTable('theme');
	$table->updateBy('id_theme', $id_theme, $datas);

	$data = array(
		'active' => $active
	);
	$table = new PoTable('theme');
	$table->updateBy('id_theme', $id, $data);
	header('location:../../admin.php?mod='.$mod);
}

// Edit Theme
elseif ($mod=='theme' AND $act=='edit'){
	$folder = $val->validasi($_POST['folder'],'xss');
	$valid = $val->validasi($_POST['file'],'xss');
	$filename = "../../../po-content/$folder/$valid";
	if (file_exists("$filename")){
		$data = $_POST['code_content'];
		$data = str_replace("textareapopojicms", "textarea", $data);
		$newdata = stripslashes($data);
		if ($newdata != ''){
			$fw = fopen($filename, 'w') or die('Could not open file!');
			$fb = fwrite($fw,$newdata) or die('Could not write to file');
			fclose($fw);
		}
	}
	header('location:../../admin.php?mod='.$mod.'&act='.$act.'&id='.$valid);
}
}
?>