Rendere i file di log accessibili a un determinato gruppo di persone

Per Hosting Linux e Performance Hosting

Per rendere le statistiche di accesso nella directory logs accessibili a un gruppo specifico di persone, colloca il seguente script PHP in una directory protetta da password:

<?php
  error_reporting(E_ALL);
  if (empty($file)) $file= 'index.html';

  // traffic.html is actually a directory
  if ('traffic.html' == $file) $file= 'traffic.html/index.html';

  function _basename($str) {
    return preg_replace(array('=\.{2,}=', '=/+='), array('', '/'), $str);
  }

  function _linkcb($matches) {
    switch (strtolower($matches[1])) {
      case 'src':
        return 'src="'.basename($GLOBALS['PHP_SELF']).'?img='.urlencode($matches[2]).'"';
    
      case 'href': 
        if (preg_match('=^(ht|f)tps?://=', $matches[2])) {
          return 'href="'.$matches[2].'" target="_external"';
        }
        return sprintf(
          'href="%s?file=%s/%s"',
          basename($GLOBALS['PHP_SELF']),
          dirname($GLOBALS['file']),
          str_replace('%23', '#', urlencode($matches[2]))
        );
    }  
    return FALSE;
  }

  // Workaround for domains not connected to ~/
  $DOCUMENT_ROOT= preg_replace('=^([/a-z0-9]+/htdocs/).*$=','\1',getenv('DOCUMENT_ROOT'));

  // Handle images
  if (isset($img)) { 
    header('Content-type: image/gif');
    switch (substr($img, 0, 7)) {
      case '/spicon': 
        $uri= 'http://'.getenv('HTTP_HOST').'/spicons/'.basename($img);
        break;
    
      case 'http://':
        $uri= $img;
        break;

      default:
        $uri= $DOCUMENT_ROOT.'logs/traffic.html/'._basename($img);
        
    }    
    readfile($uri);
    exit;
  }

  // Do some sanity checks
  $file= _basename($file);
  $filename= $DOCUMENT_ROOT.'logs/traffic.html/'.$file;
  
  if (is_dir($filename) || !file_exists($filename)) {
    header("Status: 404 Not Found");
    echo "<h1>File not found</h1><p>The request file {$filename} could not be found</p>";
    exit;
  }

  // Parse HTML
  $content= implode('', file($filename));
  $content= preg_replace_callback(
    '/(src|href) ?= ?["\']([^#][^"\']*)["\']/iU',
    '_linkcb',
    $content
  );
  echo $content;

  echo '<hr/>';
  show_source(basename($PHP_SELF));
?>