Vtiger CRM #: How to write PHP code in log4php.properties file - vtiger

I am new to Vtiger CRM. I want to write php code in log4php.properties file and also it need to be executed.
I can write the code but it is not executing at all. So kindly help me with a way which will allow to execute the file.
Also this need to be executed dynamic with separate domains.
Thanks

Open your index.php file from crm root directory and Add this code
function replace_string_in_a_file($filepath, $search, $replace) {
if (#file_exists($filepath)) {
$file = file($filepath);
foreach ($file as $index => $string) {
if (strpos($string, $search) !== FALSE)
$file[$index] = "$replace\n";
}
$content = implode($file);
return $content;
}else {
return NULL;
}
}
$filepath = $root_directory . 'log4php.properties';
$search = 'log4php.appender.A1.File=';
$replace = 'log4php.appender.A1.File=' . DOMAIN_PATH . '/logs/vtigercrm.log';
$log_properties_content = replace_string_in_a_file($filepath, $search, $replace);
if (!empty($log_properties_content)) {
file_put_contents($filepath, $log_properties_content);
}

Related

new \ZipArchive extension not working in Drupal module

Error: Class 'Drupal\checklist_download\Controller\ZipArchive' not found in Drupal\checklist_download\Controller\ChDownloadController->page() (line 42 of modules/checklist_download/src/Controller/ChDownloadController.php).
zip extension installed,
simple php file on domain working, but not in module
$zip = new \ZipArchive;
$zip->open($archive_name ,ZipArchive::CREATE|ZipArchive::OVERWRITE);
foreach ($b as $filename => $file) {
$zip->addFile($filename, $file);
}
$zip->close();
You can use Drupals own "ArchiverManager" for that:
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Archiver%21ArchiverManager.php/8.3.x
//Set path to zip-file or create empty Zip-file:
$zip_path =\Drupal::service('file_system')->saveData('','temporary://'.$zip_name.'.zip');
$zip = \Drupal::service('plugin.manager.archiver')->getInstance(['filepath' => $zip_path]);
foreach ($b as $filename => $filePath) {
$zip->add($filePath);
}

perl test mock make_path

I have a function which looks like the one written below. I need to write a unit test for the below function. I am not able to mock few values.
use File::Basename;
use File::Copy;
use File::Temp qw(tempdir);
our $CONFIG="/home/chetanv/svf.xml";
copyConfigFiles();
sub copyConfigFiles {
my $temp_dir = File::Temp->newdir();
my $targetpath = dirname("$CONFIG");
`sudo touch $tempdir/myfile`;
make_path($targetpath) if ( ! -d $targetpath );
File::Copy::copy("$temp_dir/myfile", $targetpath) or print "Problem copying file";
}
I have written the below unit test for the same below. I tried mocking "makepath" which does not seem to work.
subtest _setup(testname => "test copyConfigFiles") => sub {
my $CONFIG = "/my/dir/with/file.xml";
my $mockfileobj = Test::MockModule->new("File::Temp", no_auto => 1);
$mockfileobj->mock('newdir', sub { return "/tmp/delme"; } );
my $amockfileobj = Test::MockModule->new("File::Path", no_auto => 1);
$amockfileobj->mock('makepath', sub { return 0; } );
lives_ok { copyConfigFiles () } 'test copyConfigFiles OK';
done_testing();
};
The problem is i am not able to mock the below lines.
make_path($targetpath) if ( ! -d $targetpath );
File::Copy::copy("$temp_dir/myfile", $targetpath) or print "Problem copying file";
Any help on how i can mock the makepath function which is perl specific? I also tried to create a temporary directory and mock the global CONFIG file with the mocked file. Didn't seem to work.
If I ignore the code that can't be run because the context is missing and focus only on the two functions that you want mocked, one can do this by temporarily replacing the subs in the symbol table with local.
use warnings;
use strict;
use Data::Dump;
use File::Path qw/make_path/;
use File::Copy;
sub to_be_mocked {
my $targetpath = '/tmp/foo';
make_path($targetpath) if ! -d $targetpath;
File::Copy::copy("file.xml", $targetpath) or die;
}
sub run_with_mock {
no warnings 'redefine';
local *make_path = sub { dd 'make_path', #_; return 1 };
local *File::Copy::copy = sub { dd 'copy', #_; return 1 };
to_be_mocked();
}
run_with_mock();
__END__
# Output:
("make_path", "/tmp/foo")
("copy", "file.xml", "/tmp/foo")
Note that -d apparently can't be mocked, at least not directly.

Perl: Splitting a variable containing a path to directory

So I'm having a problem trying to split a variable that has the complete path to a directory. I want to fetch the files under 'logs' directory of this path:
'/nfs/fm/disks/fm_mydiskhere_00000/users/me/repotest/myrepo/tools/toolsdir/logs/*';
and in my Perl script, I have a variable that contains this path: '$log_file'. When I print '$log_file', it contains the entire path; I want to go to the last directory 'logs' and fetch the files under it. By the way, this complete path is in a separate configuration file that is being read by my Perl script this way:
sub read_file {
my ($log_file) = shift(#_);
info ("Using file : $log_file");
my $fh = new FileHandle ("$log_file");
printError ("Could not open this file : '$log_file' - $!") unless defined $fh;
my $contents;
{
local $/ = undef;
$contents = <$fh>;
}
$fh->close();
eval $contents;
if ($#) {
chomp $#;
my $msg = "BAD (perl) syntax in file:\n\n$#\n";
if ( $msg =~ /requires explicit package name/ ) {
$msg .= "\n -> A 'requires explicit package name' message means".
" a NON-VALID variable name was found\n";
}
die "Error: $msg\n\n";
}
return 1;
}
And I'm using variable $log_file in another subroutine this way:
my $fh = read_file ($log_file);
if ($log_file eq "abc.txt"){
while (my $line = <$fh>) {
#do something
}
}
Can anybody help me here, please? Am I missing something or using $log_file in the wrong way?
Thanks in advance!
Try:
my #files = glob($log_file);
use Data::Dumper;
print Dumper(\#files);

Parsing Microsoft Office 2013 MRU Lists in Registry using Perl

I am currently trying to parse the keys in a Windows 7 registry containing the MRU lists for Microsoft Office 2013. However when I attempt to run the Perl script in RegRipper it says the plugin was not successfully run. Im not sure if there is a syntax error in my code or if it is unable to parse the registry as I have it written. The biggest problem is that one of the keys is named after the user's LiveId (it appear as LiveId_XXXXXXX) and this changes from user to user so i would like this plugin to work no matter what the user's LiveId is. Thanks!
my $reg = Parse::Win32Registry->new($ntuser);
my $root_key = $reg->get_root_key;
# ::rptMsg("officedocs2013_File_MRU v.".$VERSION); # 20110830 [fpi] - redundant
my $tag = 0;
my $key_path = "Software\\Microsoft\\Office\\15.0";
if (defined($root_key->get_subkey($key_path))) {
$tag = 1;
}
if ($tag) {
::rptMsg("MSOffice version 2013 located.");
my $key_path = "Software\\Microsoft\\Office\\15.0";
my $of_key = $root_key->get_subkey($key_path);
if ($of_key) {
# Attempt to retrieve Word docs
my $word_mru_key_path = 'Software\\Microsoft\\Office\\15.0\\Word\\User MRU';
my $word_mru_key = $of_key->get_subkey($word_mru_key_path);
foreach ($word_mru_key->get_list_of_subkeys())
{
if ($key->as_string() =~ /LiveId_\w+/)
{
$word = join($key->as_string(),'\\File MRU');
::rptMsg($key_path."\\".$word);
::rptMsg("LastWrite Time ".gmtime($word_key->get_timestamp())." (UTC)");
my #vals = $word_key->get_list_of_values();
if (scalar(#vals) > 0) {
my %files
# Retrieve values and load into a hash for sorting
foreach my $v (#vals) {
my $val = $v->get_name();
if ($val eq "Max Display") { next; }
my $data = getWinTS($v->get_data());
my $tag = (split(/Item/,$val))[1];
$files{$tag} = $val.":".$data;
}
# Print sorted content to report file
foreach my $u (sort {$a <=> $b} keys %files) {
my ($val,$data) = split(/:/,$files{$u},2);
::rptMsg(" ".$val." -> ".$data);
}
}
else {
::rptMsg($key_path.$word." has no values.");
}
else {
::rptMsg($key_path.$word." not found.");
}
::rptMsg("");
}
}
The regex
LiveId_(\w+)
will grab the string after LiveId_ and you can reference it with a \1 like this

perl regex match and store specific character in scalar variable

Now suppose say i have this line in a file:
my %address = (
or any such similar line in which i have defined the hash.
I want to find the character "(" in the line and store "address" in say $hash_name. How do I do it?
Basic idea is to capture the name of the hash defined in the files.
I am trying to do is,
foreach $line <MYFILE> {
if($line =~ /($/ {
How do I proceed further?
Not sure if I understood your problem, but, how about:
my %hash;
while (my $line = <MYFILE>) {
if ($line =~ /\%(\w+)\s*=\s*\($/) {
$hash{$1} = 1;
}
}
open (F1,"inputfile.txt") or die("unable to open inputfile.txt");
my $hash_name
while (<F1>) {
if (/%(\w+) *= *\(/) {
$hash_name = $1;
print $hash_name;
}
}