Constructing shell command including php variables - php

When this shell_exec command runs within my function, only the first url parameter is passed to the receiving php file. I suspect I am not constructing the command correctly.
shell_exec("curl https://inklines.art/process_emails.php?url=" . $full_site_url . "&domain=" . $domain . "&site=" . $site_title . "&title=" . $title . "&permalink=" . $permalink);
How should the command be constructed to pass all the variables?
Thanks much!

Related

PHP __DIR__ working locally but not on production

I have a script in a Wordpress plugin and I am trying to incorporate logging using fwrite().
My code is like this:
$log_url = __DIR__ . '/logs/update-email-publish-tasks/' . date("n.j.Y") . '.txt';
$logfile = fopen($log_url, "w");
fwrite($logfile, 'Starting tasks at ' . date("H.i.s") . "\n");
//A bunch of code that logs stuff;
fwrite($logfile, "\n" . 'Finished at !' . date("H.i.s") . "\n");
fclose($logfile);
This is working fine locally but does not work on production. I am wondering if I need to use something besides __DIR__? I also tried WP_CONTENT_DIR which did not work.
FWIW I am hosting production on Pantheon.

PHP command to execute powershell script as admin

I want to run PowerShell script as admin from PHP. I am using below command to run the script, but it does not get executed as admin.
Any help how to get this working ?
I am using older version of XAMP.
$output=shell_exec('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command E:\XAMPP_DV\htdocs\project\lib\pwdreset.ps1 ' . $user . " " . $pwd);
I'm going to go out on a limb and assume you don't have policies set up properly:
$output = shell_exec('powershell -ExecutionPolicy Bypass -NoProfile -File "E:\XAMPP_DV\htdocs\project\lib\pwdreset.ps1" ' . $user . ' ' . $pwd);
In almost all cases, the path to powershell.exe is in the PATH environment variable, and the ExecutionPolicy is set to RemoteSigned by default.
From Microsoft TechNet the following solution is suggested
$output=shell_exec('powershell -noprofile -command "&{ start-process powershell -ArgumentList -noprofile -file E:\XAMPP_DV\htdocs\project\lib\pwdreset.ps1 ' . $user . ' ' . $pwd . ' -verb RunAs}"');

7zip command executes from command line but not within php script

I am attempting to write a script which automatically unzips 7zip archives. I'm able to get the command to run from the command prompt, but it doesn't work when running from within the php script.
Here's what I have in terms of code:
$filefolder = "F:/dev/";
$filename = "archive.7z";
$filepath = $filefolder . $filename;
$unzip = "cmd /c 7z x " . $filePath . " -o" . $fileFolder . " -spf";
print_r($unzip . "<br>"); //checking to make sure the command is formed correctly
exec($unzip, $outcome, $rtnStatus);
print_r($outcome);
print_r($rtnStatus);
The following is returned for $outcome and $rtnStatus:
Array ( )
1
What am I missing here?

php shell_exec() interpolation

I have a php script that builds a dynamic command string (calls a perl script), then executes the command, like this:
$cmd_string = "perl $pushFile";
foreach($cmd_args AS $argName => $arg){
$cmd_string .= ' --' . $argName . '="' . $arg . '"';
}
$output = shell_exec('export PERL5LIB=/mnt/path/to/custom:$PERL5LIB && ' . $cmd_string . ' 2>&1');
I am getting failures that I think are being caused by interpolation of some of the arguments. For example is one of the arguments is '246+8GT>-', it gets turned into '246 8GT ' and an error that the string is unterminated. But, if I print_r $cmd_string to the screen and execute it via command line, or copy/paste it into the $cmd_string variable, it executes properly. I am stumped. How can I make sure these arguments are being passed properly? I tried this:
$output = shell_exec('export PERL5LIB=/mnt/path/to/custom:$PERL5LIB && ' . escapeshellcmd($cmd_string) . ' 2>&1');
but get the same result. Help?
You are escaping the comamand string, after it has been built.
Try this:
$cmd_string = "perl $pushFile";
foreach($cmd_args AS $argName => $arg){
$cmd_string .= ' --' . $argName . '="' . escapeshellarg($arg) . '"';
}
$output = shell_exec('export PERL5LIB=/mnt/path/to/custom:$PERL5LIB && ' . $cmd_string . ' 2>&1');

Executing AppleScript with PHP shell_exec() and OSAScript

My goal is to be able to submit a search query from a web form and have an AppleScript execute the search in DEVONagent. The AppleScript works fine in terminal but I get an error when having PHP do a shell_exec().
<?php
$theQuery = $_GET["Query"];
$cmd = "theSearch=\"$theQuery\" osascript -e \"set theSearch to system attribute " . "\\" . "\"theSearch" . "\\" . "\"\" -e \"tell application " . "\\" . "\"DEVONagent" . "\\" . "\"\" -e \"search theSearch using set " . "\\" . "\"Web (Deep Link)" . "\\" . "\"\" -e \"end tell\" 2>&1";
echo "<pre>$cmd</pre><BR><BR>";
$theResponse = shell_exec ( $cmd );
echo "Your search for \"$theQuery\" has started and the results will be emailed to you once complete.";
echo "<pre>$theResponse</pre>";
?>
I end up with the following error from the $theResponse echo:
83:92: syntax error: Expected end of line but found identifier. (-2741)
I'm thinking maybe a permissions thing but I just cannot figure it out.

Categories