File Modification Time Action


This action allows you to display the modification time of any file on the filesystem. Save this as actions/filemtime.php:

 <?php
/*
        filemtime action
        Displays the modification time of a file
        Syntax: {{filemtime path="/path/to/file"}}
*/


$path = trim($vars['path']);

if (file_exists($path)) {
        echo "<font size=-2>" . date("M d, Y h:i:s a", filemtime($path)) . "</font>";
} else {
        echo "<!-- ERROR (filemtime): '" . htmlspecialchars($path) ."' not found -->";
}

?>


If you find this action useful, post a comment and let me know! Also, feel free to comment with requests for feature enhancements. --JasonHuebel


CategoryUserContributions
Comments
Comment by JasonHuebel
2005-08-05 15:52:11
BTW, this action probably shouldn't be used on anything but an intranet site since it allows you to put a path to any file anywhere on the filesystem.
Comment by JavaWoman
2005-08-05 19:54:39
Nice idea Jason! I could imagine some handy uses for this.

Two suggestions:

1. Apart from allowing any file on the system, (which could be easily solved by matching the path with the current path), it's not entirely secure in this form; off the top of my head, you'll probably want to apply trip_tags() to the path parameter, as well as htmlspecialchars() instead of htmlspecialchars_ent() so that any entities are escaped as well.
2. Rather than using the old-fashioned and decprecated font tag, it would be better to use a span tag with a class (e.g., "filetime"), to be styled with the strylesheet.
Comment by JasonHuebel
2005-08-05 23:43:19
I'll look into those things. I'm going to improve this over the weekend, I think. It was just a fast and dirty hack. :-)
Comment by JasonHuebel
2005-08-06 04:26:29
As far as I can tell, strip_tags and htmlspecialchars aren't required with the filemtime() function, since it isn't executing a shell command. However, I've made changes to include error checking and error output (in the form of an HTML comment),
Comment by JavaWoman
2005-08-08 21:18:11
The strip_tags() and htmlspecialchars() weren't intended for the benefit of the filemtime() function. Consider what happens if someone puts a (link to) a nasty script as the "file path": that will result in an error message on screen - which would (probably) execute that script.

Of course, hiding the error message in a comment has the same security effect: 'view source' won't execute scripts. :)
Comment by JasonHuebel
2008-01-24 19:45:09
Been a while since I submitted this, but I wanted to add that allowing any path is intentional since I've been using it to show the last modification date of files outside of the web root path.

I'm considering changing the action to include the option of jailing it to the web root path and should have those modifications done in the next couple of weeks (when I have time to look at it).
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki