Executing a PHP script from VBScript - php

I have a classic ASP page that needs to access a file generated via a PHP script. Is it possible to call the PHP script somehow, from within VBScript?
And before anyone asks, I do not have the time to port either script. They are both very complex and I only need a short term solution at this time.
Now the below is not generating errors, but the file is not being generated. I'm ready to give up.
str = "D:\TTnav_Alpha\Alpha\Framework\EnvMgr\generate_jira_list.php"
Set objShell = CreateObject("WScript.Shell")
'response.write(str)
objShell.Run """C:\Program Files (x86)\PHP\php.exe"" " & str ,3,true
Set objShell = Nothing

My .vbs code successfully calls a script in .php file.
In my .vbs file I have this:
set wshell = CreateObject("WScript.Shell")
wshell.Run "C:\xampp\php\php.exe -f C:\Temp\test2.php"
set wshell = nothing
Double quotes must enclose the path if there are spaces within the path, e.g.:
set wshell = CreateObject("WScript.Shell")
wshell.Run """C:\my xampp dir\php\php.exe"" " & "-f C:\Temp\test2.php"
set wshell = nothing

Just invoke WScript.Shell as explained at How do I execute a DOS command / batch file / exe from ASP?
<%
' Untested
set wshell = CreateObject("WScript.Shell")
wshell.run "c:\dos\php c:\site\script.php"
set wshell = nothing
%>
Edit: You are now trying to execute this:
C:\Program Files (x86)\PHP\php.exeD:\TTnav_Alpha\Alpha\Framework\EnvMgr\generate_jira_list.php
This is an invalid command (if you copy it to a command prompt, it won't run). You need to quote the paths with spaces and separate the command from the argument:
"C:\Program Files (x86)\PHP\php.exe" D:\TTnav_Alpha\Alpha\Framework\EnvMgr\generate_jira_list.php
In VBScript you escape quotes doubling them so you want:
objShell.Run """C:\Program Files (x86)\PHP\php.exe"" " & str,3,false
By the way, that 3 means Activates the window and displays it as a maximized window. Don't forget to remove it before going live.
Ref: Run Method (Windows Script Host)

Related

Erubis not working when executed from PHP

I'm calling a Ruby script from PHP with system('ruby show.rb filename').
The show.rb looks like this:
require 'erubis'
f = ARGV[0]
puts f
input = File.read(f)
eruby = Erubis::Eruby.new(input)
puts eruby.result(binding())
When executing the command ruby show.rb filename in the console everything is printed out, but when calling it from PHP it's only printing the filename, but the template is not rendered. To see errors I piped stderr -> stdout in the PHP call and I got this
/Library/Ruby/Gems/2.3.0/gems/erubis-2.7.0/lib/erubis/converter.rb:132:in 'scan': invalid byte sequence in US-ASCII (ArgumentError) from /Library/Ruby/Gems/2.3.0/gems/erubis-2.7.0/lib/erubis/converter.rb:132:in 'convert_input' from /Library/Ruby/Gems/2.3.0/gems/erubis-2.7.0/lib/erubis/converter.rb:36:in 'convert' from /Library/Ruby/Gems/2.3.0/gems/erubis-2.7.0/lib/erubis/engine.rb:30:in 'initialize' from show.rb:5:in 'new' from show.rb:5:in temp/Newsletter.rb'
I'm new to Ruby. Are there maybe any option parameters to fix this? What is PHP doing differently there, that the error can occur?
I don't know why, but when opening a erubis template file inside show.rb called by PHP it must be opened as a binary file. So I only had to change show.rb to:
require 'erubis'
f = ARGV[0]
handle = File.open(f, "rb") # "rb" fixed the issue.
input = handle.read()
handle.close()
eruby = Erubis::Eruby.new(input)
puts eruby.result(binding())

Command line command doesn't work in Python - doesn't send file

I am using the youtube-upload command line script to (oddly enough) upload a video to youtube. I'm passing in my variables from PHP, into Python, which generates the command string and passes it to
result = subprocess.call(copmmand, shell=True)
The command generated by my script is as follows:
youtube-upload --description="$(< \sites/default/files/descriptions/5799.txt)" --title="Title" --category=Music sites/default/files/videos/5799.mp4
or (youtube-upload documentation lists with a prefixed backslash, either achieves the same result)
youtube-upload --description="$(< sites/default/files/descriptions/5799.txt)" --title="Title" --category=Music sites/default/files/videos/5799.mp4
Now the script works perfectly aside from the description text file is not passed. Outside of python, in the command line, it does it. I've tried passing the full path of the file too...but it's not happening.
There are no errors in the stack trace output
Any ideas?
P.S. I'm passing it as a file because when I tried to pass in a string from php in the json object, I couldn't retrieve it on the other end. Can anyone maybe point me at the right direction for this issue?
I got around this by opening the file into a variable and passing that into the command instead
with open ("/home/mmallett/Sites/GottaLoveSoul/sites/default/files/descriptions/" + nid + ".txt", "r") as myfile:
data=myfile.read().replace('\n', '')
upload = 'youtube-upload --description="' + data + '" --title="' + title + '" --category=Music ' + video
result = subprocess.check_output(upload, shell=True)

Python called from PHP but SQLite3 not working

I have this PHP script:
<?php
if($_GET["name"] != null && $_GET["name"] != ""){
$name = urlencode($_GET["name"]);
$name = htmlentities($name);
$title = urlencode($_GET["title"]);
$title = htmlentities($title);
$art = urlencode($_GET["art"]);
$art = htmlentities($art);
$output = array();
exec("python add.py $name $title $art",$output);
}
?>
and here's the add.py file:
import sys
import sqlite3
name = sys.argv[1]
title = sys.argv[2]
art = sys.argv[3]
tup = (name,title,art)
conn = sqlite3.connect('arts.db')
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS arts(name,title,art)')
c.execute('INSERT INTO arts VALUES(?,?,?)',tup)
conn.commit()
conn.close()
When the file runs there is no arts.db file in my current directory. Not only that, when I debugged my program by adding print statements every here and there I realised that my program runs till conn = sqlite3.connect('arts.db') and then exits before the statement is executed.
There is no error in my program because I used the python editor in the terminal (I use Ubuntu) and then I could execute this program successfully but this doesn't happen when I execute this from the PHP script.
If you're trying to reference an existing arts.db file, which is not in the current directory when the PHP script runs, then it's probably easiest to refer to the file by its full path, i.e. change the line...
conn = sqlite3.connect('arts.db')
...to something like...
conn = sqlite3.connect('/the/full/path/to/arts.db')
If you're trying to create a new arts.db, it's highly probable that the webserver process doesn't have permission to create the file in the current directory when the PHP script runs.
It's probably safer not to give PHP permission to write to the directory containing the PHP script[s], so create a directory somewhere else to store the file, give it the appropriate permissions, and use the full path to that directory in the script.
Update
i have figured out that i dont have permissions to write hence i
created a db using another program and created a table in it but still
for inserting values into it i require permissions, can you tell me
how to do that?
Problem is that any process spawned by the PHP script will inherit the permissions from the parent process, and will have the same limitations.
On recent versions of Ubuntu, the /var/tmp directory can always be written to by any user, so you could put it there with...
conn = sqlite3.connect('/var/tmp/arts.db')
...or you can put it anywhere else by modifying the permissions, with...
$ sudo chgrp www-data /the/full/path/to/directory
$ sudo chmod g+ws /the/full/path/to/directory
Of course, this means there's not much point in using a seprate Python script, so you may as well do it all in PHP.

passing php variable to bash script that uses shflags

I am trying to make a PHP program triggered by a web submit tell a bash script to run with a single command line parameter. I am using the shflags command line parser for bash.
The pertinent part of the PHP script is as follows:
// generate unique filename
$destinationFolder = Mage::getBaseDir('media') . DS . 'webforms' . DS . 'xml';
$filename = $destinationFolder . DS . $result->getId().'.xml';
// create folder
if (!(#is_dir($destinationFolder) || #mkdir($destinationFolder, 0777, true))) {
throw new Exception("Unable to create directory '{$destinationFolder}'.");
}
// export to file
$xmlObject->getNode()->asNiceXml($filename);
// define parameters to pass
exec ( '/opt/bitnami/apache2/htdocs/sfb/scripts/xform.sh --xmlfile'.' '.$filename);
}
}
?>
The bash script (xform.sh) (just a test script) is as follows.
#!/bin/bash
. ./shflags
echo "foo" >> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/$$".txt"
echo "foo" >> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/foo.txt
DEFINE_string 'xmlfilename' 'test' 'filename of current x.xml file from webforms' 'Z'
FLAGS "$#" || exit 1
eval set -- "${FLAGS_argv}"
echo "xml file was" ${FLAGS_xmlfilename} >> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/foo.txt
The bash script works correctly from the command line, i.e.
$xform.sh --xmlfilename 1.xml
writes "xml file was 1.xml" to the foo.txt file.
When the PHP script is triggered from the web, the first part works correctly, i.e. it writes "foo" to the two target files, foo.txt and $$.txt. However, the xmlfilename variable is not coming along, and I really need that file name to be passed to the command line! (Note I should not need to use escapeshellarg because the file name is generated by my PHP program, not by user input.)
I have checked all the file permissions I can think of. xform.sh and shflags are both members of the www-data (Apache) group, owned by Apache, and a+x.
My suspicions are that the problem is related either to a) my PHP exec syntax or b) file permissions. Everything works as intended except the bit after xform.sh in this line!
exec ( '/opt/bitnami/apache2/htdocs/sfb/scripts/xform.sh --xmlfile'.' '.$filename);
UPDATE:
I've narrowed the problem some more by isolating the problem with some test code. With:
$script="echo";
$xmlfilename="$filename";
$target=">> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/foo.txt";
exec ("$script $xmlfilename $target");
...
PHP correctly writes the $filename to foo.txt, so $script works when value is "echo" and $filename works too.
When I set $script to a different simple form of the xform script that (only) writes the data to the file, that also works correctly.
So the problem is specifically with something that happen when PHP tries to write the $filename as a command line variable. Does a script run by Apache need more permissions than usual if it includes a command line variable?
Sigh.
In your exec() call you have the flag as --xmlfile but you are calling it from the command line as --xmlfilename

PHP, problem with exec...how do I make sure the execution is working?

I am uploading a video, which is supposed to generate three screenshot thumbnails. I have the same upload code running in both admin and front-end, but for some odd reason the thumb is only being generated when I upload from front end, and not from backend...
My directory structure
root/convert.php (this is the file running through exec call)
(the following two files are the upload files running in user-end and admin-end respectively)
root/upload.php
root/siteadmin/modules/videos/edit.php
I believe convert.php is not being run from admin-side for some reason. The command is something like:
$cmd = $cgi . $config['phppath']. ' ' .$config['BASE_DIR']. '/convert.php ' .$vdoname. ' ' .$vid. ' ' .$ff;echo $cmd;die;
exec($cmd. '>/dev/null &');
And echoing out the exec $cmd, I get this:
/usr/bin/php /home/testsite/public_html/dev/convert.php 1272.mp4 1272 /home/testsite/public_html/dev/video/1272.mp4
How do I make sure convert.php is being run?
EDIT: OK, now I am sure it is not being executed from admin-side, any ideas why?
http://php.net/manual/en/function.exec.php
"return_var" - If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.
Another way to determine if exec actually runs the convert.php file, add some debugging info in convert.php (e.g. write something to a file when the covert.php script starts).
Just an Idea
you could print "TRUE" in the convert script when it runs successfully.
don't add >/dev/null &
check the return value of exec
$value = exec($cmd);
if($value == 'TRUE')
// did run sucessfully
}
chmod 755 convet.php
you also make sure the first line of convert.php is:
#!/usr/bin/php
check the full path of php cli executable.
Also make sure convert.php las unix line ending ("\n")

Categories