<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<title>Generated Image Table using PHP</title>
  <LINK REL="stylesheet"   HREF="/style/style_reformat.html">
  <META NAME-EQUIV="Pragma" CONTENT="no-cache">
  <META NAME="author"      CONTENT="Denice Deatrich">
  <META NAME="description" CONTENT="Generated Image Table via PHP">
  <META NAME="keywords"    CONTENT="PHP generator images table HTML">
<! FILE:    tabgen.html
    PURPOSE:    create an html image table from a list of image files
    AUTHOR:    D.C. Deatrich
    DATE:    Aug 2000
    CHANGES:    
-->
</head>
<body>
<H1>Generated Image Table using PHP</H1>
<HR>

<?php
  
/** Defines **/
  
define("HEADER", 1 );
  
define("TRAILER", 2 );
  
define("MAX_LINE_SZ", 512 ); // arbitrary max length of an ascii line
  
define("WEBTREE", "http://penguin.epfl.ch/" );
  
define("FROM", "mailto&#058;Denice&#046;Deatrich&#064;epfl.ch" );
  
define("RESULTS", "$DOCUMENT_ROOT/downloads" );
  
define("DOWNLOADS", "/downloads" ); // files older than 5 min rm-ed by cron

  /** Global Vars **/


  /**    Function : create a tmp pathname
   **    Returns:   pathname
   **/
  
function buildPath( $string ) {
    if( ! (
$tmp = getenv("TMPDIR")) ) {
      
$tmp = "/tmp";
    }
    
$tmpf = tempnam( $tmp, "F" );
    
$timestamp = microtime( );  // use # of usecs to randomize directory name
    
$arr = explode( " ", $timestamp );
    
$timestamp = $arr[1] . "_" . $arr[0];
    
$p = $tmpf . $timestamp;
    return
$p;
  }

  
/**    Function : insert an HTML header or trailer into document
   **    Returns:   n/a
   **/
  
function insertInto( $fd, $type ) {
    global
$background, $foreground, $HTTP_REFERER, $PHP_SELF,
     
$title, $url;
    
$background = "white"; $foreground = "black";
    if(
$type == HEADER ) {
      
$s = "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n";
      
fputs( $fd, $s, MAX_LINE_SZ );
      
$s = "<!-- Formatted via: " . $HTTP_REFERER . "\n source webtree: " .
       
"" . WEBTREE . "\n using script: " . $PHP_SELF . "\n " . FROM .
       
" -->\n";
      
fputs( $fd, $s, MAX_LINE_SZ );
      
$s = "<!-- generated on " . date("H:i d.m.y") . " -->\n";
      
fputs( $fd, $s, MAX_LINE_SZ );
      
$s = "<html>\n<head>\n";
      
fputs( $fd, $s, MAX_LINE_SZ );
      
$s = "<title>" . $title . "</title>\n</head>\n";
      
fputs( $fd, $s, MAX_LINE_SZ );
      
$s = "<body bgcolor=\"" . $background . "\"" . " text=\"" . $foreground
       
. "\">\n\n";
      
fputs( $fd, $s, MAX_LINE_SZ );
      
$s = "<!-- uncomment the following to add some text at the top -->\n";
      
fputs( $fd, $s, MAX_LINE_SZ );
      
$s = "<!-- COMMENT BEGINS\n";
      
fputs( $fd, $s, MAX_LINE_SZ );
      
$s = "<h1 align=\"center\">" . $title . "</h1>\n
       <p>Put a description here..</p>"
.
      
"\n COMMENT ENDS -->\n";
      
fputs( $fd, $s, MAX_LINE_SZ );
    }
    else {
      
$s = "\n<hr>";
      if(
strlen($url) > 0 ) {
        
$s = $s .
         
"\n<address><a href=\"" .$url. "\">" .$url. "</a></address>";
      }
      
fputs( $fd, $s, MAX_LINE_SZ );
      
$s = "\n Last modification time/date: " . date("H:i d.m.y")
      .
"</div></body>\n</html>\n";
      
fputs( $fd, $s, MAX_LINE_SZ );
    }
  }

  
/**    Function : getList
   **    Args:      $f        - name of file to work on
   **    Returns:   >0 - okay -- the # of files; 0 -empty file; -1 error
   **   Notes:     
   **/
  
function getList( $f ) {
    global
$list;
    
$res = -1;
    
$lineNo = 0;
    if( (
$src = fopen($f, "r")) ) {
      while(
$s = fgets($src, MAX_LINE_SZ) ) { // for each line
        
if( strlen( ($s = trim($s)) ) > 0 ) {
          
array_push( $list, trim($s) );
          
$lineNo++;
        }
      }
      
fclose( $src );
      
$res = ($lineNo > 0) ? $lineNo : 0;
    }
    else {
      
printf("Sorry, your input file <FONT COLOR=\"red\"><I>%s</I></FONT>
       cannot be opened"
, $f );
    }

    return
$res;
  }
// function getList ends

  /**    Function : output the string to file
   **    Args:      $fp  - ptr to open file
   **               $s   - string
   **/
  
function output( $fp, $s ) {
    
fwrite( $fp, $s, strlen($s) );
  }


  
/**    Function : generate the HTML file
   **    Args:      $f        - name of file to work on
   **               $realName - basename of file from user
   **    Returns:   path to reformatted file
   **   Notes:     a bit of a monster...
   **/
  
function htmlize( $f, $realName, $filetype ) {
    global
$list;
    
$res = "";
    
$p = RESULTS . "/" . basename($f);
    
$dir = $p;
    if(
mkdir($p, 0700) ) {
      
$p = $p . "/" . $realName . ".html";
      if( (
$dest = fopen($p, "w+")) ) {
        
insertInto( $dest, HEADER );    // insert HTML-header
        
$str = " <table cols=4 bgcolor= \"ivory\" width=100% border=1> ";
        
output( $dest, $str );
        
$numImg = count( $list );
        
$rem = $numImg%4;
        for(
$i=0; $i < $numImg; $i++ ) {
          
$s = $list[$i];
          
$pat = "([\t ]+)";
          
$words = split( $pat, $s );
          if(
count($words) > 1 ) {
            
$img = $words[0];
            
$alt = substr( $s, strlen($words[0])+1 );
          }
          else {
            
$img = $s;
            
$alt = "";
          }
          
$newrow = ($i%4 == 0) ? true : false;
          if(
$newrow ) {
            if(
$i != 0 ) {
              
$str = " \n</tr>";
              
output( $dest, $str );
            }
            
$str = " \n<tr>";
            
output( $dest, $str );
          }
          
$str = " <td width=100 height=100 align=\"center\">
           <a target=\"Full Image\" href=\""
. $img .
           
"\"><img width=100 height=100 src=\"" . $img . "\" alt=\"" .
           
$alt . "\"></a></td>\n";
          
output( $dest, $str );
        }
        if(
$rem != 0 ) {
          for(
$i=0; $i < (4 - $rem); $i++ ) {
            
$str = " <td width=100 height=100 align=\"center\">
             &nbsp;</td>\n"
;
            
output( $dest, $str );
          }
        }
        
$str = " </tr></table>\n";
        
output( $dest, $str );
        
insertInto( $dest, TRAILER );
        
fclose( $dest );
        
// finally, give web-path to file for download
        
$p = DOWNLOADS . "/" . basename($dir) . "/" . basename($p);
        
$res = $p;
      }
    }
    return
$res;
  }
// function htmlize ends

  /** 'main' starts **/

  // Vars:
  
$list = array();
  
$title = "Image Table";
  
$url = "";

  if(
is_array($HTTP_POST_VARS) ) { // then we got here normally with POST
    
while( list($key, $val) = each($HTTP_POST_VARS) ) {
      if(
strlen($val) > 0 ) {      // paranoid..
        
switch( $key ) {
        case
"page":
          
$title = trim( $page );
          break;
        case
"contact":
          
$url = trim( $contact );
          break;
        }
      }
    }
  }

  if(
$argv ) {
//    echo "DEBUG: Cmd line args $argv[0]<BR>";
  
}
  if(
file_exists($userfile) ) {
    
$f = buildPath( $userfile_name );  // generate tmp pathname
    
if( strlen($f) > 0 ) {
      if(
rename($userfile, $f) ) {  // rename the file to tmp name
        
$ar = "";
        
/** fork the 'file' command to provide funky info **/
        
$cmd = EscapeShellCmd( "/usr/bin/file -b $f" );
        
exec( $cmd, $ar, $ret );
        if(
$ret == 0 ) {
          
// need some logic to test for file type - ie: should be text-based
          
if( eregi("executable", $ar[0]) || eregi("object", $ar[0]) ||
           
eregi("relocatable", $ar[0]) || eregi("archive", $ar[0]) ||
           
eregi("data", $ar[0]) ) {
            
printf("Sorry, your input file <FONT COLOR=\"red\"><I>%s</I></FONT>
             does not seem to be a text file, but reports file type:
             <I>%s</I>"
, $userfile_name, $ar[0] );
          }
          else {
            
/** now process the filelist **/
            
if( ($res = getList($f)) <= 0 ) {
              
printf("Sorry, your input file <font color=\"red\"><i>%s</i>
              </font> has a problem: %s<BR>\n"
, $userfile_name, $res );
              exit( );
            }

            
/** now process the file && provide link for user **/
            
$html = htmlize( $f, $userfile_name, $ft );

            
/** output table with resulting data **/
            
$h = $REMOTE_ADDR . " (" . gethostbyaddr($REMOTE_ADDR) . ")";
            echo
"<table border=\"3\" width=\"100%\">\n";
            echo
"<tr><th bgcolor=\"#FFFF99\" valign=\"top\">Information</th>
             <th bgcolor=\"#FFFF99\" valign=\"top\">Result</th></tr>\n"
;
            echo
"<tr><td align=\"left\" bgcolor=\"#FFFFFF\"><B>File:</b></td>
             <td align=\"left\" bgcolor==\"#CDB5CD\">$userfile_name</td></tr>"
;
            echo
"<tr><td align=\"left\" bgcolor=\"#FFFFFF\">
             <B>Received from:</b></td>
             <td align=\"left\" bgcolor==\"#CDB5CD\">$h</TD></TR>"
;
            echo
"<tr><td align=\"left\" bgcolor=\"#FFFFFF\">
             <b>Formatted file:</b></td>
             <td align=\"left\" bgcolor==\"#CDB5CD\">
              <A href=\"$html\">"
.basename($html)."</A>&nbsp;(shift-click to download)</td></tr>";
            echo
"</table>\n";
          }
        }

        
/** cleanup! **/
/*
        if( unlink($f) ) {
          rmdir( dirname($f) );
        }
*/
      
}
    }
  }
  else {
    
printf("Sorry, your input file <FONT COLOR=\"red\"><I>%s</I></FONT>
     was invalid."
, $userfile_name );
  }
?>

<HR>
<address>
<FONT SIZE=-1><A HREF="MAILTO:Denice.Deatrich@epfl.ch">Denice Deatrich</A></FONT>
</address>
<!-- Created: Sat Oct  2 14:32:11 MEST 1999 -->
<!-- hhmts start -->
Last modified: Fri Aug 25 17:15:18 MEST 2000
<!-- hhmts end -->
</body>
</html>