Advertisement
puneet

kloc

Nov 6th, 2011
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.71 KB | None | 0 0
  1. <?php
  2.  ###################################################################################################
  3. # KLOC Calculator
  4. # by: Mayank Gandhi, www.mgzhome.com
  5. #                    www.albinator.com/product/
  6. #
  7. # Special thanks to Amit Arora
  8. #
  9. # You can try my other scripts at www.mgzhome.com/project.html
  10. # Try my featured system Albinator: www.albinator.com/product/
  11. #
  12. # This is a Freeware, you can freely distribute/modify this,
  13. # Please don't remove this copyright information.
  14. ###################################################################################################
  15.  
  16.   $self_name     = "kloc.php";
  17.   $phpversion = phpversion();
  18.   if($phpversion < "4.1.0")
  19.   $_POST   = &$HTTP_POST_VARS;
  20.  
  21.   $DOCUMENT_ROOT   = getenv("DOCUMENT_ROOT");
  22.  
  23.   $s = isset($_POST['s']) ? $_POST['s'] : '0';
  24.  
  25.   if($s != '1')
  26.   {
  27.     echo(makeTemplate( '1' ));
  28.       echo("<div align='center'>");
  29.     echo('<form name="getValues" method="post" action="kloc.php">');
  30.     echo('
  31. <table width="98%" border="0" cellspacing="4" cellpadding="5" align="center">
  32.  <tr>
  33.    <td width="50%" valign="top"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Root
  34.      Path to the Parent directory</b><br>
  35.      <font size="1">The absolute path to your code directory, e.g. /usr/bin/myname/<br>
  36.      <br>
  37.      <b>Note:</b> no trailing slash</font></font></td>
  38.    <td width="59%" valign="top"><font face="Verdana, Arial, Helvetica, sans-serif" size="1">
  39.      <input type="text" name="root_path" size="50" value="'.$DOCUMENT_ROOT.'">
  40.     <br><input type="checkbox" name="subfolders" value="1"> include sub-folders
  41.      </font></td>
  42.  </tr>
  43.  <tr>
  44.    <td width="50%" valign="top"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Sub-Directories
  45.      to include<br>
  46.      </b><font size="1">List of all sub-directories you wish to calculate from.
  47.      Don\'t remove "/" as it counts from the root path (separate by commans e.g.
  48.      dir1,dir2) </font></font></td>
  49.    <td width="59%" valign="top"><font face="Georgia, Times New Roman, Times, serif" size="2">
  50.      <input type="text" name="subDirs" size="50" value="/,">
  51.      </font></td>
  52.  </tr>
  53.  <tr>
  54.    <td width="50%" valign="top"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Extentions
  55.      to include<br>
  56.      </b><font size="1">the extentions of the files you want to calculate, if
  57.      you have many to include and less to exclude then just give "." in the include
  58.      extensions and provide the list to exclude in the next variable. e.g. .php,
  59.      .html <br>
  60.      <br>
  61.      <b>Tip:</b> use . for all files</font></font></td>
  62.    <td width="59%" valign="top"><font face="Georgia, Times New Roman, Times, serif" size="2">
  63.      <input type="text" name="masks_include_var" size="50">
  64.      </font></td>
  65.  </tr>
  66.  <tr>
  67.    <td width="50%" valign="top"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Extentions
  68.      to exclude<br>
  69.      </b><font size="1">the extentions of the files you want to exclude to calculate,
  70.      if you have many to include and less to exclude then just give "." in the
  71.      include extensions and provide the list to exclude in the next variable.
  72.      e.g. .php, .html </font></font></td>
  73.    <td width="59%" valign="top"><font face="Georgia, Times New Roman, Times, serif" size="2">
  74.      <input type="text" name="masks_exclude_var" size="50">
  75.      </font></td>
  76.  </tr>
  77.  <tr>
  78.    <td width="50%"><font face="Georgia, Times New Roman, Times, serif" size="2"></font></td>
  79.    <td width="59%" valign="top"><font face="Georgia, Times New Roman, Times, serif" size="2">
  80.      <input type="hidden" name="s" value="1">
  81.      <input type="submit" name="Submit" value="start calculating &gt;">
  82.      </font></td>
  83.  </tr>
  84. </table>
  85.          ');
  86.     echo('</form>');
  87.     echo("</div>");
  88.     echo(makeTemplate( '2' ));
  89.     exit;
  90.   }
  91.  
  92.   set_time_limit(0);
  93.   $bQuote = get_magic_quotes_gpc();
  94.   $root_path     = ($bQuote ? stripslashes($_POST['root_path']) : $_POST['root_path']);
  95.   $subDirs       = explode(",", ($bQuote ? stripslashes($_POST['subDirs']) : $_POST['subDirs']));
  96.   $masks_include = explode(",", ($bQuote ? stripslashes($_POST['masks_include_var']) : $_POST['masks_include_var']));
  97.   $masks_exclude = explode(",", ($bQuote ? stripslashes($_POST['masks_exclude_var']) : $_POST['masks_exclude_var']));
  98.   $subfolders = isset($_POST['subfolders']) ? $_POST['subfolders'] : '0';
  99.   $tf_count = '';
  100.   $sMask = '';
  101.   $lMask = '';
  102.   $kloc = 0;
  103.  
  104.   $mtime = microtime();
  105.   $mtime = explode(" ",$mtime);
  106.   $mtime = $mtime[1] + $mtime[0];
  107.   $starttime = $mtime;
  108.  
  109.   echo(makeTemplate( '1' ));
  110.   echo("<a href='#report'>see Summary Count</a> :: <a href='?redo'>reCalculate</a>");
  111.  
  112.   while( $e = each($masks_include) )
  113.   {
  114.     if($e[0] != '0')
  115.       $sMask .= "|";
  116.  
  117.       $sMask .= "($e[1])";
  118.   }
  119.   $sMask = "/$sMask/";
  120.  
  121.   while( $e = each($masks_exclude) )
  122.   {
  123.     if($e[0] != '0')
  124.       $lMask .= "|";
  125.  
  126.       $lMask .= "($e[1])";
  127.   }
  128.   if($lMask != "()")
  129.   $lMask = "/$lMask/";
  130.   else
  131.   $lMask = "";
  132.  
  133.   if(count($subDirs) == 0)
  134.   $subDirs[] = "/";
  135.  
  136. //  while ( $e = each($subDirs) )
  137.   $counter = 0;
  138.   while ( count($subDirs) > $counter )
  139.   {
  140.     $f_count   = 0;
  141.     $files_to_go = '';
  142.  
  143.     $files = array();
  144.     if(file_exists("$root_path".$subDirs[$counter]))
  145.     $d = dir( "$root_path".$subDirs[$counter] );
  146.  
  147.     $currentDirpath =  "$root_path".$subDirs[$counter];
  148.  
  149.     while( $fn = @$d->read() )
  150.     {
  151.       if  ( filetype( $currentDirpath."/$fn" ) == "file" && preg_match( $sMask, $fn ) && $fn != $self_name)
  152.         {
  153.         if($lMask)
  154.         {
  155.             if(!preg_match( $lMask, $fn ))
  156.               $files[ $fn ] = filetype( $currentDirpath."/$fn" );
  157.         }
  158.         else
  159.               $files[ $fn ] = filetype( $currentDirpath."/$fn" );
  160.         }
  161.       else if(filetype( $currentDirpath."/$fn" ) == "dir" && $fn != "." && $fn != ".." && $subfolders == "1")
  162.       {
  163.         if(!in_array($subDirs[$counter]."$fn", $subDirs))
  164.         {
  165.             $add_dir = $subDirs[$counter]."/$fn";
  166.             $subDirs[] = str_replace( '//', '/', $add_dir);
  167.         }
  168.       }  
  169.     }
  170.  
  171.     if ($files)
  172.     {
  173.         while ( $f = each($files) )
  174.         {
  175.             $fn = $f[0];
  176.             if ( is_readable( $currentDirpath."/$fn" ) && preg_match( $sMask, $fn ) )
  177.             {
  178.             $e_temp = $counter;
  179.             $f_count++;
  180.  
  181.             $filedata = file($currentDirpath."/$fn");
  182.             $kloc += count($filedata);
  183.            
  184.                 $files_to_go .= "<li>$f[0], <b>".count($filedata)." lines</b></li>\n";
  185.             }
  186.         }
  187.     }
  188.     else
  189.     {
  190.         echo "<br><font face='Arial' color='#800000' size='2'>No files found in directory: <b>$currentDirpath</b></font><br>";
  191.       $f_count = -1;
  192.     }
  193.  
  194.     if(!$f_count)
  195.     {
  196.         echo "<br><font face='Arial' color='#800000' size='2'>No files found in directory: <b>$currentDirpath</b></font><br>";
  197.     }
  198.     else if($f_count > 0)
  199.     {
  200.        $tf_count += $f_count;
  201.  
  202.        if($subDirs[$counter] != "/")
  203.        echo("<p><br>Location: <b>$currentDirpath</b><p>$files_to_go</ul>\n");
  204.     }
  205.    
  206.    $counter++;
  207.    @$d->close();
  208.   }
  209.  
  210.   $mtime = microtime();
  211.   $mtime = explode(" ",$mtime);
  212.   $mtime = $mtime[1] + $mtime[0];
  213.   $endtime = $mtime;
  214.   $totaltime = ($endtime - $starttime);
  215.  
  216.   echo("<br><br>\n<a name='report'></a>");
  217.   echo("<b>Summary Count</b>\n");
  218.   echo("<ul>");
  219.   echo("<li>FILES :: $tf_count</li>");
  220.   echo("<li>DIRECTORIES :: ".(count($subDirs) - 1)."</li>");
  221.   echo("<li>L O C :: $kloc</li>");
  222.   echo("<li>K L O C :: ".($kloc/1000)."</li>");
  223.   echo("<li>TIME TAKEN :: $totaltime seconds</li>");
  224.   echo("</ul>");
  225.   echo(makeTemplate( '2' ));
  226.  
  227.  
  228.  
  229.  
  230. function makeTemplate( $place )
  231. {
  232.  
  233. $header_val =<<<__HTML_END_
  234.  
  235. <html>
  236. <head>
  237. <title>KLOC Calculator</title>
  238. <style type="text/css">
  239. <!--
  240. .ts { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; }
  241. .tn { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt; }
  242. -->
  243. </style>
  244. </head>
  245.  
  246. <body background="bg.gif">
  247. <table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
  248.   <tr>
  249.     <td>
  250.       <table width="95%" border="1" cellspacing="0" cellpadding="4" bordercolor="#000000" align="center" class="tn">
  251.         <tr bgcolor="#006699" align="right">
  252.           <td><font size="4" color="#FFFFFF">KLOC Calculator&nbsp;</font></td>
  253.         </tr>
  254.         <tr bgcolor="#EEEEEE">
  255.           <td>
  256.             <p>&nbsp;</p>
  257.  
  258. __HTML_END_;
  259.  
  260. $footer_val =<<<__HTML_END_
  261.  
  262.             </font>
  263.          <br>
  264.           </td>
  265.         </tr>
  266.       </table>
  267.     <br>
  268. <div align="center" class="ts">powered by: <a href="http://www.mgzhome.com/project.html#kloc" target=_blank>KLOC Calculator</a> &copy; copyright 2003, Mayank Gandhi</div></font>
  269. <br><br>
  270.  
  271. <table width="95%" border="0" cellspacing="0" cellpadding="2" align="center" class="ts">
  272.   <tr>
  273.     <td colspan="2" align="center" class="tn" bgcolor="#EEEEEE"><b>My Other featured scripts</b></td>
  274.   </tr>
  275.   <tr align="center" bgcolor="#FCFCFC">
  276.     <td width="50%"><br><a href="http://www.albinator.com/product/" target="_blank"><img src="albinator.gif" width="200" height="111" alt="Albinator" border="0"></a><br><br></td>
  277.     <td width="50%"><br><a href="http://www.mgzhome.com/scripts/extpwr/" target="_blank"><img src="extremepower.gif" width="140" height="160" alt="eXtreme power" border="0"></a><br><br></td>
  278.   </tr>
  279.   <tr align="center">
  280.     <td width="50%" bgcolor="#CCCCCC">advanced photo album/gallery system in over 8 languages</td>
  281.     <td width="50%" bgcolor="#DDDDDD">perfect solution 4 secure-login, virtual community features</td>
  282.   </tr>
  283. </table>
  284.  
  285.     </td>
  286.   </tr>
  287. </table>
  288. </body>
  289. </html>
  290.  
  291. __HTML_END_;
  292.  
  293.  
  294. if($place == '1')
  295. return ( $header_val );
  296.  
  297. else if($place == '2')
  298. return ( $footer_val );
  299. }
  300.  
  301. ?>
  302.  
  303.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement