Run gradle from PHP file - php

I am trying to compile an android app from a PHP file:
<?php
$command = "sh build.sh";
$output = shell_exec($command);
print $output;
?>
My file build.sh:
#!/bin/bash
export JAVA_HOME=/var/www/vhosts/mydomain.com/android-sdk/jdk1.8.0_73/
export PATH=${PATH}:${JAVA_HOME}/bin
export GRADLE_HOME=/var/www/vhosts/mydomain.com/android-sdk/gradle-2.11/
export PATH=${PATH}:${GRADLE_HOME}/bin
export GRADLE_USER_HOME=/var/www/vhosts/mydomain.com/workspace/gradle_user_home/
gradle --info --debug build 2>&1
When I open the PHP file in the browser, I get this error:
* What went wrong: 00:45:56.317 [ERROR] [org.gradle.BuildExceptionReporter] Failed to load native library 'libnative-platform.so' for Linux amd64.
If instead of Shell_exec I use exec() it shows just this:
[ERROR] [org.gradle.BuildExceptionReporter]
If I run gradle from the command line, everything works fine, I think the issue must be with the user, with shell_exec() the gradle command is exectuted with apache user, but I can not find a solution.
Thanks!

Related

error in Run PHP code with command line linux

My system is :
Ubuntu 20.04
Php 7.4
Nginx
I have a php file with following content in /var/www/hamrah.com/create.php path :
<?php
$output = shell_exec('bash flutter create test');
echo "<pre>$output</pre>";
i run this command in terminal and And this command executes correctly (creates a flutter project for me)
cd /var/www/hamrah.com && php /var/www/hamrah.com/create.php
BUT !
But when I put this command in Crontab, the command is not executed properly and gives an error
In Crontab :
* * * * * cd /var/www/hamrah.com && php /var/www/hamrah.com/create.php
Note: If I put shell_exec(' bash ls') instead of shell_exec('bash flutter creat test') in the php file, it will run well and some commands like the flutter create command will not be executed.
error : bash: flutter: No such file or directory
You can test
<?php
$output = shell_exec('bash /opt/flutter/bin/flutter create test');
echo "<pre>$output</pre>";
Cron doesn't read user's environment variables, the default value of PATH is /usr/bin:/bin, usually your flutter SDK won't be located in these 2 folders, so you need explictly specify the location of the flutter SDK. Add this line to the crontab.
PATH=/usr/bin:/bin:/path-to-flutter-sdk-bin
Use the following command if you don't know where flutter is.
which flutter

Ruby command not executing in PHP shell_exec function

I want to run WPScan through PHP file. I am using shell_exec function to call the WPScan command, but it throws out following error:
[ERROR] cannot load such file -- bundler/setup
WPScan needs ruby to be running in the server and WPScan command can only run inside the WPScan directory. When we run this command outside the WPScan directory, than it throws out the same following error:
[ERROR] cannot load such file -- bundler/setup
Means browser is not able to locate the files needed by the WPScan, and all the files are placed inside the document root of the website. Other all commands are working through shell_exec, but ruby command throws out a error. In short it is unable to find the files needed by the WPScan.
The command is:
ruby wpscan.rb --url http://example.com/ --follow-redirection --log
I tried using full path also:
/usr/local/rvm/rubies/ruby-2.3.0/bin/ruby /var/www/html/wp-scan/wpscan.rb --url example.com --follow-redirection --log
But nothing works.
$wp_command = shell_exec('/usr/local/rvm/rubies/ruby-2.3.0/bin/ruby /var/www/html/wp-scan/wpscan.rb --url example.com --follow-redirection --log');
echo $wp_command;
die;
Try something like this:
$wp_command = shell-exec('cd /var/www/html/wp-scan && ruby wpscan.rb --url example.com --follow-redirection --log');
echo $wp_command;
die;
Should work if you install WPScan and RVM correctly.

Invoking a python script from a PHP script

I am running a PHP web script locally for testing purposes through terminal with command: php -S localhost:8000.
<?php $command = '/usr/bin/python /Users/Jupiter/Desktop/NC/www/createHarbourContainter.py'; exec($command); ?>
I am trying to call this python script that exists in the same directory:
#!/usr/bin/python
import os
import sys
save_path = '/Users/Jupiter/Desktop/NC/harbours/'
name = sys.argv[1]
def newHarbourContainer():
os.makedirs(save_path + name)
def main():
newHarbourContainer()
if __name__ == '__main__':
main()
This python script has chmod x+ set to it.
I am able to run the python script from the terminal :python createHarbourContainter.py
What I am unable to do is get the PHP function exec() to invoke the python script. Help needed!
In PHP you can use backticks ` to execute shell commands, so try this:
<?php
`/usr/bin/python /Users/Jupiter/Desktop/NC/www/createHarbourContainter.py`
?>
See http://php.net/manual/en/language.operators.execution.php for more info
So I have found a solution:
$output = shell_exec('python createHarbourContainer.py')
And to get back an output into the browser:
echo "<pre>$output</pre>";
I was running the server in PHP Development Server.
Using bash command: php -S localhost:8000 from directory where index.php is located.
The server was logging all input and output from browser interface.
I realized that when calling: /usr/bin/python from php the PHP Development Serverwould halt and open python interpreter.
And setting the whole path to the python script I wanted executed didn't work because the PHP script didn't have permission.
How this can help someone else in future.

bash script does not work when called from PHP file?

I am trying to run a bash script from the web using php exec() method. The same bash script when ran from the terminal ./test.sh works fine executes all the command without any error. But when i try to run it from the command line it would absolutely not work. They both are in the same location. I have even give chmod 777 permission to that file.
Commnads like ls , pwd etc work but cf isn't working ?
test.sh
/usr/bin/cf login -a https://api.stage1.ng.bluemix.net -o jetmak#ca.blue.com -s dev -u jenk#ca.blue.com -p pass22
/usr/bin/cf api
/usr/bin/cf
executeshellfromphp.php
header('Content-Type: application/json');
$bluemixid = $_POST['bluemixid'];
$bluemixpwd = $_POST['bluemixpswd'];
$env = $_POST['env'];
$restart = $_POST['action'];
$result =shell_exec('sh /var/www/shellscriptphp/test.sh '.$env.' '.$restart.' '.$bluemixid.' '.$bluemixpwd.' ');
echo json_encode(array("result"=>$result));
exit();
Error:
FAILED
Config error: Error writing to manifest file:.cf/config.json
open .cf/config.json: no such file or directory

Crystal Report by using CRExport via PHP

I'm trying to generate a crystal report via a php script.
I was able to successfully use crexport to generate a pdf report. However when I try to execute the script via php's exec command I get this error.
c:\inetpub\wwwroot\mamobile\crexport -F c:\inetpub\wwwroot\mamobile\reports\customer.rpt -O c:\inetpub\wwwroot\mamobile\output\test.pdf -E pdf -S testdb
Crystal Reports Exporter Command Line Utility. Version 2.1.11.1103
Copyright(c) 2011 Rainforest Software Solution http://www.rainforestnet.com
Misc Error: Load report failed.
Type "crexport -?" for help
It works fine via the command prompt.
This is unlikely but is your report by any chance still open in the Crystal Designer when you were running this script? I was getting this error when the .rpt file was open.
it works.
remember to not be with the open rpt file in graphics editor
$output = '';
$command = 'c:\inetpub\wwwroot\mamobile\crexport -F c:\inetpub\wwwroot\mamobile\reports\customer.rpt -O c:\inetpub\wwwroot\mamobile\output\test.pdf -E pdf -S testdb';
exec($command, $output);
var_dump($output);

Categories