-
Notifications
You must be signed in to change notification settings - Fork 5
/
AllowedUploadsPlugin.inc.php
executable file
·164 lines (138 loc) · 4.46 KB
/
AllowedUploadsPlugin.inc.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* @file plugins/generic/allowedUploads/AllowedUploadsPlugin.inc.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2003-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class AllowedUploadsPlugin
* @ingroup plugins_generic_allowedUploads
*
* @brief Allowed Uploads plugin class
*/
use APP\core\Application;
use APP\template\TemplateManager;
use PKP\core\JSONMessage;
use PKP\plugins\GenericPlugin;
use PKP\plugins\PluginRegistry;
use PKP\validation\ValidatorFactory;
class AllowedUploadsPlugin extends GenericPlugin {
/**
* Called as a plugin is registered to the registry
* @param $category String Name of category plugin was registered to
* @return boolean True iff plugin initialized successfully; if false,
* the plugin will not be registered.
*/
function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path);
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) return true;
if ($success && $this->getEnabled()) {
HookRegistry::register('SubmissionFile::validate', array($this, 'checkUploadWizard'));
HookRegistry::register('submissionfilesuploadform::validate', array($this, 'checkUpload'));
}
return $success;
}
/**
* Get the plugin display name.
* @return string
*/
function getDisplayName() {
return __('plugins.generic.allowedUploads.displayName');
}
/**
* Get the plugin description.
* @return string
*/
function getDescription() {
return __('plugins.generic.allowedUploads.description');
}
/**
* @copydoc Plugin::getActions()
*/
function getActions($request, $verb) {
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
return array_merge(
$this->getEnabled()?array(
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array('verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic')),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
):array(),
parent::getActions($request, $verb)
);
}
/**
* @copydoc Plugin::manage()
*/
function manage($args, $request) {
switch ($request->getUserVar('verb')) {
case 'settings':
$context = $request->getContext();
$templateMgr = TemplateManager::getManager($request);
$templateMgr->registerPlugin('function', 'plugin_url', array($this, 'smartyPluginUrl'));
$this->import('AllowedUploadsSettingsForm');
$form = new AllowedUploadsSettingsForm($this, $context->getId());
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
/**
* Check the uploaded file in wizard
*/
function checkUploadWizard($hookName, $params) {
$props = $params[2];
$locale = $params[4];
if ($fileName = $props['name'][$locale]){
$errors =& $params[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
$fileName = $props['name'][$locale];
$tmp = explode('.',$fileName);
$extension = strtolower(end($tmp));
$allowedExtensions = $this->getSetting($context->getId(), 'allowedExtensions');
if ($allowedExtensions){
$allowedExtensionsArray = array_filter(array_map('trim', explode(';', $allowedExtensions )), 'strlen');
if (!in_array($extension, $allowedExtensionsArray)){
$errors[] = __('plugins.generic.allowedUploads.error', array('allowedExtensions' => $allowedExtensions));
}
}
}
}
/**
* Check the uploaded file
*/
function checkUpload($hookName, $params) {
$form = $params[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
$userVars = $request->getUserVars();
$fileName = $userVars['name'];
$tmp = explode('.',$fileName);
$extension = strtolower(end($tmp));
$allowedExtensions = $this->getSetting($context->getId(), 'allowedExtensions');
if ($allowedExtensions){
$allowedExtensionsArray = array_filter(array_map('trim', explode(';', $allowedExtensions )), 'strlen');
if (!in_array($extension, $allowedExtensionsArray)){
$form->addError('allowedFileType', __('plugins.generic.allowedUploads.error', ['allowedExtensions' => $allowedExtensions]));
}
}
return false;
}
}
?>