Skip to content

Commit

Permalink
Merge c6c0f38 into 28b2399
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Nov 9, 2016
2 parents 28b2399 + c6c0f38 commit 3aa323a
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 152 deletions.
1 change: 1 addition & 0 deletions appinfo/application.php
Expand Up @@ -57,6 +57,7 @@ public function __construct (array $urlParams = array()) {
return new BackgroundScanner(
$c->query('ScannerFactory'),
$c->query('L10N'),
$c->query('AppConfig'),
$c->getServer()->getRootFolder(),
$c->getServer()->getUserSession()
);
Expand Down
9 changes: 7 additions & 2 deletions controller/settingscontroller.php
Expand Up @@ -41,9 +41,10 @@ public function index() {
$data = $this->settings->getAllValues();
return new TemplateResponse('files_antivirus', 'settings', $data, 'blank');
}

/**
* Save Parameters
*
* @param string $avMode - antivirus mode
* @param string $avSocket - path to socket (Socket mode)
* @param string $avHost - antivirus url
Expand All @@ -52,9 +53,11 @@ public function index() {
* @param int $avChunkSize - Size of one portion
* @param string $avPath - path to antivirus executable (Executable mode)
* @param string $avInfectedAction - action performed on infected files
* @param $avStreamMaxLength - reopen socket after bytes
* @param int $avMaxFileSize - file size limit
* @return JSONResponse
*/
public function save($avMode, $avSocket, $avHost, $avPort, $avCmdOptions, $avChunkSize, $avPath, $avInfectedAction) {
public function save($avMode, $avSocket, $avHost, $avPort, $avCmdOptions, $avChunkSize, $avPath, $avInfectedAction, $avStreamMaxLength, $avMaxFileSize) {
$this->settings->setAvMode($avMode);
$this->settings->setAvSocket($avSocket);
$this->settings->setAvHost($avHost);
Expand All @@ -63,6 +66,8 @@ public function save($avMode, $avSocket, $avHost, $avPort, $avCmdOptions, $avChu
$this->settings->setAvChunkSize($avChunkSize);
$this->settings->setAvPath($avPath);
$this->settings->setAvInfectedAction($avInfectedAction);
$this->settings->setAvStreamMaxLength($avStreamMaxLength);
$this->settings->setAvMaxFileSize($avMaxFileSize);

return new JSONResponse(
array('data' =>
Expand Down
4 changes: 4 additions & 0 deletions css/settings.css
Expand Up @@ -11,6 +11,10 @@
text-align: right;
}

.section-antivirus .a-left{
text-align: left;
}

.shaded{
opacity: .3;
}
Expand Down
18 changes: 6 additions & 12 deletions js/settings.js
Expand Up @@ -131,19 +131,13 @@ var antivirusSettings = antivirusSettings || {

function av_mode_show_options(str){
if ( str == 'daemon'){
$('p.av_socket').hide('slow');
$('p.av_host').show('slow');
$('p.av_port').show('slow');
$('p.av_path').hide('slow');
$('p.av_socket, p.av_path').hide('slow');
$('p.av_host, p.av_port, p.av_stream_max_length').show('slow');
} else if ( str == 'socket' ) {
$('p.av_socket').show('slow');
$('p.av_path').hide('slow');
$('p.av_host').hide('slow');
$('p.av_port').hide('slow');
} else if (str == 'executable'){
$('p.av_socket').hide('slow');
$('p.av_host').hide('slow');
$('p.av_port').hide('slow');
$('p.av_socket, p.av_stream_max_length').show('slow');
$('p.av_path, p.av_host, p.av_port').hide('slow');
} else if (str == 'executable'){
$('p.av_socket, p.av_host, p.av_port, p.av_stream_max_length').hide('slow');
$('p.av_path').show('slow');
}
}
Expand Down
27 changes: 20 additions & 7 deletions lib/appconfig.php
Expand Up @@ -15,6 +15,8 @@
* @method string getAvSocket()
* @method string getAvHost()
* @method int getAvPort()
* @method int getAvMaxFileSize()
* @method int getAvStreamMaxLength()
* @method string getAvCmdOptions()
* @method int getAvChunkSize()
* @method string getAvPath()
Expand All @@ -24,6 +26,8 @@
* @method null setAvSocket(string $avsocket)
* @method null setAvHost(string $avHost)
* @method null setAvPort(int $avPort)
* @method null setAvMaxFileSize(int $fileSize)
* @method null setAvStreamMaxLength(int $streamMaxLength)
* @method null setAvCmdOptions(string $avCmdOptions)
* @method null setAvChunkSize(int $chunkSize)
* @method null setAvPath(string $avPath)
Expand All @@ -32,19 +36,28 @@

class AppConfig {
private $appName = 'files_antivirus';

/** @var IConfig */
private $config;

private $defaults = array(
private $defaults = [
'av_mode' => 'executable',
'av_socket' => '/var/run/clamav/clamd.ctl',
'av_host' => '',
'av_port' => '',
'av_cmd_options' => '',
'av_chunk_size' => '1024',
'av_chunk_size' => '8192',
'av_path' => '/usr/bin/clamscan',
'av_max_file_size' => -1,
'av_stream_max_length' => '26214400',
'av_infected_action' => 'only_log',
);

];

/**
* AppConfig constructor.
*
* @param IConfig $config
*/
public function __construct(IConfig $config) {
$this->config = $config;
}
Expand All @@ -56,7 +69,7 @@ public function __construct(IConfig $config) {
public function getCmdline(){
$avCmdOptions = $this->getAvCmdOptions();

$shellArgs = array();
$shellArgs = [];
if ($avCmdOptions) {
$shellArgs = explode(',', $avCmdOptions);
$shellArgs = array_map(function($i){
Expand Down Expand Up @@ -103,7 +116,7 @@ public function getAppValue($key) {
* @param string $value
* @return string
*/
public function setAppvalue($key, $value) {
public function setAppValue($key, $value) {
return $this->config->setAppValue($this->appName, $key, $value);
}

Expand All @@ -115,7 +128,7 @@ public function setAppvalue($key, $value) {
*/
protected function setter($key, $args) {
if (array_key_exists($key, $this->defaults)) {
$this->setAppvalue($key, $args[0]);
$this->setAppValue($key, $args[0]);
} else {
throw new \BadFunctionCallException($key . ' is not a valid key');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/avirwrapper.php
Expand Up @@ -61,7 +61,7 @@ public function fopen($path, $mode){
if (is_resource($stream) && $this->isWritingMode($mode)) {
try {
$scanner = $this->scannerFactory->getScanner();
$scanner->initAsyncScan();
$scanner->initScanner();
return CallBackWrapper::wrap(
$stream,
null,
Expand Down
122 changes: 72 additions & 50 deletions lib/backgroundscanner.php
Expand Up @@ -24,17 +24,15 @@ class BackgroundScanner {
/** @var \OCP\Files\Folder[] */
protected $userFolders;

/**
* @var ScannerFactory
*/
/** @var ScannerFactory */
private $scannerFactory;


/**
* @var IL10N
*/
/** @var IL10N */
private $l10n;

/** @var AppConfig */
private $appConfig;

/** @var string */
protected $currentFilesystemUser;

Expand All @@ -46,17 +44,20 @@ class BackgroundScanner {
*
* @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory
* @param IL10N $l10n
* @param AppConfig $appConfig
* @param IRootFolder $rootFolder
* @param IUserSession $userSession
*/
public function __construct(ScannerFactory $scannerFactory,
IL10N $l10n,
AppConfig $appConfig,
IRootFolder $rootFolder,
IUserSession $userSession
){
$this->rootFolder = $rootFolder;
$this->scannerFactory = $scannerFactory;
$this->l10n = $l10n;
$this->appConfig = $appConfig;
$this->userSession = $userSession;
}

Expand All @@ -66,41 +67,8 @@ public function __construct(ScannerFactory $scannerFactory,
*/
public function run(){
// locate files that are not checked yet
$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory');
try {
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$qb->select(['fc.fileid'])
->from('filecache', 'fc')
->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid'))
->innerJoin(
'fc',
'storages',
'ss',
$qb->expr()->andX(
$qb->expr()->eq('fc.storage', 'ss.numeric_id'),
$qb->expr()->orX(
$qb->expr()->like('ss.id', $qb->expr()->literal('local::%')),
$qb->expr()->like('ss.id', $qb->expr()->literal('home::%'))
)
)
)
->where(
$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId))
)
->andWhere(
$qb->expr()->orX(
$qb->expr()->isNull('fa.fileid'),
$qb->expr()->gt('fc.mtime', 'fa.check_time')
)
)
->andWhere(
$qb->expr()->like('fc.path', $qb->expr()->literal('files/%'))
)
->andWhere(
$qb->expr()->neq('fc.size', $qb->expr()->literal('0'))
)
;
$result = $qb->execute();
$result = $this->getFilesForScan();
} catch(\Exception $e) {
\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
return;
Expand All @@ -115,15 +83,7 @@ public function run(){
if (!$owner instanceof IUser){
continue;
}
$this->initFilesystemForUser($owner);
$view = Filesystem::getView();
$path = $view->getPath($fileId);
if (!is_null($path)) {
$item = new Item($this->l10n, $view, $path, $fileId);
$scanner = $this->scannerFactory->getScanner();
$status = $scanner->scan($item);
$status->dispatch($item, true);
}
$this->scanOneFile($owner, $fileId);
// increased only for successfully scanned files
$cnt = $cnt + 1;
} catch (\Exception $e){
Expand All @@ -133,6 +93,68 @@ public function run(){
$this->tearDownFilesystem();
}

protected function getFilesForScan(){
$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory');
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();

$sizeLimit = intval($this->appConfig->getAvMaxFileSize());
if ( $sizeLimit === -1 ){
$sizeLimitExpr = $qb->expr()->neq('fc.size', $qb->expr()->literal('0'));
} else {
$sizeLimitExpr = $qb->expr()->andX(
$qb->expr()->neq('fc.size', $qb->expr()->literal('0')),
$qb->expr()->lt('fc.size', $qb->expr()->literal((string) $sizeLimit))
);
}

$qb->select(['fc.fileid'])
->from('filecache', 'fc')
->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid'))
->innerJoin(
'fc',
'storages',
'ss',
$qb->expr()->andX(
$qb->expr()->eq('fc.storage', 'ss.numeric_id'),
$qb->expr()->orX(
$qb->expr()->like('ss.id', $qb->expr()->literal('local::%')),
$qb->expr()->like('ss.id', $qb->expr()->literal('home::%'))
)
)
)
->where(
$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId))
)
->andWhere(
$qb->expr()->orX(
$qb->expr()->isNull('fa.fileid'),
$qb->expr()->gt('fc.mtime', 'fa.check_time')
)
)
->andWhere(
$qb->expr()->like('fc.path', $qb->expr()->literal('files/%'))
)
->andWhere( $sizeLimitExpr )
;
return $qb->execute();
}

/**
* @param IUser $owner
* @param int $fileId
*/
protected function scanOneFile($owner, $fileId){
$this->initFilesystemForUser($owner);
$view = Filesystem::getView();
$path = $view->getPath($fileId);
if (!is_null($path)) {
$item = new Item($this->l10n, $view, $path, $fileId);
$scanner = $this->scannerFactory->getScanner();
$status = $scanner->scan($item);
$status->dispatch($item, true);
}
}

/**
* @param int $fileId
* @return IUser|null
Expand Down
4 changes: 0 additions & 4 deletions lib/db/itemmapper.php
Expand Up @@ -11,12 +11,8 @@
use OCP\IDb;
use OCP\AppFramework\Db\Mapper;

use OCA\Files_Antivirus\Db\Item;

class ItemMapper extends Mapper {
public function __construct(IDb $db) {
parent::__construct($db, 'files_antivirus', '\OCA\Files_Antivirus\Db\Item');
}


}
6 changes: 3 additions & 3 deletions lib/item.php
Expand Up @@ -12,7 +12,7 @@
use OCA\Files_Antivirus\Status;
use OCA\Files_Antivirus\Activity;

class Item implements iScannable{
class Item implements IScannable{
/**
* Scanned fileid (optional)
* @var int
Expand Down Expand Up @@ -182,12 +182,12 @@ public function processClean(Status $status, $isBackground) {
$result = $stmt->execute(array($this->id));
if (\OCP\DB::isError($result)) {
//TODO: Use logger
$this->logError(__METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage($result));
$this->logError(__METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage());
}
$stmt = \OCP\DB::prepare('INSERT INTO `*PREFIX*files_antivirus` (`fileid`, `check_time`) VALUES (?, ?)');
$result = $stmt->execute(array($this->id, time()));
if (\OCP\DB::isError($result)) {
$this->logError(__METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage($result));
$this->logError(__METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage());
}
} catch(\Exception $e) {
\OCP\Util::writeLog('files_antivirus', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
Expand Down
1 change: 0 additions & 1 deletion lib/notification.php
Expand Up @@ -13,7 +13,6 @@ public static function sendMail($path){
if (!\OCP\User::isLoggedIn()){
return;
}
$config = \OC::$server->getConfig();
$user = \OC::$server->getUserSession()->getUser();
$email = $user->getEMailAddress();
$displayName = $user->getDisplayName();
Expand Down

0 comments on commit 3aa323a

Please sign in to comment.