2010-11-02 63 views
-1

我正在複製網站(複製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後端?

我很抱歉,我無法解釋更好,我可以勉強解釋自己...

+6

糟糕的標題。不要說你的問題。 – EboMike 2010-11-02 23:14:26

回答

1

是的,你的代碼是相當馬虎。您將HTML,業務邏輯甚至應該在庫或幫助函數中的一些代碼混合在一起,但這是您在不使用PHP框架時所得到的結果。我發現一個合適的MVC框架使得我的PHP代碼與Python或Java中可以找到的任何東西一樣可讀。看看CodeIgniterKohana

+0

謝謝:)這將有助於很多:) – cdog5000 2010-11-02 23:33:50

+0

@ cdog5000我期待在CodeIgniter標籤中看到你: - ] – treeface 2010-11-02 23:34:27

+0

@treeface希望在那裏見到你:P! – cdog5000 2010-11-02 23:40:51

1

考慮使用MVC模式。它看起來更好,維護起來更容易,並且還有很多其他優點。