Location: PHPKode > projects > WB News > admin/directory.php
<?php

/*========================================================*\
||########################################################||
||#                                                      #||
||#     WB News v1.0.0                                   #||
||# ---------------------------------------------------- #||
||#     Copyright (c) 2004-2005                          #||
||#     Created: 6th September 2005                      #||
||#     Filename: directory.php                          #||
||#                                                      #||
||########################################################||
/*========================================================*/

/**
 *  @author $Author: pmcilwaine $
 *	@version $Id: directory.php,v 1.2.2.1 2008/02/03 09:24:39 pmcilwaine Exp $
 */

require_once( "global.php" );
$auth->login_if( !$auth->is_logged() );

switch ( $_GET["mode"] )
{
case "theme":
	$tmpl->SetFilename( USE_AJAX ? BuildPath("admin/ajax/directory.ixml") : BuildPath("admin/theme-directory.ihtml") );
	if ( USE_AJAX )
	{
		$dirs =& $tmpl->AddParam( "dirs", array() );
		$path = "" != $_GET["curdir"] ? "/" . $_GET["curdir"] : NULL;

		if ( isset($_GET["curdir"]) )
		{
			chdir( $config["installdir"] . "/templates$path" );
			$tmpl->AddParam( "current", str_replace( $config["installdir"] . "/", "", getcwd() ) );
			
			$url = str_replace( $config["installdir"] . "/", "", dirname(getcwd()) );
			if ( $config["installdir"] == $url )
			{
				$url = FALSE;
			}
			
			$dirs[] = array(
				"enabled" => "false",
				"label" => "Parent [DIR]",
				"formval" => "",
				"url" => $url
				);
			foreach ( glob("*") as $file )
			{
				if ( is_dir( $file ) && $file != "CVS" )
				{
					$dirs[] = array(
						"enabled" => "true",
						"label" => htmlspecialchars($file),
						"formval" => htmlspecialchars($file),
						"url" => htmlspecialchars( str_replace( $config["installdir"] . "/", "", getcwd() ) . "/" . $file )
						);
				}
			}
		}
		else
		{
		}
	}
	break;
case "image":

	$myform = "image";

	if ( $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["form"]) && $_POST["form"] == $myform )
	{
		switch ( Submit() )
		{
		case "Upload":
			
			$err_msg = array();
			
			$curdir = sanitize_post( "curdir" );
			$directory = $_SERVER["DOCUMENT_ROOT"] . $curdir;
			if ( !is_dir( $directory ) )
			{
				$err_msg["dir"] = "Directory in use is not an actual directory";
			}

			if ( !is_writable($directory) )
			{
				$err_msg["dir"] = "Do not have write permissions on directory";
			}

			if ( !is_uploaded_file( $_FILES["image"]["tmp_name"] ) )
			{
				$err_msg["file"] = "Couldn't upload file";
			}
			
			if ( !in_array( pathinfo( $_FILES["image"]["name"], PATHINFO_EXTENSION ), array( "gif","jpg","jpeg","png") ) )
			{
				$err_msg["file"] = "Must be gif, jpg or png";
			}
			
			if ( count($err_msg) > 0 )
			{
				$_SESSION["formdata"] = $_POST;
				$_SESSION["err_msg"] = $err_msg;
				redirect( make_url(array("mode"=>$_GET["mode"],"curdir"=>$curdir)) );
				exit;
			}
			
			$image = $directory . "/" . $_FILES["image"]["name"];
			if ( !move_uploaded_file( $_FILES["image"]["tmp_name"], $image ) )
			{
				Message( "Couldnt upload file" );
			}
			else
			{
				Message( "Uploaded $image" );
			}

			
			redirect( make_url(array("mode"=>$_GET["mode"],"curdir"=>$curdir)) );
			exit;
		}
	}

	$tmpl->SetFilename( USE_AJAX ? BuildPath("admin/ajax/directory.ixml") : BuildPath("admin/image-browser.ihtml") );
	$tmpl->AddParam( "action", make_url_html() );
	
	if ( USE_AJAX && isset( $_GET["curdir"] ) )
	{
		$directory = $_SERVER["DOCUMENT_ROOT"] . $_GET["curdir"];
		if ( is_dir( $directory ) )
		{
			chdir( $directory );
			$dirs =& $tmpl->AddParam( "dirs", array() );
			$current = str_replace( $_SERVER["DOCUMENT_ROOT"], "", getcwd() );
			
			$tmpl->AddParam( "current", "" == $current ? "/" : $current );
			$dirs[] = array(
				"enabled" => "false",
				"label" => "Parent [DIR]",
				"formval" => NULL,
				"url" => dirname( str_replace( $_SERVER["DOCUMENT_ROOT"], "", $directory ) ),
				"file_type" => is_dir( $directory ) ? "dir" : "file"
				);
			
			foreach ( glob("*") as $file )
			{
				$extension = pathinfo( $file, PATHINFO_EXTENSION );
				if ( $file != "CVS" && (is_dir($file) || in_array( $extension, array("gif","png","jpg") )) )
				{
					$dirs[] = array(
						"enabled" => "true",
						"formval" => htmlspecialchars( $file ),
						"label" => htmlspecialchars( $file ),
						"url" => str_replace( "//", "/", str_replace( $_SERVER["DOCUMENT_ROOT"], "", $directory ) . "/" . $file ),
						"file_type" => is_file( $file ) ? "file" : "dir"
						);
				}
			}
		}
		else
		{
			return FALSE;
		}
	}
	else if ( USE_AJAX && !isset( $_GET["curdir"] ) )
	{
		return FALSE;
	}
	
	$formdata =& $tmpl->AddParam( "formdata", array() );
	$formdata["hidden"] = array(
		"form" => $myform,
		"curdir" => NULL
		);
	
	if ( isset($_SESSION["formdata"]) )
	{
		if ( isset($_SESSION["err_msg"]) )
		{
			$tmpl->AddParam( "msg", $_SESSION["err_msg"] );
		}

		unset( $_SESSION["formdata"], $_SESSION["err_msg"] );
	}

	$tmpl->AddParam( "buttons", "Upload" );
	
	break;
}

if ( USE_AJAX )
{
	header( "Content-Type: text/xml" );
}

echo $tmpl->GetHTML();
return;

?>
Return current item: WB News