I am making a replication website ( replicating codingbat.com ), And I have been designing it all day and I have to a halt where I need to decide to go with Either a Java Backend or PHP Backend. I have a Java one done already but it just doesnt seem like it is very productive and secure:
<?php
include ("/var/www/challenges/challenges/java/java.class.php");
function getChallenge()
{
return new JavaIntroIIIChallenge();
}
class JavaIntroIIIChallenge extends JavaChallenge
{
public $abbas = array();
public function __construct()
{
$this->abbas = $this->getABBAs();
parent::__construct1(3, "public static void printABBA(String a, String b) {\n\n}\n",
$this->getStringLiteral($this->abbas));
}
public function getStringLiteral($abbas) {
$return = "";
foreach ($abbas as $ab){
$return .= $ab[0] . ":" . $ab[1]. " ";
}
echo $return;
return $return;
}
public function getABBAs()
{
$array = array();
$array1 = array("x" , "y", "Hello" , "a" , "b", "c" , "5", "t");
$array2 = array("y" , "x", "CodeMeh" , "b" , "c", "a" , "0", "z");
$rand = array_rand($array1, 5);
return array(array($array1[$rand[0]], $array2[$rand[0]]), array($array1[$rand[1]], $array2[$rand[1]]), array($array1[$rand[2]], $array2[$rand[2]]),array($array1[$rand[3]], $array2[$rand[3]]),
array($array1[$rand[4]], $array2[$rand[4]]));
}
public function getFullCode($cName, $subCode)
{
return "public class $cName {\npublic static void main(String[] args) {System.out.println(\"HELLO\");for(String arg:args){System.out.println(\"HELLO\");String[] ab = arg.split(\":\");printABBA(ab[0], ab[1]);}} $subCode }";
}
public function getResults($output)
{
$results = array("success" => false);
if (empty($output))
{
$results['success'] = false;
} else
{
$count = 0;
$index = 0;
foreach ($output['output'] as $line)
{
$string = $this->abbas_literal[$index][0] . $this->abbas_literal[$index][1] . $this->abbas_literal[$index][1] . $this->abbas_literal[$index][0];
//echo $string . "123";
if (strtolower($string) == strtolower($line)){
$results['tr'][$index]['expected'] = $string;
$results['tr'][$index]['thisrun'] = $line;
$results['tr'][$index]['ok'] = "✓";
$results['tr'][$index]['color'] = "green";
$count++;
} else {
$results['tr'][$index]['expected'] = $string;
$results['tr'][$index]['thisrun'] = $line;
$results['tr'][$index]['ok'] = "X";
$results['tr'][$index]['color'] = "red";
}
if($index == 4) {
break;
}
$index++;
}
if($count == 5){
$results['success'] = true;
}
}
return $results;
}
public function printSolution()
{
return "<p><div align=left><code>public static void printHelloWorld() {<br><p>System.out.println(\"Hello world!\");</p><br>}<br></code></div></p>";
}
public function getDescription()
{
return $this->desc;
}
}
?>
<?php
include ("/var/www/challenges/challenges/base.class.php");
abstract class JavaChallenge extends BaseChallenge
{
private $code = "";
private $args = array();
public function __construct($id, $code)
{
parent::__construct($id);
$this->code = $code;
}
public function __construct1($id, $code, $args)
{
parent::__construct($id);
$this->code = $code;
$this->args = $args;
}
/**
* Prints the body of the challenge
*/
public function printBody()
{
$cName = "c" . $this->id . "_" . rand(0, getrandmax());
if (!$this->isFinished())
{
//<div class="windowbg"> <span class="topslice"><span></span></span>
if (isset($_POST['purchase']))
{
if (purchase("Solution for " . $this->name, "You purchased the solution to " . $this->
name, $this->points * 2))
{
$this->message("You purchased the Solution!", $this->printSolution());
} else
{
$this->message("Insufficient funds!", "Unable to purchase this item!");
}
}
?>
<div class="sp_block">
<div>
<center>
<?php
if (isset($_POST['code']))
{
$code = $_POST['code'];
$output = $this->getOutput($cName, $code);
if (!$output['error'])
{
$results = $this->getResults($output);
?>
<table width="50%" border="2">
<tbody>
<tr>
<th>Expected</th>
<th>This Run</th>
<th></th>
<th></th>
</tr>
<?php
foreach ($results['tr'] as $row)
{
echo "<tr><td> " . $row['expected'] . "</td><td><pre>" . $row['thisrun'] .
"</pre></td><td>" . $row['ok'] . "</td><td bgcolor=" . $row['color'] .
"> </td></tr>";
}
?>
</tbody>
</table>
<?php
if ($results['success'])
{
?>
<img width="30" height="30" src="http://www.codemeh.com/forums/Themes/default/images/tick.png"><font size="+2">+<?php
echo $this->points;
?> Points</font>
</center>
<?php
$this->award();
}
} else
{
$this->message("Error!", $output['problem']);
}
}
?><br /><br />
</div>
<center>
<span style="font-size:16px">
<?php
echo htmlspecialchars($this->getDescription());
?></pre>
</span><br>
<br>
<div style="width: 85%;" align="right"><form method="post"><input name="purchase" value="Purchase The Solution (<?php
echo $this->points * 2;
?> Points)" type="submit" /></form></div><br />
<form method="post">
<script language="javascript" type="text/javascript" src="***"></script>
<script language="javascript" type="text/javascript">
editAreaLoader.init({
id : "code" // textarea id
,syntax: "java" // syntax to be uses for highgliting
,start_highlight: true // to display with highlight mode on start-up
});
</script>
<textarea id="code" name="code" rows="30" cols="100"><?php
echo isset($_POST['code']) ? $_POST['code'] : $this->code;
?></textarea>
<br>
<input type="submit" />
</form>
</center>
</div>
<?php
//<span class="botslice"><span></span></span> </div>
} else
{
$this->message("Error!", "You have already finished " . $this->name . ".");
}
}
/**
* Gets the final output of the code.
*/
public function getOutput($name, $code)
{
global $SSH;
$ssh = ssh2_connect('127.0.0.1', 22);
ssh2_auth_password($ssh, 'root', '***');
echo $ssh;
$output = array('error' => false, 'problem' => "", 'output' => array());
$full = $this->getFullCode($name, $code);
$write = file_put_contents(JAVA_SOURCE_FOLDER . $name . ".java", stripslashes($full));
if ($write)
{
//$javac = ssh2_exec($ssh, "javac -d " . JAVA_COMPILED_FOLDER . " " .
// JAVA_SOURCE_FOLDER . $name . ".java");
// exec a command and return a stream
$javac = getData(ssh2_exec($ssh, "javac -d " . JAVA_COMPILED_FOLDER . " " .
JAVA_SOURCE_FOLDER . $name . ".java"));
if (strlen($javac) == 0)
{
$cd = "cd " . JAVA_COMPILED_FOLDER;
$chmod = "chmod 755 " . $name . ".class";
$java = "java -Xmx50M -Djava.security.manager -Djava.security.policy==" .
JAVA_HOME_FOLDER . "my.policy " . $name . " ";
if (!empty($this->args))
{
foreach ($this->args as $arg)
{
$java .= $arg . " ";
}
}
$data = getData(ssh2_exec($ssh, "$cd\n$chmod\n$java"));
if (strlen($data) > 0)
{
$output['output'] = explode("\n", $data);
} else
{
$output['error'] = true;
$output['problem'] = "Nothing was printed.";
}
} else
{
$output['error'] = true;
$output['problem'] = "Compilation error: $javac.";
}
} else
{
$output['error'] = true;
$output['problem'] = "File contents = $write, So the file was not able to be written.";
}
return $output;
}
public abstract function getDescription();
/**
* Gets the results of the output.
*/
public abstract function getResults($output);
/**
* Gets the finished compilable code.
*/
public abstract function getFullCode($cName, $subCode);
}
?>
That is the current PHP version… It looks very sloppy to me and that is the nicest I can make it… So I wanted to know your opinion, Should I go with a Java backend and make the Java application print out some HTML? Or should I continue with the PHP backend?
I am sorry I cannot explain better, I can barely explain it myself…
Yes, your code is fairly sloppy. You’re mixing together your HTML, your business logic, and even some code that should be in libraries or helper functions, but that’s what you get when you don’t use a PHP framework. I find that a proper MVC framework makes my PHP code just as readable as anything you can find in Python or Java. Have a look at CodeIgniter or Kohana.