Skip to content

Commit

Permalink
MDL-75850 core: file redact
Browse files Browse the repository at this point in the history
File redact is a core plugin to remove or obsure from a file (image, doc) prior to publication/release.
Currently, it has an EXIF remover service using exiftool command, but it is also possible to
add other services related to file redacting.

The plugin admin settings is under Server as it's parent.

Co-authored-by: Huong Nguyen <[email protected]>
  • Loading branch information
meirzamoodle and HuongNV13 committed Aug 28, 2024
1 parent 056f473 commit 72b43c3
Show file tree
Hide file tree
Showing 16 changed files with 1,214 additions and 16 deletions.
13 changes: 13 additions & 0 deletions .upgradenotes/MDL-75850-2024082809421816.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
issueNumber: MDL-75850
notes:
core_files:
- message: |
The following are the changes made:
- New hook after_file_created
- In the \core_files\file_storage, new additional param $notify (default is true) added to:
- ::create_file_from_storedfile()
- ::create_file_from_pathname()
- ::create_file_from_string()
- ::create_file()
If true, it will trigger the after_file_created hook to re-create the image.
type: improved
43 changes: 43 additions & 0 deletions admin/settings/fileredact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Configure the settings for fileredact.
*
* @package core_admin
* @copyright Meirza <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

if ($hassiteconfig) {
if (!$ADMIN->locate('fileredact')) {
$ADMIN->add('server', new admin_category('fileredact', get_string('fileredact', 'core_files')));
}
// Get settings from each service.
$servicesdir = "{$CFG->libdir}/classes/fileredact/services/";
$servicefiles = glob("{$servicesdir}*_service.php");
foreach ($servicefiles as $servicefile) {
$servicename = basename($servicefile, '_service.php');
$classname = "\\core\\fileredact\\services\\{$servicename}_service";
if (class_exists($classname)) {
$fileredactsettings = new admin_settingpage($servicename, new lang_string("fileredact:$servicename", 'core_files'));
call_user_func("{$classname}::add_settings", $fileredactsettings);
$ADMIN->add('fileredact', $fileredactsettings);
}
}
}
23 changes: 23 additions & 0 deletions lang/en/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@
$string['contenthash'] = 'Content hash';
$string['eventfileaddedtodraftarea'] = 'File added to draft area';
$string['eventfiledeletedfromdraftarea'] = 'File deleted from draft area';
$string['fileredact'] = 'File redact';
$string['fileredact:exifremover'] = 'EXIF remover';
$string['fileredact:exifremover:emptyremovetags'] = 'Remove tags can not be empty!';
$string['fileredact:exifremover:enabled'] = 'Enable EXIF remover';
$string['fileredact:exifremover:enabled_desc'] = 'By default, EXIF Remover only supports JPG files using PHP GD, or ExifTool if it is configured.
This degrades the quality of the image and removes the orientation tag.
To enhance the performance of EXIF Remover, please configure the ExifTool settings below.
More information about installing ExifTool can be found at {$a->link}';
$string['fileredact:exifremover:failedprocessexiftool'] = 'Redaction failed: failed to process file with ExifTool!';
$string['fileredact:exifremover:failedprocessgd'] = 'Redaction failed: failed to process file with PHP gd!';
$string['fileredact:exifremover:heading'] = 'ExifTool';
$string['fileredact:exifremover:mimetype'] = 'Supported MIME types';
$string['fileredact:exifremover:mimetype_desc'] = 'To add new MIME types, ensure they\'re included in the <a href="./tool/filetypes/index.php">File Types</a>.';
$string['fileredact:exifremover:removetags'] = 'The EXIF tags that will be removed.';
$string['fileredact:exifremover:removetags_desc'] = 'The EXIF tags that need to be removed.';
$string['fileredact:exifremover:tag:all'] = 'All';
$string['fileredact:exifremover:tag:gps'] = 'GPS only';
$string['fileredact:exifremover:tooldoesnotexist'] = 'Redaction failed: ExifTool does not exist!';
$string['fileredact:exifremover:toolpath'] = 'Path to ExifTool';
$string['fileredact:exifremover:toolpath_desc'] = 'To use the ExifTool, please provide the path to the ExifTool executable.
Typically, on Unix/Linux systems, the path is /usr/bin/exiftool.';
$string['privacy:metadata:file_conversions'] = 'A record of the file conversions performed by a user.';
$string['privacy:metadata:file_conversion:usermodified'] = 'The user who started the file conversion.';
$string['privacy:metadata:files'] = 'A record of the files uploaded or shared by users';
Expand Down
50 changes: 50 additions & 0 deletions lib/classes/fileredact/hook_listener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core\fileredact;

use core\hook\filestorage\after_file_created;

/**
* Allow the plugin to call as soon as possible before the file is created.
*
* @package core
* @copyright Meirza <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class hook_listener {
/**
* Execute the available services after creating the file.
*
* @param after_file_created $hook
*/
public static function redact_after_file_created(after_file_created $hook): void {
$storedfile = $hook->storedfile;

// The file mime-type must be present. Otherwise, bypass the process.
if (empty($storedfile->get_mimetype())) {
return;
}

$manager = new manager($storedfile);
$manager->execute();

// Iterates through the errors returned by the manager and outputs each error message.
foreach ($manager->get_errors() as $e) {
debugging($e->getMessage());
}
}
}
93 changes: 93 additions & 0 deletions lib/classes/fileredact/manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core\fileredact;

use stored_file;

/**
* Fileredact manager.
*
* Manages and executes redaction services.
*
* @package core
* @copyright Meirza <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class manager {

/** @var array Holds an array of error messages. */
private $errors = [];

/**
* Constructor.
*
* @param stored_file $filerecord The file record as a stdClass object, or null if not available.
*/
public function __construct(
/** @var stored_file $filerecord File record. */
private readonly stored_file $filerecord
) {
}

/**
* Executes redaction services.
*/
public function execute(): void {
// Get the file redact services.
$services = $this->get_services();
foreach ($services as $serviceclass) {
try {
if (class_exists($serviceclass)) {
$service = new $serviceclass($this->filerecord);
// For the given service, execute them if they are enabled, and the given mime type is supported.
if ($service->is_enabled() && $service->is_mimetype_supported($this->filerecord->get_mimetype())) {
$service->execute();
}
}
} catch (\Throwable $e) {
$this->errors[] = $e;
}
}
}

/**
* Returns a list of applicable redaction services.
*
* @return string[] return list of services.
*/
protected function get_services(): array {
global $CFG;
$servicesdir = "{$CFG->libdir}/classes/fileredact/services/";
$servicefiles = glob("{$servicesdir}*_service.php");
$services = [];
foreach ($servicefiles as $servicefile) {
$servicename = basename($servicefile, '_service.php');
$serviceclass = "\\core\\fileredact\\services\\{$servicename}_service";
$services[] = $serviceclass;
}
return $services;
}

/**
* Retrieves an array of error messages.
*
* @return array An array of error messages.
*/
public function get_errors(): array {
return $this->errors;
}
}
Loading

0 comments on commit 72b43c3

Please sign in to comment.