Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created November 1, 2017 17:04
Show Gist options
  • Save jhyland87/9235ec50a02b03703139199c362ed03b to your computer and use it in GitHub Desktop.
Save jhyland87/9235ec50a02b03703139199c362ed03b to your computer and use it in GitHub Desktop.
<?php
function getFilePerms( $filename ){
$perms = fileperms( $filename );
switch ($perms & 0xF000) {
case 0xC000: // socket
$info = 's';
break;
case 0xA000: // symbolic link
$info = 'l';
break;
case 0x8000: // regular
$info = '-';
break;
case 0x6000: // block special
$info = 'b';
break;
case 0x4000: // directory
$info = 'd';
break;
case 0x2000: // character special
$info = 'c';
break;
case 0x1000: // FIFO pipe
$info = 'p';
break;
default: // unknown
$info = 'u';
}
// Owner
$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ?
(($perms & 0x0800) ? 's' : 'x' ) :
(($perms & 0x0800) ? 'S' : '-'));
// Group
$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ?
(($perms & 0x0400) ? 's' : 'x' ) :
(($perms & 0x0400) ? 'S' : '-'));
// World
$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ?
(($perms & 0x0200) ? 't' : 'x' ) :
(($perms & 0x0200) ? 'T' : '-'));
return $info;
}
$filename = 'data.txt';
$scriptname = basename(__FILE__);
$cfg = array(
'date_fmt' => 'F j, Y, g:i:s a (U)',
'output_lines' => 20
);
if ( isset( $_REQUEST['delete']) ){
@unlink($filename);
echo 'Successfully deleted ' . $filename . '. Redirecting you back to <a href="'.$scriptname.'">'. $scriptname .'</a>';
echo '<meta http-equiv="refresh" content="0.5; url='.$scriptname.'">';
exit();
}
echo '<small>Click <a href="'.$scriptname.'?delete"><strong>here</strong></a> to delete <em>'.$filename.'</em> and recreate from scratch</small>';
$handle = fopen($filename, 'a+') or die('Cannot open file: '.$filename);
$data = sprintf("This line was added by %s at %s\n", $_SERVER['REMOTE_ADDR'], date($cfg['date_fmt']));
fwrite($handle, $data);
$stat = fstat($handle);
$uid = posix_getpwuid($stat['uid']);
$gid = posix_getgrgid($stat['gid']);
$contents = file_get_contents( $filename );
$lines = explode("\n", trim( $contents ));
fclose($handle);
echo "<h3>Stats for <em>${filename}</em></h3>";
$fmt = '<em>%-15s</em> : <strong>%s</strong>'.PHP_EOL;
echo '<pre>';
printf( $fmt, 'Owner', $uid['name'] );
printf( $fmt, 'Group', $gid['name'] );
printf( $fmt, 'UID', $uid['uid'] );
printf( $fmt, 'GID', $gid['gid'] );
printf( $fmt, 'Perms (octal)', substr(sprintf('%o', fileperms($filename)), -4) );
printf( $fmt, 'Perms (mode)', getFilePerms($filename));
printf( $fmt, 'Created', date($cfg['date_fmt'], $stat['ctime']) );
printf( $fmt, 'Modified', date($cfg['date_fmt'], $stat['mtime']) );
printf( $fmt, 'Accessed', date($cfg['date_fmt'], $stat['atime']) );
printf( $fmt, 'Size (bytes)', date('r', $stat['size']) );
printf( $fmt, 'Lines', count($lines) );
echo '</pre>';
echo '<hr/>';
echo "Contents (" . ( count($lines) > $cfg['output_lines'] ? "last ".$cfg['output_lines'] : "all ".count($lines)) . " lines in reverse order)";
echo '<pre>';
$output = array_reverse($lines);
$output = array_slice($output, 0, $cfg['output_lines']);
$fmt = "%0".strlen((string)count($lines))."d";
foreach( $output as $k => $l){
if ( $k === 0 ) echo "<strong>";
printf("$fmt|%s\n", (count($lines)-$k), $l );
if ( $k === 0 ) echo "</strong>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment