<?php

$preview = "Preview_" . substr($file, 0, -3) . "jpg";
if (isset($_POST['file-text'])) {
    // 1_Mar_8_2012~11-22-41.txt  1_Mar_8_2012~11-22-41-edit-42.txt
    $edit = strpos($file, '-edit-');
    $name = is_bool($edit) ? substr($file, 0, -4) : substr($file, 0, $edit);
    $int = 1;
    while (file_exists("scans/thumb/Preview_{$name}-edit-{$int}.jpg")) {
        $int++;
    }
    copy("scans/thumb/{$preview}", "scans/Preview_{$name}-edit-{$int}.jpg");
    if (SaveFile("scans/file/Scan_{$name}-edit-{$int}.txt", $_POST['file-text'])) {
        Print_Message("Saved", "You have successfully edited {$file}", 'center');
        $file = "{$name}-edit-{$int}.txt";
    }
}
echo "<div class=\"box box-full\" id=\"text-editor\"><div id=\"preview_links\"></div>" . "<img src=\"scans/thumb/{$preview}\"><br/>" . '<form action="index.php?page=Edit&file=' . $file . '" method="POST"><textarea name="file-text">' . html(file_get_contents("scans/file/Scan_{$file}")) . "</textarea><br/>" . '<input value="Save" type="submit"/><input type="button" value="Cancel" onclick="history.go(-1);"/></forum></div>';
Update_Links("Scan_{$file}", $PAGE);
                                 }
                                 @rmdir($CANDIR);
                                 $endTime = time();
                                 # Remove Crop Option / set lastScan
                                 if ($WIDTH !== 0 && $HEIGHT !== 0 || $ROTATE !== 0) {
                                     $strip = true;
                                 } else {
                                     setcookie('lastScan', json_encode(array("raw" => $S_FILENAME, "preview" => $P_FILENAME, "fields" => array("scanner" => $SCANNER, "source" => $SOURCE, "quality" => $QUALITY, "duplex" => $DUPLEX, "size" => $SIZE, "ornt" => $ORNT, "mode" => $MODE, "bright" => $BRIGHT, "contrast" => $CONTRAST, "scale" => $SCALE, "filetype" => $FILETYPE, "lang" => $LANG, "set_save" => $SET_SAVE))), time() + 86400, substr($_SERVER['PHP_SELF'], 0, strlen(end(explode('/', $_SERVER['PHP_SELF']))) * -1), $_SERVER['SERVER_NAME']);
                                 }
                                 $ORNT = $ORNT == '' ? 'vert' : $ORNT;
                                 echo "<script type=\"text/javascript\">var ornt=document.createElement('input');ornt.name='ornt0';ornt.value='{$ORNT}';ornt.type='hidden';document.scanning.appendChild(ornt);" . ($ROTATE != "0" ? "var p=document.createElement('p');p.innerHTML='<small>Changing orientation will void select region.</small>';getID('opt').appendChild(p);" : '') . "\$(document).ready(function(){document.scanning.scanner.disabled=true;" . (isset($strip) ? "stripSelect();" : '') . "});</script>";
                                 # Check if image is empty and post error, otherwise post image to page
                                 if (!file_exists("scans/thumb/{$P_FILENAME}")) {
                                     Print_Message("Could not scan", '<p style="text-align:left;margin:0;">This is can be cauesed by one or more of the following:</p>' . '<ul><li>The scanner is not on.</li><li>The scanner is not connected to the computer.</li>' . '<li>You need to run the <a href="index.php?page=Access%20Enabler">Access Enabler</a>.</li>' . (file_exists("/tmp/scan_file{$SCANNER}.{$RAW}") ? "<li>Removing <code>/tmp/scan_file{$SCANNER}.{$RAW}</code> may help.</li>" : '') . '<li><code>' . $user . '</code> does not have permission to write files to the <code>' . getcwd() . '/scans</code> folder.</li>' . '<li>You may have to <a href="index.php?page=Config">re-configure</a> the scanner.</li></ul>' . $notes, 'left');
                                 } else {
                                     Update_Links($S_FILENAME, $PAGE);
                                     Update_Preview("scans/thumb/{$P_FILENAME}");
                                 }
                                 echo '<script type="text/javascript">if(document.scanning.scanner.childNodes.length>1)document.scanning.reset.disabled=true;</script>';
                                 if ($ct > 3) {
                                     Print_Message("Info", 'Multiple scans made, only displaying last one, go to <a href="index.php?page=Scans&amp;filter=3&amp;T2=' . ($startTime - 1) . '&T1=' . ($endTime + 1) . '">Scanned Files</a> for the rest', 'center');
                                 }
                             }
                             checkFreeSpace($FreeSpaceWarn);
                         }
                     }
                 }
             }
         }
     }
 }