我想要一些反馈意见

时间:2010-11-02 23:10:32

标签: java php mysql

我正在制作一个复制网站(复制codingbat.com),而且我一直在设计它,我不得不停下来,我需要决定使用Java后端或PHP后端。我已经完成了一个Java,但它似乎并不像是非常高效和安全:

 <?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'] = "&#10003;";
                    $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'] .
                                ">&nbsp;&nbsp;&nbsp;&nbsp;</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);
    }

?>

这是当前的PHP版本......对我来说看起来非常草率,这是我能做到的最好的...所以我想知道你的意见,我应该使用Java后端并制作Java应用程序打印出一些HTML?或者我应该继续使用PHP后端吗?

对不起,我无法解释得更好,我自己几乎无法解释......

2 个答案:

答案 0 :(得分:1)

考虑使用MVC模式。它看起来更好,更易于维护,并具有许多其他优点。

答案 1 :(得分:1)

是的,你的代码相当草率。您将HTML,业务逻辑,甚至一些应该在库或辅助函数中的代码混合在一起,但这是您在不使用PHP框架时获得的代码。我发现一个合适的MVC框架使我的PHP代码与您在Python或Java中可以找到的任何内容一样可读。请查看CodeIgniterKohana

相关问题