1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2015 Victor Dubiniuk <[email protected]> |
4
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
5
|
|
|
* later. |
6
|
|
|
* See the COPYING-README file. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OCA\Files_Antivirus\Controller; |
10
|
|
|
|
11
|
|
|
use \OCP\AppFramework\Controller; |
12
|
|
|
use \OCP\IRequest; |
13
|
|
|
use \OCP\IL10N; |
14
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
15
|
|
|
|
16
|
|
|
use \OCA\Files_Antivirus\Db\Rule; |
17
|
|
|
use \OCA\Files_Antivirus\Db\RuleMapper; |
18
|
|
|
|
19
|
|
|
class RuleController extends Controller { |
20
|
|
|
|
21
|
|
|
private $logger; |
22
|
|
|
private $l10n; |
23
|
|
|
private $ruleMapper; |
24
|
|
|
|
25
|
|
|
public function __construct($appName, IRequest $request, $logger, IL10N $l10n, RuleMapper $ruleMapper) { |
26
|
|
|
parent::__construct($appName, $request); |
27
|
|
|
$this->logger = $logger; |
28
|
|
|
$this->l10n = $l10n; |
29
|
|
|
$this->ruleMapper = $ruleMapper; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns all rules |
34
|
|
|
* @return JSONResponse |
35
|
|
|
*/ |
36
|
|
|
public function listAll() { |
37
|
|
|
$statuses = $this->ruleMapper->findAll(); |
38
|
|
|
return new JSONResponse(array('statuses'=>$statuses)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Removes all rules |
43
|
|
|
* @return JSONResponse |
44
|
|
|
*/ |
45
|
|
|
public function clear() { |
46
|
|
|
$this->ruleMapper->deleteAll(); |
47
|
|
|
return new JSONResponse(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Resets a table to initial state |
52
|
|
|
* @return JSONResponse |
53
|
|
|
*/ |
54
|
|
|
public function reset() { |
55
|
|
|
$this->ruleMapper->deleteAll(); |
56
|
|
|
$this->ruleMapper->populate(); |
57
|
|
|
return new JSONResponse(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Adds/Updates a rule |
62
|
|
|
* @param int $id |
63
|
|
|
* @param int $statusType |
64
|
|
|
* @param string $match |
65
|
|
|
* @param string $description |
66
|
|
|
* @param int $status |
67
|
|
|
* @return JSONResponse |
68
|
|
|
*/ |
69
|
|
|
public function save($id, $statusType, $match, $description, $status) { |
70
|
|
|
if ($id) { |
71
|
|
|
$rule = $this->ruleMapper->find($id); |
72
|
|
|
} else { |
73
|
|
|
$rule = new Rule(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$rule->setStatusType($statusType); |
77
|
|
|
$rule->setDescription($description); |
78
|
|
|
$rule->setStatus($status); |
79
|
|
|
|
80
|
|
|
if ($statusType === \OCA\Files_Antivirus\Db\Rule::RULE_TYPE_CODE) { |
81
|
|
|
$rule->setResult($match); |
82
|
|
|
} else { |
83
|
|
|
$rule->setMatch($match); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($id) { |
87
|
|
|
$newRule = $this->ruleMapper->update($rule); |
88
|
|
|
} else { |
89
|
|
|
$newRule = $this->ruleMapper->insert($rule); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return new JSONResponse($newRule); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Deletes a rule |
97
|
|
|
* @param int $id |
98
|
|
|
* @return JSONResponse |
99
|
|
|
*/ |
100
|
|
|
public function delete($id) { |
101
|
|
|
try { |
102
|
|
|
$rule = $this->ruleMapper->find($id); |
103
|
|
|
$this->ruleMapper->delete($rule); |
104
|
|
|
} catch (\Exception $e) { |
105
|
|
|
//TODO: Handle |
106
|
|
|
} |
107
|
|
|
return new JSONResponse($rule); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|