Location: PHPKode > scripts > robots.wizard(tm) > robots-wizardtm/classes.php
<?php
/* robots.wizard classes.php copyright 2006 Betsy A. Gamrat all rights reserved 

    This program 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.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Contact Information: Betsy A. Gamrat hide@address.com

*/
/*
Class definitions for robots.wizard.  Also includes the tag generation for the
input tags.
*/
include_once "constants.php";

class cl_file
{
	// file class - common attributes of directory entries

        var $id;	// hierarchical id for fast direct access
        var $name;	// name of file/link/directory
        var $disallowed;// flag to indicate whether the object is disallowed
        var $changed;	// indicates whether the object state is different than the current robots.txt

	function display_dir_entry_HTML($dirname="")
	{
		/* 
		All directory entries are displayed with this HTML.

		This function encapsulates the input tag in a span which is used
		to apply the color for the entry.  Each file/directory has a check
		box to indicate allow/disallow.
		*/

        	echo '<span class="';
	        if ($this->changed)
        	        echo 'changed_';
	        echo ($this->disallowed)?'dis':'';
        	echo 'allowed" >';

	        echo '<input type="checkbox" ';
        	echo ($this->disallowed)?'checked="checked"':'';
	        echo ' onclick="changestate(this);update();" name="'.$this->id;
        	echo '" ';
	        echo 'value="'.$dirname.$this->name.'" ';
                echo 'title="'.$dirname.$this->name.'" ';
        	echo 'id="'.$this->id.'"';
	        echo ' />';

        	echo $this->name;

	        echo "</span><br />\n";
	}
}

class cl_directory extends cl_file
{
	// directory class - attributes of directories

        var $subs;	// array of subdirectories
        var $files;	// array of files/links
        var $dirname;	// name of the directory (whole path name)
        var $show;	// whether the directory is expanded or not
	var $opendir;	// flag to indicate whether the directory could be opened/read

	function display_directory_HTML()
	{
		/*
		Directories may be preceded with a expand/collapse button and a check/clear all box.
		
		This function produces that HTML.  The expand/collapse button is disabled if a 
		directory is disallowed.
		*/

        	echo '<input type="submit" class="submit" ';
		if (!$this->opendir)
			echo 'disabled title="Directory could not be read.  Check permissions" ';	
		if ($this->disallowed)
		 	echo 'disabled title="Directory is disallowed, file level access is disabled." ';
		echo 'id="'.EXP.$this->id.'" ';
        	echo 'value="';
	        echo ($this->show)?COLLAPSE_DIR:EXPAND_DIR;
        	echo '" name="';
	        echo ($this->show)?COL:EXP;
        	echo $this->id.'" />&nbsp;';

		/* If a directory is expanded, the check/clear all box is displayed. */

	        if ($this->show)
        	{
	                echo '<input type="checkbox" title="Check all"';
        	        echo ' id="'.CB.substr($this->id,3).'" onclick="check_clear_all(this);change_check_title(this);update();" />&nbsp;';
	        }
	}

function display_expanded_HTML()
{
	/* This input tag is used to preserve the state of directories that are expanded. */

        echo '<input type="hidden" value="'.EXPAND_DIR.'" name="'.EXP;
        echo $this->id;
        echo '" />';
}


        function read_directory($disallow_dir,$disallow_file,&$disallow_ids,&$index,$expand=UNKNOWN)
        {
		/*
		Reads the directory.

		This function uses disallow_dir and disallow_file to apply the robots.txt settings
		to the directory displays.  It creates disallow_ids and index.  disallow_ids is an
		array of all the ids that are disallowed.  This is used to ensure that files/directories
		which are changed from disallowed to allowed are properly processed.  index is an
		array used to index all the files and directories by their ids.  This allows faster
		access for subsequent runs.
		*/

		/* i is the numeric component of the id.  This assignment removes the "dir." prefix. */
                $i=(strlen($this->id) > 4)?substr($this->id,4):"";

                $dirname=$this->dirname;

		/* Each id is dir or file, followed by a hierarchical number (dir.1.2.0 or file.3.1.4) */
                $dsid='dir';
                $dfid='file';
                if ($i != "")
                {
                        $dsid.=SEP.$i;
                        $dfid.=SEP.$i;
                }

		/* This converts the directory name to a relative name so it can be checked in the disallow_dir array. */
                $rel_dir=substr($dirname,strlen(TOP)-1);
                $rel_dir=substr($rel_dir,0,strlen($rel_dir)-1);
                if ($expand!=EXPAND)
                        $this->disallowed=in_array($rel_dir,$disallow_dir);

		/* Open and read the directory */
		$this->opendir=true;
                if ($dh = opendir($dirname))
                {
			/* sid and fid are subdirectory id and file id.  They are used to update the index array. */
                        $sid=0; $fid=0;
                        while (($f = readdir($dh)) !== FALSE)
                        {
                                if ($f[0] != '.') // hidden files and directories are disregarded.
                                {
				switch (filetype($dirname.'/'.$f))
                                        {
                                                case "dir":
                                                        $sub = new cl_directory;
                                                        $sub->id=$dsid.SEP.$sid;
                                                        $sub->name=$f;
                                                        $sub->dirname=$dirname.$f.'/';
                                                        $sub->show=false;
							$sub->opendir=true;
                                                        $sub->disallowed=in_array($rel_dir.'/'.$f,$disallow_dir);
                                                        $sub->changed=false;
                                                        $this->subs[$sid]=$sub;
                                                        $index[$sub->id]=&$this->subs[$sid];
                                                        $sid++;
                                                        if ($sub->disallowed)
                                                                $disallow_ids[$sub->id]=($expand==EXPAND)?EXPAND:UNKNOWN;
                                                        break;
                                                case "file":
                                                case "link":
                                                        $file = new cl_file;
                                                        $file->id=$dfid.SEP.$fid;
                                                        $file->name=$f;
                                                        $file->disallowed=in_array($rel_dir.'/'.$f,$disallow_file);
                                                        $file->changed=false;
                                                        $this->files[$fid]=$file;
                                                        $index[$file->id]=&$this->files[$fid];
                                                        $fid++;
                                                        if ($file->disallowed)
                                                                $disallow_ids[$file->id]=($expand==EXPAND)?EXPAND:UNKNOWN;
                                                        break;
                                                default:
                                                        echo "Unk: /$f";
                                                        break;
                                        }
                                }
                        }
                        closedir($dh);
                }
		else
			$this->opendir=false;
        }

        function display()
        {
		/*
		Displays the directory and files
		*/
                $f=$this->files;
		display_div_file();
                if (count($f) > 0)
                {
                        foreach ($f as $k => $v)
                                $v->display_dir_entry_HTML($this->dirname);
                }
		else
			display_no_files();
		display_end_div();
                $d=$this->subs;
		display_div_dir();
                if (count($d) > 0)
                {
                        foreach ($d as $k => $v)
                        {
                                $v->display_directory_HTML();
                                $v->display_dir_entry_HTML($this->dirname);
                                if (($v->show==true) && ($v->disallowed==false))
                                {
                                        $v->display_expanded_HTML();
                                        $v->display();
                                }
                        }
		}
		else
			display_no_dirs();
		display_end_div();
		display_spacer();
        }

        function hide()
        {
		/* 
		If the state of a directory is changed to collapse, this method effects the
		change for the object.
		*/

                $this->show=false;
                $f=$this->files;
                if (count($f) > 0)
                        foreach ($f as $k => $v)
                                $v->show=false;
                $d=$this->subs;
                if (count($d) > 0)
                        foreach ($d as $k => $v)
                        {
                                $v->show=false;
                                $v->hide();
                        }
        }
}
?>
Return current item: robots.wizard(tm)