Jump to content

Dropdown menu help please


semperfi

Recommended Posts

Hi i have a online/offline script that works great no issues at all. In order to change the html, php file to display when offline I need to point it to that file via input field. What i would like is to change the input filed and have a dropdown that list all the folders and subfolders within the the directory where all the templates are located. I have found a php snippet on the forum that does exactly what i want. The issue is now is to remove the input filed in the script and insert the snippet found here and thats where the issue comes. I'm hoping some guru here can easily help me out :D. please see below for the code

 

online/offline code ( input filed i would like to replace with dropdown selector <input type="text" name="txttemplate" value="%s" id="txttemplate" />)

<?php 
session_start();
$page_offline 			= TRUE;
$page_offline_title 	= " ";
$page_offline_message 	= " Offline";
$admin_ip				= "1.1.1.1";
$admin_password		= "*****************";
$page_offline_template 	= "template/path/index.php";
$page_offline_foradmin	= TRUE;
ini_set("display_errors", "0");
error_reporting(E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR);
$page = strstr($_SERVER['PHP_SELF'], "off.php") ? "admin" : "website";

// Try login
$login_error = FALSE;
if(isset($_POST['txtpassword'])){
if(md5($_POST['txtpassword']) == $admin_password){
	$_SESSION['offline_admin'] = TRUE;
}else{
	$login_error = TRUE;
}
}

// Save config settings
$offline 		= $page_offline ? 'checked="checked"' : "";
$title 			= $page_offline_title;
$message 		= $page_offline_message;
$saved 			= FALSE;
$template		= $page_offline_template;
$adminoffline 	= $page_offline_foradmin ? 'checked="checked"' : "";

if(isset($_POST['btnsave']) && $_SESSION['offline_admin']){
$val_offline 	= isset($_POST['chkoffline']) && ($_POST['chkoffline'] == "true") ? TRUE : FALSE;
$val_title 		= htmlspecialchars($_POST['txttitle']);
$val_message 	= htmlspecialchars($_POST['txtmessage']);
$val_template 	= htmlspecialchars($_POST['txttemplate']);
$val_password 	= htmlspecialchars($_POST['txtpassword']);
$val_adminoffline 	= isset($_POST['chkconstruct']) && ($_POST['chkconstruct'] == "true") ? TRUE : FALSE;

$offline 		= $val_offline ? 'checked="checked"' : "";
$title 			= $val_title;
$message 		= $val_message;
$template		= $val_template;
$adminoffline 	= $val_adminoffline ? 'checked="checked"' : "";

if(empty($val_password)){
	page_offline_save($val_offline, $val_title, $val_message, $val_template, $adminoffline);
}else{
	page_offline_save($val_offline, $val_title, $val_message, $val_template, $adminoffline, $val_password);
}

$saved = TRUE;
}

// Templates
$admin_style_tpl = <<<ADMINSTYLETPL
body{
font-family:Arial, Helvetica, sans-serif;
font-size:11px; 
text-align: center;
background-color:#F9F9F9;
color:#555555;
}
#wrapper{
width:400px;
padding:20px;
margin: 0 auto;
border:1px solid #DADADA;
background-color: #FFF;
text-align: left;
}
h2{
margin: 0px 0px 10px 0px;
padding: 0px;
font-size: 24px;
color:#464646;
font-family:Georgia,"Times New Roman",Times,serif;
font-style: italic;
font-weight: normal;
}
label{
display: block;
width: 150px;
font-weight: bold;
padding: 14px 0px 3px 0px;
}
input{
-moz-border-radius-bottomleft:3px;
-moz-border-radius-bottomright:3px;
-moz-border-radius-topleft:3px;
-moz-border-radius-topright:3px;
width: 300px;
background:#F5F5F5 none repeat scroll 0 0;
border:1px solid #CCCCCC;
color:#666666;
padding: 4px 8px;
}	
#chkoffline, #chkconstruct{
width: 20px;
}
.error{
border: solid 1px #CC0000;
}

ADMINSTYLETPL;

$admin_login_tpl = <<<ADMINLOGINTEMPLATE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<style>
%s
</style>
</head>

<body>
<div id="wrapper">
<h2>Login</h2>
<form id="form1" name="form1" method="post" action="">
	<label for="txtpassword">Password</label>
  		<input type="password" name="txtpassword" %s id="txtpassword" /><br /><br />
  		<input name="btnlogin" type="submit" value="Login" />
   </form>
</div>
</body>
</html>

ADMINLOGINTEMPLATE;

$admin_config_tpl = <<<ADMINCONFIGTEMPLATE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
%s
</style>
</head>

<body>
<div id="wrapper">
<h2></h2>
<form id="frmadmin" name="frmadmin" method="post" action="">
   		<label for="chkoffline">Offline</label>
   		<input name="chkoffline" id="chkoffline" %s type="checkbox" value="true" />
	<label for="chkconstruct">Show offline to admin</label>
   		<input name="chkconstruct" id="chkconstruct" %s type="checkbox" value="true" />
	<label for="txttitle">Title</label>
  		<input type="text" name="txttitle" value="%s" id="txttitle" />
        <label for="txtmessage">Message</label>
  		<input type="text" name="txtmessage" value="%s" id="txtmessage" />
	<label for="txttemplate">Template</label>
  		<input type="text" name="txttemplate" value="%s" id="txttemplate" />
        <label for="txtpassword">Admin password</label>
  		<input type="password" name="txtpassword" id="txtpassword" />
  		<br /> Leave this field empty if you don't want to change the admin password.
	<br /><br />
  		<input name="btnsave" type="submit" value="Save" />
   </form>
</div>
</body>
</html>

ADMINCONFIGTEMPLATE;

$admin_saved_tpl = <<<ADMINSAVEDTEMPLATE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Offline Admin</title>
<style>
%s
</style>
</head>

<body>
<div id="wrapper">
<h2>Configuration saved</h2>
The configuration is saved succesfully. You can continue to <a href="off.php">config</a> or go to your <a href="../">Homepage</a> or back to <a href="index.php">admin panel</a>.
</div>
</body>
</html>

ADMINSAVEDTEMPLATE;

if($page == "admin"){

// If user not is loggedin then show login screen
if(!isset($_SESSION['offline_admin']) || !$_SESSION['offline_admin']){
	echo sprintf($admin_login_tpl, $admin_style_tpl, ($login_error) ? 'class="error"' : '');
}else{
	if(!$saved){
		echo sprintf($admin_config_tpl, $admin_style_tpl, $offline, $adminoffline, $title, $message, $template);
	}else{
		echo sprintf($admin_saved_tpl, $admin_style_tpl);
	}
}
exit();

}else{

if($page_offline && (($_SERVER['REMOTE_ADDR'] != $admin_ip) || $page_offline_foradmin)){

	ob_start();
	include($page_offline_template);
	$template = ob_get_contents();
	ob_end_clean();

	// Replace variables
	$template = str_replace(array("{TITLE}", "{MESSAGE}"), array($page_offline_title, $page_offline_message), $template);

	// Show template
	echo $template;
	exit();
}

}

// Write settings to file
function page_offline_save($offline, $title, $message, $template, $adminoffline, $password=""){
$lines = explode("\n", file_get_contents("offline.php"));

// Set configuration
$offline_state = ($offline) ? "TRUE" : "FALSE";
$lines[2] = '$page_offline 			= ' . $offline_state . ';';
$lines[3] = '$page_offline_title 	= "' . $title . '";';
$lines[4] = '$page_offline_message 	= "' . $message . '";';
$lines[5] = '$admin_ip				= "' . $_SERVER['REMOTE_ADDR'] . '";';
$lines[7] = '$page_offline_template 	= "' . $template . '";';
$adminoffline_state = ($adminoffline) ? "TRUE" : "FALSE";
$lines[8] = '$page_offline_foradmin	= ' . $adminoffline_state . ';';
if(!empty($password)){
	$lines[6] = '$admin_password		= "' . md5($password) . '";';
}

// Save to file
file_put_contents("off.php", implode("\n", $lines));
}
?>

 

snippet found here on the forum

<?php 

$parent_directory = 'path/to/directory';
$file_types = 'html,htm,php,etc';

//===================================================//
// FUNCTION: directoryToArray                        //
//                                                   //
// Parameters:                                       //
//  - $root: The directory to process                //
//  - $to_return: f=files, d=directories, b=both     //
//  - $file_types: the extensions of file types to   //
//                 to return if files selected       //
//===================================================//
function directoryToArray($root, $to_return='b', $file_types=false) {
  $array_items = array();
  if ($file_types) { $file_types=explode(',',$file_types); }
  if ($handle = opendir($root)) {
    while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {

        $add_item = false;
        $type = (is_dir($root. "/" . $file))?'d':'f';
        $name = preg_replace("/\/\//si", "/", $file);

        if ($type=='d' && ($to_return=='b' || $to_return=='d') ) {
          $add_item = true;
        }

        if ($type=='f' && ($to_return=='b' || $to_return=='f') ) {
          $ext = end(explode('.',$name));
          if ( !$file_types || in_array($ext, $file_types) ) {
            $add_item = true;
          }
        }

        if ($add_item) {
          $array_items[] = array ( 'name'=>$name, 'type'=>$type, 'root'=>$root);
        }
      }
    } // End While
    closedir($handle);
  } // End If
  return $array_items;
}



if (isset($_POST[pickfile])) {

  // User has selected a file take whatever action you want based
  // upon the values for folder and file

} else {

    echo '
<html>
<head>
  <script type="text/javascript">
    function changeFolder(folder) {
      document.pickFile.submit();
    }
  </script>
</head>

<body>';


echo "<form name=\"pickFile\" method=\"POST\">\n";

$directoryList = directoryToArray($parent_directory,'d');

echo "<select name=\"folder\" onchange=\"changeFolder(this.value);\">\n";
foreach ($directoryList as $folder) {
  $selected = ($_POST[folder]==$folder[name])? 'selected' : '';
  echo "<option value=\"$folder[name]\" $selected>$folder[name]</option>\n"; 
}
echo '</select><br><br>';

$working_folder = ($_POST[folder]) ? $_POST[folder] : $directoryList[0][name];

$fileList = directoryToArray($parent_directory.'/'.$working_folder,'f',$file_types);

echo "<select name=\"file\">\n";
foreach ($fileList as $file) {
  echo "<option value=\"$file[name]\">$file[name]</option>\n"; 
}
echo '</select><br><br>';

echo "<button type=\"submit\" name=\"pickfile\">Submit</button>\n";

echo "</form>\n";
echo "</body>\n";
echo "</html>\n";

}
?>

 

Thank you in advance for any help...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.