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