2018-07-13 18:39:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2020-03-30 19:57:38 +02:00
|
|
|
* Copyright (c) Codiad & Kent Safranski (codiad.com), Telaaedifex distributed
|
2018-07-13 18:39:55 +02:00
|
|
|
* as-is and without warranty under the MIT License. See
|
|
|
|
* [root]/license.txt for more. This information must remain intact.
|
|
|
|
*/
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
require_once( '../../lib/diff_match_patch.php' );
|
|
|
|
require_once( '../../common.php' );
|
|
|
|
require_once( __DIR__ . '/class.archive.php' );
|
2018-07-13 18:39:55 +02:00
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
class Filemanager extends Common {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
const PATH_REGEX = '/[^\w\-\._@]/';
|
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// PROPERTIES
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// METHODS
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// -----------------------------||----------------------------- //
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Construct
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
public function __construct() {}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Clean a path
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public static function cleanPath( $path ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
// Prevent going out of the workspace
|
|
|
|
while ( strpos( $path, '../' ) !== false ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$path = str_replace( '../', '', $path );
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
|
|
|
|
if( self::isAbsPath( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$full_path = $path;
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$full_path = WORKSPACE . "/" . $path;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If a file with an invalid character exists and the user is
|
|
|
|
* trying to rename or delete it, allow the actual file name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
$invalid_characters = preg_match( '/[^A-Za-z0-9\-\._@\/\(\) ]/', $path );
|
|
|
|
|
|
|
|
if( $invalid_characters && ! ( $_GET['action'] == "modify" || $_GET['action'] == "delete" ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
exit( formatJSEND( "error", "Error, the filename contains invalid characters, please either rename or delete it." ) );
|
|
|
|
} elseif( $invalid_characters && ( $_GET['action'] == "modify" || $_GET['action'] == "delete" ) ) {
|
|
|
|
} else {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$path = preg_replace( '/[^A-Za-z0-9\-\._@\/\(\) ]/', '', $path );
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// DUPLICATE (Creates a duplicate of the object - (cut/copy/paste)
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function copy( $source, $destination, $replace = false ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => null,
|
|
|
|
);
|
|
|
|
|
|
|
|
$source = self::formatPath( $source );
|
|
|
|
$destination = self::formatPath( $destination );
|
|
|
|
$new_destination = $destination;
|
|
|
|
$path_parts = pathinfo( $destination );
|
|
|
|
$i = 1;
|
|
|
|
|
|
|
|
if( ! $replace ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
do {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if( is_dir( $new_destination ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$new_destination = rtrim( $destination, "/" ) . " $i/";
|
|
|
|
} elseif( is_file( $new_destination ) ) {
|
2019-05-01 15:11:50 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if( isset( $path_parts["extension"] ) ) {
|
2019-05-01 15:11:50 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$new_destination = str_replace( ".{$path_parts["extension"]}", " {$i}.{$path_parts["extension"]}", $destination );
|
|
|
|
} else {
|
2019-05-01 15:11:50 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$new_destination = $destination . " $i";
|
2019-05-01 15:11:50 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
} while( ( is_file( $new_destination ) || is_dir( $new_destination ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( file_exists( $source ) ) {
|
|
|
|
|
|
|
|
if( is_file( $source ) ) {
|
2019-05-01 15:11:50 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
copy( $source, $new_destination );
|
|
|
|
$response["status"] = "success";
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
self::recursive_copy( $source, $new_destination );
|
|
|
|
$response["status"] = "success";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Invalid Source";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
return $response;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2020-03-30 19:57:38 +02:00
|
|
|
// CREATE (Creates a new file or directory)
|
2018-09-21 03:44:10 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
public function create( $path, $type, $content = "" ) {
|
|
|
|
|
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => null,
|
|
|
|
);
|
|
|
|
$path = self::formatPath( $path );
|
|
|
|
|
|
|
|
if( Permissions::has_create( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
// Create file
|
|
|
|
if( $type == "file" ) {
|
|
|
|
|
|
|
|
if ( ! file_exists( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if ( $file = fopen( $path, 'w' ) ) {
|
|
|
|
|
|
|
|
// Write content
|
|
|
|
if ( $content ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
fwrite( $file, $content );
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
fclose( $file );
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "success";
|
|
|
|
$response["mtime"] = filemtime( $path );
|
|
|
|
} else {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Cannot Create File";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "File Already Exists";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
} elseif( $type == "directory" ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if ( ! is_dir( $path ) ) {
|
|
|
|
|
|
|
|
mkdir( $path );
|
|
|
|
$response["status"] = "success";
|
|
|
|
} else {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Directory Already Exists";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "You do not have permission to create files in this project";
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// DELETE (Deletes a file or directory (+contents or only contents))
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function delete( $path, $follow, $keep_parent = false ) {
|
|
|
|
|
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => null,
|
|
|
|
);
|
|
|
|
|
|
|
|
if( ! Common::checkPath( $path ) ) {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "You do not have access to delete this file";
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$path = self::formatPath( $path );
|
|
|
|
if ( file_exists( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
self::recursive_delete( $path, $follow, $keep_parent );
|
|
|
|
$response["status"] = "success";
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Path Does Not Exist ";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
return $response;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
public function find( $path, $query, $options = array() ) {
|
|
|
|
|
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => null,
|
|
|
|
);
|
|
|
|
$current_path = getcwd();
|
|
|
|
$path = self::formatPath( $path );
|
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
if ( ! function_exists( 'shell_exec' ) ) {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Shell_exec() Command Not Enabled.";
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
chdir( $path );
|
|
|
|
$input = str_replace( '"', '', $query );
|
2018-09-21 03:44:10 +02:00
|
|
|
$cmd = 'find -L ';
|
|
|
|
$strategy = '';
|
2020-03-30 19:57:38 +02:00
|
|
|
if ( ! empty( $options ) && isset( $options["strategy"] ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$strategy = $options["strategy"];
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
switch ( $strategy ) {
|
|
|
|
|
|
|
|
case 'substring':
|
2020-03-30 19:57:38 +02:00
|
|
|
|
|
|
|
$cmd = "$cmd -iname " . escapeshellarg( '*' . $input . '*' );
|
2018-09-21 03:44:10 +02:00
|
|
|
break;
|
|
|
|
case 'regexp':
|
2020-03-30 19:57:38 +02:00
|
|
|
|
|
|
|
$cmd = "$cmd -regex " . escapeshellarg( $input );
|
2018-09-21 03:44:10 +02:00
|
|
|
break;
|
|
|
|
case 'left_prefix':
|
|
|
|
default:
|
2020-03-30 19:57:38 +02:00
|
|
|
$cmd = "$cmd -iname " . escapeshellarg( $input . '*');
|
2018-09-21 03:44:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
$cmd = "$cmd -printf \"%h/%f %y\n\"";
|
|
|
|
$output = shell_exec( $cmd );
|
|
|
|
$file_arr = explode( "\n", $output );
|
|
|
|
$output_arr = array();
|
|
|
|
|
|
|
|
error_reporting( 0 );
|
|
|
|
|
|
|
|
foreach ( $file_arr as $i => $fentry ) {
|
|
|
|
|
|
|
|
$farr = explode( " ", $fentry );
|
|
|
|
$fname = trim( $farr[0] );
|
|
|
|
if ( $farr[1] == 'f' ) {
|
|
|
|
|
|
|
|
$ftype = 'file';
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$ftype = 'directory';
|
|
|
|
}
|
|
|
|
if ( strlen( $fname ) != 0 ) {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$fname = $path . substr( $fname, 2 );
|
2018-09-21 03:44:10 +02:00
|
|
|
$f = array( 'path' => $fname, 'type' => $ftype );
|
|
|
|
array_push( $output_arr, $f );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( count( $output_arr ) == 0 ) {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "No Results Returned";
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "success";
|
|
|
|
$response["index"] = $output_arr;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function formatPath( $path ) {
|
|
|
|
|
|
|
|
if( self::isAbsPath( $path ) ) {
|
|
|
|
|
|
|
|
$path = self::cleanPath( $path );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$path = WORKSPACE . "/" . self::cleanPath( $path );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( is_dir( $path ) ) {
|
|
|
|
|
|
|
|
$path = rtrim( $path, '/' ) . '/';
|
|
|
|
}
|
|
|
|
return( $path );
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2020-03-30 19:57:38 +02:00
|
|
|
// INDEX (Returns list of files and directories)
|
2018-09-21 03:44:10 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
public function index( $path ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => null,
|
|
|
|
);
|
|
|
|
$relative_path = rtrim( self::cleanPath( $path ), '/' ) . '/';
|
|
|
|
$path = self::formatPath( $path );
|
|
|
|
|
|
|
|
if( file_exists( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$index = array();
|
|
|
|
|
|
|
|
if( is_dir( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$files = $this->index_path( $path );
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "success";
|
|
|
|
$response["data"] = array( "index" => $files );
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Not A Directory";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Path Does Not Exist";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
return $response;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
function index_path( $path ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$paths = array();
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if( is_dir( $path ) && $handle = opendir( $path ) ) {
|
|
|
|
|
|
|
|
while( false !== ( $f = readdir( $handle ) ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if ( "$f" != '.' && "$f" != '..' ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$p = "$path" . DIRECTORY_SEPARATOR . "$f";
|
|
|
|
$p = str_replace( "//", "/", $p );
|
|
|
|
$rp = realpath( $p );
|
|
|
|
$path_info = pathinfo( $p );
|
|
|
|
|
|
|
|
if( is_dir( $p ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$children = $this->is_empty( $p ) ? null : array();
|
|
|
|
|
|
|
|
$paths[] = array(
|
|
|
|
|
|
|
|
"basename" => $path_info["basename"],
|
|
|
|
"children" => $children,
|
|
|
|
"dirname" => str_replace( WORKSPACE . "/", "", $p ),
|
|
|
|
"extension" => null,
|
|
|
|
"filename" => $path_info["filename"],
|
|
|
|
"full_dirname" => $path_info["dirname"],
|
|
|
|
"full_path" => $p,
|
|
|
|
"path" => str_replace( WORKSPACE . "/", "", $p ),
|
|
|
|
"type" => "directory",
|
|
|
|
);
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$paths[] = array(
|
|
|
|
"basename" => $path_info["basename"],
|
|
|
|
"dirname" => str_replace( WORKSPACE . "/", "", $p ),
|
|
|
|
"extension" => isset( $path_info["extension"] ) ? $path_info["extension"] : null,
|
|
|
|
"filename" => $path_info["filename"],
|
|
|
|
"path" => str_replace( WORKSPACE . "/", "", $p ),
|
|
|
|
"type" => "file",
|
|
|
|
);
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
closedir( $handle );
|
|
|
|
usort( $paths, array( $this, "sorter" ) );
|
|
|
|
return $paths;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
|
|
|
|
function is_empty( $dir ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$pass = true;
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if( is_dir( $dir ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$handle = opendir( $dir );
|
|
|
|
while( false !== ( $entry = readdir( $handle ) ) ) {
|
|
|
|
|
|
|
|
if( $entry != "." && $entry != ".." ) {
|
|
|
|
|
|
|
|
$pass = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( $handle );
|
|
|
|
}
|
|
|
|
return $pass;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
function sorter( $a, $b ) {
|
|
|
|
|
|
|
|
$basename = strnatcmp( $a["basename"], $b["basename"] );
|
|
|
|
$type = strnatcmp( $a["type"], $b["type"] );
|
|
|
|
$result = 0;
|
|
|
|
|
|
|
|
if( $type == 0 ) {
|
|
|
|
|
|
|
|
$result = $basename;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$result = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
2020-03-30 19:57:38 +02:00
|
|
|
// MODIFY (Modifies a file name/contents or directory name)
|
2018-09-21 03:44:10 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
public function modify( $path, $content, $patch = false, $mtime = 0 ) {
|
|
|
|
|
|
|
|
// Change content
|
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => null,
|
|
|
|
);
|
|
|
|
$path = self::formatPath( $path );
|
|
|
|
|
|
|
|
if( $content == ' ' ) {
|
|
|
|
|
|
|
|
$content = ''; // Blank out file
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! Permissions::has_write( $path ) ) {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "You do not have access to write to this file.";
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $patch && ! $mtime ) {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "invalid mtime parameter not found";
|
|
|
|
$response["mtime"] = $mtime;
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( is_file( $path ) ) {
|
|
|
|
|
|
|
|
$serverMTime = filemtime( $path );
|
|
|
|
$fileContents = file_get_contents( $path );
|
|
|
|
|
|
|
|
if( $patch && $mtime != $serverMTime ) {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Client is out of sync";
|
|
|
|
//DEBUG : file_put_contents($this->path.".conflict", "SERVER MTIME :".$serverMTime.", CLIENT MTIME :".$this->mtime);
|
|
|
|
return $response;
|
|
|
|
} elseif( strlen( trim( $patch ) ) == 0 && ! $content ) {
|
|
|
|
|
|
|
|
// Do nothing if the patch is empty and there is no content
|
|
|
|
$response["status"] = "success";
|
|
|
|
$response["data"] = array(
|
|
|
|
"mtime" => $serverMTime
|
|
|
|
);
|
|
|
|
return $response;
|
|
|
|
}
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if( $file = fopen( $path, 'w' ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if( $patch ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$dmp = new diff_match_patch();
|
|
|
|
$p = $dmp->patch_apply( $dmp->patch_fromText( $patch ), $fileContents );
|
|
|
|
$content = $p[0];
|
|
|
|
//DEBUG : file_put_contents($this->path.".orig",$fileContents );
|
|
|
|
//DEBUG : file_put_contents($this->path.".patch", $this->patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fwrite( $file, $content ) === false ) {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "could not write to file";
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
// Unless stat cache is cleared the pre-cached mtime will be
|
|
|
|
// returned instead of new modification time after editing
|
|
|
|
// the file.
|
|
|
|
clearstatcache();
|
|
|
|
$response["status"] = "success";
|
|
|
|
$response["data"] = array(
|
|
|
|
"mtime" => filemtime( $path )
|
|
|
|
);
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
fclose( $file );
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Cannot Write to File";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Not A File";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function move( $path, $new_path ) {
|
|
|
|
|
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
);
|
|
|
|
$path = self::formatPath( $path );
|
|
|
|
$new_path = self::formatPath( $new_path );
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if ( ! file_exists( $new_path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if( rename( $path, $new_path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "success";
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Could Not Rename";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Path Already Exists";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
return $response;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2020-03-30 19:57:38 +02:00
|
|
|
// OPEN (Returns the contents of a file)
|
2018-09-21 03:44:10 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
public function open( $path ) {
|
2018-10-11 16:17:41 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => null,
|
|
|
|
);
|
2018-10-11 16:17:41 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$relative_path = self::cleanPath( $path );
|
|
|
|
$path = self::formatPath( $path );
|
|
|
|
|
|
|
|
if ( is_file( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$output = file_get_contents( $path );
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if ( extension_loaded( 'mbstring' ) ) {
|
|
|
|
|
|
|
|
if ( ! mb_check_encoding( $output, 'UTF-8' ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if ( mb_check_encoding( $output, 'ISO-8859-1' ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$output = utf8_encode( $output );
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$output = mb_convert_encoding( $content, 'UTF-8' );
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "success";
|
|
|
|
$response["data"] = array(
|
|
|
|
"content" => $output,
|
|
|
|
"mtime" => filemtime( $path ),
|
|
|
|
"read_only" => ( ! Permissions::has_write( $path ) ),
|
|
|
|
);
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Error, {$path} is not a file.";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
return $response;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2020-03-30 19:57:38 +02:00
|
|
|
// OPEN IN BROWSER (Return URL)
|
2018-09-21 03:44:10 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
public function preview( $path ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$protocol = ( ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] != 'off' ) || $_SERVER['SERVER_PORT'] == 443 ) ? "https://" : "http://";
|
|
|
|
$domainName = $_SERVER['HTTP_HOST'];
|
|
|
|
$url = $protocol . WSURL . '/' . self::cleanPath( $path );
|
|
|
|
$response = array(
|
|
|
|
"status" => "success",
|
|
|
|
"data" => rtrim( $url, "/" ),
|
|
|
|
);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function recursive_copy( $source, $destination ) {
|
|
|
|
|
|
|
|
$dir = opendir( $source );
|
|
|
|
@mkdir( $source );
|
|
|
|
|
|
|
|
if( is_dir( $source ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
@mkdir( $destination );
|
|
|
|
} else {
|
2019-05-01 15:11:50 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( false !== ( $file = readdir( $dir ) ) ) {
|
|
|
|
|
|
|
|
if ( ( $file != '.' ) && ( $file != '..' ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if ( is_dir( $source . '/' . $file ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
self::recursive_copy( $source . '/' . $file, $destination . '/' . $file );
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
copy( $source . '/' . $file, $destination . '/' . $file );
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
}
|
|
|
|
closedir( $dir );
|
|
|
|
}
|
|
|
|
|
|
|
|
static function recursive_delete( $path, $follow, $keep_parent = false ) {
|
|
|
|
|
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => null,
|
|
|
|
"keep_parent" => $keep_parent,
|
|
|
|
);
|
|
|
|
$status = "none";
|
|
|
|
|
|
|
|
if ( is_file( $path ) ) {
|
|
|
|
|
|
|
|
unlink( $path );
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
2020-03-30 19:57:38 +02:00
|
|
|
|
|
|
|
$files = array_diff( scandir( $path ), array( '.', '..' ) );
|
|
|
|
foreach ( $files as $file ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if( is_link( $path . "/" . $file ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if( $follow ) {
|
|
|
|
|
|
|
|
$status = self::recursive_delete( $path . "/" . $file, $follow, false );
|
|
|
|
}
|
|
|
|
$status = unlink( $path . "/" . $file );
|
|
|
|
} elseif( is_dir( $path . "/" . $file ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$status = self::recursive_delete( $path . "/" . $file, $follow, false );
|
|
|
|
} else {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$status = unlink( $path . "/" . $file );
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( $keep_parent === false ) {
|
|
|
|
|
|
|
|
$status = rmdir( $path );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$response["message"] = "Removed $path";
|
|
|
|
$response["status"] = $status;
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reverse_recursive_delete( $start, $stop, $files ) {
|
|
|
|
|
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => null,
|
|
|
|
);
|
|
|
|
$path_array = explode( "/", $start );
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
$p = implode( "/", $path_array );
|
|
|
|
|
|
|
|
if( is_dir( $p ) && $p !== $stop ) {
|
|
|
|
|
|
|
|
if( $files ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$this->recursive_delete( $p, true );
|
|
|
|
} else {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$files = array_diff( scandir( $p ), array( '.', '..' ) );
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if( count( $files ) == 0 ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$this->recursive_delete( $p, true );
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
break;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
break;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
array_pop( $path_array );
|
|
|
|
} while( count( $path_array ) > 1 );
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
return $response;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2020-03-30 19:57:38 +02:00
|
|
|
// SEARCH
|
2018-09-21 03:44:10 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
public function search( $path, $query, $options ) {
|
|
|
|
|
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => null,
|
|
|
|
);
|
|
|
|
|
|
|
|
if( ! common::isAbsPath( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$path = WORKSPACE . "/$path";
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if ( ! function_exists( 'shell_exec' ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "Shell_exec() Command Not Enabled.";
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$return = array();
|
|
|
|
$input = str_replace( '"', '', $query );
|
|
|
|
$cmd = 'find -L ' . escapeshellarg( $path ) . ' -iregex ' . escapeshellarg( '.*' . $options["filetype"] ) . ' -type f -print0 | xargs -0 grep -i -I -n -R -H ' . escapeshellarg( $input ) . '';
|
|
|
|
$output = shell_exec( $cmd );
|
|
|
|
$output_arr = explode( "\n", $output );
|
|
|
|
foreach ( $output_arr as $line ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$data = explode( ":", $line );
|
|
|
|
$da = array();
|
|
|
|
if ( count( $data ) > 2 ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$da['line'] = $data[1];
|
|
|
|
$da['file'] = str_replace( $path, '', $data[0] );
|
|
|
|
$da['result'] = str_replace( WORKSPACE . '/', '', $data[0] );
|
|
|
|
$da['string'] = str_replace( $data[0] . ":" . $data[1] . ':', '', $line );
|
|
|
|
$return[] = $da;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
if ( count( $return ) == 0 ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "No Results Returned";
|
2018-09-21 03:44:10 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "success";
|
|
|
|
$response["data"] = array();
|
|
|
|
$response["data"]["index"] = $return;
|
|
|
|
$response["data"]["cmd"] = $cmd;
|
|
|
|
$response["data"]["output"] = $output;
|
|
|
|
$response["data"]["output_array"] = $output_arr;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
return $response;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
public function stitch( $path ) {
|
|
|
|
|
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => "",
|
|
|
|
);
|
|
|
|
|
|
|
|
if( ! Permissions::has_write( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "You do not have access to write to this file.";
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! common::isAbsPath( $path ) ) {
|
|
|
|
|
|
|
|
$path = WORKSPACE . "/$path";
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = $_POST["path"];
|
|
|
|
$tmp = DATA . "tmp/$path/";
|
|
|
|
$dir = dirname( $path );
|
|
|
|
$name = basename( $path );
|
|
|
|
$files = scandir( $tmp );
|
|
|
|
|
|
|
|
if( ! is_dir( $dir ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
mkdir( $dir, 0755, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach( $files as $id => $file ) {
|
|
|
|
|
|
|
|
if( $file !== "." && $file !== ".." ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$data = file_get_contents( $cache_path . $file );
|
|
|
|
$handle = fopen( $path, "a" );
|
|
|
|
$status = fwrite( $handle, $data );
|
|
|
|
fclose( $handle );
|
|
|
|
unlink( $cache_path . $file );
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
|
|
|
|
$tmp_array = explode( "/", $path );
|
|
|
|
$remove = array();
|
|
|
|
|
|
|
|
while( count( $tmp_array ) > 0 ) {
|
|
|
|
|
|
|
|
$remove[] = DATA . "tmp/" . implode( "/", $tmp_array );
|
|
|
|
array_pop( $tmp_array );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach( $tmp_array as $id => $i ) {
|
|
|
|
|
|
|
|
rmdir( $i );
|
|
|
|
}
|
|
|
|
return $response;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2020-03-30 19:57:38 +02:00
|
|
|
// UPLOAD (Handles uploads to the specified directory)
|
2018-09-21 03:44:10 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
public function upload_blob( $path, $index, $blob ) {
|
|
|
|
|
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => "",
|
|
|
|
);
|
|
|
|
|
|
|
|
if( ! Permissions::has_write( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "You do not have access to write to this file.";
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! common::isAbsPath( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$path = WORKSPACE . "/$path";
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! is_dir( UPLOAD_CACHE . "$path/" ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
mkdir( UPLOAD_CACHE . "$path/", 0755, true );
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$handle = fopen( UPLOAD_CACHE . "$path/$index", "a" );
|
|
|
|
$status = fwrite( $handle, $blob );
|
|
|
|
fclose( $handle );
|
|
|
|
|
|
|
|
if( $status === false ) {
|
|
|
|
|
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "File could not be written to.";
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$response["status"] = "success";
|
|
|
|
$response["path"] = $path;
|
|
|
|
$response["bytes"] = $status;
|
|
|
|
$response["message"] = "$status bytes written to file.";
|
|
|
|
}
|
|
|
|
return $response;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
public function upload_stitch( $path ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response = array(
|
|
|
|
"status" => "none",
|
|
|
|
"message" => "",
|
|
|
|
);
|
|
|
|
$status = true;
|
|
|
|
|
|
|
|
if( ! Permissions::has_write( $path ) ) {
|
2018-09-21 03:44:10 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "You do not have access to write to this file.";
|
|
|
|
return $response;
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
if( ! common::isAbsPath( $path ) ) {
|
2019-05-01 15:11:50 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$path = WORKSPACE . "/$path";
|
|
|
|
}
|
|
|
|
|
|
|
|
$cache_path = UPLOAD_CACHE . "$path/";
|
|
|
|
$dir = dirname( $path );
|
|
|
|
$name = basename( $path );
|
|
|
|
|
|
|
|
if( ! is_dir( $dir ) ) {
|
2019-05-01 15:11:50 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
mkdir( $dir, 0755, true );
|
2019-05-01 15:11:50 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$files = scandir( $cache_path );
|
2019-05-01 15:11:50 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
foreach( $files as $id => $file ) {
|
|
|
|
|
|
|
|
if( $file !== "." && $file !== ".." ) {
|
|
|
|
|
|
|
|
$data = file_get_contents( $cache_path . $file );
|
|
|
|
$handle = fopen( $path, "a" );
|
|
|
|
$status = fwrite( $handle, $data );
|
|
|
|
fclose( $handle );
|
|
|
|
unlink( $cache_path . $file );
|
|
|
|
}
|
|
|
|
}
|
2019-05-01 15:11:50 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$rm_status = $this->reverse_recursive_delete( $cache_path, UPLOAD_CACHE, false );
|
|
|
|
|
|
|
|
if( $status === false ) {
|
2019-05-01 15:11:50 +02:00
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "error";
|
|
|
|
$response["message"] = "File could not be written to.";
|
2019-05-01 15:11:50 +02:00
|
|
|
} else {
|
|
|
|
|
2020-03-30 19:57:38 +02:00
|
|
|
$response["status"] = "success";
|
|
|
|
$response["path"] = $path;
|
|
|
|
$response["bytes"] = $status;
|
|
|
|
$response["message"] = "$status bytes written to file.";
|
|
|
|
$response["remove"] = $rm_status;
|
2019-05-01 15:11:50 +02:00
|
|
|
}
|
2020-03-30 19:57:38 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function upload_clean_stitches() {
|
|
|
|
|
|
|
|
$path = UPLOAD_CACHE;
|
|
|
|
|
2018-09-21 03:44:10 +02:00
|
|
|
}
|
2019-05-01 15:11:50 +02:00
|
|
|
}
|