Jump to content
xisto Community
miCRoSCoPiC^eaRthLinG

Help: Trying To Create Web-based Compiler W/ Php

Recommended Posts

Hi guys,

Need some serious scripting help. I'm trying to come up with a web-based compiler.

here's how it's going to work.

1. The user is given a form with a large textarea to enter code (lets assume C++).

2. Upon submit the script takes the entire text from the textarea and writes it to a file.

3. Then it calls GCC the linux C compiler and passes this file as an arguement to it, as well as another arguement which is the name of the output file.

4. It then chmods the output file to make it executable.

5. Then it runs the output binary and pipes the console output from that into another text file.

6. Then it dumps this text file back onto your screen.

 

Here's what I've tried to do so far:

 

1. The HTML Form

<html><head></head><body><p>Web-based Compiler</p><form action="compilerparse.php" method="post" name="compiler" id="compiler">  <p>Enter your code below:</p>  <p>     <textarea name="code" cols="80" rows="20" id="code"></textarea>  </p>  <p>     <input name="Compile" type="submit" id="Compile" value="Compile">    <input name="Clear" type="reset" id="Clear" value="Clear">  </p></form></body></html>

2. Upon clicking submit - this form sends the output to a php script.

<?phpif (isset($_POST['code'])){        // Get the code from the form        $code = $_POST['code'];        // Sets infile and outfile name        $codefilename = "temp.c";        $outfilename = "temp.out";        // Open file for writing        $fh=fopen($codefilename,'w') or die ($php_errormsg);        // Write the code         fputs($fh,$code);        // Close file        fclose($fh);        // Sets up compiler command string. FORMAT: gcc codefile -o outputfile        $compilercommand = "gcc " . $codefile . " -o " . $outfile;        // Calls the compiler using system() command and catches output in a var        system($compilercommand, $outputresult);       // Display output/error report        echo $outputresult;}

I've tried doing only upto the compiling part here, but somethings going wrong - the codefile isn't being written at all, leave apart compiling. I'm utter crap when it comes to php coding - so need some serious help here. :P

 

Also I tried to figure out whether the php script is reading the textarea at all by echoing the $code variables.. I found out it's NOT. I'd tried out a simple "hello world" sample...

#include <iostream>using namespace std;int main (void){    cout << "Hello World!" << endl;    return 0;}

When I echo the $code var, this code is shown as:

#include using namespace std; int main (void) { cout << "Hello World!" << endl; return 0; }
i.e. The whole code becomes one single line of text - which is all fine, since all commands are semicolon ";" delimited and gcc won't have any problems with that. What I can't figure is, WHY the #include <iostream.h> is being chopped off to simply #include. Does it have anything to do with the <> braces ?? If so whats the workaround ?

 

Thanks in advance to whoever cracks this php demons skull :P

Share this post


Link to post
Share on other sites

M^E have you been stealing my ideas?Damn, it was only a few days ago I thought wouldn't it be neat if I wrote a PHP script that could emulate the work of a C/C++ compiled console program and output it to browser, although a lot of flaws with how it would be displayed, etc, but I would overcome this by only working on functions that would work.I haven't looked at what you're wanting to do yet, but I'll look at this when I got more time, just thought I'd pop in since I haven't been seen here in a few days.Web based compilation has been done, I know it exists, but I haven't seen any source code for such a thing but it won't be too hard to implement as long as you've got a strong understanding of the language.I'll now read what you want and dream about all the possibilities.Cheers,MCOK so I couldn't help but stay a bit longer to add input.Make sure the directory you're writing to is writable, another thing would be the environment, users are usually chroot'ed to stay only inside space allocated to a web environment, I guess you're personally doing this on your own server? If that's the case, you'll need to figure out who the owner of the file belongs too, hopefully it's the user and not the server's user in which case you won't have rights to the file without some other commands which I dare not suggest be used in PHP at all, it's really a door for some problems.You really need to send your code through a parser to make sure nothing bad has been entered, it's a real security problem this, but for now, lets work on getting it working then deal with all security issues later.brb with working code :P, <iostream> exists, but not to the browser, it's considering it a HTML tag, and being handled by the browser, check source code.MC

Share this post


Link to post
Share on other sites

hehe thanks mc :P There's this saying in one of the Indian languages (Hindi) - Do harami ek hi sochte hai - which in proper terms means, Great men think alike (fools seldom differ too) :P and in literral translation it would be more likeTwo b@st@rds always arrive upon the same conclusion. ... lol :P

 

Even I've looked around and found some sites which offer web-based compilations. Quite fast too - but none of them offer the code/methods to setup one yourself. I think it can be done if we try this way - am just not getting the syntax right.

 

Here are the sites:

1. http://forums.xisto.com/no_longer_exists/

2. http://forums.xisto.com/no_longer_exists/

 

I'm trying to do this with the primary intention of putting up such a compiler on the opensource collaborative devlopment site we're going to launch very soon.

 

What I intend to do is put a limit on the amount of code you can compile - so you can compile and test only small pieces of code and not overburden the system with huge compiles. Just so that you can write up something and test quickly in absence of a compiler at hand.

 

Also - another security consideration we'll have to sort out is that, you can execute some nasty code to read segments of system memory through this - or cause some rogue code to do something bad to the system. Have to find a way to jail the input file/output binary to a particular directory and not letting it act on anything outside that dir. That I think can easily be sorted out with linux... but the web interface is my main concern now :P

Share this post


Link to post
Share on other sites

Dear M^E,First a suggestion for the HTML form:The <TEXTAREA> tag can be modifiedby adding a WRAP= attribute.WRAP= specifies how to handle word-wrapping inside the text area.WRAP="OFF" --- word wrapping is disabled (the default)WRAP="PHYSICAL" --- the text is displayed and submitted word-wrapped.WRAP="VIRTUAL" --- the test is displayed word-wrapped, but is submitted as typed.

Share this post


Link to post
Share on other sites

m^e im a bit confused....are you an indian in thailand or is your location wrong. I mean if you know hindi and that to that particular saying and the translation too :P means u've lived in India for quite some time or you're in touch with indian thai's....Im sorry but im absolutely NO help with programming of any sort except well LOGO which i learnt in the first grade..:P

Share this post


Link to post
Share on other sites

 

<?php echo '<' . '?xml version="1.0" encoding="utf-8"?' . '>'; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd xmlns="http://www.w3.org/1999/xhtml/; xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Title</title></head><body> <h4>Web-based Compiler</h4> <form name="compiler" id="compiler" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <p>Enter your code below:</p> <br> <textarea name="code" id="code" cols="80" rows="20"><?php if(isset($_POST['code'])) echo $_POST['code']; ?></textarea> <br> <input name="Compile" id="Compile" value="Compile" type="submit" /> <input name="Clear" id="Clear" value="Clear" type="reset" /> </form><?php if(isset($_POST['code'])){ $code = $_POST['code']; $codefilename = 'temp.cc'; $outfilename = 'temp.out'; $fh = fopen($codefilename, 'w') or die ('Failed writing file'); fwrite($fh, $code); fclose($fh); $compilercommand = 'g++ ' . $codefilename . ' -o ' . $outfilename . ' -O3'; system($compilercommand); $path = `pwd`; $file = $path . $outfilename; $file = str_replace("\n", '/', $file); $file1 = `$file`; echo "<pre>$file1</pre>";}?></body></html>
OK compilation works, user however is apache, but program seems to work if we change the content to something that's allowed to execute, will try cgi-bin's method and see if that works.How we could output this, is if we write the output to another file and just bring that up? although lets work on trying to output straight to the browser first.Well, hopefully this has given you some ground to work off, I'll come back when it's not so early in the morning, might not be back again, since I got a 21st to attend tonight.Signing off, 6 in the morning.MCOK screw the above, except the code.We need to figure out how to chcon -t httpd_sys_script_exec_t temp.out and to also include our current working directory in the PATH, since this has been the only way I've been able to successfully run the script.I temporary changed the PATH to reflect where it's located, e.g. PATH=$PAhttp://forums.xisto.com/no_longer_exists/ then ran the chcon command above on the temp.out file. Then tested it, everything worked. So now all that's needed is a workaround.MCStayed up till 6:43am :P

Share this post


Link to post
Share on other sites

Thanks for the code MC. And as for the idea, if you look at dates, my post here (please note, only staff can see this post, sorry guys) clearly shows I had the idea first (look at the timestamps). Not that I'm complaining, I'm going to work on an assembly one soon. The code MC provided will be helpful. And my idea was for this forum anyway, so :P.

M^E, to answeer your question which none bothered to answer, the reason "<include iostream.h>" does not get outputted with echo is that it is interpreted as html tags, and since the browser doesn't recognize them it ignores them. They will appear in the source if you view it.

And MC, what do the "`" symbols mean, I'm not familiar with them (the accent mark things)?

~Viz

Share this post


Link to post
Share on other sites

Hey vizskywalker,` is known as Backticks in programming/console, in PHP it's one of the methods used to execute a command, instead of using backticks we could have used system(), etc.I use backticks a lot to execute a command within a command, usually in console for filling in desired information without me needing to type it in. e.g. if I am going to find all the RPMs I've installed that are based on my current kernel.rpm -qa | grep `uname -r`What the backticks do is execute the command uname -r which displays the kernel version I'm running, grep then grabs anything from the rpm query-all of anything that matches the exact kernel number in the queries.Not why I would use backticks, I would probably use it to update kernel specific modules if I had just installed a new kernel and needed the modules to be updated.PHP manual should have reference to backticks.As for the idea, mine was not like the above, I was actually thinking of making PHP actually capable of outputting C/C++ programs without the need of compilation, so pretty much making PHP the compiler for C/C++ programs and having the ability to execute it.MC

Share this post


Link to post
Share on other sites

That sounds cool (I had a similar idea for some other languages, I never really liked c++, but I'm starting to appreciate it). How would you go about getting user input for the php run c++ program?~Viz

Share this post


Link to post
Share on other sites

GUYSSSSS PROBLEM SOLVED

 

YAHOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO :D

 

Check here: http://www.hugedomains.com/domain_profile.cfm?d=antilost&e=com

 

It's done it's done it's doneeeeee ... :(

 

Now it's time to design our parser... Now it's possible to compile using ANY compiler - C++/Java/whatever...

 

I've managed to figure out both the PHP way and also through CGI scripts written in C :D

 

Regards,

m^e

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.