Passing code to python from PHP - php

I have a custom codechecker in python, Also there is a bigger project running in PHP, which stores users code in MySQL database.
I am new to python, so I'm not sure how I can pass the code from PHP to Python.
Do I have to store the file to the filesystem to pass it to Python? (In that case too many files might be created, and their cleanup after execution has to be taken care)

To expand on Brad's answer, there's several options, each with pros & cons...
Pipes (ie: STDIN/STDOUT): proc_open()
Shared memory: shmop_open()
AF_UNIX family sockets: socket_bind()
You'll probably want to use the first option but read up on the others before making a commitment.

Just use STDIN/OUT.
See proc_open() to get at the pipes you need.

Related

How do I make variables in PHP and pass them to Ruby?

I have a script running in Ruby, and another in PHP, and, for my needs it's not possible to make it all run in the same scripting language.
I want to use PHP to create a variable in Ruby. Currently, I have this code:
PHP:
$config = fopen("config.txt","w+");
fwrite($config,$sArguments);
fclose($config);
Ruby:
while true do
file = File.new("config.txt", "r")
config = file.gets
file.close
end
The PHP will write the next configuration in a file and Ruby will read it and turn it into a variable. It works, but Ruby has to work on reading and reading the file, and it sometimes fail to read it correct, so this code is very badly optimized.
Is there a faster way to pass information from a PHP script to a Ruby script?
I'm no expert in PHP but I think "How to run Ruby code from Python (Python-Ruby bridge)" provides some ways of how to accomplish communication between the two languages. Although the page speaks of Ruby-Python communication, I'm pretty sure some of the suggestions can be implemented for Ruby and PHP.
One solution is to use XML-RPC to transfer simple data types from PHP to Ruby. Ruby has native support for XML-RPC and I think there are libraries for PHP to enable support for XML-RPC.
You can also use pipes. That is, you call your PHP script via Ruby's IO.popen. I've done something similar for Ruby-Python in "Feasibility of using pipe for ruby-python communication" but I have not really evaluated the performance.
It's difficult to say what the problem is exactly from the information given, but when you say Ruby sometimes fails to read I'm guessing it's your Ruby script reading the file when PHP is half-way through writing it.
An easy fix would be for PHP to write it to a temporary file and then move or rename the temporary file so it can be picked up by the Ruby script. Move/rename is atomic so there is no chance of a half written file being read.
A couple of improvements you could also make:
Use something like the listen gem to only read the file on an update.
I'm not sure what data you're passing, but you could use something like YAML as an interchange format. Catch any parsing exceptions and log the received data to aid debugging.
Taking a step back, if you're on a "real O/S", a simpler option would be to use message-based IPC via Unix sockets, assuming persistence is not a concern. Have a look at this page on "Introduction to IPC in Ruby" for a good intro with examples on passing messages between processes.

How to access variable values of a php script from a python script?

How can I access my php script from a Python script?
I need my Python script to be able to access the variables within the php script. (By the way, I'm new to php and Python.)
Thanks in advance.
If I understand it correctly, you have a service in PHP, and want to communicate with another one in Python.
Now, this is not really related to PHP or Python: this is quite a classic issue of integration and there are several ways to accomplish it; without more details about your problem, it may be very difficult to be specific about a solution and what kind of approach could be the better for you, but below you can find some ideas.
You could for instance save the status from PHP service in an ad-hoc table in the database, and then query it from the Python service.
Another way could be to use a RESTful approach: the information is available as a resource, accessible via a GET query; in PHP you would have a small handler that would just return a small JSON (or XML, if you like that kind of stuff), and in Python you would have instead the client. Of course, there are security issues to consider, but I think you got the idea.
For more information, I recommend you having a look at an interesting series written some time ago by Paul Stovell about integration. It is very accessible, and shows several approaches - although not all of them apply to your current issue.
Elaborate. Is the PHP file local? On a webserver? Where's the python file?
If the php file is on a server with the python file, use an exec statement.
If the python file is local and the php file is on a server, then you need to use urllib.
If both are local, write an interpreter...

Best design for PHP/ASP application that makes calls to remote servers

We run multiple Windows/IIS/.Net sites (up to 30+ sites per server). Each site is customized for the individual customer via a configuration file that contains the settings.
I am tasked with writing a small tool that will 'grep' all of the config files on a certain server for a particular config setting (or settings) and return the values for a nice tabled web page display. It will save many groups lots of time, especially since most groups don't have access to production servers, but they need to know how a customer is currently configured.
I have working code that finds all .config files from a starting path, I can easily extend this to do my grep'ing. Here are the challenges:
I want to aggregate this data from MULTIPLE servers. That means, the tool will be hosted on its own server -- and will make calls to a list of servers.
I'm limited to using .NET/ASP on the actual servers (they won't install PHP on IIS), but I'm writing the tool in PHP.
PROPOSED DESIGN: From my vantage point, I'm thinking the best way to accomplish this is to write my PHP tool and have it make AJAX or CURL requests to ASP scripts that live on each server in the list. Each ASP script could do the recursive directory parsing to find the config files and individually grep the files for the data, and return it in the RESPONSE.
Is that the best way to accomplish this? Should the ASP or PHP side do the 'heavy lifting'? Is their a recommended data format I should be using to pass the data.
Any ideas or samples would be great. If you need more info, I can provide!
Thanks!
Update: Here's an example of a config. Its a basic ASP file that gets included in other scripts.
custConfig1 = " 8,9,6:5:5 "
custConfig2 = " On "
I think you're bang on using PHP for the "receiving" script, and pretty sure you have that in hand.
Based on the format of your example config file, you could use ExecuteGlobal in classic ASP to load each file as you loop through them in your recursive directory lookup. Then you can use the custConfig1 et al. names in your script. e.g. (pseudo)
for each file
output("custConfig1") = custConfig1
next
Return what you need as JSON using a handy library and then do all the "hard" work of collating it and outputting it in PHP.
Yes, "grep" (if by that you mean importing a text file and using reg expressions to navigate it) isn't the best solution, in my humble opinion, use either JSON or XML as the format, and use PHP's built in XML or JSON tools.
JSON: http://php.net/manual/en/book.json.php
XML: http://php.net/manual/en/book.simplexml.php
You could use the DOM to navigate XML alternatively to SimpleXML, but SimpleXML is easier to learn (again, in my opinion) and will work for your needs.

exec function in PHP and passthru?

Hello I have a couple questions about PHP exec() and passthru().
1)
I never used exec() in PHP but I have seen it is sometimes used with imagemagick. I am now curious, what is some other common uses where exec is good in a web application?
2)
About 6 years ago when I first started playing around with PHP I did not really know anything, just very basic stuff and I had a site that got compromised and someone setup there own PHP file that was using the passthru() function to pass a bunch of traffic throught my site to download free music or video and I got hit with a 4,000$ bandwidth charge from my host! 6 years later, I know soo much more about how to use PHP but I still don't know how this ever happened to me before. How can someone beable to add a file to my server through bad code?
1] Exec() is really useful when you:
A) Want to run a program/utility on the server that php doesn't have a command equivalent for. For example ffmpeg is common utility run via an exec call (for all sorts of media conversion).
B) Running another process - which you can block or NOT block on - that's very powerful. Sometimes you qant a pcnt_fork though, or similar, along with the correct CL args for non blocking.
C) Another example is when I have to process XSLT 2.0 - I have to exec() a small java service I have running to handle the transformations. Very handy. PHP doesn't support XSLT 2.0 transformations.
2] Damn that's a shame.
Well, lots of ways. Theres a family of vulnerability called, "remote file include vulns", that basically allow an attacker to include arbitrary source and thus execute it on your server.
Take a look at: http://lwn.net/Articles/203904/
Also, mentioned above, say your doing something like (Much simplified):
exec("someUnixUtility -f $_GET['arg1']");
Well, imagine the attacker does, url.come?arg1="blah;rm -rf /", your code will basically boil down to:
exec("someUnixUtility -f blah; rm -rf /");
Which in unix, you separate commands w/the ; So yeah - that could be a lot of damage.
Same with a file upload, imagine you strip the last four chars (.ext), to find the extension.
Well, what about something like this "exploit.php.gif", then you strip the extension, so you have exploit.php and you move it into your /users/imgs/ folder. Well, all the attacker has to do now is browse to users/imgs/exploit.php and they can run any code they want. You've been owned at that point.
Use exec or when you want to run a different program.
The documentation for passthru says:
Warning
When allowing user-supplied data to be passed to this function, use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.
Someone had probably found a security hole in your script which allowed them to run arbitrary commands. Use the given functions to sanitise your inputs next time. Remember, nothing sent from the client can ever be trusted.
exec() allows you to use compiled code that is on your server, which would run faster than php, which is interpreted.
So if you have a large amount of processing that needs to be done quickly, exec() could be useful.

Calling fcsh from PHP script

My question is whether or not Flex's fcsh can be called from within a PHP script. Here is the background:
I have been created a simple process that creates a simple quiz/tutorial by converting a text file into a .mxml file and compiling to a .swf file using the mxmlc compiler. This works well from the command line, but I wanted to make the process easier by creating a web-interface to do this. My initial attempts with PHP's exec() function have not worked. The Python scripts I use to create the .mxml file work fine (using exec()), but I have not been able to get the mxmlc compiler to work.
After searching on the Web and on this site, I believe that using fcsh (instead of mxmlc) may be the way to go. Using fcsh would certainly compile the .mxml file faster (after the first run), and I think that fcsh can be launched as a service that might be able to be called from PHP.
On the other hand, maybe I am approaching this the wrong way. Would it be better to write a Flex application that calls fcsh and avoid using PHP?
Edit: Using fcshctl as hasseg suggested in his answer below worked very well. Thanks Ali.
The problem with calling fcsh from within scripts is that it works as an interactive shell instead of taking command-line arguments, compiling, and returning an exit status. There are different ways to get around this, which I've listed in this blog post of mine, where I mainly talk about fcshctl (which is my own solution for this,) but at the bottom of the post I've also listed other similar solutions to get fcsh integrated into nonstandard build workflows.
There are a few other ways in php to execute an external script. They are exec(), passthru(), system(), and backticks i.e. the key to the left of the 1 key. Each one has a different purpose and return mechanism.
You may have to put the command that executes your executable into a script and call that script via one of these functions.
Is there a particular reason why you can't use mxmlc directly? It seems like it would be easier to call than fcsh. Just specify all your compiler options in a XML file run it like mxmlc -load-config path/to/config.xml. You can find an example of the XML configuration format in FLEX_HOME/frameworks/flex-config.xml.

Categories