PHP Classes

File: navtree001_sample3.php

Recommend this page to a friend!
  Classes of Mark Quah   NavTree   navtree001_sample3.php   Download  
File: navtree001_sample3.php
Role: Example script
Content type: text/plain
Description: Web-based File Management
Class: NavTree
Explorer-like tree
Author: By
Last change: Bug fix
Date: 21 years ago
Size: 25,806 bytes
 

Contents

Class file image Download
<?php // FILE: navtree001_sample3.php // To do simple web-based directory management. // To make this work, it requires: // 1. this file // 2. class_navtree.php // 3. class_navtree_inc.php // 4. image stored in ./IMAGE/NAVTREE, refer to SCREENSHOT.txt // Example script is appended at the end of the class. You can rip out // the example and make this file a class say "class_webdir.php" // // You proceed to specify the base_dir, and correspond base_url which // is the highest level of the directory the class will display. // Use your own logic to determine if you allow admin mode by settin // $admin=FALSE. Pull out the admin button will also work. // // You cannot instantiate multiple copy of the class as the core is // still the javascript which does not support class. Class is used for // lumping all the functions and variables together than for multiple // instances. // // Note the programming logic is very similar to the PageLogic class that // I used. This is my style of programming. You can use PacgeLoic class to // simplify this script. include_once "class_navtree.php"; CLASS WEBDIR extends NAVTREE { var $base_dir; // physical location var $base_url; // URL location var $navdir_curdir=""; // current display page var $recurse_level=999; // set directory recurse depth, 1, top directory and child only // 999 should be a reasonable limit var $current_depth=0; var $admin; // admin privilege TRUE/FALSE var $navdir_admin_mode=FALSE; // navdir_admin_mode "TRUE"/"FALSE" var $navdir_action; // variable from hidden form NAVDIR to determine action var $navtree; function WEBDIR($base_dir, $base_url, $admin, $nav_id, $nav_image_dir) { global $HTTP_POST_VARS; $this->base_dir = $base_dir; $this->base_url = $base_url; $this->admin = $admin; $this->recurse_level=0; $this->navdir_curdir = $base_dir; $this->url_offset = strlen($this->base_dir)+1; //----- define the node type and the image to use $this->NAVTREE($nav_image_dir); $this->nav_id= $nav_id; $this->t_folder = $this->CreateType("folder"); $this->t_document = $this->CreateType("document"); $this->t_action = $this->CreateType("action"); //----- DEBUG: CHECK WHAT DATA HAS BEEN POSTED IN // foreach ($HTTP_POST_VARS as $name => $value) echo "<H6> $name : [$value]<BR></H6>"; //----- Extract FORM data $this->navdir_action = ""; if ( isset($HTTP_POST_VARS['navdir_action']) ) $this->navdir_action = strtoupper($HTTP_POST_VARS['navdir_action']); $this->navdir_item = ""; if ( isset($HTTP_POST_VARS['navdir_item']) ) $this->navdir_item = $HTTP_POST_VARS['navdir_item']; $this->navdir_extension = ""; if ( isset($HTTP_POST_VARS['navdir_extension']) ) $this->navdir_extension = $HTTP_POST_VARS['navdir_extension']; $this->navdir_admin_mode = "FALSE"; if ( isset($HTTP_POST_VARS['navdir_admin_mode']) ) $this->navdir_admin_mode = $HTTP_POST_VARS['navdir_admin_mode']; $this->navdir_curdir = ""; if ( isset($HTTP_POST_VARS['navdir_curdir']) ) $this->navdir_curdir = $HTTP_POST_VARS['navdir_curdir']; //--- STYLE SHEET echo "<style>"; echo ".small, #small { font-size: 8pt; font-weight: bold; padding:0; margin:0; text-align: center}"; echo "</style>"; } function SetDepth($depth) { $this->recurse_level=$depth; } function GenFormNAVDIR() { //----- Dislay Hidden Form $this->NAVDIRForm(); //----- FORM LOGIC switch ($this->navdir_action) { case "ADDDIR" : //$navdir_item: parent directoy name //$navdir_extension: new child directory name $this->AddDir(); break; case "DELDIR" : //$navdir_item: directory to be deleted $this->DelDir(); break; case "RENDIR": // navdir_item: directory name // navdir_extension: new directory name $this->RenDir(); break; case "ADDFILE": // navdir_item: directory name $this->AddFile(); break; case "PROCFILE": // file upload // navdir_item directory name // base_dir: base directory name $this->ProcFile(); break; case "DELFILE": // navdir_item: file to be deleted $this->DelFile(); break; case "RENFILE": // navdir_item: directory name // navdir_extension: new directory name $this->RenFile(); break; case "ADDINFO": // navdir_item: file name // navdir_extension: description $this->AddInfo(); break; default: DisplayMainHTML(); break; } // end switch } // FORMLOGIC //===== END OF FORM LOGIC =============================================== //===== PAGE MAIN ROUTINE =============================================== // Display Admin mode button function DisplayAdminButton($txt="Administration mode") { // Admin mode button $status = ( $this->navdir_admin_mode == "TRUE" ? "CHECKED":""); echo "<INPUT TYPE=CHECKBOX NAME='navdir_admin_input' VALUE='TRUE' $status". " ONCLICK=\"NAVDIR_ADMIN_MODE();\" >$txt"; } function DisplayMain() { global $HTTP_POST_VARS; //----- define root nodes and prepare a stack $this->main_node = $this->CreateNode(-1, $this->t_folder, "close", "ROOT", "NOT DISPLAYED"); $this->f_count = 0; $admin_display = $this->admin && ($this->navdir_admin_mode == "TRUE"); //----- make sure directory follow the path $this->navdir_curdir = dirname($this->navdir_curdir); if ( substr($this->navdir_curdir,0, strlen($this->base_dir)) != $this->base_dir ) $this->navdir_curdir=$this->base_dir; //----- read in tree data $this->TreeReadDir($this->navdir_curdir, $this->main_node, $admin_display, 0, $this->navdir_curdir); //----- define the tree $dir_tree = $this->CreateTree($this->main_node, $this->nav_id, NAV_SETALL|NAV_HIDEROOT); $this->SetTreeStyle($dir_tree, 'navs_content', 'border: groove 2; padding: 5; font-size: 8pt; '. 'font-family: Arial, Helvetica; background: transparent'); //----- Now Display The tree $this->LoadData(); return $dir_tree; } // DisplayMain function TreeReadDir( $dir_name, // directory to be parsed $parent_node, // parent node for this directory to attach to $admin, // display admin action? $depth, // depth (level) of this directory $parent_dir // parent directory name ) { //##### DO SOME CHECKING // check depth $depth = $depth + 1; if ($depth > $this->recurse_level) return; // Check directory if ( !($dir = @opendir($dir_name)) ) return "Can't open directory: $dir_name<BR>"; //##### ADD THE DIRECTORY AS NODE TO PARENT NODE //----- GET DIRECTORY DESCRIPTION $dir_fname = basename($dir_name); $dir_navinfo="$dir_name/$dir_fname.navinfo"; $dir_txt = ""; if ( file_exists("$dir_navinfo") ) { $fp=fopen("$dir_navinfo", "rt"); if ($fp) { rewind($fp); while (! feof($fp)) $dir_txt .= fgets($fp, 512); fclose($fp); } //$dir_txt = addcslashes($dir_txt, "\"/'"); $dir_txt = htmlentities($dir_txt); $dir_txt = str_replace("\n", "<br>", $dir_txt); } //----- SET ACTION: jump to this directory $action = "<A HREF=\"javascript:NAVDIR_CURDIR('$parent_dir');\">$dir_fname</A>"; //----- SET STATE: first level or the parent == base dir then open $state = ( ($depth == 1 || $parent_dir == $this->navdir_curdir) ? "open":"close"); //----- CREATE THE NODE $dir_node = $this->CreateNode($parent_node, $this->t_folder, $state, $action, $dir_txt); //##### ADD ADMINISTRATION ACTIONS if ($admin) $this->AddDirAdmin($dir_node, $dir_name); //##### PROCESS FILE UNDER THIS DIRECTORY //----- read in file list into $file_array $file_array['dir'] = array(); $file_array['file'] = array(); while (($file = readdir($dir)) !== false) { // ignore file begins with ".", include ".", ".." which is directory if ( substr($file, 0, 1) == ".") continue; $file_name = "$dir_name/$file"; $file_type = filetype($file_name); $file_array[$file_type][] = $file_name; } //----- Display directory first, use recursive function to display the node if ( count($file_array['dir']) > 0) { asort($file_array['dir']); foreach ($file_array['dir'] as $file_type => $file_name) $this->TreeReadDir($file_name, $dir_node, $admin, $depth, $dir_name); }// count file_array['file'] //----- Display file list if ( count($file_array['file']) ) { asort($file_array['file']); foreach ($file_array['file'] as $file_type => $file_name) { // ignore description file with .navinfo extension if (strstr($file_name, ".navinfo")) continue; // get description of the file from .navinfo if exists $file_txt = ""; if ( file_exists("$file_name.navinfo") ) { $fp=fopen("$file_name.navinfo", "rt"); if ($fp) { rewind($fp); while (! feof($fp)) $file_txt .= fgets($fp, 512); fclose($fp); } $file_txt = htmlentities($file_txt); $file_txt = str_replace("\n", "<br>", $file_txt); } // now create the node $file = basename($file_name); $file_url = $this->base_url . substr($file_name, $this->url_offset); $file_url = "<A HREF='$file_url'>$file</A>"; $file_node = $this->CreateNode($dir_node, $this->t_document, "close", $file_url, $file_txt); // if admin then create a few option for user if ($admin) $this->AddNodeAdmin($file_node, $file_name); } // foreach } // count file_array['file'] closedir($dir); }// Read Dir function NewFieldName() { $this->f_count ++; return "F_".$this->f_count; } function AddDirAdmin($dir_node, $dir_name) { // Put all the action button into content of a new admin node $action = "<TABLE ID=small BORDER=1 BGCOLOR=#CCCCCC>"; // Add new file $f_name=$this->NewFieldName(); $js_call= "NAVDIR_SUBMIT('ADDFILE','$dir_name');"; $action .= "<TR><TD>"; $action .= "<INPUT ID=small TYPE=SUBMIT VALUE='UPLOAD' ONCLICK=\"$js_call\"><BR>"; // Change/Add Info $f_name=$this->NewFieldName(); $navfile_base=$dir_name."/".basename($dir_name); $js_call= "NAVDIR_SUBMIT_EXT('ADDINFO','$navfile_base','$f_name');"; $action .= "<TR><TD>"; $action .= "<TABLE BORDER=0><TR><TD>"; $action .= "<INPUT ID=small TYPE=SUBMIT VALUE='Change directory desciption' ONCLICK=\"$js_call\">"; $action .= "<TR><TD ALIGN=CENTER>"; $action .= "<TEXTAREA ROWS=3 NAME=$f_name></TEXTAREA><BR>"; $action .= "</TABLE>"; // rename directory if ($dir_name != $this->base_dir) { $f_name=$this->NewFieldName(); $js_call= "NAVDIR_SUBMIT_EXT('RENDIR','$dir_name','$f_name');"; $action .= "<TR><TD>"; $action .= "<TABLE CLASS=small BORDER=0><TR><TD>"; $action .= "<INPUT ID=SMALL TYPE=SUBMIT VALUE='Rename' ONCLICK=\"$js_call\">"; $action .= "<TR><TD>"; $action .="<INPUT ID=small NAME=$f_name>"; $action .="</TABLE>"; } // add directory $f_name=$this->NewFieldName(); $js_call= "NAVDIR_SUBMIT_EXT('ADDDIR','$dir_name','$f_name');"; $action .= "<TR><TD>"; $action .= "<TABLE CLASS=small BORDER=0><TR><TD>"; $action .= "<INPUT ID=small TYPE=SUBMIT VALUE='Add Directory' ONCLICK=\"$js_call\">"; $action .= "<TR><TD>"; $action .="<INPUT ID=small NAME=$f_name><BR>"; $action .= "</TABLE>"; // del directory $f_name=$this->NewFieldName(); $js_call= "NAVDIR_SUBMIT('DELDIR','$dir_name');"; $action .= "<TR><TD>"; $action .= "<INPUT ID=SMALL TYPE=SUBMIT VALUE='Delete this directory' ONCLICK=\"$js_call\">"; // end big table $action .="</TABLE>"; //Create Directory Admin Node $dir_admin=$this->CreateNode($dir_node, $this->t_action, "close", "Directory admin", $action); } function AddNodeAdmin($file_node, $file_name) { global $t_action; global $f_count; global $navtree; // Put all the action button into content of a new admin node $action = "<TABLE ID=small BORDER=1 BGCOLOR=#CCCCCC>"; // Change/Add Info $f_name=$this->NewFieldName(); $js_call= "NAVDIR_SUBMIT_EXT('ADDINFO','$file_name','$f_name');"; $action .= "<TR><TD>"; $action .= "<TABLE CLASS=small BORDER=0 ><TR><TD>"; $action .= "<TR><TD>"; $action .= "<INPUT CLASS=small TYPE=SUBMIT VALUE='Change file info' ONCLICK=\"$js_call\">"; $action .= "<TR><TD>"; $action .="<TEXTAREA CLASS=small ROWS=3 NAME=$f_name></TEXTAREA>"; $action .= "</TABLE>"; // rename file $f_name=$this->NewFieldName(); $js_call= "NAVDIR_SUBMIT_EXT('RENFILE','$file_name','$f_name');"; $action .= "<TR><TD>"; $action .= "<INPUT CLASS=small TYPE=SUBMIT VALUE='Rename' ONCLICK=\"$js_call\"><BR>"; $action .= "<INPUT CLASS=small NAME=$f_name>"; // del file $f_name=$this->NewFieldName(); $js_call= "NAVDIR_SUBMIT('DELFILE','$file_name');"; $action .= "<TR><TD>"; $action .= "<INPUT CLASS=small TYPE=SUBMIT VALUE='Delete this file' ONCLICK=\"$js_call\">"; // Okay, add node admin $action .= "</TABLE>"; $file_admin=$this->CreateNode($file_node, $this->t_action, "close", "File admin", "$action"); } function AddDir() { $navdir_item = $this->navdir_item; $navdir_extension = $this->navdir_extension; $des_name = "$navdir_item/$navdir_extension"; if (! mkdir($des_name, 0700) ) $this->JSAlert("FAILED: create directory $navdir_extension failed!!!"); else $this->JSAlert("SUCCESS: directory $navdir_extension created!!!"); $this->JSScript("GOMAIN();"); } function DelDir() { $navdir_item = $this->navdir_item; if ( ! rmdir($navdir_item) ) $this->JSAlert("Remove directory $navdir_item failed!!!"); else $this->JSAlert("SUCCESS: directory removed!!!"); $this->JSScript("GOMAIN();"); } function RenDir() { $navdir_item = $this->navdir_item; $navdir_extension = $this->navdir_extension; $old_file = $navdir_item; $new_file = dirname($navdir_item)."/".$navdir_extension; if ( ! rename($old_file, $new_file)) $this->JSAlert("FAILED: fail to rename $navdir_item to $navdir_extension!!"); else $this->JSAlert("SUCCESS: Rename $navdir_item to $navdir_extension!!"); $this->JSScript("GOMAIN();"); } function AddInfo() { $navdir_item = $this->navdir_item; $navdir_extension = $this->navdir_extension; $info_file = "$navdir_item.navinfo"; $fp=fopen($info_file, "wb"); if ($fp) { rewind($fp); fwrite($fp, $navdir_extension); fclose($fp); $this->JSAlert("SUCCESS: Info for $navdir_item added.!!!"); } else $this->JSAlert("FAILED: Add info file $info_file failed!!!"); $this->JSScript("GOMAIN();"); } function AddFile() { global $HTTP_POST_VARS; $base_dir = $this->base_dir; $base_url = $this->base_url; $navdir_item = $this->navdir_item; $navdir_admin_mode = $HTTP_POST_VARS['navdir_admin_mode']; echo '<FORM NAME="NAVFILE" METHOD="POST" ENCTYPE="multipart/form-data">'; echo "<INPUT TYPE=HIDDEN NAME='navdir_action' VALUE='PROCFILE'>"; echo "<INPUT TYPE=HIDDEN NAME='navdir_item' VALUE='$navdir_item'>"; echo "<INPUT TYPE=HIDDEN NAME='navdir_admin_mode' VALUE='$navdir_admin_mode'>"; echo "<INPUT TYPE=HIDDEN NAME='base_dir' VALUE='$base_dir'>"; echo "<INPUT TYPE=HIDDEN NAME='base_url' VALUE='$base_url'>"; echo "<INPUT CLASS=small TYPE=SUBMIT VALUE='Upload this file'>"; echo "<BR><INPUT CLASS=small TYPE=FILE NAME=UPLOAD><BR>"; echo "</FORM>"; } function DelFile() { $navdir_item = $this->navdir_item; $navdir_extension = $this->navdir_extension; if ( ! unlink($navdir_item) ) $this->JSAlert("FAILED: Unable to delete $navdir_item!!"); else $this->JSAlert("SUCCESS: $navdir_item deleted!!"); $this->JSScript("GOMAIN();"); } function RenFile() { $navdir_item = $this->navdir_item; $navdir_extension = $this->navdir_extension; $old_file = $navdir_item; $new_file = dirname($navdir_item)."/".$navdir_extension; if ( ! rename($old_file, $new_file)) $this->JSAlert("FAILED: Rename $navdir_item to $navdir_extension FAILED!!"); else $this->JSAlert("SUCCESS: $navdir_item renamed to $navdir_extension!!"); $this->JSScript("GOMAIN();"); } function ProcFile() { global $HTTP_POST_FILES; global $HTTP_POST_VARS; $navdir_item = $this->navdir_item; $base_dir = $this->base_dir; // Get the filename // foreach ($HTTP_POST_FILES['UPLOAD'] as $key=>$value) // echo "<H6>$key: [$value]<BR></H6>"; if ($HTTP_POST_FILES['UPLOAD'] == "none") $this->JSScript("alert('No file upload!!);GOMAIN();"); // now read in file info $tmp_name = $HTTP_POST_FILES['UPLOAD']['tmp_name']; $file_name = basename($HTTP_POST_FILES['UPLOAD']['name']); $file_size = $HTTP_POST_FILES['UPLOAD']['size']; $file_type = $HTTP_POST_FILES['UPLOAD']['type']; // DEBUG echo "TMP:[$tmp_name]<BR>NAME:[$file_name]<BR>SIZE[$file_size]<BR>TYPE[$file_type]<BR>"; echo "UPLOAD_DIR:[$navdir_item]<BR>"; // some check $des_file = $navdir_item."/".$file_name; if ($file_size == 0) $this->JSScript("alert('FAILED $name has zero file size!!');GOMAIN();"); elseif ( ! is_uploaded_file($tmp_name) ) $this->JSScript("alert('FAILED: It is not a upload file!!');GOMAIN();"); elseif ( substr($des_file, 0, strlen($base_dir)) != $base_dir ) $this->JSScript("alert('FAILED: not in base directory!!');GOMAIN();"); elseif ( strstr($navdir_item, "..") ) $this->JSScript("alert('Directory traversal is not allowed.'); GOMAIN();"); // looks okay, let move to the proper place if ( !copy($tmp_name, $des_file) ) $this->JSScript("alert('FAILED: cannot copy!!');GOMAIN();"); // success $this->JSScript("alert('File uploaded!!');GOMAIN();"); } //====== MISCELLANEOUS FUNCTION STARTS HERE function JSAlert($msg) { //$msg=addcslashes($msg, "/'"); echo "<script>alert('$msg');</script>"; } function JSScript($msg) { echo "<script>$msg</script>"; } //===== IN-LINE HTML CODE STARTS HERE ================================= function NAVDIRForm() { global $HTTP_SERVER_VARS; $this_page = $HTTP_SERVER_VARS['PHP_SELF']."?".strftime("%H%M%S"); //$this_page = $HTTP_SERVER_VARS['PHP_SELF']; ?> <STYLE> .small {font-size: 8pt; font-weight:bold; padding:0; margin:0} </STYLE> <FORM NAME="NAVDIR" METHOD="POST" ACTION='<?php echo $this_page; ?>' > <INPUT TYPE=HIDDEN NAME="navdir_action" VALUE="" > <INPUT TYPE=HIDDEN NAME="navdir_admin_mode" VALUE='<?php echo $this->navdir_admin_mode; ?>' > <INPUT TYPE=HIDDEN NAME="navdir_item" VALUE=""> <INPUT TYPE=HIDDEN NAME="navdir_extension" VALUE=""> <INPUT TYPE=HIDDEN NAME="navdir_extension2" VALUE=""> <INPUT TYPE=HIDDEN NAME="navdir_curdir" VALUE='<?php echo $this->navdir_curdir; ?>' > </FORM> <SCRIPT> function GOMAIN() { document.forms.NAVDIR.navdir_action.value=""; document.forms.NAVDIR.navdir_item.value=""; document.forms.NAVDIR.submit(); } function NAVDIR_SUBMIT(action, navdir_item) { if ( confirm("Confirm " + action + " on " + navdir_item + "?") ) { document.forms.NAVDIR.navdir_action.value=action; document.forms.NAVDIR.navdir_item.value=navdir_item; document.forms.NAVDIR.submit(); } } function NAVDIR_SUBMIT_EXT(action, navdir_item, fieldname) { if ( confirm("Confirm " + action + " on " + navdir_item + "?") ) { document.forms.NAVDIR.navdir_action.value=action; document.forms.NAVDIR.navdir_item.value=navdir_item; value = document.getElementById(fieldname).value; document.forms.NAVDIR.navdir_extension.value=value; document.forms.NAVDIR.submit(); } } function NAVDIR_ADMIN_MODE() { if (document.getElementById("navdir_admin_input").checked) value="TRUE"; else value="FALSE"; document.forms.NAVDIR.navdir_admin_mode.value=value; GOMAIN(); } function NAVDIR_CURDIR(cur_dir) { document.forms.NAVDIR.navdir_curdir.value=cur_dir; GOMAIN(); } </script> <?php } // HTMLCommon } // end CLASS ?> <?php //**** EXAMPLE SCRIPT **************************************************** //foreach ($HTTP_POST_VARS as $key => $value) echo "<H6>$key: [$value] </H6><P>"; //----- CONFIGURE the root directory and the corresponding URL address // remember to put / at the back of base_url //$base_dir = $HTTP_SERVER_VARS['DOCUMENT_ROOT']."/TEST"; //$base_url = "/TEST/"; $base_dir = $HTTP_SERVER_VARS['DOCUMENT_ROOT']; $base_url = "/"; $admin = TRUE; $dir_mgt = new WEBDIR($base_dir, $base_url, $admin, "NAVTREE", "./IMAGE/NAVTREE"); // I set it to not more than 5 level to display, // when you click on the directory icon, it will // goto that directory's parent directory. // this is to aovid those deep-level directory loading $dir_mgt->SetDepth(5); //----- some debug // echo "<H6>Action [$navdir_action] Item [$navdir_item] Extension [$navdir_extension]". // "Admin mode [$navdir_admin_mode]</H6><BR>"; // This is HTML page $dir_tree = $dir_mgt->DisplayMain(); // GenFormNav will always call this function function DisplayMainHTML() { global $dir_mgt; global $dir_tree; ?> <! display the admin mode button -> <table border=0 padding=0 cellspacing=0 margin=0 width=80% align=center> <tr><TD> <?php $dir_mgt->DisplayAdminButton("Admin button"); ?> <tr><td> <! Set up a division to hold the tree, take note the id is NAVTREE -> <DIV id='NAVTREE' style="background:lightblue"> <?php $dir_mgt->PrintTree($dir_tree); ?></DIV> </table> <BR> <?php } // End DisplayMainHTML // BELOW IS YOUR WEB TEMPLATE ?> <HTML> <TITLE></TITLE> <BODY PADDING=0 MARGIN=0 WIDTH=100%> <! HEADER -> <SPAN style='text-align:center;background:lightgreen; width: 100%'><H1>My header</H1></SPAN> <! Now the body -> <h2><CENTER>Demo of the Web-based directory management</CENTER></h2> <HR> <BR> <! Slip in a hidden form, immediately after BANNER or other static object This determine which page to display Note that after this staement, different branches causes different HTML to be displayed. -> <?php $dir_mgt->GenFormNAVDIR(); ?> <! Display Your footer here if you want to -> <table width=100% bgcolor=lightgreen> <TD><CENTER>My Footer</CENTER></TD> </BODY> </HTML>