Add admin configuration section

This commit is contained in:
Victor Dubiniuk 2014-04-15 19:22:58 +03:00
Родитель 2edc09f310
Коммит 15326ae101
6 изменённых файлов: 297 добавлений и 20 удалений

67
ajax/settings.php Normal file
Просмотреть файл

@ -0,0 +1,67 @@
<?php
/**
* Copyright (c) 2014 Victor Dubiniuk <victor.dubiniuk@gmail.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
\OCP\JSON::checkAdminUser();
\OCP\JSON::callCheck();
$action = isset($_POST['action']) ? $_POST['action'] : '';
switch ($action){
case 'list':
$query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*files_antivirus_status`');
$result = $query->execute(array());
$statuses = $result->fetchAll();
\OCP\JSON::success(array('statuses'=>$statuses));
break;
case 'reset':
$query = \OCP\DB::prepare('DELETE FROM `*PREFIX*files_antivirus_status`');
$query->execute(array());
\OCA\Files_Antivirus\Status::init();
\OCP\JSON::success();
break;
case 'save':
$ruleId = isset($_POST['id']) ? intval($_POST['id']) : false;
$statusType = isset($_POST['status_type']) ? intval($_POST['status_type']) : 0;
if ($statusType === \OCA\Files_Antivirus\Status::STATUS_TYPE_CODE){
$field = 'result';
} else {
$field = 'match';
}
$data = array();
$data[] = $statusType;
$data[] = isset($_POST['match']) ? $_POST['match'] : '';
$data[] = isset($_POST['description']) ? $_POST['description'] : '';
$data[] = isset($_POST['status']) ? intval($_POST['status']) : 0;
if ($ruleId){
$data[] = $ruleId;
$query = \OCP\DB::prepare('UPDATE `*PREFIX*files_antivirus_status` SET `status_type`=(?),'
.' `'. $field .'`=(?), `description`=(?), `status`=(?) WHERE `id`=?');
} else {
$query = \OCP\DB::prepare('INSERT INTO `*PREFIX*files_antivirus_status` (`status_type`,'
.' `'. $field .'`, `description`, `status`) VALUES (?, ?, ?, ?)');
}
$query->execute($data);
$result = array();
if (!$ruleId){
$result['id'] = \OCP\DB::insertid('`*PREFIX*files_antivirus_status`');
}
\OCP\JSON::success($result);
break;
case 'delete':
$ruleId = isset($_POST['id']) ? intval($_POST['id']) : 0;
$query = \OCP\DB::prepare('DELETE FROM `*PREFIX*files_antivirus_status` WHERE `id`=?');
$query->execute(array($ruleId));
\OCP\JSON::success();
break;
default:
break;
}
exit();

23
css/settings.css Normal file
Просмотреть файл

@ -0,0 +1,23 @@
.section-antivirus .spoiler{
display:none;
margin-top: 14px;
}
.shaded{
opacity: .3;
}
#antivirus-reset{
float: right;
}
#antivirus-add{
margin: 10px;
padding-left: 21px;
background-position: 3px 50%;
}
.section-antivirus h3{
float: left;
margin-top: 7px;
}

Просмотреть файл

@ -1,3 +1,135 @@
var antivirusSettings = antivirusSettings || {
statuses : [
{ value : 0, title : t('files_antivirus', 'Clean')},
{ value : 1, title : t('files_antivirus', 'Infected')},
{ value : -1, title : t('files_antivirus', 'Unchecked')}
],
types : [
{ value : 1, title : t('files_antivirus', 'Scanner exit status') },
{ value : 2, title : t('files_antivirus', 'Scanner output') },
],
init : function(){
$.post(OC.filePath('files_antivirus', 'ajax', 'settings.php'), {action : 'list'},
function onSuccess(response){
if (!response || !response.statuses){
return;
}
for (var i = 0; i < response.statuses.length; i++) {
antivirusSettings.renderRow(response.statuses[i]);
}
}
);
},
renderRow : function(data){
var row = $('<tr />').data('id', data.id).appendTo($('#antivirus-statuses'));
$('<td class="icon-checkmark shaded" />').appendTo(row);
antivirusSettings.renderSelect(
$('<td class="status-type" />').appendTo(row),
{options : antivirusSettings.types, current : data.status_type}
);
$('<td class="match editable" />').appendTo(row).text(
(data.status_type == 1 ? data.result : data.match)
);
$('<td class="description editable" />').appendTo(row).text(data.description);
antivirusSettings.renderSelect(
$('<td class="scan-result" />').appendTo(row),
{ options : antivirusSettings.statuses, current : data.status }
);
$('<td class="icon-delete" />').appendTo(row);
},
onSave : function(){
var node = $(this),
row = $(node).parent(),
data = {
action : 'save',
id : row.data('id'),
status_type : row.find('.status-type select').val(),
match : row.children('.match').text(),
description : row.children('.description').text(),
status : row.find('.scan-result select').val()
};
$.post(OC.filePath('files_antivirus', 'ajax', 'settings.php'), data,
function onSuccess(response){
if (response && response.id){
row.data('id', response.id);
}
node.addClass('shaded');
}
);
},
onEdit : function(node){
if ($(node).find('input').length){
return;
}
var current = $(node).text();
$(node).text('');
$('<input />').val(current)
.on('blur', function(){
var newValue = $(this).val();
if (newValue !== current){
$(node).parents('tr').first().find('td.icon-checkmark').removeClass('shaded');
}
$(this).remove();
$(node).text(newValue);
})
.on('keyup', function(event){
if (event.keyCode === 27) {
$(this).val(current);
$(this).blur();
event.preventDefault();
}
if (event.keyCode === 13) {
$(this).blur();
event.preventDefault();
}
})
.on('keydown', function(){
if (event.keyCode === 9) {
$(this).parent('td').siblings('td.editable').first().trigger('click');
event.preventDefault();
}
})
.appendTo(node)
.focus()
;
},
deleteRow : function(){
var row = $(this).parent();
row.hide();
$.post(OC.filePath('files_antivirus', 'ajax', 'settings.php'), {action : 'delete', id : row.data('id')},
function onSuccess(response){
row.remove();
}
);
},
renderSelect : function(parent, data){
var select = $('<select />')
.on('change', function(){
$(this).parents('tr').first().find('td.icon-checkmark').removeClass('shaded');
});
for (var i=0; i<data.options.length; i++){
var option = $('<option />');
option.attr('value', data.options[i].value)
.text(data.options[i].title)
;
if (data.options[i].value == data.current){
option.attr('selected', '');
}
select.append(option);
}
parent.append(select);
}
};
function av_mode_show_options(str){
if ( str == 'daemon'){
$('p.av_socket').hide('slow');
@ -17,6 +149,37 @@ function av_mode_show_options(str){
}
}
$(document).ready(function() {
$('#antivirus-advanced').on('click', function () {
$('.section-antivirus .spoiler').toggle();
antivirusSettings.init();
});
$('#antivirus-reset').on('click', function (){
$.post(OC.filePath('files_antivirus', 'ajax', 'settings.php'), {action : 'reset'},
function onSuccess(){
$('#antivirus-statuses tbody td').remove();
antivirusSettings.init();
});
});
$('#antivirus-add').on('click', function (){
antivirusSettings.renderRow({
id : '',
status_type : 1,
result : '',
description : '',
status : 0
});
$('#antivirus-statuses tbody tr:last-child td.editable').first().trigger('click');
});
$('#antivirus-statuses tbody').on('click', 'td.editable', function(){
console.log(this);
antivirusSettings.onEdit(this);
});
$('#antivirus-statuses tbody').on('click', 'td.icon-delete', antivirusSettings.deleteRow);
$('#antivirus-statuses tbody').on('click', 'td.icon-checkmark', antivirusSettings.onSave);
$("#av_mode").change(function () {
var str = $("#av_mode").val();
av_mode_show_options(str);

Просмотреть файл

@ -124,13 +124,13 @@ class Scanner {
$avMode = \OCP\Config::getAppValue('files_antivirus', 'av_mode', 'executable');
switch($avMode) {
case 'daemon':
self::$instance = new Scanner_External(false);
self::$instance = new \OCA\Files_Antivirus\Scanner_External(false);
break;
case 'socket':
self::$instance = new Scanner_External(true);
self::$instance = new \OCA\Files_Antivirus\Scanner_External(true);
break;
case 'executable':
self::$instance = new Scanner_Local();
self::$instance = new \OCA\Files_Antivirus\Scanner_Local();
break;
default:
self::$instance = false;

Просмотреть файл

@ -45,6 +45,7 @@ if($_POST){
// fill template
$tmpl = new OCP\Template( 'files_antivirus', 'settings');
$tmpl->assign('requesttoken', OCP\Util::callRegister());
OCP\Util::addStyle('files_antivirus', 'settings');
OCP\Util::addScript('files_antivirus', 'settings');
foreach($params as $param => $default){
$value = OCP\Config::getAppValue('files_antivirus', $param, $default);

Просмотреть файл

@ -1,18 +1,41 @@
<form id="antivirus" action="#" method="post">
<div class="section">
<h2><?php p($l->t('Antivirus Configuration'));?></h2>
<p class='av_mode'><label for="av_mode"><?php p($l->t('Mode'));?></label>
<select id="av_mode" name="av_mode"><?php print_unescaped(html_select_options(array('executable' => $l->t('Executable'), 'daemon' => $l->t('Daemon'), 'socket' => $l->t('Daemon (Socket)')), $_['av_mode'])) ?></select>
</p>
<p class='av_socket'><label for="av_socket"><?php p($l->t('Socket'));?></label><input type="text" id="av_socket" name="av_socket" value="<?php p($_['av_socket']); ?>" title="<?php p($l->t('Clamav Socket.')).' '.$l->t('Not required in Executable Mode.'); ?>"></p>
<p class='av_host'><label for="av_host"><?php p($l->t('Host'));?></label><input type="text" id="av_host" name="av_host" value="<?php p($_['av_host']); ?>" title="<?php p($l->t('Address of Antivirus Host.')). ' ' .$l->t('Not required in Executable Mode.');?>"></p>
<p class='av_port'><label for="av_port"><?php p($l->t('Port'));?></label><input type="text" id="av_port" name="av_port" value="<?php p($_['av_port']); ?>" title="<?php p($l->t('Port number of Antivirus Host.')). ' ' .$l->t('Not required in Executable Mode.');?>"></p>
<p class='av_chunk_size'><label for="av_chunk_size"><?php p($l->t('Stream Length'));?></label><input type="text" id="av_chunk_size" name="av_chunk_size" value="<?php p($_['av_chunk_size']); ?>" title="<?php p($l->t('ClamAV StreamMaxLength value in bytes.')). ' ' .$l->t('Not required in Executable Mode.');?>"> bytes</p>
<p class='av_path'><label for="av_path"><?php p($l->t('Path to clamscan'));?></label><input type="text" id="av_path" name="av_path" value="<?php p($_['av_path']); ?>" title="<?php p($l->t('Path to clamscan executable.')). ' ' .$l->t('Not required in Daemon Mode.');?>"></p>
<p class='infected_action'><label for="infected_action"><?php p($l->t('Action for infected files found while scanning'));?></label>
<select id="infected_action" name="infected_action"><?php print_unescaped(html_select_options(array('only_log' => $l->t('Only log'), 'delete' => $l->t('Delete file')), $_['infected_action'])) ?></select>
</p>
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']);?>" />
<input type="submit" value="<?php p($l->t('Save'));?>" />
<div class="section section-antivirus">
<form id="antivirus" action="#" method="post">
<fieldset class="personalblock">
<h2><?php p($l->t('Antivirus Configuration'));?></h2>
<p class='av_mode'><label for="av_mode"><?php p($l->t('Mode'));?></label>
<select id="av_mode" name="av_mode"><?php print_unescaped(html_select_options(array('executable' => $l->t('Executable'), 'daemon' => $l->t('Daemon'), 'socket' => $l->t('Daemon (Socket)')), $_['av_mode'])) ?></select>
</p>
<p class='av_socket'><label for="av_socket"><?php p($l->t('Socket'));?></label><input type="text" id="av_socket" name="av_socket" value="<?php p($_['av_socket']); ?>" title="<?php p($l->t('Clamav Socket.')).' '.$l->t('Not required in Executable Mode.'); ?>"></p>
<p class='av_host'><label for="av_host"><?php p($l->t('Host'));?></label><input type="text" id="av_host" name="av_host" value="<?php p($_['av_host']); ?>" title="<?php p($l->t('Address of Antivirus Host.')). ' ' .$l->t('Not required in Executable Mode.');?>"></p>
<p class='av_port'><label for="av_port"><?php p($l->t('Port'));?></label><input type="text" id="av_port" name="av_port" value="<?php p($_['av_port']); ?>" title="<?php p($l->t('Port number of Antivirus Host.')). ' ' .$l->t('Not required in Executable Mode.');?>"></p>
<p class='av_chunk_size'><label for="av_chunk_size"><?php p($l->t('Stream Length'));?></label><input type="text" id="av_chunk_size" name="av_chunk_size" value="<?php p($_['av_chunk_size']); ?>" title="<?php p($l->t('ClamAV StreamMaxLength value in bytes.')). ' ' .$l->t('Not required in Executable Mode.');?>"> bytes</p>
<p class='av_path'><label for="av_path"><?php p($l->t('Path to clamscan'));?></label><input type="text" id="av_path" name="av_path" value="<?php p($_['av_path']); ?>" title="<?php p($l->t('Path to clamscan executable.')). ' ' .$l->t('Not required in Daemon Mode.');?>"></p>
<p class='infected_action'><label for="infected_action"><?php p($l->t('Action for infected files found while scanning'));?></label>
<select id="infected_action" name="infected_action"><?php print_unescaped(html_select_options(array('only_log' => $l->t('Only log'), 'delete' => $l->t('Delete file')), $_['infected_action'])) ?></select>
</p>
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']);?>" />
<input type="submit" value="<?php p($l->t('Save'));?>" />
</fieldset>
</form>
<hr />
<button id="antivirus-advanced"><?php p($l->t('Advanced')) ?></button>
<div class="spoiler">
<h3><?php p($l->t('Rules')) ?></h3>
<button id="antivirus-reset"><?php p($l->t('Reset to defaults')) ?></button>
<table id="antivirus-statuses" class="grid">
<thead>
<tr>
<th></th>
<th><?php p($l->t('Match by')) ?></th>
<th><?php p($l->t('Scanner exit status or signature to search')) ?></th>
<th><?php p($l->t('Description')); ?></th>
<th><?php p($l->t('Mark as')) ?></th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<button id="antivirus-add" class="icon-add"><?php p($l->t('Add a rule')) ?></button>
</div>
</form>
</div>