<?php
include("./functions.php");
echo("<script src=\"./pop.js\" type=\"text/javascript\" language=\"javascript\"></script>");

echo "<script type=\"text/javascript\">
function CSB(){
if (document.mform.all.checked){
xxx = true;
} else {
xxx = false;
}
for (i = 0; i < document.forms['mform'].length; i++) {
document.forms['mform'].elements[i].checked = xxx
}
}
</script>\n";

if($_REQUEST['submit'] == "Delete All Selected") {
	foreach($_REQUEST['selchk'] AS $checked) {
		if(@unlink($checked)){
			echo "<div align=\"center\"><b>Removed:</b> ".$checked."</div><br />";
		} else {
			echo "<div align=\"center\"><b>Error while removeing:</b> ".$checked."</div><br />";
		}
	}
}

if(isset($_REQUEST['remove']) == '1') {
    //remove dir
    $exceptions = array(".", "..");
	if(delete_files($_REQUEST['file'], $exceptions, true)) {
		echo "deletion successed";
	} else {
		echo "deletion failed";
	}
}

function mk_txt_line_array($buf) {
    $lnarray = split("\n", $buf);

  return $lnarray;
}

$search = array ("'<script[^>]*?>.*?</script>'si",  // Strip out javascript
                 "'<[/!]*?[^<>]*?>'si",          // Strip out HTML tags
                 //"'([rn])[s]+'",                // Strip out white space
                 "'&(quot|#34);'i",                // Replace HTML entities
                 "'&(amp|#38);'i",
                 "'&(lt|#60);'i",
                 "'&(gt|#62);'i",
                 "'&(nbsp|#160);'i",
                 "'&(iexcl|#161);'i",
                 "'&(cent|#162);'i",
                 "'&(pound|#163);'i",
                 "'&(copy|#169);'i",
                 "'&#(d+);'e");                    // evaluate as php

$replace = array ("",
                 "",
                 //"\1",
                 "\"",
                 "&",
                 "<",
                 ">",
                 " ",
                 chr(161),
                 chr(162),
                 chr(163),
                 chr(169),
                 "chr(\1)");

$dir = dirname(__FILE__)."/tmp/";
if (is_dir($dir)){
   if ($dh = opendir($dir)){
       while (($file = readdir($dh)) !== false){
       $file_ext = substr(strrchr($file, '.'), 1);
         if($file_ext == "zip"){
           @unlink($dir . $file);
           //echo "$file : $file_ext <br>";
         }
       }
       closedir($dh);
   }
}

$tmpdir = dirname(__FILE__)."/tmp/";
$tmpdir = str_replace("\\", "/", $tmpdir);
$tmpdir = str_replace("//", "/", $tmpdir);

$files = filelist($tmpdir, 1, 0, "all", 1);

echo "<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"5\" bordercolor=\"#000000\" align=\"center\">
<form name=\"mform\" id=\"FormName\" action=\"\" method=\"post\">";
echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td><input type=checkbox onClick=\"return CSB();\" name=all></td></tr>";

$z=0;
if(count($files) > 0){
foreach ($files as $list){//print array
	//echo "Path: " . $list['path'] . " ==> Name: " . $list['name'] ."<br><br>";

   $path = str_replace($tmpdir, "", $list['path']);
   $path = str_replace("\\", "/", $path);
   $path = str_replace("//", "/", $path);
   //echo $path."<br>";
   @chdir($tmpdir.$path);

   $filepth = $tmpdir . $path . $list['name'];

   if(is_dir($list['name'])){
   	$z = $z+1;
   	//echo "<tr><td><input type=\"checkbox\" onclick=\"checkAll(document.getElementById('FormName'), 'results".$z."',this);\"> <b>" . $path . $list['name'] ."</b> <a href=\"./uploadarticles_frame.php?file=$filepth&remove=1\" onclick=\"return confirm('Are you sure?')\"><small>Remove all files and sub-folder(s)</small></a></td></tr>";
   	echo "<tr><td><font face=verdana, arial><b>" . $path . $list['name'] ."</b> - <a href=\"./uploadarticles_frame.php?file=$filepth&remove=1\" onclick=\"return confirm('Are you sure?')\"><small>Remove all files and sub-folder(s)</small></a></td></tr>";
   } else {
    $fp = fopen($filepth, "r");
    $buffer = fread($fp, filesize($filepth));
    fclose($fp);

	$fntext = preg_replace($search, $replace, $buffer);
	$resultln = mk_txt_line_array(trim($fntext));
	$txttitle = trim($resultln[0]);

	if($txttitle == "\r"){
		$txttitle = trim($resultln[1]);
	}

	$txttitle = preg_replace($search, $replace, $txttitle); //must be after $txtdescription

    //echo "<tr><td><font face=verdana, arial>&nbsp;&nbsp;&nbsp;<input name=\"sltart[]\" type=\"checkbox\" value=\"$filepth\" class=\"results$z\"> [" . $list['name'] . "] - " . trim($txttitle). " <small>(<a href=\"javascript:OpenWin('./previewarticles.php?show=1&file=$filepth', 'help', 600, 400);\">Preview</a> | <a href=\"javascript:OpenWin('./editarticles.php?edit=1&file=$filepth', 'help', 600, 400);\">Edit</a> | <a href=\"./uploadarticles_frame.php?file=$filepth&remove=1\" onclick=\"return confirm('Are you sure?')\">Remove</a>)</small></td></tr>";
    echo "<tr><td><font face=verdana, arial>&nbsp;&nbsp;&nbsp;<small>[" . $list['name'] . "] - " . trim($txttitle). "</small></td><td><a href=\"javascript:OpenWin('./previewarticles.php?show=1&file=$filepth', 'help', 600, 400);\"><small>Preview</small></a></td><td><a href=\"javascript:OpenWin('./editarticles.php?edit=1&file=$filepth', 'help', 600, 400);\"><small>Edit</small></a></td><td><input type=checkbox name=selchk[] value=\"{$filepth}\"></a></td></tr>";
   }
}
} else {
  echo "<div align=\"center\"><font face=verdana, arial><b>Please upload at least one article...</b></div>";
}

echo "<tr><td colspan=\"4\" align=\"right\"><input type=submit name=submit value=\"Delete All Selected\" onclick=\"return confirm('Are you sure?');\" style=\"color: #000000; font-size: 8pt; font-weight: bold; border: 1px ridge #000000; background-color: #E1EBFB\"></td></tr>";
echo "</form></table>";
?>