From 0304ae61358e021ce0a03334f3780734dec87218 Mon Sep 17 00:00:00 2001 From: Marco Dickert Date: Tue, 18 Jul 2017 15:37:33 +0200 Subject: [PATCH] made encoding check only, when function exists --- build/libifm.php | 13 ++++++++----- ifm.php | 13 ++++++++----- src/main.php | 13 ++++++++----- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/build/libifm.php b/build/libifm.php index e3b5ae7..0360db5 100644 --- a/build/libifm.php +++ b/build/libifm.php @@ -1810,14 +1810,17 @@ function IFM( params ) { // gets the content of a file // notice: if the content is not JSON encodable it returns an error private function getContent( array $d ) { - if( $this->config['edit'] != 1 ) echo json_encode( array( "status" => "ERROR", "message" => "You are not allowed to edit files." ) ); + if( $this->config['edit'] != 1 ) + echo json_encode( array( "status" => "ERROR", "message" => "You are not allowed to edit files." ) ); else { $this->chDirIfNecessary( $d['dir'] ); - if( file_exists( $d['filename'] ) ) { + if( file_exists( $d['filename'] ) && is_readable( $d['filename'] ) ) { $content = @file_get_contents( $d['filename'] ); - $utf8content = mb_convert_encoding( $content, 'UTF-8', mb_detect_encoding( $content, 'UTF-8, ISO-8859-1', true ) ); - echo json_encode( array( "status" => "OK", "data" => array( "filename" => $d['filename'], "content" => $utf8content ) ) ); - } else echo json_encode( array( "status" => "ERROR", "message" => "File not found" ) ); + file_put_contents( "debugifm.txt", "content: ".$content."\n\n\n" ); + if( function_exists( "mb_check_encoding" ) && ! mb_check_encoding( $content, "UTF-8" ) ) + $content = utf8_encode( $content ); + echo json_encode( array( "status" => "OK", "data" => array( "filename" => $d['filename'], "content" => $content ) ) ); + } else echo json_encode( array( "status" => "ERROR", "message" => "File not found or not readable." ) ); } } diff --git a/ifm.php b/ifm.php index 515f0ca..45993cb 100644 --- a/ifm.php +++ b/ifm.php @@ -1810,14 +1810,17 @@ function IFM( params ) { // gets the content of a file // notice: if the content is not JSON encodable it returns an error private function getContent( array $d ) { - if( $this->config['edit'] != 1 ) echo json_encode( array( "status" => "ERROR", "message" => "You are not allowed to edit files." ) ); + if( $this->config['edit'] != 1 ) + echo json_encode( array( "status" => "ERROR", "message" => "You are not allowed to edit files." ) ); else { $this->chDirIfNecessary( $d['dir'] ); - if( file_exists( $d['filename'] ) ) { + if( file_exists( $d['filename'] ) && is_readable( $d['filename'] ) ) { $content = @file_get_contents( $d['filename'] ); - $utf8content = mb_convert_encoding( $content, 'UTF-8', mb_detect_encoding( $content, 'UTF-8, ISO-8859-1', true ) ); - echo json_encode( array( "status" => "OK", "data" => array( "filename" => $d['filename'], "content" => $utf8content ) ) ); - } else echo json_encode( array( "status" => "ERROR", "message" => "File not found" ) ); + file_put_contents( "debugifm.txt", "content: ".$content."\n\n\n" ); + if( function_exists( "mb_check_encoding" ) && ! mb_check_encoding( $content, "UTF-8" ) ) + $content = utf8_encode( $content ); + echo json_encode( array( "status" => "OK", "data" => array( "filename" => $d['filename'], "content" => $content ) ) ); + } else echo json_encode( array( "status" => "ERROR", "message" => "File not found or not readable." ) ); } } diff --git a/src/main.php b/src/main.php index 98a67bc..c672fda 100644 --- a/src/main.php +++ b/src/main.php @@ -363,14 +363,17 @@ class IFM { // gets the content of a file // notice: if the content is not JSON encodable it returns an error private function getContent( array $d ) { - if( $this->config['edit'] != 1 ) echo json_encode( array( "status" => "ERROR", "message" => "You are not allowed to edit files." ) ); + if( $this->config['edit'] != 1 ) + echo json_encode( array( "status" => "ERROR", "message" => "You are not allowed to edit files." ) ); else { $this->chDirIfNecessary( $d['dir'] ); - if( file_exists( $d['filename'] ) ) { + if( file_exists( $d['filename'] ) && is_readable( $d['filename'] ) ) { $content = @file_get_contents( $d['filename'] ); - $utf8content = mb_convert_encoding( $content, 'UTF-8', mb_detect_encoding( $content, 'UTF-8, ISO-8859-1', true ) ); - echo json_encode( array( "status" => "OK", "data" => array( "filename" => $d['filename'], "content" => $utf8content ) ) ); - } else echo json_encode( array( "status" => "ERROR", "message" => "File not found" ) ); + file_put_contents( "debugifm.txt", "content: ".$content."\n\n\n" ); + if( function_exists( "mb_check_encoding" ) && ! mb_check_encoding( $content, "UTF-8" ) ) + $content = utf8_encode( $content ); + echo json_encode( array( "status" => "OK", "data" => array( "filename" => $d['filename'], "content" => $content ) ) ); + } else echo json_encode( array( "status" => "ERROR", "message" => "File not found or not readable." ) ); } }