|
Cookbook /
IncludeFileSummary: How to include an external file (from the same file system)
Version: 0.2 (2007-12-07)
Prerequisites:
Status: Stable, Maintained
Maintainer: ThomasP
Categories: Includes
QuestionHow do I include an external file (from the same file system)? AnswerThere are two simple and quick solutions, and one more elaborate and flexible one that should be the safest choice. Solution 1a: unfiltered unsanitised inclusion of any fileUse only if you can trust all wiki editors. Do not use on a public wiki! The following markup lets you include any file, relativ to the path you set in the markup definition. Any file on the system can be included, if you give the right relativ path in the markup. Note that files are not filtered or sanitised for output. Code may be interpreted. This can be potentially disastrous. Markup('includefile', 'directives', '/\\(:includefile\\s+([-\\/.\\w]+)\\s*:\\)/e',
"Keep(implode('', file('/home/yourlocalfilesystempath/public_html/inc/$1')))");
With this in place, the markup For a sanitised and preformatted output for showing code file content see next solution, although it is still allowing any file on the system to be shown. For a safe solution restricting files to be in a uploads direcory and checking read permission see solution 3 below.
Solution 1b: sanitised and filtered for showing any file contentUse only if you can trust all wiki editors. Do not use on a public wiki! No attempt is made to ask for any read authorisation or restrict the file path to uploads/ or other defined directories. You could show content of any file on your system. This could be a god solution for someone wanting to show contents of php files from the cookbook directory for instance. Place in config.php: Markup('showsource', 'directives', "/\\(:showsource\\s?(.*?)\\s*?:\\)/e",
"ShowSource(PSS('$1'))");
function ShowSource($filename) {
$text = htmlspecialchars(file_get_contents($filename));
return "<pre class='escaped'>".Keep($text)."</pre>";
}
Use markup HansB December 07, 2007, at 07:00 AM Solution 2: hardcoded skin template inclusionIn a skin template file, you can use Solution 3: sanitised and filtered and access restricted for showing file contentThis recipe includes the content of a given file (or set of files) in the output of the wiki page. "In the output" means that the inclusion is dynamic (like a link) - the markup in the wiki page itself remains unchanged. It is assumed that you have the SecureAttachments recipe in place, so uploaded files are gathered in a non-webserver-accessible space (usually below The basic syntax is (:includefile filename.txt options:) The file is taken from the upload directory corresponding to the wiki page where the markup is placed in. You can also specify a set of files, using the
Security issues:
On inclusion it is checked whether the current user has permission to access the file, using the ThomasP March 19, 2007, at 12:04 AM Releases
Notes
See Also
Contributors
Comments(An old comment I made here can still be found in the code of this page. ThomasP March 19, 2007, at 12:01 AM) QuestionsCan I add a .cgi file too that will produce output via the post method? No. As this recipe is mainly for including the contents of a file, this should be done in a separate recipe. 07.23.06 Can the html files be included during search? A: Not by interpreting the html, only the content of the html files.
2007.08.29 NeI I have discovered that I can create a directory A: There is a way to upload to page specific directories, a la uploads/GROUP/PAGE/myfile.txt. (Check out the config vars, probably just set $UploadPrefixFmt = '/$Group/$Name';.) ThomasP December 07, 2007, at 04:06 AM
There is a slight background highlighting of a text file I'm including in the Wiki page. In this case just like that seen when I use the markup [@ ...text... @]. Can I get rid of this background highlighting somehow? A: The file text is wrapped in a code tag with class "escaped". If you can allow it to adjust this css class in your css file then things are probably done. Another idea is to extend the code in IncludeFile.php around variable "htmlStyles" to allow also for background color options. ThomasP December 07, 2007, at 04:06 AM
Thanks for this. I have it working...BUT, I am trying to get the include to bring in a text file that includes code for the wiki itself, so that in the page, the right words are bolded, and it's formated correctly for the layout of the wiki. When it pulls the txt file in however, it just outputs the code as code. I have tried a few things, and am stuck. Is it possible to do this, or am I better of creating 26 pages of normal wiki pages, as I am creating a lexicon, and need a dictionary like page setup? TIA Can I embed .pdf files directly into my page??? Franzi |