<?php
/*********************************************/
/* PHP Line Counter                          */
/* Devin Smith (php@arzynik.com)             */
/* 2004-02-01 http://www.arzynik.com         */
/*********************************************/

/* Config */
$dontscandirs = array("./logs","./files","./images","./db","./debugImages","./examples","./extern","./ezcMail","./nbProject","./phpseclib");


$dir = "./";
count_main($dir);


function count_main($dir) {
  global $content, $dirs, $totallines, $totalfiles, $totaldirs, $dontscandirs, $totalbytes, $dontscandirs;


  echo "<html><head><title>Counting lines of '$dir'</title></head><body bgcolor=#FFFFFF>\n"
      ."<font face=arial,verdana,helvetica size=2 color=black>\n"
      .countlines($dir)
      ."</font><p align=right><font face=arial,verdana,helvetica size=2 color=black><b>Copyright &copy; 2004 \n"
      ."<a href=http://www.arzynik.com><font color=blue>Arzynik.com</font></a></b></font></p>\n"
      ."</body></html>\n";
}


function countlines($dir) {
  global $content, $dirs, $totallines, $totalfiles, $totaldirs, $dontscandirs, $totalbytes;
  listdir($dir);
  return "The directory '<b>".$dir."</b>' contains <b>".getfilesize($totalbytes)."</b> of <b>".addcomma($totallines)."</b> lines of code in <b>".addcomma($totalfiles)."</b> files within <b>".addcomma($totaldirs)."</b> subdirectories.";
}


function listdir($dir, $level_count = 0) {
  global $content, $dirs, $totallines, $totalfiles, $totaldirs, $dontscandirs, $totalbytes;
  if (!@($thisdir = opendir($dir))) return;
  while ($item = readdir($thisdir) ) {
    if (is_dir("$dir/$item") && (substr("$item", 0, 1) != '.') && !in_array($dir.$item,$dontscandirs)) {
      listdir("$dir/$item", $level_count + 1);
    }
  }
  if ($level_count > 0) {
    $dir = ereg_replace("[/][/]", "/", $dir);
    $handle=opendir($dir);
    while ($file = readdir($handle)) $filelist[] = $file;
    asort($filelist);
    while (list ($key, $file) = each ($filelist)) {
      if ($file != "." && $file != ".." && !is_dir($file) && iswebtype($file)) {
        $linecount = count(file($dir."/".$file));
        $totallines = $totallines + $linecount;
        $totalfiles++;
        $totalbytes = $totalbytes + filesize($dir."/".$file);
      }
    }
    $totaldirs++;
  }
}


function iswebtype($file) {
  $filetypes = array("php","php4","php3","phtml","html","htm","js","asp","xml","css","bml","cgi","cfm","apm","jhtml","xhtml","aspx");
  foreach ($filetypes as $type) if (substr($file,strlen($file)-strlen($type)-1) == ".".$type) return TRUE;
  return FALSE;
}


function getfilesize($size) {
  if ($size != 0) {
    if ($size>=1099511627776) {
      $size = round($size / 1024 / 1024 / 1024 / 1024, 2);
      $suff = "TB";
    } elseif ($size>=1073741824) {
      $size = round($size / 1024 / 1024 / 1024, 2);
      $suff = "GB";
    } elseif ($size>=1048576) {
      $size = round($size / 1024 / 1024, 2);
      $suff = "MB";
    } elseif ($size>=1024) {
      $size = round($size / 1024, 2);
      $suff = "KB";
    } elseif ($size<1024) {
      $size = round($size / 1024, 2);
      $suff = "Byte";
    }
  } else {
    $suff = "Byte";
  }
  if ($size == 1 || $size == 0) $size = $size." ".$suff;
  else $size = "<b>".$size."</b> ".$suff."s";
  return $size;
}


function addcomma($int) {
    $retval ='';
    $intarray = array();
  while(strlen($int)>3) {
    $intarray[] = substr($int,-3,3);
    $int = substr($int,0,strlen($int)-3);
  }
  foreach ($intarray as $item) {
    $retval .= ",".$item;
  }
  return $int.$retval;
}
?>