#!/usr/bin/perl
package RVL::File;
# WebSite: http://www.rvglobalsoft.com
# Unauthorized copying is strictly forbidden and may result in severe legal action.
# Copyright (c) 2013 RV Global Soft Co.,Ltd. All rights reserved.
#
# =====YOU MUST KEEP THIS COPYRIGHTS NOTICE INTACT AND CAN NOT BE REMOVE =======
# Copyright (c) 2013 RV Global Soft Co.,Ltd. All rights reserved.
# This Agreement is a legal contract, which specifies the terms of the license
# and warranty limitation between you and RV Global Soft Co.,Ltd. and RV2Factor Product for RV Global Soft.
# You should carefully read the following terms and conditions before
# installing or using this software. Unless you have a different license
# agreement obtained from RV Global Soft Co.,Ltd., installation or use of this software
# indicates your acceptance of the license and warranty limitation terms
# contained in this Agreement. If you do not agree to the terms of this
# Agreement, promptly delete and destroy all copies of the Software.
#
# ===== Grant of License =======
# The Software may only be installed and used on a single host machine.
#
# ===== Disclaimer of Warranty =======
# THIS SOFTWARE AND ACCOMPANYING DOCUMENTATION ARE PROVIDED "AS IS" AND
# WITHOUT WARRANTIES AS TO PERFORMANCE OF MERCHANTABILITY OR ANY OTHER
# WARRANTIES WHETHER EXPRESSED OR IMPLIED. BECAUSE OF THE VARIOUS HARDWARE
# AND SOFTWARE ENVIRONMENTS INTO WHICH RV SITE BUILDER MAY BE USED, NO WARRANTY OF
# FITNESS FOR A PARTICULAR PURPOSE IS OFFERED. THE USER MUST ASSUME THE
# ENTIRE RISK OF USING THIS PROGRAM. ANY LIABILITY OF RV GLOBAL SOFT CO.,LTD. WILL BE
# LIMITED EXCLUSIVELY TO PRODUCT REPLACEMENT OR REFUND OF PURCHASE PRICE.
# IN NO CASE SHALL RV GLOBAL SOFT CO.,LTD. BE LIABLE FOR ANY INCIDENTAL, SPECIAL OR
# CONSEQUENTIAL DAMAGES OR LOSS, INCLUDING, WITHOUT LIMITATION, LOST PROFITS
# OR THE INABILITY TO USE EQUIPMENT OR ACCESS DATA, WHETHER SUCH DAMAGES ARE
# BASED UPON A BREACH OF EXPRESS OR IMPLIED WARRANTIES, BREACH OF CONTRACT,
# NEGLIGENCE, STRICT TORT, OR ANY OTHER LEGAL THEORY. THIS IS TRUE EVEN IF
# RV GLOBAL SOFT CO.,LTD. IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. IN NO CASE WILL
# RV GLOBAL SOFT CO.,LTD.'S LIABILITY EXCEED THE AMOUNT OF THE LICENSE FEE ACTUALLY PAID
# BY LICENSEE TO RV GLOBAL SOFT CO.,LTD.
# ===============================
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