1
0
mirror of https://github.com/misterunknown/ifm.git synced 2025-08-09 17:46:31 +02:00

added server-side search function

This commit is contained in:
Marco Dickert
2017-07-28 15:36:33 +02:00
parent 392d990288
commit e720bce160
3 changed files with 226 additions and 138 deletions

View File

@@ -2206,6 +2206,7 @@ function IFM( params ) {
case "zipnload": $this->zipnload( $_REQUEST); break;
case "remoteUpload": $this->remoteUpload( $_REQUEST ); break;
case "multidelete": $this->deleteMultipleFiles( $_REQUEST ); break;
case "searchItems": $this->searchItems( $_REQUEST ); break;
case "getFolderTree": $this->getFolderTree( $_REQUEST ); break;
default:
echo json_encode( array( "status" => "ERROR", "message" => "Invalid api action given" ) );
@@ -2253,52 +2254,8 @@ function IFM( params ) {
elseif( $result == "." ) {}
elseif( $result != ".." && substr( $result, 0, 1 ) == "." && $this->config['showhiddenfiles'] != 1 ) {}
else {
$item = array();
$item["name"] = $result;
if( is_dir( $result ) ) {
$item["type"] = "dir";
if( $result == ".." )
$item["icon"] = "icon icon-up-open";
else
$item["icon"] = "icon icon-folder-empty";
} else {
$item["type"] = "file";
if( in_array( substr( $result, -7 ), array( ".tar.gz", ".tar.xz" ) ) )
$type = substr( $result, -6 );
elseif( substr( $result, -8 ) == ".tar.bz2" )
$type = "tar.bz2";
else
$type = substr( strrchr( $result, "." ), 1 );
$item["icon"] = $this->getTypeIcon( $type );
$item["ext"] = strtolower($type);
}
if( $this->config['showlastmodified'] == 1 ) { $item["lastmodified"] = date( "d.m.Y, G:i e", filemtime( $result ) ); }
if( $this->config['showfilesize'] == 1 ) {
$item["size"] = filesize( $result );
if( $item["size"] > 1073741824 ) $item["size"] = round( ( $item["size"]/1073741824 ), 2 ) . " GB";
elseif($item["size"]>1048576)$item["size"] = round( ( $item["size"]/1048576 ), 2 ) . " MB";
elseif($item["size"]>1024)$item["size"] = round( ( $item["size"]/1024 ), 2 ) . " KB";
else $item["size"] = $item["size"] . " Byte";
}
if( $this->config['showpermissions'] > 0 ) {
if( $this->config['showpermissions'] == 1 ) $item["fileperms"] = substr( decoct( fileperms( $result ) ), -3 );
elseif( $this->config['showpermissions'] == 2 ) $item["fileperms"] = $this->filePermsDecode( fileperms( $result ) );
if( $item["fileperms"] == "" ) $item["fileperms"] = " ";
$item["filepermmode"] = ( $this->config['showpermissions'] == 1 ) ? "short" : "long";
}
if( $this->config['showowner'] == 1 ) {
if ( function_exists( "posix_getpwuid" ) && fileowner($result) !== false ) {
$ownerarr = posix_getpwuid( fileowner( $result ) );
$item["owner"] = $ownerarr['name'];
} else $item["owner"] = false;
}
if( $this->config['showgroup'] == 1 ) {
if( function_exists( "posix_getgrgid" ) && filegroup( $result ) !== false ) {
$grouparr = posix_getgrgid( filegroup( $result ) );
$item["group"] = $grouparr['name'];
} else $item["group"] = false;
}
if( is_dir( $result ) ) $dirs[] = $item;
$item = $this->getItemInformation( $result );
if( $item['type'] == "dir" ) $dirs[] = $item;
else $files[] = $item;
}
}
@@ -2309,6 +2266,55 @@ function IFM( params ) {
echo json_encode( array_merge( $dirs, $files ) );
}
private function getItemInformation( $name ) {
$item = array();
$item["name"] = $name;
if( is_dir( $name ) ) {
$item["type"] = "dir";
if( $name == ".." )
$item["icon"] = "icon icon-up-open";
else
$item["icon"] = "icon icon-folder-empty";
} else {
$item["type"] = "file";
if( in_array( substr( $name, -7 ), array( ".tar.gz", ".tar.xz" ) ) )
$type = substr( $name, -6 );
elseif( substr( $name, -8 ) == ".tar.bz2" )
$type = "tar.bz2";
else
$type = substr( strrchr( $name, "." ), 1 );
$item["icon"] = $this->getTypeIcon( $type );
$item["ext"] = strtolower($type);
}
if( $this->config['showlastmodified'] == 1 ) { $item["lastmodified"] = date( "d.m.Y, G:i e", filemtime( $name ) ); }
if( $this->config['showfilesize'] == 1 ) {
$item["size"] = filesize( $name );
if( $item["size"] > 1073741824 ) $item["size"] = round( ( $item["size"]/1073741824 ), 2 ) . " GB";
elseif($item["size"]>1048576)$item["size"] = round( ( $item["size"]/1048576 ), 2 ) . " MB";
elseif($item["size"]>1024)$item["size"] = round( ( $item["size"]/1024 ), 2 ) . " KB";
else $item["size"] = $item["size"] . " Byte";
}
if( $this->config['showpermissions'] > 0 ) {
if( $this->config['showpermissions'] == 1 ) $item["fileperms"] = substr( decoct( fileperms( $name ) ), -3 );
elseif( $this->config['showpermissions'] == 2 ) $item["fileperms"] = $this->filePermsDecode( fileperms( $name ) );
if( $item["fileperms"] == "" ) $item["fileperms"] = " ";
$item["filepermmode"] = ( $this->config['showpermissions'] == 1 ) ? "short" : "long";
}
if( $this->config['showowner'] == 1 ) {
if ( function_exists( "posix_getpwuid" ) && fileowner($name) !== false ) {
$ownerarr = posix_getpwuid( fileowner( $name ) );
$item["owner"] = $ownerarr['name'];
} else $item["owner"] = false;
}
if( $this->config['showgroup'] == 1 ) {
if( function_exists( "posix_getgrgid" ) && filegroup( $name ) !== false ) {
$grouparr = posix_getgrgid( filegroup( $name ) );
$item["group"] = $grouparr['name'];
} else $item["group"] = false;
}
return $item;
}
private function getConfig() {
$ret = $this->config;
$ret['inline'] = ( $this->mode == "inline" ) ? true : false;
@@ -2345,6 +2351,28 @@ function IFM( params ) {
}
}
private function searchItems( $d ) {
$this->chDirIfNecessary( $d['dir'] );
try {
$results = $this->searchItemsRecursive( $d['pattern'] );
echo json_encode( $results );
} catch( Exception $e ) {
echo json_encode( array( "status" => "ERROR", "message" => $e->getMessage() ) );
}
}
private function searchItemsRecursive( $pattern, $dir="" ) {
$items = array();
$dir = $dir ? $dir : '.';
foreach( glob( $this->pathCombine( $dir, $pattern ) ) as $result ) {
array_push( $items, $this->getItemInformation( $result ) );
}
foreach( glob( $this->pathCombine( $dir, '*') , GLOB_ONLYDIR ) as $subdir ) {
$items = array_merge( $items, $this->searchItemsRecursive( $pattern, $subdir ) );
}
return $items;
}
private function getFolderTree( $d ) {
echo json_encode(
array_merge(

120
ifm.php
View File

@@ -2206,6 +2206,7 @@ function IFM( params ) {
case "zipnload": $this->zipnload( $_REQUEST); break;
case "remoteUpload": $this->remoteUpload( $_REQUEST ); break;
case "multidelete": $this->deleteMultipleFiles( $_REQUEST ); break;
case "searchItems": $this->searchItems( $_REQUEST ); break;
case "getFolderTree": $this->getFolderTree( $_REQUEST ); break;
default:
echo json_encode( array( "status" => "ERROR", "message" => "Invalid api action given" ) );
@@ -2253,52 +2254,8 @@ function IFM( params ) {
elseif( $result == "." ) {}
elseif( $result != ".." && substr( $result, 0, 1 ) == "." && $this->config['showhiddenfiles'] != 1 ) {}
else {
$item = array();
$item["name"] = $result;
if( is_dir( $result ) ) {
$item["type"] = "dir";
if( $result == ".." )
$item["icon"] = "icon icon-up-open";
else
$item["icon"] = "icon icon-folder-empty";
} else {
$item["type"] = "file";
if( in_array( substr( $result, -7 ), array( ".tar.gz", ".tar.xz" ) ) )
$type = substr( $result, -6 );
elseif( substr( $result, -8 ) == ".tar.bz2" )
$type = "tar.bz2";
else
$type = substr( strrchr( $result, "." ), 1 );
$item["icon"] = $this->getTypeIcon( $type );
$item["ext"] = strtolower($type);
}
if( $this->config['showlastmodified'] == 1 ) { $item["lastmodified"] = date( "d.m.Y, G:i e", filemtime( $result ) ); }
if( $this->config['showfilesize'] == 1 ) {
$item["size"] = filesize( $result );
if( $item["size"] > 1073741824 ) $item["size"] = round( ( $item["size"]/1073741824 ), 2 ) . " GB";
elseif($item["size"]>1048576)$item["size"] = round( ( $item["size"]/1048576 ), 2 ) . " MB";
elseif($item["size"]>1024)$item["size"] = round( ( $item["size"]/1024 ), 2 ) . " KB";
else $item["size"] = $item["size"] . " Byte";
}
if( $this->config['showpermissions'] > 0 ) {
if( $this->config['showpermissions'] == 1 ) $item["fileperms"] = substr( decoct( fileperms( $result ) ), -3 );
elseif( $this->config['showpermissions'] == 2 ) $item["fileperms"] = $this->filePermsDecode( fileperms( $result ) );
if( $item["fileperms"] == "" ) $item["fileperms"] = " ";
$item["filepermmode"] = ( $this->config['showpermissions'] == 1 ) ? "short" : "long";
}
if( $this->config['showowner'] == 1 ) {
if ( function_exists( "posix_getpwuid" ) && fileowner($result) !== false ) {
$ownerarr = posix_getpwuid( fileowner( $result ) );
$item["owner"] = $ownerarr['name'];
} else $item["owner"] = false;
}
if( $this->config['showgroup'] == 1 ) {
if( function_exists( "posix_getgrgid" ) && filegroup( $result ) !== false ) {
$grouparr = posix_getgrgid( filegroup( $result ) );
$item["group"] = $grouparr['name'];
} else $item["group"] = false;
}
if( is_dir( $result ) ) $dirs[] = $item;
$item = $this->getItemInformation( $result );
if( $item['type'] == "dir" ) $dirs[] = $item;
else $files[] = $item;
}
}
@@ -2309,6 +2266,55 @@ function IFM( params ) {
echo json_encode( array_merge( $dirs, $files ) );
}
private function getItemInformation( $name ) {
$item = array();
$item["name"] = $name;
if( is_dir( $name ) ) {
$item["type"] = "dir";
if( $name == ".." )
$item["icon"] = "icon icon-up-open";
else
$item["icon"] = "icon icon-folder-empty";
} else {
$item["type"] = "file";
if( in_array( substr( $name, -7 ), array( ".tar.gz", ".tar.xz" ) ) )
$type = substr( $name, -6 );
elseif( substr( $name, -8 ) == ".tar.bz2" )
$type = "tar.bz2";
else
$type = substr( strrchr( $name, "." ), 1 );
$item["icon"] = $this->getTypeIcon( $type );
$item["ext"] = strtolower($type);
}
if( $this->config['showlastmodified'] == 1 ) { $item["lastmodified"] = date( "d.m.Y, G:i e", filemtime( $name ) ); }
if( $this->config['showfilesize'] == 1 ) {
$item["size"] = filesize( $name );
if( $item["size"] > 1073741824 ) $item["size"] = round( ( $item["size"]/1073741824 ), 2 ) . " GB";
elseif($item["size"]>1048576)$item["size"] = round( ( $item["size"]/1048576 ), 2 ) . " MB";
elseif($item["size"]>1024)$item["size"] = round( ( $item["size"]/1024 ), 2 ) . " KB";
else $item["size"] = $item["size"] . " Byte";
}
if( $this->config['showpermissions'] > 0 ) {
if( $this->config['showpermissions'] == 1 ) $item["fileperms"] = substr( decoct( fileperms( $name ) ), -3 );
elseif( $this->config['showpermissions'] == 2 ) $item["fileperms"] = $this->filePermsDecode( fileperms( $name ) );
if( $item["fileperms"] == "" ) $item["fileperms"] = " ";
$item["filepermmode"] = ( $this->config['showpermissions'] == 1 ) ? "short" : "long";
}
if( $this->config['showowner'] == 1 ) {
if ( function_exists( "posix_getpwuid" ) && fileowner($name) !== false ) {
$ownerarr = posix_getpwuid( fileowner( $name ) );
$item["owner"] = $ownerarr['name'];
} else $item["owner"] = false;
}
if( $this->config['showgroup'] == 1 ) {
if( function_exists( "posix_getgrgid" ) && filegroup( $name ) !== false ) {
$grouparr = posix_getgrgid( filegroup( $name ) );
$item["group"] = $grouparr['name'];
} else $item["group"] = false;
}
return $item;
}
private function getConfig() {
$ret = $this->config;
$ret['inline'] = ( $this->mode == "inline" ) ? true : false;
@@ -2345,6 +2351,28 @@ function IFM( params ) {
}
}
private function searchItems( $d ) {
$this->chDirIfNecessary( $d['dir'] );
try {
$results = $this->searchItemsRecursive( $d['pattern'] );
echo json_encode( $results );
} catch( Exception $e ) {
echo json_encode( array( "status" => "ERROR", "message" => $e->getMessage() ) );
}
}
private function searchItemsRecursive( $pattern, $dir="" ) {
$items = array();
$dir = $dir ? $dir : '.';
foreach( glob( $this->pathCombine( $dir, $pattern ) ) as $result ) {
array_push( $items, $this->getItemInformation( $result ) );
}
foreach( glob( $this->pathCombine( $dir, '*') , GLOB_ONLYDIR ) as $subdir ) {
$items = array_merge( $items, $this->searchItemsRecursive( $pattern, $subdir ) );
}
return $items;
}
private function getFolderTree( $d ) {
echo json_encode(
array_merge(

View File

@@ -243,6 +243,7 @@ f00bar;
case "zipnload": $this->zipnload( $_REQUEST); break;
case "remoteUpload": $this->remoteUpload( $_REQUEST ); break;
case "multidelete": $this->deleteMultipleFiles( $_REQUEST ); break;
case "searchItems": $this->searchItems( $_REQUEST ); break;
case "getFolderTree": $this->getFolderTree( $_REQUEST ); break;
default:
echo json_encode( array( "status" => "ERROR", "message" => "Invalid api action given" ) );
@@ -290,52 +291,8 @@ f00bar;
elseif( $result == "." ) {}
elseif( $result != ".." && substr( $result, 0, 1 ) == "." && $this->config['showhiddenfiles'] != 1 ) {}
else {
$item = array();
$item["name"] = $result;
if( is_dir( $result ) ) {
$item["type"] = "dir";
if( $result == ".." )
$item["icon"] = "icon icon-up-open";
else
$item["icon"] = "icon icon-folder-empty";
} else {
$item["type"] = "file";
if( in_array( substr( $result, -7 ), array( ".tar.gz", ".tar.xz" ) ) )
$type = substr( $result, -6 );
elseif( substr( $result, -8 ) == ".tar.bz2" )
$type = "tar.bz2";
else
$type = substr( strrchr( $result, "." ), 1 );
$item["icon"] = $this->getTypeIcon( $type );
$item["ext"] = strtolower($type);
}
if( $this->config['showlastmodified'] == 1 ) { $item["lastmodified"] = date( "d.m.Y, G:i e", filemtime( $result ) ); }
if( $this->config['showfilesize'] == 1 ) {
$item["size"] = filesize( $result );
if( $item["size"] > 1073741824 ) $item["size"] = round( ( $item["size"]/1073741824 ), 2 ) . " GB";
elseif($item["size"]>1048576)$item["size"] = round( ( $item["size"]/1048576 ), 2 ) . " MB";
elseif($item["size"]>1024)$item["size"] = round( ( $item["size"]/1024 ), 2 ) . " KB";
else $item["size"] = $item["size"] . " Byte";
}
if( $this->config['showpermissions'] > 0 ) {
if( $this->config['showpermissions'] == 1 ) $item["fileperms"] = substr( decoct( fileperms( $result ) ), -3 );
elseif( $this->config['showpermissions'] == 2 ) $item["fileperms"] = $this->filePermsDecode( fileperms( $result ) );
if( $item["fileperms"] == "" ) $item["fileperms"] = " ";
$item["filepermmode"] = ( $this->config['showpermissions'] == 1 ) ? "short" : "long";
}
if( $this->config['showowner'] == 1 ) {
if ( function_exists( "posix_getpwuid" ) && fileowner($result) !== false ) {
$ownerarr = posix_getpwuid( fileowner( $result ) );
$item["owner"] = $ownerarr['name'];
} else $item["owner"] = false;
}
if( $this->config['showgroup'] == 1 ) {
if( function_exists( "posix_getgrgid" ) && filegroup( $result ) !== false ) {
$grouparr = posix_getgrgid( filegroup( $result ) );
$item["group"] = $grouparr['name'];
} else $item["group"] = false;
}
if( is_dir( $result ) ) $dirs[] = $item;
$item = $this->getItemInformation( $result );
if( $item['type'] == "dir" ) $dirs[] = $item;
else $files[] = $item;
}
}
@@ -346,6 +303,55 @@ f00bar;
echo json_encode( array_merge( $dirs, $files ) );
}
private function getItemInformation( $name ) {
$item = array();
$item["name"] = $name;
if( is_dir( $name ) ) {
$item["type"] = "dir";
if( $name == ".." )
$item["icon"] = "icon icon-up-open";
else
$item["icon"] = "icon icon-folder-empty";
} else {
$item["type"] = "file";
if( in_array( substr( $name, -7 ), array( ".tar.gz", ".tar.xz" ) ) )
$type = substr( $name, -6 );
elseif( substr( $name, -8 ) == ".tar.bz2" )
$type = "tar.bz2";
else
$type = substr( strrchr( $name, "." ), 1 );
$item["icon"] = $this->getTypeIcon( $type );
$item["ext"] = strtolower($type);
}
if( $this->config['showlastmodified'] == 1 ) { $item["lastmodified"] = date( "d.m.Y, G:i e", filemtime( $name ) ); }
if( $this->config['showfilesize'] == 1 ) {
$item["size"] = filesize( $name );
if( $item["size"] > 1073741824 ) $item["size"] = round( ( $item["size"]/1073741824 ), 2 ) . " GB";
elseif($item["size"]>1048576)$item["size"] = round( ( $item["size"]/1048576 ), 2 ) . " MB";
elseif($item["size"]>1024)$item["size"] = round( ( $item["size"]/1024 ), 2 ) . " KB";
else $item["size"] = $item["size"] . " Byte";
}
if( $this->config['showpermissions'] > 0 ) {
if( $this->config['showpermissions'] == 1 ) $item["fileperms"] = substr( decoct( fileperms( $name ) ), -3 );
elseif( $this->config['showpermissions'] == 2 ) $item["fileperms"] = $this->filePermsDecode( fileperms( $name ) );
if( $item["fileperms"] == "" ) $item["fileperms"] = " ";
$item["filepermmode"] = ( $this->config['showpermissions'] == 1 ) ? "short" : "long";
}
if( $this->config['showowner'] == 1 ) {
if ( function_exists( "posix_getpwuid" ) && fileowner($name) !== false ) {
$ownerarr = posix_getpwuid( fileowner( $name ) );
$item["owner"] = $ownerarr['name'];
} else $item["owner"] = false;
}
if( $this->config['showgroup'] == 1 ) {
if( function_exists( "posix_getgrgid" ) && filegroup( $name ) !== false ) {
$grouparr = posix_getgrgid( filegroup( $name ) );
$item["group"] = $grouparr['name'];
} else $item["group"] = false;
}
return $item;
}
private function getConfig() {
$ret = $this->config;
$ret['inline'] = ( $this->mode == "inline" ) ? true : false;
@@ -382,6 +388,32 @@ f00bar;
}
}
private function searchItems( $d ) {
$this->chDirIfNecessary( $d['dir'] );
if( strpos( $d['pattern'], '/' ) !== false ) {
echo json_decode( array( "status" => "ERROR", "message" => "Pattern must not contain slashes" ) );
exit( 1 );
}
try {
$results = $this->searchItemsRecursive( $d['pattern'] );
echo json_encode( $results );
} catch( Exception $e ) {
echo json_encode( array( "status" => "ERROR", "message" => $e->getMessage() ) );
}
}
private function searchItemsRecursive( $pattern, $dir="" ) {
$items = array();
$dir = $dir ? $dir : '.';
foreach( glob( $this->pathCombine( $dir, $pattern ) ) as $result ) {
array_push( $items, $this->getItemInformation( $result ) );
}
foreach( glob( $this->pathCombine( $dir, '*') , GLOB_ONLYDIR ) as $subdir ) {
$items = array_merge( $items, $this->searchItemsRecursive( $pattern, $subdir ) );
}
return $items;
}
private function getFolderTree( $d ) {
echo json_encode(
array_merge(