<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * @package Scripts
 */

// define the path context
chdir('..');
define('EASY_GUI_APP_DIR', '../');


include_once('config.inc.php');
include_once('classes/core/Site.class.php');

include_once('classes/system/Dir.class.php');
include_once('classes/system/File.class.php');

include_once('interface/egScript.class.php');
include_once('interface/egManager.class.php');
include_once('interface/egContainer.class.php');
include_once('interface/egMenuItem.class.php');
include_once('interface/egFrame.class.php');
include_once('interface/egToolTip.class.php');
include_once('interface/egRichText.class.php');
include_once('interface/egLink.class.php');
include_once('interface/egStatic.class.php');
include_once('interface/egEdit.class.php');
include_once('interface/egCheckBox.class.php');
include_once('interface/egRadioButton.class.php');
include_once('interface/egPressButton.class.php');
include_once('interface/egDropDownListBox.class.php');
include_once('interface/egPropertyList.class.php');
include_once('interface/egPropertySheet.class.php');
include_once('interface/egPropertyPage.class.php');
include_once('interface/egTreeNode.class.php');
include_once('interface/egTreeList.class.php');
include_once('interface/egSubWindow.class.php');


$site = &Site::getInstance(); // constructor checks the $_GET, $_POST and $_SESSION arrays

//security check : user must be identified to upload some file
$user = $site->getCurrentUser();
if(!$user || $user->getUserId() == USER_ID_ANONYMOUS) {
	die("access denied");
	exit();
}



if(!isset($_GET['type'])) {
  $_GET['type'] = 'access';
}

switch($_GET['type']) {
  case 'access' :
    $default_log_file = 'access.log';
    break;
  case 'error' :
    $default_log_file = 'error.log';
    break;
}


$treeListWidth = 450;
$treeListTop = 10;

$manager = &egManager::getInstance();
$main = &new egContainer("main_ctn", 0, 0, 0, 0);

$usersManager = &$site->getUsersManager();

$listContainer = &new egContainer("list_ctn", 10, $treeListTop+2, $treeListWidth-1, 129, true);
$frame = &new egFrame("fra_1", "", 10, $treeListTop, $treeListWidth, 120);

$usersArray = $usersManager->getUsersArrayForDisplay();

$vPos = 2;

$dir = &new Dir(LOGS_FOLDER, REPOSITORY_FOLDER);

$listing = $dir->getListing();

for($i = 0; $i < count($listing); ++$i){
  $node = &$listing[$i];

  if(strpos($node->getName(), $_GET['type']) !== false) {
    $action = "setSelectedFile(\'".$node->getName()."\');";
    $link = &new egLink('log_file_'.$i, $node->getName(), 10, $vPos, 200, 18, $action, "interface/icons/icon_log_".$_GET['type'].".png");
    $listContainer->addObject($link);
  	$vPos += 20;
  }
}

$fileNameStatic = &new egStatic("file_name_static", LANG_FILE_MENU_SELECTED." : ", 10, 140, 120, 0, true);
$fileNameValue  = &new egStatic("file_name_value", "", 140, 140, 350, 0, false);

$deleteLink = &new egLink("lnk_delete", LANG_LOG_MENU_REMOVE, 470, 15, 200, 18, "launchFileDeletionWindow();", "interface/icons/remove.png");


$subFrame = &new egFrame("sbfr_1", "frame 2", 10, 160, 660, 375);
$subWindow = &new egSubWindow("sbw_1", "", 11, 161, 658, 373);



$main->addObject($fileNameStatic);
$main->addObject($fileNameValue);

$main->addObject($deleteLink);


$main->addObject($frame);
$main->addContainer($listContainer);

$main->addObject($subWindow);
$main->addObject($subFrame);


$scriptPath = $site->getScriptPath();
$url = 'http://'.$site->getHostName();
if(strlen($scriptPath)) {
  $url .= '/'.$scriptPath;
}

$scripts = new egScript();
$line = "
var selectedFile = '';
function launchFileDeletionWindow() {
  var vars = 'path='+ '".REPOSITORY_FOLDER."/' + '".LOGS_FOLDER."/' + selectedFile;
  openWindow('delete_node.php?'+'mode=ask_delete_file&'+vars,330, 150, 0, 0);
}
function setSelectedFile(file) {
  selectedFile = file;
  document.getElementById('file_name_value').innerHTML = '".REPOSITORY_FOLDER."/' + '".LOGS_FOLDER."/' + file;

  if(document.getElementById('sbw_1') != null) {
    document.getElementById('sbw_1').src = '".$url."' + '/../".REPOSITORY_FOLDER."/".LOGS_FOLDER."/' + file;
  }
}
setSelectedFile('$default_log_file');
";

$scripts->addLine($line);
$manager->addScripts($scripts);
$manager->addContainer($main);
print($manager->getHTML());

?>