-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAshFileIntegrityHandler.php
More file actions
137 lines (120 loc) · 4.74 KB
/
AshFileIntegrityHandler.php
File metadata and controls
137 lines (120 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* @file plugins/generic/ashFileIntegrity/AshFileIntegrityHandler.php
*
* Copyright (c) 2025 AshVisualTheme
* Copyright (c) 2014-2025 Simon Fraser University
* Copyright (c) 2003-2025 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class AshFileIntegrityHandler
* @ingroup plugins_generic_ashFileIntegrity
*
* @brief Handler for file integrity operations accessed via URL.
*/
namespace APP\plugins\generic\ashFileIntegrity;
use APP\plugins\generic\ashFileIntegrity\classes\AshFileIntegrityScanScheduledTask;
use APP\handler\Handler;
use APP\notification\NotificationManager;
use PKP\core\Core;
use PKP\core\JSONMessage;
use PKP\core\PKPApplication;
use PKP\security\Role;
use PKP\security\Validation;
use PKP\notification\Notification;
class AshFileIntegrityHandler extends Handler
{
/**
* Executes the file integrity scan.
* Calls the scheduled task to initiate the scan and sends a success notification if the user is an admin/manager.
*
* @param array $args Arguments passed to the handler.
* @param Request $request The request object.
* @return JSONMessage
*/
public function runScan($args, $request)
{
$authResult = $this->_authorizeRequest($request);
if ($authResult !== true) {
return $authResult;
}
// Increase execution time limit to prevent timeouts during heavy scanning
set_time_limit(0);
// Explicitly require the task file to avoid autoloader issues
$taskFile = dirname(__FILE__) . '/classes/AshFileIntegrityScanScheduledTask.php';
if (file_exists($taskFile)) {
require_once($taskFile);
} else {
error_log('[AshFileIntegrity] WARNING: Task file not found at ' . $taskFile);
}
$task = new AshFileIntegrityScanScheduledTask();
$success = $task->executeActions(true);
$notificationManager = new NotificationManager();
if ($success) {
$notificationManager->createTrivialNotification(
$request->getUser()->getId(),
Notification::NOTIFICATION_TYPE_SUCCESS,
['contents' => __('plugins.generic.fileIntegrity.scan.success')]
);
} else {
$notificationManager->createTrivialNotification(
$request->getUser()->getId(),
Notification::NOTIFICATION_TYPE_ERROR,
['contents' => __('plugins.generic.fileIntegrity.scan.error')]
);
}
return new JSONMessage(true);
}
/**
* Clears the plugin's JSON cache.
* Deletes all .json files in the plugin's integrityFilesScan cache directory.
*
* @param array $args Arguments passed to the handler.
* @param Request $request The request object.
* @return JSONMessage
*/
public function clearCache($args, $request)
{
$authResult = $this->_authorizeRequest($request);
if ($authResult !== true) {
return $authResult;
}
// Defines the location of the cache directory.
$cacheDir = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'integrityFilesScan';
if (is_dir($cacheDir)) {
// Finds all JSON files within the cache directory and deletes them.
$files = glob($cacheDir . DIRECTORY_SEPARATOR . '*.json');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
}
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification($request->getUser()->getId(), Notification::NOTIFICATION_TYPE_SUCCESS, ['contents' => __('plugins.generic.fileIntegrity.cache.clear.success')]);
return new JSONMessage(true);
}
/**
* Authorizes the request by checking CSRF and user roles.
*
* @param Request $request
* @return bool|JSONMessage True on success, JSONMessage on failure.
*/
private function _authorizeRequest($request)
{
// Validate the CSRF token
if (!$request->checkCSRF()) {
return new JSONMessage(false, __('form.csrfInvalid'));
}
// Ensures the user is a site administrator. The context is site-level (0).
if (!Validation::isAuthorized(Role::ROLE_ID_SITE_ADMIN, PKPApplication::CONTEXT_SITE)) {
return new JSONMessage(false, 'Authorization failed.');
}
return true;
}
}
// Alias this class to 'Handler' within the namespace so OJS Router can instantiate it automatically
// when loaded via the LoadHandler hook.
if (!class_exists('AshFileIntegrityHandler')) {
class_alias(AshFileIntegrityHandler::class, 'AshFileIntegrityHandler');
}