CHips L MINI SHELL

CHips L pro

Current Path : /proc/2/root/usr/local/rvglobalsoft/rvglobalsoft/lib/RVL/
Upload File :
Current File : //proc/2/root/usr/local/rvglobalsoft/rvglobalsoft/lib/RVL/File.pm

#!/usr/bin/perl
package RVL::File;
##LICENSE##
use strict;
use warnings;
use File::MimeInfo;
use File::Which;
use File::Find::Rule;
use Fcntl qw(:flock SEEK_END);
use Class::Std::Utils;
{
    sub mime_type {
    	my ($filename) = shift;
   	
    	return (defined($filename) && RVL::String::is_string($filename)) 
                       ? File::MimeInfo->new()->mimetype($filename) 
                       : undef;

#        my $res = undef;
#        if (defined($filename)) {
#        	my $cmd = `file -z -b --mime-type $filename`;
#        	chomp($cmd);
#        	$res = $cmd;
#        }
#        return $res;
    }
  
    sub extension_loaded {
    	my ($extension) = shift;
    	return (which($extension)) ? 1 : 0;
    }
    
    sub get_bin_extension_loaded {
    	my ($driver) = (defined($_[0]) && $_[0] ne "") ? $_[0] : "";
    	my @pathBin = which($driver);
    	return (defined($pathBin[0])) ? $pathBin[0] : undef;
    }

    sub filesize {
    	my ($filename) = shift;
    	return (-f $filename) ? (-s $filename) : 0;
    }
    
    sub file_get_contents {
	    my ($filename) = shift;
	    my ($contents) = "";
	    if (open(my $FILEHANDLE, '<', $filename)) { 
	    	flock($FILEHANDLE, LOCK_EX); 
	        #seek $FILEHANDLE , 10, 0;
	        while (my $line = <$FILEHANDLE>) {
	            $contents .= $line;
	        }
	        flock($FILEHANDLE, LOCK_UN);
	        close($FILEHANDLE);
	    }
	    return $contents;
	}
	
    sub file {
        my $filename = (defined($_[0])) ? $_[0] : undef;
        my $flags = (defined($_[1])) ? $_[1] : undef;
        my @aLines = ();
        
        if (open(my $FILEHANDLE, '<', $filename)) {
        	flock($FILEHANDLE, LOCK_EX);
            #seek $FILEHANDLE , 10, 0;
	        while (my $line = <$FILEHANDLE>) {
	        	$line =~ s/\r|\n//g;
	        	if (defined($flags)) {
	        		if ($flags eq "FILE_SKIP_EMPTY_LINES" && $line eq "") {
	        		} else {
	        			push(@aLines, $line);
	        		}
	        	} else {
	        		push(@aLines, $line);
	        	}
            }
            flock($FILEHANDLE, LOCK_UN);
	        close($FILEHANDLE);
        }
        return @aLines;
    }
    
    sub listDirs {
    	my ($dirname) = (defined($_[0]) && RVL::String::is_string($_[0])) ? $_[0] : "";
    	my ($level) = (defined($_[1])) ? $_[1] : undef;
    	
    	if (-d $dirname) {
    		if (defined($level)) {
    			return  File::Find::Rule->maxdepth($level)->directory->in($dirname);
    		} else {
    			return File::Find::Rule->directory->in($dirname);
    		}
    	}
    	
    	return ();
    	#return (-d $dirname) ? File::Find::Rule->directory->in($dirname) : ();
    }
    
    sub listFiles {
    	my ($dirname) = (defined($_[0]) && RVL::String::is_string($_[0])) ? $_[0] : "";
    	my ($patterns) = (defined($_[1]) && RVL::String::is_string($_[1]) && $_[1] ne "") ? $_[1] : "*";
    	my ($ignore) = (defined($_[2]) && RVL::String::is_string($_[2]) && $_[2] ne "") ? $_[2] : "*.svn-base";
    	
    	return (-d $dirname) ? File::Find::Rule->file()->name($patterns)->not_name($ignore)->in($dirname) : ();
    }
    
    sub parse_ini_file {
	    my ($filename) = shift;
	    my $res = {};
	    
	    if (-f $filename) {
	    	if (open(my $FILERead, '<', $filename)) {       
	            my $openGroup = '';
	            flock($FILERead, LOCK_EX);
	            while (<$FILERead>) {
	                $_ =~ s/\r|\n//gi;
	                next if (/^$/gi);
	                next if (/^#/);
	    
	                #found group
	                if (/^\[(.*?)\]$/) {
	                    $openGroup = $1;
	                    $openGroup =~ s/^ +//gi;
	                    $openGroup =~ s/ +$//gi;
	                    $res->{$openGroup} = {};
	                    next;
	                }
	    
	                #normal ini
	                my ($key, $value) = split(/=/, $_, 2);
	                $key = RVL::String::trim($key);
	                $value = RVL::String::trim($value);
	    
	                if ($openGroup) {
	                    if (!exists $res->{$openGroup}->{$key}) {
	                        $res->{$openGroup}->{$key} = $value;
	                    }
	                } else {
	                    $res->{$key} = $value;
	                }
	    
	            }
	            flock($FILERead, LOCK_UN);
	            close($FILERead);
	        }
	    }
	    
	    return $res;
	}
	
	sub write_ini_file {
        my ($filename, $iniConf) = @_;
        #RVL::logMessage('======================================' . $filename, __CONSTANT__::RVL_LOG_DEBUG);
        #RVL::logMessage('======================================' . $iniConf->{enabled}, __CONSTANT__::RVL_LOG_DEBUG);
        my $writeLine = '';
        my @aLines    = ();
        my @aLine1d   = ();
        my @aLine2d   = ();
        my $iswriteIniFile = 0;
        foreach my $confKey1 (sort keys %{$iniConf}) {
            #If 1d ini
            #RVL::logMessage('======================================' . $confKey1, __CONSTANT__::RVL_LOG_DEBUG);
            if (!RVL::String::is_hash($iniConf->{$confKey1})) {
            	#RVL::logMessage('======================================  ' . $confKey1 .  "   \$iniConf->{$confKey1} " .$iniConf->{$confKey1} , __CONSTANT__::RVL_LOG_DEBUG);
                $writeLine = sprintf("%s=%s", $confKey1, $iniConf->{$confKey1});
                push(@aLine1d, $writeLine);
                next;
            }
            $writeLine = sprintf("[%s]", $confKey1);
            push(@aLine2d, $writeLine);
            foreach my $confKey2 (sort keys %{ $iniConf->{$confKey1} }) {
                $writeLine =
                  sprintf("%s=%s", $confKey2, $iniConf->{$confKey1}->{$confKey2});
                push(@aLine2d, $writeLine);
            }
        }
    
        push(@aLines, @aLine1d);
        push(@aLines, @aLine2d);
    
        if (open(my $FILEWrite, '>', $filename)) {
            foreach my $writeLine (@aLines) {
                chomp($writeLine);
                print $FILEWrite $writeLine."\n";
            }
            close($FILEWrite);
            $iswriteIniFile = 1;
        } else {
            $iswriteIniFile = $!.": $filename";
        }
        
        #RVL::logMessage('======================================  ' . $iswriteIniFile , __CONSTANT__::RVL_LOG_DEBUG);
        return $iswriteIniFile;
    }
    
    sub mkdirp {
	    my $pathName = $_[0];	    
	    my $permission = defined($_[1]) ? oct($_[1]) : oct("0644");
	    my @checkPath = split("/", $pathName);
	    my $pathNow = '';
	    my $ismkdir = 1;
	    mkdir($pathName);
	    for (my $countCheckPath=1;$countCheckPath < @checkPath; $countCheckPath++){
	        $pathNow .= "/" . $checkPath[$countCheckPath];
	        if (!-d $pathNow) {
	            if (-w getParentDir($pathNow)) {
	                mkdir($pathNow);
	                chmod $permission, $pathNow;
	            } else {
	                $ismkdir = 0;
	            }
	        }
	    }
	    return $ismkdir;
	}
	
	sub getParentDir {
	    my $dir = defined($_[0]) ? $_[0] : '';
	    my @splitPath = grep{$_ ne ''} split('/', $dir);
	    pop(@splitPath);
	    my $parentPath = join('/', @splitPath);
	    $parentPath = $dir =~ /^\//gi ? '/'.$parentPath : $parentPath;
	    $parentPath = $parentPath eq '' ? '.' : $parentPath;
	    $parentPath = $dir =~ /\/$/gi ? $parentPath.'/' : $parentPath;
	    $parentPath =~ s/\/\//\//gi;
	    return $parentPath;
	}
	
}
1;

=pod

=head1 RVL::FILE

RVL Lib File.

=head1 HOW IT WORKS

=head2 mime_type($filename)
@param String $filename - File name source
@return String - MIMETYPE
simple: mime_type("/home/<user>/public_html/exam.jpg");

=head2 extensions($filename)
@param String $filename - File name source
@return String - Extensions file
simple: extensions("clamscan");

=head2 extension_loaded($driver)
@param String $driver - Driver
@return Boolean
simple: extension_loaded("perl");

=head2 get_bin_extension_loaded
@param String $driver - Driver
@return String - Bin path
simple: get_bin_extension_loaded("perl");

=head2 filesize($filename)
@param String $filename - File name source
@return Interger - Unit Bytes
simple: filesize("/home/<user>/public_html/exam.txt");

=head2 file_get_contents($filename)
@param String $filename - File name source
@return String - Content file
simple: file_get_contents("/home/<user>/public_html/exam.txt");
reference: http://www.developingwebs.net/perl/file_handling.php

=head2 file($filename)
@param String $filename - File name source
@return Array - Line content file
simple: file("/home/<user>/public_html/exam.txt");

=head2 listDirs($dirname)
@param String $dirname - Directory name
@return Array - List dirs
simple: file_get_contents("/home/<user>/public_html/");

=head2 listFiles($dirname, $patterns)
@param String $dirname - Directory name
@param String $patterns - Pattern
@return Array - List files
simple: RVL::File::listFiles("/home/<user>/public_html/");
           RVL::File::listFiles("/home/<user>/public_html/", "*.txt|*.php");

=head2 parse_ini_file($filename)
@param String $filename - File name source
@return HASH
simple: parse_ini_file("/home/<user>/public_html/exam.ini");

=cut

Copyright 2K16 - 2K18 Indonesian Hacker Rulez