<?php

require_once("template.php");
require_once(
"db.php");

$gl_max 100;

function 
countfiles($dir)
{
  
$handle opendir($dir);
  
$count 0;
  while (
$file readdir($handle))
    if (
$file != '.' && $file != '..')
      
$count $count 1;
  
closedir($handle);
  return 
$count;     
}

function 
ocrImage($img$dir "")
{
  
$xp explode("."$img);
  
$base $xp[0];
  
//  print $base."<br />";

  
if ($dir != "")
    
$base $dir."/".$base;


  
$cmd1 "convert ".escapeshellarg($base.'.'.$xp[1])." ".$base.".tif";
  
$cmd2 "tesseract ".escapeshellarg($base).".tif ".$base;
  
/* print "cmd1 = ".$cmd1."<br />"; */
  /* print "cmd2 = ".$cmd2."<br />"; */
  
$h exec($cmd1);
  
$h exec($cmd2);
  
/* print file_get_contents($base.".txt"); */


  //  print "base = ".$base."<br />";
  
$res =  trim(@file_get_contents($base.".txt"));
  @
unlink($base.".tif");
  @
unlink($base.".txt");
  return 
$res;
}

function 
oldestfile($dir)
{
  
$handle opendir($dir);
  
$oldest "";
  
$min 0;

  while (
$file readdir($handle))
    {
      if (
$file != "." && $file != "..")
    {
      
$md filemtime($dir."/".$file);
      if (
$min == || $md $min)
        {
          
$oldest $file;
          
$min $md;
        }
    }
    }
  
closedir($handle);
  return 
$oldest;
}

function 
checkname($file)
{
  
/* print "file = ".$file."<br />"; */
  /* print "strlen = ".strlen($file)."<br />"; */
  /* print "1=".substr_count($file, ".")."<br />"; */
  /* print "2=".strtolower(substr($file, strlen($file) - 4))."<br />"; */
  /* print "3=".strtolower(substr($file, strlen($file) - 5))."<br />"; */

  
if (strlen($file) <= ||
      
substr_count($file".") != ||
      
substr_count($file"/") != ||
      (
strtolower(substr($filestrlen($file) - 4)) != ".jpg" &&
       
strtolower(substr($filestrlen($file) - 5)) != ".jpeg"))
    return 
False;
  return 
True;
}

function 
uploadfile($file)
{
  global 
$gl_max;
  
$updir "/var/www/web200/upload";
  
$final $updir."/".basename($file);
  
$count countfiles($updir);
  if (
$count >= $gl_max)
    @
unlink($updir."/".oldestfile($updir));
  
  if (!
checkname($_FILES['img']['name']))
    return 
1;
  else
    {
      
//      print $final;
      
@unlink($final);
      if (
move_uploaded_file($_FILES['img']['tmp_name'],
                 
$final))
    return 
0;
    }
  return -
1;
}

function 
get_delit($regno)
{
  print 
"SELECT comment from delit WHERE regno='".$regno."';";
  
$req = @mysql_query("SELECT comment from delit WHERE regno='".$regno."';");

  
$res = array();
  while (
$data = @mysql_fetch_assoc($req))
    
$res[] = $data["comment"];
  return 
$res;
}

if (isset(
$_POST['name']) &&
    isset(
$_FILES['img']))
  {
    
$img $_FILES['img']['name'];
    
//    print "img = ".$img."<br />";
    
$ret uploadfile($img);
    if (
$ret == -1)
      print (
"Error Uploading the file");
    else if (
$ret != 0)
      print 
"Incorrect file";
    else
      {
    
//    print "Upload OK";
    
$regno ocrimage($img"/var/www/web200/upload");
    
//    print $regno;
    //    print "regno=".$regno."<br />";
    
$res get_delit($regno);
    
add_table_content("tpl/table.html.tpl"$res);
      }
  }
else
  print 
"No file uploaded";

?>