DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:dirlisting

Directory Listing Syntax PlugIn

Compatible with DokuWiki

No compatibility info given!

plugin Show content of server directories.

Last updated on
2005-09-20
Provides
Syntax

The missing download url means that this extension cannot be installed via the Extension Manager. Please see Publishing a Plugin on dokuwiki.org. Recommended are public repository hosts like GitHub, GitLab or Bitbucket.

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Security warning (please read plugin security guidelines): This extension exposes information that might be valuable to a hacker. It is not recommended in a public installation.

Similar to dir, nspages

Tagged with file, listing

Use it carefully, you can use it at your own risk.

— Be aware that if you have anonymous editable pages users can show local directory contents!!! —

Usage

To view “c:\repository” directory:

<dirlisting "c:\repository">

…and with directory name like a title:

<dirlisting title "c:\repository">

Installation

  1. Create a dokuwiki/lib/plugins/dirlisting directory in DokuWiki's plugin directory.
  2. Copy the plugin source below to a file called syntax.php in the directory.
  3. Make sure both the new directory and the new file are readable by the web-server e.g.

Plugin Source

Copy this PHP source to DokuWiki's plugin directory as described above:

<?php
/**
 * Plugin dirlisting: Inserts content of selected directory.
 * v1.0
 *
 * Usage: <dirlisting "c:\mydir\">
 *        <dirlisting title "c:\mydir\">
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Natalia Pujol <naty@eslamejor.com>
 */
 
//if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_dirlisting extends DokuWiki_Syntax_Plugin {
 
	function getInfo() {
		return array(
			'author' => 'Natalia Pujol',
			'email' => 'naty@eslamejor.com',
			'date' => '2005-09-20',
			'name' => 'Directory Listing Plugin',
			'desc' => 'Insert content of selected directory',
			'url' =>  'http://www.dokuwiki.org/plugin:dirlisting');
	} // getInfo()
 
	function connectTo($aMode) {
		$this->Lexer->addSpecialPattern('<dirlisting.*?>', $aMode, 'plugin_dirlisting');
	} // connectTo()
 
	function getSort() {
		return 7;
	} // getSort()
 
	function getType() {
		return 'substition';
	} // getType()
 
	function handle($match, $state, $pos, Doku_Handler $handler) {
		if ($state = DOKU_LEXER_SPECIAL) {
			if (eregi("<dirlisting[\t ]+(title[\t ]+){0,1}\"(.+)\"[\t ]*>", $match, $out))
				return array($state, array($out[2],$out[1]));
		}
		return array();
	} // handle()
 
	function render($aFormat, Doku_Renderer $aRenderer, $aData) {
		if ('xhtml' != $aFormat) {
			return FALSE;
		} // if
		list($state, $match) = $aData;
		if (DOKU_LEXER_SPECIAL == $state) {
			$aRenderer->doc .= "<pre>";
			// Disrectory exists?
			if (is_dir($match[0])) { 
				// Title
				if (trim($match[1])=="title")
					$aRenderer->doc .= '<b>'.$match[0].'</b><br />';
				$aRenderer->doc .= '<table cellspacing="0" cellpadding="0" border="0">';
				if ($dh = opendir($match[0])) { 
					while (($file = readdir($dh)) !== false) {
						$filename = $match[0]."/".$file;
						$size = filesize($filename);
						if ($size>=1073741824) {
							$size /= 1073741824;
							$unit = "Gb";
						} elseif ($size>=1048576) {
							$size /= 1048576;
							$unit = "Mb";
						} elseif ($size>=1024) {
							$size /= 1024;
							$unit = "Kb";
						} else {
							$unit = "bytes";
						}
						$aRenderer->doc .= "<tr><td>".$file;
						$aRenderer->doc .= "</td><td align=\"right\">&nbsp;".(is_dir($filename)?"</td><td>&lt;DIR&gt;":number_format($size,($unit=="bytes"?0:1),".","")."<td>&nbsp;$unit</td>");
						$aRenderer->doc .= "</td></tr>";
					} 
					closedir($dh);
				} 
				$aRenderer->doc .= "</table>";
			} else {
				$aRenderer->doc .= "<br />[Directory not exists!!]<br />";
			}
			$aRenderer->doc .= "</pre>";
		} // if
		return TRUE;
	} // render()
 
  //@}
} // class syntax_plugin_dirlisting
?>

Natalia Pujol 2005-09-21

Discussion

Hints, comments, suggestions …

Some notes:

  1. Security. I'd strongly suggest to limit the accepted/processed file space to DokuWiki's 'pages' directory (and sub-directories). I don't think it's acceptable to any webmaster to allow for showing arbitrary parts of the server's disks.
  2. The (X)HTML code produced is invalid. There must not be any markup inside PRE tags (except some inline like STRONG etc.). So either remove the PRE tags and just use the TABLE markup, or – better – remove the TABLE/TR/TD stuff thus creating a table by indentions (i.e. TABs etc.) which not only saves processing time at both the server's and the user's machine but as well shows the dir-listing in a easily recognized preformatted format.
  3. You could move most parts of the actual processing from the render() to the handle() method the latter returning an array holding the relevant data while the former just performs the markup.
  4. Priority: I can't see a reason for a priority of 7 returned by getsort(). I'd suggest using a value between 300 - 500.

Matthias Watermann 2005-09-24


plugin/dirlisting.txt · Last modified: 2023-04-17 12:43 by 164.215.121.170

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki