Example #1
0
 $zoomdown = $zoom + 1;
 $n = 1;
 $file = $_GET['filename'] . $n . ".png";
 if (SPLIT_SCANNING) {
     while (file_exists($file)) {
         $n++;
         $file = $_GET['filename'] . $n . ".png";
     }
     $filecount = $n - 1;
     $n = 1;
     while ($n <= $filecount) {
         $file = $_GET['filename'] . $n . ".png";
         //split all the files
         $data = file_get_contents($file);
         $image = imagecreatefromstring($data);
         $images = split_scanning($image);
         if (count($images) == 2) {
             imagepng($images[0], $_GET['filename'] . $n . ".png");
             imagepng($images[1], $_GET['filename'] . ($n + $filecount) . ".png");
         }
         $n++;
     }
 }
 $n = 1;
 $file = $_GET['filename'] . $n . ".png";
 $pages = array();
 while (file_exists($file)) {
     $page = array();
     $page['pid'] = $n;
     $page['filename'] = $_GET['filename'];
     $pages[] = $page;
Example #2
0
function import($filename, $description = false)
{
    global $db;
    set_time_limit(240);
    $filehash = sha1_file($filename);
    //First check if this file can be imported
    $sql = "SELECT pfid,allowanother\r\n\t\tFROM processforms\r\n\t\tWHERE filehash = '{$filehash}'\r\n\t\tOR filepath = " . $db->qstr($filename);
    $pf = $db->GetAll($sql);
    $pfid = false;
    if (count($pf) >= 1) {
        if ($pf[0]['allowanother'] == 1) {
            //update record instead of creating new one
            $pfid = $pf[0]['pfid'];
        } else {
            return false;
        }
        //this form has already been processed
    }
    //Import the file
    print T_("Importing") . ": {$filename}";
    if (!$description) {
        $description = $filename;
    }
    //START TRANSACTION:
    // Don't use "StartTrans and CompleteTrans"
    // as we want to use it only for stopping the form committing half way
    // not monitoring all SQL statements for errors
    $db->BeginTrans();
    //count of missing pages
    $missingpagecount = 0;
    //generate temp file
    $tmp = tempnam(TEMPORARY_DIRECTORY, "FORM");
    //use ghostscript to convert to individual PNG pages
    exec(GS_BIN . " -sDEVICE=pngmono -r300 -sOutputFile=\"{$tmp}\"%d.png -dNOPAUSE -dBATCH \"{$filename}\"");
    //$qid = 1;
    $qid = "";
    $fid = "";
    //find the qid
    $n = 1;
    $file = $tmp . $n . ".png";
    while (file_exists($file)) {
        print T_("Finding qid") . "...";
        //open file
        $data = file_get_contents($file);
        $image = imagecreatefromstring($data);
        unset($data);
        $images = split_scanning($image);
        foreach ($images as $image) {
            $width = imagesx($image);
            $height = imagesy($image);
            $btlx = floor(BARCODE_TLX_PORTION * $width);
            if ($btlx <= 0) {
                $btlx = 1;
            }
            $btly = floor(BARCODE_TLY_PORTION * $height);
            if ($btly <= 0) {
                $btly = 1;
            }
            $bbrx = floor(BARCODE_BRX_PORTION * $width);
            if ($bbrx <= 0) {
                $bbrx = 1;
            }
            $bbry = floor(BARCODE_BRY_PORTION * $height);
            if ($bbry <= 0) {
                $bbry = 1;
            }
            $barcode = crop($image, array("tlx" => $btlx, "tly" => $btly, "brx" => $bbrx, "bry" => $bbry));
            //check for barcode
            $pid = barcode($barcode, 1, BARCODE_LENGTH_PID);
            //if failed try second location
            if (!$pid) {
                $btlx = floor(BARCODE_TLX_PORTION2 * $width);
                if ($btlx <= 0) {
                    $btlx = 1;
                }
                $btly = floor(BARCODE_TLY_PORTION2 * $height);
                if ($btly <= 0) {
                    $btly = 1;
                }
                $bbrx = floor(BARCODE_BRX_PORTION2 * $width);
                if ($bbrx <= 0) {
                    $bbrx = 1;
                }
                $bbry = floor(BARCODE_BRY_PORTION2 * $height);
                if ($bbry <= 0) {
                    $bbry = 1;
                }
                $barcode = crop($image, array("tlx" => $btlx, "tly" => $btly, "brx" => $bbrx, "bry" => $bbry));
                //check for barcode
                $pid = barcode($barcode, 1, BARCODE_LENGTH_PID2);
            }
            if ($pid) {
                //print "BARCODE: $pid<br/>";
                //get the page id from the page table
                $sql = "SELECT qid FROM pages\r\n\t\t\t\t\tWHERE pidentifierval = '{$pid}'";
                $page = $db->GetRow($sql);
                if (isset($page['qid'])) {
                    $qid = $page['qid'];
                    break 2;
                }
            }
            unset($image);
            unset($barcode);
        }
        unset($images);
        $n++;
        $file = $tmp . $n . ".png";
    }
    if ($qid != "") {
        print T_("Got qid") . ": {$qid}...";
        //create form entry in DB
        $sql = "INSERT INTO forms (fid,qid,description)\r\n\t\t\tVALUES (NULL,'{$qid}','{$description}')";
        $db->Execute($sql);
        $fid = $db->Insert_Id();
        //process each page
        $n = 1;
        $file = $tmp . $n . ".png";
        while (file_exists($file)) {
            //open file
            $data = file_get_contents($file);
            $image = imagecreatefromstring($data);
            $images = split_scanning($image);
            unset($data);
            unset($image);
            foreach ($images as $image) {
                //get the data from the image
                ob_start();
                imagepng($image);
                $data = ob_get_contents();
                ob_end_clean();
                $width = imagesx($image);
                $height = imagesy($image);
                $btlx = floor(BARCODE_TLX_PORTION * $width);
                if ($btlx <= 0) {
                    $btlx = 1;
                }
                $btly = floor(BARCODE_TLY_PORTION * $height);
                if ($btly <= 0) {
                    $btly = 1;
                }
                $bbrx = floor(BARCODE_BRX_PORTION * $width);
                if ($bbrx <= 0) {
                    $bbrx = 1;
                }
                $bbry = floor(BARCODE_BRY_PORTION * $height);
                if ($bbry <= 0) {
                    $bbry = 1;
                }
                //check for barcode
                $barcode = crop($image, array("tlx" => $btlx, "tly" => $btly, "brx" => $bbrx, "bry" => $bbry));
                $pid = barcode($barcode, 1, BARCODE_LENGTH_PID);
                //if failed try second location
                if (!$pid) {
                    $btlx = floor(BARCODE_TLX_PORTION2 * $width);
                    if ($btlx <= 0) {
                        $btlx = 1;
                    }
                    $btly = floor(BARCODE_TLY_PORTION2 * $height);
                    if ($btly <= 0) {
                        $btly = 1;
                    }
                    $bbrx = floor(BARCODE_BRX_PORTION2 * $width);
                    if ($bbrx <= 0) {
                        $bbrx = 1;
                    }
                    $bbry = floor(BARCODE_BRY_PORTION2 * $height);
                    if ($bbry <= 0) {
                        $bbry = 1;
                    }
                    $barcode = crop($image, array("tlx" => $btlx, "tly" => $btly, "brx" => $bbrx, "bry" => $bbry));
                    //check for barcode
                    $pid = barcode($barcode, 1, BARCODE_LENGTH_PID2);
                }
                if ($pid) {
                    print T_("Processing pid") . ": {$pid}...";
                    //get the page id from the page table
                    $sql = "SELECT * FROM pages\r\n\t\t\t\t\t\tWHERE pidentifierval = '{$pid}'\r\n\t\t\t\t\t\tAND qid = '{$qid}'";
                    $page = $db->GetRow($sql);
                    if (empty($page)) {
                        print T_("Pid not identified for this page, inserting into missing pages...");
                        //store in missing pages table
                        $sql = "INSERT INTO missingpages\r\n\t\t\t\t\t\t\t(mpid,fid,image)\r\n\t\t\t\t\t\t\tVALUES (NULL,'{$fid}','" . addslashes($data) . "')";
                        $db->Execute($sql);
                        $missingpagecount++;
                    } else {
                        if ($page['store'] == 1) {
                            //check if page setup is being used otherwise replace with
                            //defaultpageboxes
                            if ($page['usepagesetup'] == 0) {
                                $page = array_merge($page, defaultpageboxes($width, $height));
                            }
                            //calc transforms
                            $transforms = detecttransforms($image, $page);
                            $imagedata = '';
                            $imagefilename = '';
                            if (IMAGES_IN_DATABASE) {
                                $imagedata = addslashes($data);
                            } else {
                                $imagefilename = $fid . "-" . $page['pid'] . ".png";
                                imagepng($image, IMAGES_DIRECTORY . $imagefilename);
                            }
                            //save image to db including offset
                            $sql = "INSERT INTO formpages\r\n\t\t\t\t\t\t\t\t(fid,pid,filename,image";
                            foreach ($transforms as $key => $val) {
                                $sql .= ",{$key}";
                            }
                            $sql .= ")\r\n\t\t\t\t\t\t\t\tVALUES ('{$fid}','{$page["pid"]}','{$imagefilename}','" . $imagedata . "'";
                            foreach ($transforms as $key => $val) {
                                $sql .= ",'{$val}'";
                            }
                            $sql .= ")";
                            $db->Execute($sql);
                        }
                        if ($page['process'] == 1) {
                            //process variables on this page
                            processpage($page["pid"], $fid, $image, $transforms, $qid);
                        }
                    }
                } else {
                    $width = imagesx($image) - 1;
                    $height = imagesy($image) - 1;
                    if (BLANK_PAGE_DETECTION && is_blank_page($image, defaultpage($width, $height))) {
                        print T_("Blank page: ignoring");
                        //let this page dissolve into the ether
                    } else {
                        print T_("Could not get pid, inserting into missing pages...");
                        //store in missing pages table
                        $sql = "INSERT INTO missingpages\r\n\t\t\t\t\t\t\t(mpid,fid,image)\r\n\t\t\t\t\t\t\tVALUES (NULL,'{$fid}','" . addslashes($data) . "')";
                        $db->Execute($sql);
                        $missingpagecount++;
                    }
                }
                unset($data);
                unset($image);
                unset($imagedata);
                unset($barcode);
            }
            $n++;
            $file = $tmp . $n . ".png";
            //unset data
            unset($images);
        }
        //Update or insert record in to processforms log database
        if ($pfid == false) {
            //insert a new record as no existing for this form
            $sql = "INSERT INTO processforms (pfid,filepath,filehash,date,status,allowanother)\r\n\t\t\t\tVALUES (NULL,'{$filename}','{$filehash}',NOW(),1,0)";
            $db->Execute($sql);
            $pfid = $db->Insert_ID();
        } else {
            //update exisiting record
            $sql = "UPDATE processforms\r\n\t\t\t\tSET date = NOW(),\r\n\t\t\t\tfilepath = '{$filename}',\r\n\t\t\t\tfilehash = '{$filehash}',\r\n\t\t\t\tstatus = 1,\r\n\t\t\t\tallowanother = 0\r\n\t\t\t\tWHERE pfid = '{$pfid}'";
            $db->Execute($sql);
        }
        //Update form table with pfid
        $sql = "UPDATE forms\r\n\t\t\tSET pfid = '{$pfid}'\r\n\t\t\tWHERE fid = '{$fid}'";
        $db->Execute($sql);
    } else {
        //form could not be identified...
        //do nothing?
        print T_("Could not get qid...");
        //Update or insert record in to processforms log database
        if ($pfid == false) {
            //insert a new record as no existing for this form
            $sql = "INSERT INTO processforms (pfid,filepath,filehash,date,status,allowanother)\r\n\t\t\t\tVALUES (NULL,'{$filename}','{$filehash}',NOW(),2,0)";
            $db->Execute($sql);
        } else {
            //update exisiting record
            $sql = "UPDATE processforms\r\n\t\t\t\tSET date = NOW(),\r\n\t\t\t\tfilepath = '{$filename}',\r\n\t\t\t\tfilehash = '{$filehash}',\r\n\t\t\t\tstatus = 2,\r\n\t\t\t\tallowanother = 0\r\n\t\t\t\tWHERE pfid = '{$pfid}'";
            $db->Execute($sql);
        }
    }
    //Delete temporary pages
    $n = 1;
    $file = $tmp . $n . ".png";
    while (file_exists($file)) {
        //delete temp file
        unlink($file);
        $n++;
        $file = $tmp . $n . ".png";
    }
    //If only one page is missing, and one page in the missing pages database,
    //assume this is the missing page and process it.
    if (isset($fid)) {
        $sql = "SELECT mpid, mp.image as mpimage, p.*\r\n\t\t\tFROM forms AS f, pages AS p\r\n\t\t\tLEFT JOIN formpages AS fp ON (fp.fid = '{$fid}' and fp.pid = p.pid )\r\n\t\t\tLEFT JOIN missingpages as mp ON (mp.fid = '{$fid}')\r\n\t\t\tWHERE f.fid = '{$fid}'\r\n\t\t\tAND p.qid = f.qid\r\n\t\t\tAND fp.pid IS NULL\r\n\t\t\tAND mp.image is NOT NULL";
        $rs = $db->GetAll($sql);
        if (count($rs) == 1) {
            //There is one page in the missing database and one page missing from the form
            $row = $rs[0];
            print T_("Automatically processing the 1 missing page for this form - assuming pid:") . " {$row['pid']} - {$row['pidentifierval']}";
            $mpid = $row['mpid'];
            $image = imagecreatefromstring($row['mpimage']);
            if ($row['store'] == 1) {
                //check if page setup is being used otherwise replace with
                //defaultpageboxes
                if ($row['usepagesetup'] == 0) {
                    $row = array_merge($row, defaultpageboxes($width, $height));
                }
                //calc transforms
                $transforms = detecttransforms($image, $row);
                //save image to db including offset
                $sql = "INSERT INTO formpages\r\n\t\t\t\t\t(fid,pid,filename,image";
                foreach ($transforms as $key => $val) {
                    $sql .= ",{$key}";
                }
                $sql .= ")\r\n\t\t\t\t\tVALUES ('{$fid}','{$row["pid"]}','','" . addslashes($row['mpimage']) . "'";
                foreach ($transforms as $key => $val) {
                    $sql .= ",'{$val}'";
                }
                $sql .= ")";
                $db->Execute($sql);
            }
            if ($row['process'] == 1) {
                //process variables on this page
                processpage($row["pid"], $fid, $image, $transforms, $qid);
            }
            $sql = "DELETE \r\n\t\t\t\tFROM missingpages\r\n\t\t\t\tWHERE mpid = '{$mpid}'";
            $db->Execute($sql);
        }
        //if all pages have been entered and dected, and there are missing pages - delete them
        if ($missingpagecount > 0) {
            $sql = "SELECT count(*) AS c\r\n\t\t\t\tFROM forms AS f, pages AS p\r\n\t\t\t\tLEFT JOIN formpages AS fp ON ( fp.fid = '{$fid}' AND fp.pid = p.pid )\r\n\t\t\t\tWHERE f.fid = '{$fid}'\r\n\t\t\t\tAND p.qid = f.qid\r\n\t\t\t\tAND fp.pid IS NULL";
            $rs = $db->GetRow($sql);
            if (isset($rs['c']) && $rs['c'] == 0) {
                //there are missing pages in the mp table, but no missing pages in the form table...
                $sql = "DELETE \r\n\t\t\t\t\tFROM missingpages\r\n\t\t\t\t\tWHERE fid = '{$fid}'";
                $db->Execute($sql);
                print T_("Deleting missing pages as all form page slots filled");
            }
        }
    }
    //complete transaction
    $db->CommitTrans();
    return true;
}