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
|
<?php
namespace OCA\Checksum\Controller;
use OCP\AppFramework\Controller;
use OCP\IRequest;
use OC\Files\Filesystem;
use OCP\AppFramework\Http\JSONResponse;
class ChecksumController extends Controller {
protected $language;
public function __construct($appName, IRequest $request) {
parent::__construct($appName, $request);
// get i10n
$this->language = \OC::$server->getL10N('checksum');
}
/**
* callback function to get md5 hash of a file
* @NoAdminRequired
* @param (string) $source - filename
* @param (string) $type - hash algorithm type
*/
public function check($source, $type) {
if(!$this->checkAlgorithmType($type)) {
return new JSONResponse(
array(
'response' => 'error',
'msg' => $this->language->t('This is not a valid algorithm type.')
)
);
}
if($hash = $this->getHash($source, $type)){
return new JSONResponse(
array(
'response' => 'success',
'msg' => $hash
)
);
} else {
return new JSONResponse(
array(
'response' => 'error',
'msg' => $this->language->t('File not found.')
)
);
};
}
protected function getHash($source, $type) {
if($info = Filesystem::getLocalFile($source)) {
return hash_file($type, $info);
}
return false;
}
protected function checkAlgorithmType($type) {
return in_array($type, $this->getAllowedAlgorithmTypes());
}
protected function getAllowedAlgorithmTypes() {
return array(
'md5',
'sha1',
'sha256',
'sha512',
'crc32'
);
}
}
|