forked from Accusoft/edocr-userdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpEdocr.php
560 lines (503 loc) · 14.3 KB
/
phpEdocr.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/* phpEdocr Class 0.1.9
*
*/
require_once("library/class.curl.php");
class phpEdocr {
var $consumer_key;
var $consumer_secret;
var $token_key;
var $token_secret;
var $callback_url;
const REQUEST_TOKEN_ENDPOINT = "http://www.edocr.com/api/request_token";
const AUTHORIZE_ENDPOINT = "http://www.edocr.com/api/authorize";
const ACCESS_TOKEN_ENDPOINT = "http://www.edocr.com/api/access_token";
const CALL_API_METHOD_ENDPOINT = "http://www.edocr.com/api/echo_api";
const UPLOAD_DOCUMENT_ENDPOINT = "http://www.edocr.com/api/customer/upload";
function phpEdocr($consumer_key, $consumer_secret, $new_request=true, $require_auth = true ) {
// Initialize the consumer-key, consumer-secret, callback url. And check if an access-token already exist.
$this->consumer_key = $consumer_key;
$this->consumer_secret = $consumer_secret;
$this->callback_url = "http://edocr.com/userdocs/index.php?callbackid=" . session_id();
// Check if access-token has already been issued.
if($this->is_token_exist("access")) {
// Initialize the access-token, if one is already saved.
$accessToken = $this->get_access_token();
$this->token_key=$accessToken->key;
$this->token_secret=$accessToken->secret;
}
}
/**
* Call this function to start an authentication process.
* Sends a request for request-token, fetches a new request-token and redirect the page to edocr.com
* for authorizing the request token.
*
*/
public function new_auth_request() {
try {
// destroy the token if one exist already
$this->destroy_token();
$this->get_new_request_token();
}
catch (Exception $e) {
echo "Error : {$e->getMessage()}";
}
}
/**
* Call this function once the authorization is complete.
* Fetches an access-token once the request-token has been authorized.
*
*
*/
public function on_callback_after_authorize() {
try {
if(!$this->is_token_exist("access")) {
$requestToken = $this->get_request_token();
$accessToken = $this->get_new_access_token($requestToken);
}
else {
// Access Token found in session
$accessToken = $this->get_access_token();
}
$this->token_key=$accessToken->key;
$this->token_secret=$accessToken->secret;
}
catch (Exception $e) {
echo "Error : {$e->getMessage()}";
}
}
/**
* Obtain new request token
*
*/
private function get_new_request_token() {
$requestToken = $this->fetch_new_token();
}
/**
* Generate request-token request. Get request token response, process it. Save the token. Redirect
* the page to authorize the request-token
*
*/
private function fetch_new_token() {
$requestTokenResponse = $this->fetch_request_token();
$requestToken = $this->processResponse($requestTokenResponse);
$this->save_token("request",$requestToken->requestToken);
$this->authorize_request_token($requestToken->requestToken);
}
/**
* Process the JSON response
*
* @param string $response
* @return array
*/
public function processResponse($response) {
$resp_dec = json_decode($response);
if($resp_dec->status!="ok") {
// throw the error message.
throw new Exception($resp_dec->message);
}
return $resp_dec;
}
/**
* Generate new access-token request. Fetch and process response. Save token.
*
* @param array $requestToken
* @return array
*/
private function get_new_access_token($requestToken) {
$accessTokenResponse = $this->fetch_new_access_token($requestToken);
$accessToken = $this->processResponse($accessTokenResponse);
$this->save_token("access",$accessToken->accessToken);
return $accessToken->accessToken;
}
/**
* Build and call URL for fetching request-token
*
* @return string
*/
private function fetch_request_token() {
list($url,$params) = $this->build_request(
self::REQUEST_TOKEN_ENDPOINT ,
array(
"oauth_consumer_key" => $this->consumer_key,
),
array(
"token_secret" => null
)
);
$response = $this->get_response($url,$params);
return $response;
}
/**
* Build and redirect the page to URL for authorizing request-token
*
* @param array $requestToken
*/
private function authorize_request_token($requestToken) {
list($auth_url,$params) = $this->build_request(
self::AUTHORIZE_ENDPOINT ,
array(
"oauth_consumer_key" => $this->consumer_key,
"oauth_token" => $requestToken->key,
"oauth_callback" => $this->callback_url
),
array(
"consumer_secret" => $this->consumer_secret,
"token_secret" => $requestToken->secret
)
);
header("Location: " . $this->to_url($auth_url,$params));
}
/**
* Build and call URL for fetching access-token
*
* @param array $requestToken
* @return string
*/
private function fetch_new_access_token($requestToken) {
list($url,$params) = $this->build_request(
self::ACCESS_TOKEN_ENDPOINT ,
array(
"oauth_consumer_key" => $this->consumer_key,
"oauth_token" => $requestToken->key,
),
array(
"token_secret" => $requestToken->secret
)
);
$response = $this->get_response($url,$params);
return $response;
}
/**
* Call API method.
*
* @param string $method
* @param array $extra_params
* @param bool $require_auth
* @param string $http_method
* @return string
*/
public function call_api_method($method,$extra_params,$require_auth=true,$http_method) {
$req_params = array(
"oauth_consumer_key" => $this->consumer_key,
"method" => $method
);
if($require_auth==true) {
$req_params["oauth_token"] = $this->token_key;
}
$req_params = array_merge($req_params,$extra_params);
list($api_method_url,$params) = $this->build_request(
self::CALL_API_METHOD_ENDPOINT ,
$req_params,
array(
"consumer_secret" => $this->consumer_secret,
"token_secret" => (($require_auth==true)? $this->token_secret : "")
),
$http_method
);
$response = $this->get_response($api_method_url,$params,$http_method);
return $response;
}
/**
* Call this function to upload document.
*
* @param string $doc_path
* @param array $extra_params
* @return string
*/
public function upload_document($doc_path,$extra_params) {
$req_params = array(
"oauth_consumer_key" => $this->consumer_key,
"oauth_token" => $this->token_key
);
$req_params = array_merge($req_params,$extra_params);
list($upload_url,$params) = $this->build_request(
self::UPLOAD_DOCUMENT_ENDPOINT ,
$req_params,
array(
"consumer_secret" => $this->consumer_secret,
"token_secret" => $this->token_secret
),
"POST"
);
$response = $this->curl_upload($upload_url,$doc_path,$params);
return $response;
}
/**
* Get response from the passed URL
*
* @param string $url
* @param string $http_method
* @return string
*/
private function get_response($url,$params,$http_method="GET") {
return $this->curl_execute($url,$params,$http_method);
}
/**
* Send request to API call URL using cUrl. This function make use of the curl class.
*
* @param string $url
* @param string $http_method
* @return string
*/
private function curl_execute($url,$params,$http_method) {
if($http_method=="POST") {
$req = curl_init();
curl_setopt($req, CURLOPT_URL,$url);
curl_setopt($req, CURLOPT_TIMEOUT, 0);
curl_setopt($req, CURLOPT_POSTFIELDS, $params);
curl_setopt($req, CURLOPT_CONNECTTIMEOUT, 3600);
curl_setopt($req, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($req, CURLOPT_HEADER, 0);
curl_setopt($req, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec($req);
return $resp;
if (curl_errno($req)) {
echo (curl_error($req));
}
curl_close($req);
}
else {
$c = new curl($this->to_url($url,$params));
$c->setopt(CURLOPT_FOLLOWLOCATION, true) ;
return $c->exec() ;
if ($theError = $c->hasError()) {
throw new Exception($theError);
}
$c->close() ;
}
}
/**
* Send request to API call URL using cUrl for uploading documents.
*
* @param unknown_type $url
* @param unknown_type $doc_path
* @param unknown_type $params
* @return unknown
*/
private function curl_upload($url,$doc_path,$params) {
$params['document'] = "@".$doc_path;
$c = new curl($url) ;
$c->setopt(CURLOPT_FOLLOWLOCATION, 1) ;
$c->setopt(CURLOPT_POSTFIELDS, $params);
$c->setopt(CURLOPT_TIMEOUT, 0);
$c->setopt(CURLOPT_CONNECTTIMEOUT, 3600);
$c->setopt(CURLOPT_HEADER, 0);
$c->setopt(CURLOPT_RETURNTRANSFER, 1);
$res = $c->exec();
return $res;
if ($theError = $c->hasError()) {
throw new Exception($theError);
}
$c->close();
}
/**
* Get the saved request token.
*
* @return array
*/
private function get_request_token() {
if($this->is_token_exist("request")) {
$token = substr($_SESSION['token'],8);
return (unserialize($token));
}
else return false;
}
/**
* Get the saved access token (from session)
*
* @return array
*/
private function get_access_token() {
if($this->is_token_exist("access")) {
$token = substr($_SESSION['token'],7);
return (unserialize($token));
}
else return false;
}
/**
* Save the token (in session)
*
* @param string $type
* @param array $token
*/
public function save_token($type,$token) {
session_unregister("token");
$_SESSION['token'] = "{$type}_".serialize($token);
}
/**
* Destroy the token (from session)
*
*/
public function destroy_token() {
session_unregister("token");
unset($this->token_key);
unset($this->token_secret);
}
/**
* Check if the mentioned token type is saved.
*
* @param string $token_type
* @return bool
*/
public function is_token_exist($token_type) {
return (isset($_SESSION['token']) && substr($_SESSION['token'],0,strlen($token_type))==$token_type);
}
/**
* Build request and sign using the $sig_params
*
* @param string $http_url
* @param array $req_params
* @param array $sig_params
* @param string $http_method
* @return array
*/
private function build_request($http_url,$req_params, $sig_params,$http_method = "GET") {
$misc_req_params = $this->misc_req_params();
$req_params = array_merge($req_params,$misc_req_params);
$signature = $this->sign_request($http_url,$req_params,$sig_params,$http_method);
$req_params = array_merge($req_params,array("oauth_signature" => $signature));
// return url and the parameters
return array(
$http_url,
$req_params
);
}
/**
* All parameter names and values are escaped using the [RFC3986] percent-encoding (%xx) mechanism.
*
* @param unknown_type $http_url
* @param unknown_type $req_params
* @return unknown
*/
private function to_url($http_url,$req_params) {
return $this->get_normalized_http_url($http_url) . "?". $this->to_postdata($req_params);
}
/**
* Encode the parameters in the request
*
* @param array $req_params
* @return string
*/
private function to_postdata($req_params) {
$total = array();
foreach ($req_params as $k => $v) {
$total[] = $this->urlencodeRFC3986($k) . "=" . $this->urlencodeRFC3986($v);
}
$out = implode("&", $total);
return $out;
}
/**
* Miscellaneous parameters
*
* @return array
*/
private function misc_req_params() {
return array(
"oauth_timestamp" => time()
);
}
/**
* Sign the request using HMAC-SHA1
*
* @param string $http_url
* @param array $req_params
* @param array $sig_params
* @param string $http_method
* @return string
*/
private function sign_request($http_url,$req_params,$sig_params,$http_method) {
// base string to be signed
$base_string = $this->get_signature_base_string($http_url,$req_params,$http_method);
// key for signing the base string
$key_parts = array(
$this->consumer_secret,
($sig_params['token_secret']) ? $sig_params['token_secret'] : ""
);
$key = implode('&', $key_parts);
// signature method used is HMAC-SHA1
return base64_encode( hash_hmac('sha1', $base_string, $key, true));
}
/**
* Get the base string to generate the signature
*
* @param string $http_url
* @param array $req_params
* @param string $http_method
* @return string
*/
private function get_signature_base_string($http_url,$req_params,$http_method) {
$parts = array(
strtoupper($http_method),
$this->get_normalized_http_url($http_url),
$this->get_signable_parameters($req_params)
);
$parts = array_map(array('phpEdocr', 'urlencodeRFC3986'), $parts);
return implode('&', $parts);
}
/**
* Normalize the URL
*
* @param string $http_url
* @return string
*/
private function get_normalized_http_url($http_url) {
$parts = parse_url($http_url);
$port = @$parts['port'];
$scheme = $parts['scheme'];
$host = $parts['host'];
$path = @$parts['path'];
$port or $port = ($scheme == 'https') ? '443' : '80';
if (($scheme == 'https' && $port != '443')
|| ($scheme == 'http' && $port != '80')) {
$host = "$host:$port";
}
return "$scheme://$host$path";
}
/**
* Get the parameters to be signed.
*
* @param array $req_params
* @return string
*/
private function get_signable_parameters($req_params) {
// Grab all parameters
$params = $req_params;
// Remove oauth_signature if present
if (isset($params['oauth_signature'])) {
unset($params['oauth_signature']);
}
// Urlencode both keys and values
$keys = array_map(array('phpEdocr', 'urlencodeRFC3986'), array_keys($params));
$values = array_map(array('phpEdocr', 'urlencodeRFC3986'), array_values($params));
$params = array_combine($keys, $values);
// Sort by keys (natsort)
uksort($params, 'strnatcmp');
// Generate key=value pairs
$pairs = array();
foreach ($params as $key=>$value ) {
if (is_array($value)) {
// If the value is an array, it's because there are multiple
// with the same key, sort them, then add all the pairs
natsort($value);
foreach ($value as $v2) {
$pairs[] = $key . '=' . $v2;
}
} else {
$pairs[] = $key . '=' . $value;
}
}
// Return the pairs, concated with &
return implode('&', $pairs);
}
/**
* All parameter names and values are escaped using the [RFC3986] percent-encoding (%xx) mechanism.
*
* @param string $string
* @return string
*/
private static function urlencodeRFC3986($string) {
return str_replace('+', ' ',
str_replace('%7E', '~', rawurlencode($string)));
}
}