forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
056f473
commit 72b43c3
Showing
16 changed files
with
1,214 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.