Skip to content

Commit

Permalink
Plugin: CleanDeletedFiles: Add plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nosolored committed Nov 18, 2020
1 parent ddfdca1 commit 592ebf6
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugin/cleandeletedfiles/README.md
@@ -0,0 +1,4 @@
CleanDeleted Files Maintenance plugin
===

This plugin allows the administrator to permanently delete files marked as deleted
190 changes: 190 additions & 0 deletions plugin/cleandeletedfiles/admin.php
@@ -0,0 +1,190 @@
<?php
/* For licensing terms, see /license.txt */

/**
* Plugin.
*
* @author Jose Angel Ruiz
*
* @package chamilo.plugin.CleanDeletedFiles
*/
$cidReset = true;
require_once __DIR__.'/config.php';

api_protect_admin_script();

/** @var \CleanDeletedFilesPlugin $plugin */
$plugin = CleanDeletedFilesPlugin::create();
$plugin_info = $plugin->get_info();

$isPlatformAdmin = api_is_platform_admin();

if ($plugin->isEnabled() && $isPlatformAdmin) {
$htmlHeadXtra[] = '<script>
$(document).ready(function() {
$(".delete-file").click(function(e) {
e.preventDefault();
var path = $(this).prop("href").substr(7);
if (confirm("'.$plugin->get_lang("ConfirmDelete").'")) {
$.post(
"src/ajax.php",
{a:"delete-file", path:path},
function(data){
if (data.status == "false") {
alert(data.message);
} else {
location.reload();
}
},
"json"
);
}
});
$(".select_all").click(function(e) {
var id = $(this).prop("id").substr(7);
if( $(this).prop("checked") ) {
$(".checkbox-"+id).prop( "checked", true);
} else {
$(".checkbox-"+id).prop( "checked", false);
}
});
$("#delete-selected-files").click(function(e) {
if (confirm("'.$plugin->get_lang("ConfirmDeleteFiles").'")) {
var list = [];
$.each($(".checkbox-item:checked"), function() {
list.push($(this).prop("id"));
});
$.post(
"src/ajax.php",
{a:"delete-files-list", list:list},
function(data){
if (data.status == "false") {
alert(data.message);
} else {
location.reload();
}
},
"json"
);
}
});
});
</script>';

$nameTools = $plugin->get_lang("FileList");
Display::display_header($nameTools);
echo Display::page_header($nameTools);

$pathList = [
"app/courses",
"app/upload",
];

function findDeletedFiles($pathRelative) {
global $sizePath;
$pathAbsolute = api_get_path(SYS_PATH).$pathRelative;
$result = [];
if (is_dir($pathAbsolute)) {
$dir = dir($pathAbsolute);
while ($file = $dir->read()) {
if (is_file($pathAbsolute.'/'.$file)) {
$filesize = round(filesize($pathAbsolute.'/'.$file)/1024, 1);
$pos = strpos($file, "DELETED");
if ($pos !== FALSE) {
$result[] = [
'path_complete' => $pathAbsolute.'/'.$file,
'path_relative' => $pathRelative.'/'.$file,
'size' => $filesize,
];
$sizePath += $filesize;
}
} else if ($file!='..' && $file!='.') {
$result = array_merge($result, findDeletedFiles($pathRelative.'/'.$file));
}
}
}
return $result;
}

$sizeTotal = 0;
$i = 0;
foreach ($pathList as $pathItem) {
$sizePath = 0;
$filesDeletedList = findDeletedFiles($pathItem);
echo Display::page_subheader($plugin->get_lang("path_dir").": ".$pathItem);

if (count($filesDeletedList) > 0) {
echo "<ul>";
echo "<li>".$plugin->get_lang('FilesDeletedMark').": <strong>".count($filesDeletedList)."</strong>";
echo "<li>".$plugin->get_lang('FileDirSize').": ";
if ($sizePath >= 1024) {
echo "<strong>".round($sizePath/1024,1)." Mb</strong>";
} else {
echo "<strong>".$sizePath." Kb</strong>";
}
echo "</ul>";

$header = [
[
'<input type="checkbox" id="select_'.$i.'" class="select_all" />',
false,
null,
['style' => 'text-align:center']
],
[$plugin->get_lang('path_dir'), true],
[$plugin->get_lang('size'), true, null, ['style' => 'min-width:85px']],
[get_lang('Actions'), false],
];

$data = [];
$deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL);

foreach ($filesDeletedList as $value) {
$tools = Display::url(
$deleteIcon,
'file://'.$value['path_complete'],
['class' => 'delete-file']
);

$row = [
'<input type="checkbox"
class="checkbox-'.$i.' checkbox-item"
id="file://'.$value['path_complete'].'" />',
$value['path_relative'],
$value['size'].' '.($value['size'] >= 1024 ? 'Mb' : 'Kb'),
$tools
];
$data[] = $row;
}

echo Display::return_sortable_table(
$header,
$data,
[],
['per_page' => 100],
[]
);
} else {
$message = $plugin->get_lang('NoFilesDeleted');
echo Display::return_message($message, 'warning', false);
}
$sizeTotal += $sizePath;
echo '<hr>';
$i++;
}

if ($sizeTotal >= 1024) {
echo $plugin->get_lang('SizeTotalAllDir').": <strong>".round($sizeTotal/1024,1).' Mb</strong>';
} else {
echo $plugin->get_lang('SizeTotalAllDir').": <strong>".$sizeTotal.' Kb</strong>';
}
echo '<hr>';
echo '<a href="#" id="delete-selected-files" class="btn btn-primary">'.
$plugin->get_lang("DeleteSelectedFiles").
'</a>';

Display::display_footer();
}
10 changes: 10 additions & 0 deletions plugin/cleandeletedfiles/config.php
@@ -0,0 +1,10 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Config the plugin.
*
* @author Jose Angel Ruiz
*
* @package chamilo.plugin.CleanDeletedFiles
*/
require_once __DIR__.'/../../main/inc/global.inc.php';
3 changes: 3 additions & 0 deletions plugin/cleandeletedfiles/index.php
@@ -0,0 +1,3 @@
<?php
/* For license terms, see /license.txt */
require_once 'config.php';
1 change: 1 addition & 0 deletions plugin/cleandeletedfiles/install.php
@@ -0,0 +1 @@
<?php
17 changes: 17 additions & 0 deletions plugin/cleandeletedfiles/lang/english.php
@@ -0,0 +1,17 @@
<?php
$strings['plugin_title'] = "Clean deleted files";
$strings['plugin_comment'] = "Permanently delete files marked as deleted";
$strings['tool_enable'] = "Enable plugin";
$strings['FileList'] = "List of files marked as deleted";
$strings['SizeTotalAllDir'] = "Total size (all directories)";
$strings['NoFilesDeleted'] = "There are no files marked as deleted";
$strings['FilesDeletedMark'] = "Files marked as deleted";
$strings['FileDirSize'] = "Directory files size";
$strings['ConfirmDelete'] = "Are you sure you want to delete the file?";
$strings['ErrorDeleteFile'] = "An error occurred while deleting the file";
$strings['ErrorEmptyPath'] = "There was a problem deleting the file, the path cannot be empty";
$strings['DeleteSelectedFiles'] = "Delete selected files";
$strings['ConfirmDeleteFiles'] = "Are you sure you want to delete all the selected files?";
$strings['DeletedSuccess'] = "The file deletion was successful";
$strings['path_dir'] = "Directory";
$strings['size'] = "Size";
17 changes: 17 additions & 0 deletions plugin/cleandeletedfiles/lang/spanish.php
@@ -0,0 +1,17 @@
<?php
$strings['plugin_title'] = "Limpiar ficheros borrados";
$strings['plugin_comment'] = "Elimina de forma definitiva los ficheros marcados como eliminados";
$strings['tool_enable'] = "Activar plugin";
$strings['FileList'] = "Lista de archivos marcados como eliminados";
$strings['SizeTotalAllDir'] = "Tamaño total (todos los directorios)";
$strings['NoFilesDeleted'] = "No hay ficheros marcados como eliminados";
$strings['FilesDeletedMark'] = "Ficheros marcados como eliminados";
$strings['FileDirSize'] = "Tamaño de los ficheros del directorio";
$strings['ConfirmDelete'] = "¿Está seguro que desea borrar el fichero?";
$strings['ErrorDeleteFile'] = "Se ha producido un error al borrar el fichero";
$strings['ErrorEmptyPath'] = "Ha habido un problema al borrar el fichero, la ruta no puede ser vacía";
$strings['DeleteSelectedFiles'] = "Borrar archivos seleccionados";
$strings['ConfirmDeleteFiles'] = "¿Esta seguro que desea borrar todos los ficheros seleccionados?";
$strings['DeletedSuccess'] = "El borrado de archivos ha sido correcto";
$strings['path_dir'] = "Directorio";
$strings['size'] = "Tamaño";
14 changes: 14 additions & 0 deletions plugin/cleandeletedfiles/plugin.php
@@ -0,0 +1,14 @@
<?php
/* For licensing terms, see /license.txt */

/**
* Plugin.
*
* @author Jose Angel Ruiz
*
* @package chamilo.plugin.CleanDeletedFiles
*/

/* Plugin config */
require_once __DIR__.'/config.php';
$plugin_info = CleanDeletedFilesPlugin::create()->get_info();
35 changes: 35 additions & 0 deletions plugin/cleandeletedfiles/src/CleanDeletedFilesPlugin.php
@@ -0,0 +1,35 @@
<?php
/* For licensing terms, see /license.txt */

/**
* Clean deleted files plugin.
*
* @author Jose Angel Ruiz
*
* @package chamilo.plugin.CleanDeletedFiles
*/
class CleanDeletedFilesPlugin extends Plugin
{
public $isAdminPlugin = true;

/**
* Class constructor.
*/
protected function __construct()
{
$version = '1.0';
$author = 'José Angel Ruiz (NOSOLORED)';
parent::__construct($version, $author, ['enabled' => 'boolean']);
$this->isAdminPlugin = true;
}

/**
* @return RedirectionPlugin
*/
public static function create()
{
static $result = null;

return $result ? $result : $result = new self();
}
}
46 changes: 46 additions & 0 deletions plugin/cleandeletedfiles/src/ajax.php
@@ -0,0 +1,46 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Responses to AJAX calls
*/

require_once '../config.php';

api_protect_admin_script();

$plugin = CleanDeletedFilesPlugin::create();
$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;

switch ($action) {
case 'delete-file':
$path = isset($_REQUEST['path']) ? $_REQUEST['path'] : null;
if (empty($path)) {
echo json_encode(["status" => "false", "message"=>$plugin->get_lang('ErrorEmptyPath')]);
exit;
}

if (unlink($path)) {
Display::addFlash($plugin->get_lang("DeletedSuccess"), 'success');
echo json_encode(["status" => "true"]);
} else {
echo json_encode(["status" => "false", "message"=>$plugin->get_lang('ErrorDeleteFile')]);
}
break;
case 'delete-files-list':
$list = isset($_REQUEST['list']) ? $_REQUEST['list'] : [];
if (empty($list)) {
echo json_encode(["status" => "false", "message"=>$plugin->get_lang('ErrorEmptyPath')]);
exit;
}

foreach ($list as $value) {
if (empty($value)) {
continue;
}
unlink($value);
}

Display::addFlash($plugin->get_lang("DeletedSuccess"), 'success');
echo json_encode(["status" => "true"]);
break;
}

0 comments on commit 592ebf6

Please sign in to comment.