forked from sbs20/scanserv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.php
216 lines (170 loc) · 7.39 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
// Enable buffering
ob_start();
include_once("classes_php/Scanimage.php");
include_once("classes_php/ScanRequest.php");
include_once("classes_php/ScannerOptions.php");
include_once("classes_php/FileInfo.php");
// Write ajax header
//Content-Type: application/json; charset=utf-8
header('Content-Type: application/json');
//date_default_timezone_set('Europe/London');
class Api {
public static function HandleCmdlineRequest($request) {
$cmd = $request->data;
System::Execute($cmd, $output, $ret);
return array("cmd" => $cmd, "output" => $output, "ret" => $ret);
}
public static function HandlePreviewRequest($request) {
$wait=' -size 423x584 -fill white -background "#3C98E4" -pointsize 30 -gravity North label:"\nPlease wait..." ';
$cmd = Config::Convert.' '.$wait.' '.Config::PreviewDirectory.'preview.jpg';
System::Execute($cmd, $output, $ret);
$clientRequest = $request->data;
$scanRequest = new ScanRequest();
if (isset($clientRequest->mode)) $scanRequest->options["mode"] = $clientRequest->mode;
if (isset($clientRequest->brightness)) $scanRequest->options["brightness"] = (int)$clientRequest->brightness;
if (isset($clientRequest->contrast)) $scanRequest->options["contrast"] = (int)$clientRequest->contrast;
if (isset($clientRequest->source)) $scanRequest->options["source"] = $clientRequest->source;
if (isset($clientRequest->format)) {
$scanRequest->format = $clientRequest->format;
} else {
$scanRequest->format = Config::OutputExtension;
}
if (isset($clientRequest->device)) {
$scanRequest->device = $clientRequest->device;
$scanner = ScannerOptions::get($scanRequest->device);
$scannerOptions = $scanner["options"];
$scanRequest->options["resolution"] = array_key_exists("resolution",$scannerOptions) ? $scannerOptions["resolution"]->values[0] : 0;
}
$scanRequest->outputFilepath = Config::PreviewDirectory."preview.tif";
$scanner = new Scanimage();
$scanResponse = $scanner->Execute($scanRequest);
return $scanResponse;
}
public static function HandlePreviewToJpegRequest() {
$cmd = Config::PreviewFilter.' '.Config::PreviewDirectory.'preview.jpg <'.Config::PreviewDirectory.'preview.tif';
System::Execute($cmd, $output, $ret);
$jpg=file_get_contents(Config::PreviewDirectory.'preview.jpg');
return array("cmd" => $cmd, "output" => $output, "ret" => $ret, "jpg" => base64_encode($jpg) );
}
public static function HandleScanRequest($request) {
$clientRequest = $request->data;
$scanRequest = new ScanRequest();
if (isset($clientRequest->resolution)) $scanRequest->options["resolution"] = (int)$clientRequest->resolution;
if (isset($clientRequest->mode)) $scanRequest->options["mode"] = $clientRequest->mode;
if (isset($clientRequest->brightness)) $scanRequest->options["brightness"] = (int)$clientRequest->brightness;
if (isset($clientRequest->contrast)) $scanRequest->options["contrast"] = (int)$clientRequest->contrast;
if (isset($clientRequest->source)) $scanRequest->options["source"] = $clientRequest->source;
if (isset($clientRequest->depth)) $scanRequest->options["depth"] = (int)$clientRequest->depth;
if (isset($clientRequest->top)) $scanRequest->options["t"] = (int)$clientRequest->top;
if (isset($clientRequest->left)) $scanRequest->options["l"] = (int)$clientRequest->left;
if (isset($clientRequest->height)) $scanRequest->options["y"] = (int)$clientRequest->height;
if (isset($clientRequest->width)) $scanRequest->options["x"] = (int)$clientRequest->width;
if (isset($clientRequest->device))
$scanRequest->device = $clientRequest->device;
if (isset($clientRequest->format))
$scanRequest->format = $clientRequest->format;
else
$scanRequest->format = Config::OutputExtension;
$outputfile = Config::OutputDirectory . "Scan_" . time() . "." . $scanRequest->format;
$scanRequest->outputFilepath = $outputfile;
$scanRequest->outputFilter = Config::OutputFilter;
$scanner = new Scanimage();
$scanResponse = $scanner->Execute($scanRequest);
return $scanResponse;
}
public static function HandleFormatListRequest() {
if (!System::HasConvert()) return array(Format::OutputExtension);
$formats = array(Format::BMP,
Format::JPG,
Format::PDF,
Format::PNG,
Format::TIFF);
sort($formats);
return $formats;
}
public static function HandleOptionListRequest() {
return ScannerOptions::getAll();
}
public static function HandleFileListRequest() {
$files = array();
$outdir = Config::OutputDirectory;
foreach (new DirectoryIterator($outdir) as $fileinfo) {
if(!is_dir($outdir.$fileinfo) && preg_match('/^Scan_[0-9]*/',$fileinfo->getFilename())) {
$files[$fileinfo->getMTime()] = $fileinfo->getFilename();
}
}
krsort($files);
$dirArray = array_values($files);
$files = array();
foreach ($dirArray as $filepath) {
array_push($files, new FileInfo($outdir.$filepath));
}
return $files;
}
public static function HandleFileDeleteRequest($request) {
if (!isset($request->data)) return FALSE;
$fileInfo = new FileInfo($request->data);
return $fileInfo->Delete();
}
public static function HandleRefreshDevicesRequest() {
$devices = System::ScannerDevices();
file_put_contents(ScannerOptions::DEVICES_FILE, implode("\n", $devices));
$options = System::ScannerOptions();
file_put_contents(ScannerOptions::OPTIONS_FILE, implode("\n", $options));
return true;
}
public static function Main() {
if($_SERVER["REQUEST_METHOD"] == "POST") {
$input = file_get_contents('php://input');
$request = json_decode($input);
$responseType = $request->type;
switch ($request->type) {
case "scan":
$responseData = self::HandleScanRequest($request);
break;
case "preview":
$responseData = self::HandlePreviewRequest($request);
break;
case "fileList":
$responseData = self::HandleFileListRequest();
break;
case "fileDelete":
$responseData = self::HandleFileDeleteRequest($request);
break;
case "previewToJpeg":
$responseData = self::HandlePreviewToJpegRequest();
break;
case "cmdline":
// $responseData = self::HandleCmdlineRequest($request);
$responseData = "cmdline is disabled. If you wish to debug httpdusr permissions you will need to manually enable this in the source.";
break;
case "getFormats":
$responseData = self::HandleFormatListRequest();
break;
case "getOptions":
$responseData = self::HandleOptionListRequest();
break;
case "refreshDevices":
$responseData = self::HandleRefreshDevicesRequest();
break;
case "ping":
$responseData = "Pong@".date("Y-m-d H.i.s",time());
break;
default:
$responseType = "unknown";
$responseData = null;
break;
};
$response = array(
"type" => $responseType,
"data" => $responseData
);
echo json_encode($response);
} else {
echo "GET METHOD NOT SUPPORTED";
}
}
}
Api::Main();
?>