1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core_files\redactor;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Fileredact manager.
|
|
|
21 |
*
|
|
|
22 |
* Manages and executes redaction services.
|
|
|
23 |
*
|
|
|
24 |
* @package core_files
|
|
|
25 |
* @copyright Meirza <meirza.arson@moodle.com>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
class manager {
|
|
|
29 |
/** @var array Holds an array of error messages. */
|
|
|
30 |
private array $errors = [];
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Redacts the given file.
|
|
|
34 |
*
|
|
|
35 |
* @param string $mimetype The mime-type of the file
|
|
|
36 |
* @param string $filepath The path to the file to redact
|
|
|
37 |
* @return string|null The path to the redacted file or null if no redaction services are available.
|
|
|
38 |
*/
|
|
|
39 |
public function redact_file(
|
|
|
40 |
string $mimetype,
|
|
|
41 |
string $filepath,
|
|
|
42 |
): ?string {
|
|
|
43 |
// Get the file redact services.
|
|
|
44 |
$services = $this->get_service_classnames();
|
|
|
45 |
$serviceinstances = array_filter(
|
|
|
46 |
array_map(fn($serviceclass) => new $serviceclass(), $services),
|
|
|
47 |
fn($service) => $service->is_enabled() && $service->is_mimetype_supported($mimetype)
|
|
|
48 |
);
|
|
|
49 |
|
|
|
50 |
if (count($serviceinstances) === 0) {
|
|
|
51 |
return null;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
foreach ($serviceinstances as $servicename => $service) {
|
|
|
55 |
try {
|
|
|
56 |
return $service->redact_file_by_path($mimetype, $filepath);
|
|
|
57 |
} catch (\Throwable $e) {
|
|
|
58 |
$this->errors[] = $e;
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
return null;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Redacts the given file content.
|
|
|
67 |
*
|
|
|
68 |
* @param string $mimetype The mime-type of the file
|
|
|
69 |
* @param string $filecontent The file content to redact
|
|
|
70 |
* @return string|null The content of the redacted file
|
|
|
71 |
*/
|
|
|
72 |
public function redact_file_content(
|
|
|
73 |
string $mimetype,
|
|
|
74 |
string $filecontent,
|
|
|
75 |
): ?string {
|
|
|
76 |
// Get the file redact services.
|
|
|
77 |
$services = $this->get_file_services_for_mimetype($mimetype);
|
|
|
78 |
|
|
|
79 |
foreach ($services as $servicename => $service) {
|
|
|
80 |
try {
|
|
|
81 |
return $service->redact_file_by_content($mimetype, $filecontent);
|
|
|
82 |
} catch (\Throwable $e) {
|
|
|
83 |
$this->errors[] = $e;
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
return null;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Returns a list of applicable redaction services.
|
|
|
92 |
*
|
|
|
93 |
* @return string[] list of service classnames.
|
|
|
94 |
*/
|
|
|
95 |
public function get_service_classnames(): array {
|
|
|
96 |
global $CFG;
|
|
|
97 |
$servicesdir = "{$CFG->dirroot}/files/classes/redactor/services/";
|
|
|
98 |
$servicefiles = glob("{$servicesdir}*_service.php");
|
|
|
99 |
$services = [];
|
|
|
100 |
foreach ($servicefiles as $servicefile) {
|
|
|
101 |
$servicename = basename($servicefile, '_service.php');
|
|
|
102 |
$serviceclass = services::class . "\\{$servicename}_service";
|
|
|
103 |
|
|
|
104 |
if (!is_a($serviceclass, services\service::class, true)) {
|
|
|
105 |
continue;
|
|
|
106 |
}
|
|
|
107 |
$services[$servicename] = $serviceclass;
|
|
|
108 |
}
|
|
|
109 |
return $services;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Returns a list of file redaction services that support the given mime-type.
|
|
|
114 |
*
|
|
|
115 |
* @param string $mimetype The mime-type to filter by
|
|
|
116 |
* @return services\file_redactor_service_interface[] An array of file redaction services that support the given mime-type.
|
|
|
117 |
*/
|
|
|
118 |
protected function get_file_services_for_mimetype(string $mimetype): array {
|
|
|
119 |
return array_filter(array_map(
|
|
|
120 |
function(string $serviceclass) use ($mimetype): ?services\file_redactor_service_interface {
|
|
|
121 |
if (!is_a($serviceclass, services\file_redactor_service_interface::class, true)) {
|
|
|
122 |
return null;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
$service = new $serviceclass();
|
|
|
126 |
if ($service->is_mimetype_supported($mimetype)) {
|
|
|
127 |
return $service;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
return null;
|
|
|
131 |
},
|
|
|
132 |
$this->get_service_classnames(),
|
|
|
133 |
), fn ($service) => $service !== null);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Retrieves an array of error messages.
|
|
|
138 |
*
|
|
|
139 |
* @return array An array of error messages.
|
|
|
140 |
*/
|
|
|
141 |
public function get_errors(): array {
|
|
|
142 |
return $this->errors;
|
|
|
143 |
}
|
|
|
144 |
}
|