initial
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
/**
|
||||
* File Logs
|
||||
*
|
||||
* @package Advanced File Manager Pro
|
||||
*/
|
||||
|
||||
namespace AFMP\Modules;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
use AFMP\Modules\FileLogs\FileLogModel;
|
||||
|
||||
if ( ! class_exists( 'AFMP\\Modules\\FileLogs' ) ) :
|
||||
/**
|
||||
* Class FileLogs.
|
||||
*
|
||||
* @since 2.8
|
||||
*/
|
||||
class FileLogs {
|
||||
|
||||
/**
|
||||
* FileLogs Instance.
|
||||
*
|
||||
* @since 2.8
|
||||
* @var FileLogs $instance The single instance of the class.
|
||||
*/
|
||||
private static $instance = null;
|
||||
|
||||
|
||||
/**
|
||||
* FileLogs constructor.
|
||||
*
|
||||
* @since 2.8
|
||||
*/
|
||||
private function __construct() {
|
||||
add_filter( 'fma__opts_override', array( $this, 'opts_override' ), 1000 );
|
||||
add_action( 'admin_init', array( $this, 'admin_init' ) );
|
||||
|
||||
$this->include_files();
|
||||
}
|
||||
|
||||
/**
|
||||
* Override options.
|
||||
*
|
||||
* @param array $opts Options.
|
||||
* @since 2.8
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function opts_override( $opts ) {
|
||||
$opts['bind']['mkdir mkfile rename duplicate upload rm paste put'] = array( $this, 'write_log' );
|
||||
return $opts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write log.
|
||||
*
|
||||
* @param string $cmd Command.
|
||||
* @param array $result Result.
|
||||
* @param array $args Arguments.
|
||||
* @param \elFinder $elfinder elFinder instance.
|
||||
* @param \elFinderVolumeLocalFileSystem $volume Volume instance.
|
||||
*
|
||||
* @since 2.8
|
||||
*/
|
||||
public function write_log( $cmd, $result, $args, $elfinder, $volume ) {
|
||||
if ( ! empty( $result['removed'] ) ) {
|
||||
foreach ( $result['removed'] as $file ) {
|
||||
$this->check_performed_action( $cmd, $elfinder, $file, 'removed' );
|
||||
}
|
||||
} elseif ( ! empty( $result['added'] ) ) {
|
||||
foreach ( $result['added'] as $file ) {
|
||||
$this->check_performed_action( $cmd, $elfinder, $file );
|
||||
}
|
||||
} elseif ( ! empty( $result['changed'] ) ) {
|
||||
foreach ( $result['changed'] as $file ) {
|
||||
$this->check_performed_action( $cmd, $elfinder, $file );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check performed action.
|
||||
*
|
||||
* @since 2.8
|
||||
* @param string $cmd Command.
|
||||
* @param \elFinder $elfinder elFinder instance.
|
||||
* @param array $file File data.
|
||||
* @param string $action Action performed (added, removed, etc.).
|
||||
*/
|
||||
private function check_performed_action( $cmd, $elfinder, $file, $action = '' ) {
|
||||
$file_path = 'removed' === $action ? $file['realpath'] : $elfinder->realpath( $file['hash'] );
|
||||
|
||||
switch ( $cmd ) {
|
||||
case 'rename':
|
||||
FileLogModel::insert_item( 'renamed', $file_path, $file );
|
||||
break;
|
||||
case 'duplicate':
|
||||
FileLogModel::insert_item( 'duplicated', $file_path, $file );
|
||||
break;
|
||||
case 'upload':
|
||||
FileLogModel::insert_item( 'uploaded', $file_path, $file );
|
||||
break;
|
||||
case 'mkdir':
|
||||
case 'mkfile':
|
||||
FileLogModel::insert_item( 'created', $file_path, $file );
|
||||
break;
|
||||
case 'rm':
|
||||
FileLogModel::insert_item( 'deleted', $file_path, $file );
|
||||
break;
|
||||
case 'paste':
|
||||
FileLogModel::insert_item( 'pasted', $file_path, $file );
|
||||
break;
|
||||
case 'put':
|
||||
FileLogModel::insert_item( 'updated', $file_path, $file );
|
||||
break;
|
||||
default:
|
||||
FileLogModel::insert_item( sprintf( 'Unknown: %s', $cmd ), $file_path, $file );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin init.
|
||||
*
|
||||
* @since 2.8
|
||||
*/
|
||||
public function admin_init() {
|
||||
FileLogModel::create_table();
|
||||
}
|
||||
|
||||
/**
|
||||
* Include required files.
|
||||
*
|
||||
* @since 2.8
|
||||
*/
|
||||
private function include_files() {
|
||||
require_once plugin_dir_path( __FILE__ ) . 'filelogs/class-filelogmodel.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* FileLogs get instance.
|
||||
*
|
||||
* @since 2.8
|
||||
* @return FileLogs
|
||||
*/
|
||||
public static function get_instance() {
|
||||
if ( null === self::$instance ) {
|
||||
self::$instance = new FileLogs();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
||||
|
||||
FileLogs::get_instance();
|
||||
endif;
|
||||
+184
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
/**
|
||||
* FileLogModel class.
|
||||
*
|
||||
* @package File_Manager_Advanced_Pro
|
||||
*/
|
||||
|
||||
namespace AFMP\Modules\FileLogs;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
if ( ! class_exists( 'AFMP\\Modules\\FileLogs\\FileLogModel' ) ) :
|
||||
/**
|
||||
* FileLogModel class.
|
||||
*/
|
||||
class FileLogModel {
|
||||
/**
|
||||
* ID pf the file log.
|
||||
*
|
||||
* @since 2.8
|
||||
* @var int $id ID of the file log.
|
||||
*/
|
||||
public $id = 0;
|
||||
|
||||
/**
|
||||
* User ID of the user who performed the action.
|
||||
*
|
||||
* @since 2.8
|
||||
* @var int $user_id User ID of the user who performed the action.
|
||||
*/
|
||||
public $user_id = 0;
|
||||
|
||||
/**
|
||||
* Action performed.
|
||||
*
|
||||
* @since 2.8
|
||||
* @var string $action Action performed.
|
||||
*/
|
||||
public $action = '';
|
||||
|
||||
/**
|
||||
* Path of the file.
|
||||
*
|
||||
* @since 2.8
|
||||
* @var string $path Path of the file.
|
||||
*/
|
||||
public $path = '';
|
||||
|
||||
/**
|
||||
* Type of the file.
|
||||
*
|
||||
* @since 2.8
|
||||
* @var string $type Type of the file.
|
||||
*/
|
||||
public $type = '';
|
||||
|
||||
/**
|
||||
* Time of the action.
|
||||
*
|
||||
* @since 2.8
|
||||
* @var int $time Time of the action.
|
||||
*/
|
||||
public $time = 0;
|
||||
|
||||
/**
|
||||
* IP address of the user who performed the action.
|
||||
*
|
||||
* @since 2.8
|
||||
* @var string $ip IP address of the user who performed the action.
|
||||
*/
|
||||
public $ip = '0.0.0.0';
|
||||
|
||||
/**
|
||||
* Create the file logs table.
|
||||
*
|
||||
* @since 2.8
|
||||
*/
|
||||
public static function create_table() {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'fm_filelogs';
|
||||
$sql = 'SHOW TABLES LIKE %s';
|
||||
$prepared_sql = $wpdb->prepare( $sql, $table_name );
|
||||
$table_exists = $wpdb->get_var( $prepared_sql );
|
||||
|
||||
if ( $table_exists !== $table_name ) {
|
||||
$charset = $wpdb->get_charset_collate();
|
||||
$sql = 'CREATE TABLE IF NOT EXISTS %i (
|
||||
`id` INT ( 11 ) NOT NULL AUTO_INCREMENT,
|
||||
`user_id` INT ( 11 ) NOT NULL,
|
||||
`action` VARCHAR ( 255 ) NOT NULL,
|
||||
`path` VARCHAR ( 255 ) NOT NULL,
|
||||
`type` VARCHAR ( 255 ) NOT NULL,
|
||||
`ip` VARCHAR ( 255 ) NOT NULL,
|
||||
`time` VARCHAR ( 255 ) NOT NULL,
|
||||
PRIMARY KEY ( `id` )
|
||||
)' . $charset . ';';
|
||||
|
||||
$prepared_sql = $wpdb->prepare( $sql, $table_name );
|
||||
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
|
||||
dbDelta( $prepared_sql );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the client IP address.
|
||||
*
|
||||
* @since 2.8
|
||||
* @return string
|
||||
*/
|
||||
private static function get_client_ip() {
|
||||
$ip_attrs = array(
|
||||
'HTTP_CLIENT_IP',
|
||||
'HTTP_X_FORWARDED_FOR',
|
||||
'HTTP_X_FORWARDED',
|
||||
'HTTP_FORWARDED_FOR',
|
||||
'HTTP_FORWARDED',
|
||||
'REMOTE_ADDR',
|
||||
);
|
||||
$ip = '127.0.0.1';
|
||||
|
||||
foreach ( $ip_attrs as $ip_attr ) {
|
||||
if ( ! isset( $_SERVER[ $ip_attr ] ) ) continue;
|
||||
|
||||
if ( empty( $_SERVER[ $ip_attr ] ) ) continue;
|
||||
|
||||
$ip = sanitize_text_field( wp_unslash( $_SERVER[ $ip_attr ] ) );
|
||||
}
|
||||
|
||||
return apply_filters(
|
||||
'afmp__get_client_ip',
|
||||
$ip,
|
||||
'127.0.0.1' === $ip ? 'localhost' : ''
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new file log entry.
|
||||
*
|
||||
* @since 2.8
|
||||
* @param string $action Action performed (added, removed, renamed, etc.).
|
||||
* @param string $path Path of the file.
|
||||
* @param array $args Additional arguments (e.g., mime type).
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function insert_item( $action, $path, $args ) {
|
||||
$filelog = new self();
|
||||
|
||||
$filelog->user_id = get_current_user_id();
|
||||
$filelog->action = $action;
|
||||
$filelog->path = $path;
|
||||
$filelog->type = $args['mime'];
|
||||
$filelog->ip = self::get_client_ip();
|
||||
$filelog->time = current_time( 'timestamp' );
|
||||
|
||||
return $filelog->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the file log entry to the database.
|
||||
*
|
||||
* @since 2.8
|
||||
* @return int
|
||||
*/
|
||||
public function save() {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'fm_filelogs';
|
||||
|
||||
$data = array(
|
||||
'user_id' => $this->user_id,
|
||||
'action' => $this->action,
|
||||
'path' => $this->path,
|
||||
'type' => $this->type,
|
||||
'time' => $this->time,
|
||||
'ip' => $this->ip
|
||||
);
|
||||
|
||||
$wpdb->insert( $table_name, $data );
|
||||
$this->id = $wpdb->insert_id;
|
||||
|
||||
return $this->id;
|
||||
}
|
||||
}
|
||||
endif;
|
||||
Reference in New Issue
Block a user