Example #1
0
function definetomap($zoom, $pid, $filename)
{
    $tb = array('t', 'b');
    $lr = array('l', 'r');
    $vh = array('vert', 'hori');
    $el = array('tlx', 'tly', 'brx', 'bry');
    $image = imagecreatefrompng($filename . $pid . ".png");
    $width = imagesx($image);
    $height = imagesy($image);
    $page = defaultpage($width - 1, $height - 1);
    $offset = offset($image, false, 0, $page);
    //draw lines of corner edges
    $vert = true;
    $linewidth = 8;
    $lc = 0;
    foreach ($offset as $coord) {
        if ($vert == true) {
            $top = 0;
            if ($lc > 3) {
                $top = $height / $zoom - $height / 4 / $zoom;
            }
            //drawing a vertical line so use $coord as $x
            print "<div style='position: absolute; top:" . $top . "px; left:" . $coord / $zoom . "px; width:" . $linewidth / $zoom . "px; height:" . $height / 4 / $zoom . "px; background-color: blue;'></div>";
            $vert = false;
        } else {
            //drawing a horizontal line so use $coord as $y
            $left = 0;
            if ($lc == 3 || $lc == 7) {
                $left = $width / $zoom - $width / 4 / $zoom;
            }
            print "<div style='position: absolute; top:" . $coord / $zoom . "px; left:" . $left . "px; width:" . $width / 4 / $zoom . "px; height:" . $linewidth / $zoom . "px; background-color: blue;'></div>";
            $vert = true;
        }
        $lc++;
    }
    foreach ($tb as $a) {
        foreach ($lr as $b) {
            foreach ($vh as $c) {
                $vname = "{$a}{$b}" . "_" . $c . "_";
                $tlx = $page[strtoupper($vname . "tlx")];
                $tly = $page[strtoupper($vname . "tly")];
                $brx = $page[strtoupper($vname . "brx")];
                $bry = $page[strtoupper($vname . "bry")];
                print "<div id='{$vname}' style='position: absolute; top:" . $tly / $zoom . "px; left: " . $tlx / $zoom . "px; width:" . ($brx - $tlx) / $zoom . "px; height:" . ($bry - $tly) / $zoom . "px; background-color: green; opacity: 0.6;' class='drsElement'><div class='drsMoveHandle'>" . $vname . "</div></div>";
            }
        }
    }
    $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;
    }
    $barcodeimage = crop($image, array("tlx" => $btlx, "tly" => $btly, "brx" => $bbrx, "bry" => $bbry));
    $barcode = barcode($barcodeimage);
    if ($barcode === false) {
        $barcode = T_("NO BARCODE DETECTED");
    } else {
        if (strlen($barcode) != BARCODE_LENGTH_PID) {
            $barcode = T_("Detected but not BARCODE_LENGTH_PID length") . ": " . $barcode;
        } else {
            $barcode = T_("Detected") . ": " . $barcode;
        }
    }
    print "<div id='barcodebox'  style='position: absolute; top:" . $btly / $zoom . "px; left: " . $btlx / $zoom . "px; width:" . ($bbrx - $btlx) / $zoom . "px; height:" . ($bbry - $btly) / $zoom . "px; background-color: brown; opacity: 0.6;' class='drsElement'><div class='drsMoveHandle'>{$barcode}</div></div>";
}
Example #2
0
        BootstrapSkin::displayInternalFooter();
        die;
    }
    global $baseurl;
    $currentUser = User::getCurrent();
    $currentUser->detachAccount();
    header("Location: {$baseurl}/acc.php?action=logout");
} elseif ($action == "oauthattach") {
    $database = gGetDb();
    $database->transactionally(function () use($database) {
        try {
            global $oauthConsumerToken, $oauthSecretToken, $oauthBaseUrl, $oauthBaseUrlInternal;
            $user = User::getCurrent();
            // Get a request token for OAuth
            $util = new OAuthUtility($oauthConsumerToken, $oauthSecretToken, $oauthBaseUrl, $oauthBaseUrlInternal);
            $requestToken = $util->getRequestToken();
            // save the request token for later
            $user->setOAuthRequestToken($requestToken->key);
            $user->setOAuthRequestSecret($requestToken->secret);
            $user->save();
            $redirectUrl = $util->getAuthoriseUrl($requestToken);
            header("Location: {$redirectUrl}");
        } catch (Exception $ex) {
            throw new TransactionException($ex->getMessage(), "Connection to Wikipedia failed.", "alert-error", 0, $ex);
        }
    });
} else {
    echo defaultpage();
    BootstrapSkin::displayInternalFooter();
    die;
}
Example #3
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;
}