"none", "message" => null, ); $path = self::formatPath( $path ); // Create file if( $type == "file" ) { if ( ! file_exists( $path ) ) { if ( $file = fopen( $path, 'w' ) ) { // Write content if ( $content ) { fwrite( $file, $content ); } fclose( $file ); $response["status"] = "success"; $response["mtime"] = filemtime( $path ); } else { $response["status"] = "error"; $response["message"] = "Cannot Create File"; } } else { $response["status"] = "error"; $response["message"] = "File Already Exists"; } } elseif( $type == "directory" ) { if ( ! is_dir( $path ) ) { mkdir( $path ); $response["status"] = "success"; } else { $response["status"] = "error"; $response["message"] = "Directory Already Exists"; } } 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"] = "No access."; } else { $path = self::formatPath( $path ); if ( file_exists( $path ) ) { self::recursive_delete( $path, $follow, $keep_parent ); $response["status"] = "success"; } else { $response["status"] = "error"; $response["message"] = "Path Does Not Exist "; } } return $response; } ////////////////////////////////////////////////////////////////// // DUPLICATE (Creates a duplicate of the object - (cut/copy/paste) ////////////////////////////////////////////////////////////////// public function duplicate( $source, $destination ) { $response = array( "status" => "none", "message" => null, ); $source = self::formatPath( $source ); $destination = self::formatPath( $destination ); $new_destination = $destination; $path_parts = pathinfo( $destination ); $i = 1; do { if( is_dir( $new_destination ) ) { $new_destination = rtrim( $destination, "/" ) . " $i/"; } elseif( is_file( $new_destination ) ) { if( isset( $path_parts["extension"] ) ) { $new_destination = str_replace( ".{$path_parts["extension"]}", " {$i}.{$path_parts["extension"]}", $destination ); } else { $new_destination = $destination . " $i"; } } $i++; } while( ( is_file( $new_destination ) || is_dir( $new_destination ) ) ); if( file_exists( $source ) ) { if( is_file( $source ) ) { copy( $source, $new_destination ); $response["status"] = "success"; } else { self::recursive_copy( $source, $new_destination ); $response["status"] = "success"; } } else { $response["status"] = "error"; $response["message"] = "Invalid Source"; } return $response; } public function find( $path, $query, $options = array() ) { $response = array( "status" => "none", "message" => null, ); $current_path = getcwd(); $path = self::formatPath( $path ); if ( ! function_exists( 'shell_exec' ) ) { $response["status"] = "error"; $response["message"] = "Shell_exec() Command Not Enabled."; } else { chdir( $path ); $input = str_replace( '"', '', $query ); $cmd = 'find -L '; $strategy = ''; if ( ! empty( $options ) && isset( $options["strategy"] ) ) { $strategy = $options["strategy"]; } switch ( $strategy ) { case 'substring': $cmd = "$cmd -iname " . escapeshellarg( '*' . $input . '*' ); break; case 'regexp': $cmd = "$cmd -regex " . escapeshellarg( $input ); break; case 'left_prefix': default: $cmd = "$cmd -iname " . escapeshellarg( $input . '*'); 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 ) { $fname = $path . substr( $fname, 2 ); $f = array( 'path' => $fname, 'type' => $ftype ); array_push( $output_arr, $f ); } } if ( count( $output_arr ) == 0 ) { $response["status"] = "error"; $response["message"] = "No Results Returned"; } else { $response["status"] = "success"; $response["index"] = $output_arr; } } 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 ); } ////////////////////////////////////////////////////////////////// // INDEX (Returns list of files and directories) ////////////////////////////////////////////////////////////////// public function index( $path ) { $response = array( "status" => "none", "message" => null, ); $relative_path = rtrim( self::cleanPath( $path ), '/' ) . '/'; $path = self::formatPath( $path ); if( file_exists( $path ) ) { $index = array(); if( is_dir( $path ) && $handle = opendir( $path ) ) { while( false !== ( $object = readdir( $handle ) ) ) { if( $object != "." && $object != ".." && $object != "" ) { $full_path = $path . '/' . $object; if( is_link( $full_path ) ) { $full_path = readlink( $full_path ); } if ( is_dir( $full_path ) ) { $type = "directory"; $size = count( glob( $path . '/' . $object . '/*' ) ); } else { $type = "file"; $size = @filesize( $path . '/' . $object ); } $index[] = array( "name" => $relative_path . $object, "type" => $type, "size" => $size ); } } $folders = array(); $files = array(); foreach( $index as $item => $data ) { if ( $data['type'] == 'directory' ) { $folders[] = array( "name" => htmlentities( $data['name'], ENT_QUOTES ), "type" => $data['type'], "size" => $data['size'] ); } if ( $data['type'] == 'file' ) { $files[] = array( "name" => htmlentities( $data['name'], ENT_QUOTES ), "type" => $data['type'], "size" => $data['size'] ); } } function sorter( $a, $b, $key = 'name' ) { return strnatcmp( $a[$key], $b[$key] ); } usort( $folders, "sorter" ); usort( $files, "sorter" ); $output = array_merge( $folders, $files ); $response["status"] = "success"; $response["data"] = array( "index" => $output ); } else { $response["status"] = "error"; $response["message"] = "Not A Directory"; } } else { $response["status"] = "error"; $response["message"] = "Path Does Not Exist"; } return $response; } ////////////////////////////////////////////////////////////////// // MODIFY (Modifies a file name/contents or directory name) ////////////////////////////////////////////////////////////////// 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( $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; } if( $file = fopen( $path, 'w' ) ) { if( $patch ) { $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"; } else { // 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 ) ); } fclose( $file ); } else { $response["status"] = "error"; $response["message"] = "Cannot Write to File"; } } else { $response["status"] = "error"; $response["message"] = "Not A File"; } return $response; } public function move( $path, $new_path ) { $response = array( "status" => "none", ); $path = self::formatPath( $path ); $new_path = self::formatPath( $new_path ); if ( ! file_exists( $new_path ) ) { if( rename( $path, $new_path ) ) { $response["status"] = "success"; } else { $response["status"] = "error"; $response["message"] = "Could Not Rename"; } } else { $response["status"] = "error"; $response["message"] = "Path Already Exists"; } return $response; } ////////////////////////////////////////////////////////////////// // OPEN (Returns the contents of a file) ////////////////////////////////////////////////////////////////// public function open( $path ) { $response = array( "status" => "none", "message" => null, ); $relative_path = self::cleanPath( $path ); $path = self::formatPath( $path ); if ( is_file( $path ) ) { $output = file_get_contents( $path ); if ( extension_loaded( 'mbstring' ) ) { if ( ! mb_check_encoding( $output, 'UTF-8' ) ) { if ( mb_check_encoding( $output, 'ISO-8859-1' ) ) { $output = utf8_encode( $output ); } else { $output = mb_convert_encoding( $content, 'UTF-8' ); } } } $response["status"] = "success"; $response["data"] = array( "content" => $output, "mtime" => filemtime( $path ), ); } else { $response["status"] = "error"; $response["message"] = "Error, {$path} is not a file."; } return $response; } ////////////////////////////////////////////////////////////////// // OPEN IN BROWSER (Return URL) ////////////////////////////////////////////////////////////////// public function openinbrowser( $path ) { $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 ) ) { @mkdir( $destination ); } else { return; } while ( false !== ( $file = readdir( $dir ) ) ) { if ( ( $file != '.' ) && ( $file != '..' ) ) { if ( is_dir( $source . '/' . $file ) ) { self::recurse_copy( $source . '/' . $file, $destination . '/' . $file ); } else { copy( $source . '/' . $file, $destination . '/' . $file ); } } } closedir( $dir ); } static function recursive_delete( $path, $follow, $keep_parent = false ) { if ( is_file( $path ) ) { unlink( $path ); } else { $files = array_diff( scandir( $path ), array( '.', '..' ) ); foreach ( $files as $file ) { if ( is_link( $path . "/" . $file ) ) { if ( $follow ) { self::recursive_delete( $path . "/" . $file, $follow, false); } unlink( $path . "/" . $file ); } elseif ( is_dir( $path . "/" . $file ) ) { self::recursive_delete( $path . "/" . $file, $follow, false ); } else { unlink( $path . "/" . $file ); } } if( $keep_parent === false ) { rmdir( $path ); return; } else { return; } } } ////////////////////////////////////////////////////////////////// // SEARCH ////////////////////////////////////////////////////////////////// public function search( $path, $query, $options ) { $response = array( "status" => "none", "message" => null, ); if ( ! function_exists( 'shell_exec' ) ) { $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 | xargs grep -i -I -n -R -H ' . escapeshellarg( $input ) . ''; $output = shell_exec( $cmd ); $output_arr = explode( "\n", $output ); foreach ( $output_arr as $line ) { $data = explode( ":", $line ); $da = array(); if ( count( $data ) > 2 ) { $da['line'] = $data[1]; $da['file'] = str_replace( $path, '', $data[0] ); $da['result'] = str_replace( $path, '', $data[0] ); $da['string'] = str_replace( $data[0] . ":" . $data[1] . ':', '', $line ); $return[] = $da; } } if ( count( $return ) == 0 ) { $response["status"] = "error"; $response["message"] = "No Results Returned"; } else { $response["status"] = "success"; $response["index"] = $return; } } return $response; } ////////////////////////////////////////////////////////////////// // UPLOAD (Handles uploads to the specified directory) ////////////////////////////////////////////////////////////////// public function upload( $path ) { // Check that the path is a directory $response = array( "status" => "none", "message" => "", "files" => array(), ); if ( is_file( $path ) ) { $response["status"] = "error"; $response["message"] = "Path Not A Directory"; } else { foreach( $_FILES['upload']['name'] as $key => $value ) { if ( ! empty( $value ) ) { $filename = $value; $filepath = $path . "/$filename"; if ( @move_uploaded_file( $_FILES['upload']['tmp_name'][$key], $filepath ) ) { $info[] = array( "name" => $filename, "size" => filesize( $filepath ), "url" => $filepath, "thumbnail_url" => $filepath, "delete_url" => $filepath, "delete_type" => 'DELETE' ); } } } } return $response; } }