82 lines
1.7 KiB
PHP
82 lines
1.7 KiB
PHP
|
<?php
|
||
|
namespace OCA\Printer\Controller;
|
||
|
|
||
|
use OCP\AppFramework\Controller;
|
||
|
use OCP\IRequest;
|
||
|
use OC\Files\Filesystem;
|
||
|
use OCP\AppFramework\Http\JSONResponse;
|
||
|
|
||
|
|
||
|
class PrinterController extends Controller {
|
||
|
|
||
|
protected $language;
|
||
|
|
||
|
public function __construct($appName, IRequest $request) {
|
||
|
|
||
|
parent::__construct($appName, $request);
|
||
|
|
||
|
// get i10n
|
||
|
$this->language = \OC::$server->getL10N('printer');
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* callback function to get md5 hash of a file
|
||
|
* @NoAdminRequired
|
||
|
* @param (string) $source - filename
|
||
|
* @param (string) $orientation - Orientation of printed file
|
||
|
*/
|
||
|
public function printfile($source, $orientation) {
|
||
|
if(!$this->checkAlgorithmType($type)) {
|
||
|
return new JSONResponse(
|
||
|
array(
|
||
|
'response' => 'error',
|
||
|
'msg' => $this->language->t('Print failed!')
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
if($hash = $this->getHash($source, $type)){
|
||
|
return new JSONResponse(
|
||
|
array(
|
||
|
'response' => 'success',
|
||
|
'msg' => $this->language->t('Print succeeded!')
|
||
|
)
|
||
|
);
|
||
|
} else {
|
||
|
return new JSONResponse(
|
||
|
array(
|
||
|
'response' => 'error',
|
||
|
'msg' => $this->language->t('File to print not found.')
|
||
|
)
|
||
|
);
|
||
|
};
|
||
|
|
||
|
}
|
||
|
|
||
|
protected function getHash($source, $type) {
|
||
|
|
||
|
if($info = Filesystem::getLocalFile($source)) {
|
||
|
return hash_file($type, $info);
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
protected function checkAlgorithmType($type) {
|
||
|
$list_algos = hash_algos();
|
||
|
return in_array($type, $this->getAllowedAlgorithmTypes()) && in_array($type, $list_algos);
|
||
|
}
|
||
|
|
||
|
protected function getAllowedAlgorithmTypes() {
|
||
|
return array(
|
||
|
'md5',
|
||
|
'sha1',
|
||
|
'sha256',
|
||
|
'sha384',
|
||
|
'sha512',
|
||
|
'crc32'
|
||
|
);
|
||
|
}
|
||
|
}
|