Revision [17309]

This is an old revision of ImageListing made by GmBowen on 2007-08-01 23:19:26.

 

Image Listing

This action (and associated class code) allows the directory/file tree of the image files in the upload directory specified in the config file to be shown. There are two files. The first is directory.php which should be placed in the actions directory. The second is FileDisplayClass.inc.php which is a modification of a class file obtained from www.phpclasses.org. If a thumbnail gallery is desired or necessary, see the code at GmBowen...this action was developed to support those who do not have GD on their servers but for whom a listing of uploaded images may be useful.

Use: Place {{directory}} on a page to obtain the listings of uploaded image files (in the directory as specified by wakka config).

Note: The code was developed to support a course I'm teaching where thumbnail galleries were unnecessary. This code is (a) not optimized (there's some unused functions that could be stripped from the class, unused variables passed across to the functions, etc), (b) allows images to be opened in a new window (using target=_BLANK) which some people consider to be a poor approach, and (c) it needs a URL to be set manually in the class file (sorry, I couldn't figure out how to do it in a function....feel free to fix it if you know how).

Place this code in the actions directory as directory.php
<?php
// Directory listing action directory.php written by GMBowen for a SSHRC research project
// for use in Wikka wiki. Code released under GPL.
// REQUIRES "FileDisplayClass.inc.php" which is placed in the directory "scripts" in wikka root
// USE: {{directory}} (no parameters) ... base directory is that set for uploads in wikka.config.php

$link = $this->config["base_url"].$this->MiniHref($method, $tag);

$fldr=$_GET['fldr'];

$fldr=strip_tags($fldr);

if ($fldr) {echo "<<&nbsp;<a href='$link'>Back</a>&nbsp;<<";} else {echo "Click on a directory name to see a list of files.";}

$activefolder = $this->config["upload_path"]."/".$fldr;

require_once('scripts/FileDisplayClass.inc.php');

$FileDisplay = New FileDisplay;
$FileDisplay->showContents($activefolder, $link, $fldr);

if ($fldr) {echo "<<&nbsp;<a href='$link'>Back</a>&nbsp;<<";} else {echo "&nbsp;&nbsp;";}
?>


Place this code in a directory called scripts placed in wikka root named as FileDisplayClass.inc.php...
<?php
// FileDisplayClass.inc.php (new Version 1.0) is a modification of the class file obtained from www. phpclasses.org
// adapted for use in wikka wiki by GmBowen for a SSHRC research project. File supports the action "directory.php" &
// shows directories & files in the upload directory. Modifications released under a GPL license.

// USE: This file should be placed in the directory "scripts" placed in wikka root.
// The javascript which opens a box with the wikka file location needs the url to be set manually (feel free to
// adapt so that this is done automatically & repost this code). See Line 220.

/*
    ClassName: FileDisplay (Obtained from www.phpclasses.org) under the LGPL license.
   
    See readme.txt and manual.txt for more info. (available at http://www.phpclasses.org/browse/package/1369.html)
   
    ADAPTED version: Greg Lane, 30th December 2003
   
*/


class FileDisplay
{

/*
Public Properties
*/
 
    var $piclink;  
    var $rootdir;
    var $showperms = false;
    var $showsize = true;
    var $showtype = true;
    var $showmodified = true;
    var $extallow = "jpg,jpeg,gif,png";
    var $arrsortkeys = array("+type","+filename","+filesize");
//  var $extallow = "";
    var $inputtype;

/*
Private Properties
*/
     
   
    var $colnum;
    var $arrdircontents = array();

/*
Public Methods 
*/
 

    function showContents($targetdir, $link, $fldr)
    {      
        global $wakka;
        $showdir;
        $getrootdir;
        $this->getColNum();

        if(strlen($targetdir) == 0)
        {
            $showdir = $this->rootdir; 
        } else {
           
            $getrootdir = substr($targetdir, 0, strlen($this->rootdir));
           
            // This checks to see if the rootdir is a sub string of the target directory
            if($this->rootdir == $getrootdir){
                $showdir = $targetdir;
            } else {
                $targetdir = $this->rootdir;
                $showdir = $targetdir;
            }
        }
               
?>
            <table width="100%" bgcolor="#FFFFFF" cellspacing="2" cellpadding="1">
                <tr>
                    <td align="left" colspan="<?php echo($this->colnum) ?>">
                    </td>
                </tr>
                <tr>
                    <td bgcolor="#CCCCCC" align="left" colspan="2" width="25%">
                        <font class="theader">File Name</font>
                    </td>
<?php
        if($this->showperms){
            $this->maketableheader("Permissions");
        }
        if($this->showsize){
            $this->maketableheader("File Size");
        }            
        if($this->showtype){
            $this->maketableheader("Type");
        }                  
        if($this->showmodified){
            $this->maketableheader("Date Modified");
        }
?>                 
                </tr>
<?php
            $this->getDirContents($this->rootdir,$targetdir,$link,$inputtype);
?>
            </table>
<?php  
   
    }

/*
Private Methods
*/

   
    function getDirContents($docroot,$targetdir, $link, $piclink)
    {
    global $wakka;
        $link;
        $dirempty;      // Holds Boolean true/false depending whether directory is empty or full
        $dir;          
        $fullpath;
        $newtargetdir;
        $prevdir;
        $cnt;
           
        $cnt=0;
        if(strlen($targetdir) == 0)
        {
            $targetdir = $docroot; 
        }
            $dirempty = true;
            $dir = opendir($targetdir);
            while ($diritem = readdir($dir)){
                $additem = false;
                $fullpath = $targetdir . "/" . $diritem;
                    if(is_file($targetdir . "/" . $diritem)){
                   
                   
                        if($this->checkext($diritem)){
                            $dirempty = false;
                            $newtarget = $targetdir . "/" . $diritem;
                            $additem = true;
                            }
                       
                       
                    } elseif(is_dir($targetdir . "/" . $diritem) && ($diritem != ".") && ($diritem != "..")) {
                       
                        $dirempty = false;
                        $newtarget = $link . $targetdir . $diritem;
                        $additem = true;   
                    }
           
                    if($additem){
                        $this->arrdircontents[] = array(
                                        "fullpath" => $newtarget,
                                        "filename" => $diritem,
                                        "filesize" => filesize($fullpath),
                                        "type" => filetype($fullpath),
                                        "fileperms" => $this->convertperms(fileperms($fullpath)),
                                        "filemodified" => date("Y-m-d H:i:s", filemtime($fullpath)),
                                        "fileicon" => $this->selectfileicon($diritem,filetype($fullpath))
                                    );         
                    }
                }      
        closedir($dir);


       
        if($dirempty){
?>     
                <tr>
                    <td valign="top" colspan="<?php echo($this->colnum) ?> " align="center">
                        Directory is empty<br>
                    </td>
                </tr>          
<?php      
        } else {
            $arrsortkeys = $this->arrsortkeys;
            $this->aasort($this->arrdircontents, array($arrsortkeys));
            $this->printFileInfo($fullpath, $link, $piclink);          
           
        }

        $prevdir = substr($targetdir, 0, strrpos($targetdir, "/"));
               
?>
                <tr>
                    <td valign="top" colspan="<?php echo($this->colnum) ?>">
<?php
        if($targetdir != $this->rootdir)
        {
        echo "";
        } else {
            echo("&nbsp;");
        }
?>                 
                    </td>
                </tr>
<?php      
    }

           
    function printFileInfo($fullpath, $link, $piclink)
    {
    global $wakka;
    // declare variables for this function

                foreach($this->arrdircontents as $sdiritem){   
       
?>
                <tr>
                    <td valign="top" align="left"></td>
                    <td valign="middle"><font class="itemtext">

<?
    // code to move between directories
    if($this->checkext($sdiritem["filename"])=="0") {
        $link = $wakka->config["base_url"].$wakka->MiniHref($method, $tag)."&amp;fldr=".$sdiritem["filename"];
        echo "<a href=\"$link\" >".htmlspecialchars($sdiritem["filename"])."</a>";
        }
        else
        {
        $fldr = $_GET['fldr'];
        $pix=$wakka->config["upload_path"]."/".$fldr."/".$sdiritem["filename"];
        echo "<a href=\"$pix\" target=_BLANK title=\"This picture will open in a NEW window!!\">".htmlspecialchars($sdiritem["filename"])."</a>";

// THE LINE BELOW NEEDS THE CORRECT URL TO BE SET!!!!!
        $pix2 = "http://131.202.167.33/3511wiki/".$pix;

        echo "<small>&nbsp;&nbsp;[<a href=\"javascript:void(prompt('The following is the image link...right-click &amp; copy to copy to the clipboard (then paste into your wiki page).', '$pix2'))\" title=\"Image Link\">link</a>]</small>";
        }
    echo "</font></td>";

        if($this->showperms){
            $this->maketablecell($sdiritem["fileperms"]);
        }
        if($this->showsize){
                if($this->checkext($sdiritem["filename"])=="0") {
                    $this->maketablecell("N/A");
                }  
                else
                {
                $this->maketablecell($this->convertFileSize($sdiritem["filesize"]));
                }
        }            
        if($this->showtype){
                $this->maketablecell($sdiritem["type"]);
        }            
        if($this->showmodified){
                $this->maketablecell($sdiritem["filemodified"]);      
            }
        echo "</tr>";
        }
    }
   
    function maketablecell($cellcontent)
    {
        echo('<td valign="middle"><font class="itemtext">');
        echo(htmlspecialchars($cellcontent));
        echo('</font></td>');
    }

       
    function maketableheader($cellcontent)
    {
        echo('<td valign="middle" bgcolor="#CCCCCC"><font class="theader">');
        echo(htmlspecialchars($cellcontent));
        echo('</font></td>');
    }  
   
   
    function getColNum()
    {
        $this->colnum = 2;
        if($this->showsize){
            $this->colnum += 1;
        }
        if($this->showtype){
            $this->colnum += 1;
        }      
        if($this->showmodified){
            $this->colnum += 1;
        }
        if($this->showperms){
            $this->colnum += 1;
        }
    }

   
    function aasort(&$array, $args) {
        foreach($args as $arg) {
            $order_field = substr($arg, 1, strlen($arg));
            foreach($array as $array_row) {
                $sort_array[$order_field][] = $array_row[$order_field];
            }
            $sort_rule .= '$sort_array['.$order_field.'], '.($arg[0] == "+" ? SORT_ASC : SORT_DESC).',';
        }
        eval ("array_multisort($sort_rule".' &$array);');
    }
       
   
  function convertPerms($inperms)
  {
    $permstr;   // holds the 'rwxrwxrwx' style permission string to be returned

    if($inperms & 0x1000)     // FIFO pipe
      $permstr = 'p';
    elseif($inperms & 0x2000) // Character special
      $permstr = 'c';
    elseif($inperms & 0x4000) // Directory
      $permstr = 'd';
    elseif($inperms & 0x6000) // Block special
      $permstr = 'b';
    elseif($inperms & 0x8000) // Regular
      $permstr = '-';
    elseif($inperms & 0xA000) // Symbolic Link
      $permstr = 'l';
    elseif($inperms & 0xC000) // Socket
      $permstr = 's';
    else                         // UNKNOWN
      $permstr = 'u';

    // owner
    $permstr .= (($inperms & 0x0100) ? 'r' : '-') .
           (($inperms & 0x0080) ? 'w' : '-') .
           (($inperms & 0x0040) ? (($inperms & 0x0800) ? 's' : 'x' ) :
                                    (($inperms & 0x0800) ? 'S' : '-'));

    // group
    $permstr .= (($inperms & 0x0020) ? 'r' : '-') .
           (($inperms & 0x0010) ? 'w' : '-') .
           (($inperms & 0x0008) ? (($inperms & 0x0400) ? 's' : 'x' ) :
                                    (($inperms & 0x0400) ? 'S' : '-'));

    // world
    $permstr .= (($inperms & 0x0004) ? 'r' : '-') .
            (($inperms & 0x0002) ? 'w' : '-') .
            (($inperms & 0x0001) ? (($inperms & 0x0200) ? 't' : 'x' ) :
                                   (($inperms & 0x0200) ? 'T' : '-'));
    return $permstr;
  }
   
    function convertFileSize($inbytes)
    {
        if($inbytes < 1000){
            return $inbytes . " bytes" ;
        }
        if($inbytes >= 1000)
        {
        $inbytes = $inbytes/1000;  
        return number_format($inbytes) . " KB";
        }
    }

   
    function checkext($inputstr){
        if(strlen($this->extallow) > 0){               
            $inputstrlen = strlen($inputstr);
            $revstr = strrev($inputstr);           
            $cutstr = $inputstrlen - strpos($revstr,".");
            $inputsubstr = strtolower(substr($inputstr,$cutstr,$inputstrlen));

            $arrExtAllow = explode(",", strtolower($this->extallow));
            $extpass = false;
           
                foreach($arrExtAllow as $sExt){
                    if($sExt == $inputsubstr){
                        return true;
                        $extpass = true;   
                    }
                }
               
            if($extpass == false)
            {
                return false;  
            }      
        } else {
            return true;   
        }
    }
   
    function dirlinks($indirlinks)
    {
        $lastdir;
        $end;
        $i;
        $output;
        $arrDirLinks;
        $dirlink;
       
        $arrDirLinks = explode("/",$indirlinks);
        $end=count($arrDirLinks);
        $i=0;
       
        foreach($arrDirLinks as $dirlink){

            $i = $i + 1;

            if($i == 1){
                $lastdir = $dirlink;
            } else {
                $lastdir = $lastdir . "/" . $dirlink;
            }
                               
            if ($i == $end){
                if($i == 1){
                    $output = $dirlink;
                } else {
                    $output = $output . "/" . $dirlink;
                }  
            } else {
                if($i == 1) {
                    $output = $output . $dirlink;                  
                } else {
                    $output = $output . $dirlink;
                }
            }
        }
        return $output;
    }

    function selectfileicon($inputstr,$inputtype)
    {
        if($inputtype != "dir"){
        $inputstrlen = strlen($inputstr);
        $revstr = strrev($inputstr);
        $cutstr = $inputstrlen - strpos($revstr,".");
        $inputsubstr = substr($inputstr,$cutstr,$inputstrlen);
        switch ($inputsubstr) {
/*
        case "PDF";
            return "pdficon.png";
            break;
*/

        default;
            return "";
        }  
        } else {
            return "";
        }
    }
}
?>

This is the directory in the designated upload directory root....
http://gmbtst.msvu.ca/wikitest/dirlist1.jpg

This is the file listing in the associated page directory (shows image files only)....
http://gmbtst.msvu.ca/wikitest/dirlist2.jpg

This shows the pop-up with the URL of the image files highlighted....
http://gmbtst.msvu.ca/wikitest/dirlist3.jpg


CategoryUserContributions
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki