dirlisting plugin by Natalia Pujol
Show content of server directories.
Last updated on 2005-09-20. Provides Syntax.
No compatibility info given!
Similar to dir.
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!!! — |
|---|
To view “c:\repository” directory:
<dirlisting "c:\repository">
…and with directory name like a title:
<dirlisting title "c:\repository">
dokuwiki/lib/plugins/dirlisting directory in DokuWiki's plugin directory.syntax.php in the directory.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_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://wiki.splitbrain.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, &$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, &$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\"> ".(is_dir($filename)?"</td><td><DIR>":number_format($size,($unit=="bytes"?0:1),".","")."<td> $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
Hints, comments, suggestions …
Some notes:
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 easyly recognized preformatted format.render() to the handle() method the latter returning an array holding the relevant data while the former justs performs the markup.getsort(). I'd suggest using a value between 300 - 500.Matthias Watermann 2005-09-24