PageRenderTime 24ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/judge/ideone/lib.php

https://github.com/yuxiaoye1223/onlinejudge
PHP | 217 lines | 142 code | 17 blank | 58 comment | 19 complexity | 2b98fd809f8435bb5be9e0e9b359beab MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Online Judge for Moodle //
  7. // https://github.com/hit-moodle/moodle-local_onlinejudge //
  8. // //
  9. // Copyright (C) 2009 onwards Sun Zhigang http://sunner.cn //
  10. // //
  11. // This program is free software; you can redistribute it and/or modify //
  12. // it under the terms of the GNU General Public License as published by //
  13. // the Free Software Foundation; either version 3 of the License, or //
  14. // (at your option) any later version. //
  15. // //
  16. // This program is distributed in the hope that it will be useful, //
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  19. // GNU General Public License for more details: //
  20. // //
  21. // http://www.gnu.org/copyleft/gpl.html //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////
  24. /**
  25. * ideone.com judge engine
  26. *
  27. * @package local_onlinejudge
  28. * @copyright 2011 Sun Zhigang (http://sunner.cn)
  29. * @author Sun Zhigang
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. defined('MOODLE_INTERNAL') || die();
  33. require_once(dirname(__FILE__)."/../../../../config.php");
  34. require_once($CFG->dirroot."/local/onlinejudge/judgelib.php");
  35. class judge_ideone extends judge_base
  36. {
  37. //TODO: update latest language list through ideone API
  38. protected static $supported_languages = array(
  39. 7 => 'Ada (gnat-4.3.2, ideone.com)',
  40. 13 => 'Assembler (nasm-2.07, ideone.com)',
  41. 45 => 'Assembler (gcc-4.3.4, ideone.com)',
  42. 104 => 'AWK (gawk) (gawk-3.1.6, ideone.com)',
  43. 105 => 'AWK (mawk) (mawk-1.3.3, ideone.com)',
  44. 28 => 'Bash (bash 4.0.35, ideone.com)',
  45. 110 => 'bc (bc-1.06.95, ideone.com)',
  46. 12 => 'Brainf**k (bff-1.0.3.1, ideone.com)',
  47. 11 => 'C (gcc-4.3.4, ideone.com)',
  48. 27 => 'C# (mono-2.8, ideone.com)',
  49. 1 => 'C++ (gcc-4.3.4, ideone.com)',
  50. 44 => 'C++0x (gcc-4.5.1, ideone.com)',
  51. 34 => 'C99 strict (gcc-4.3.4, ideone.com)',
  52. 14 => 'CLIPS (clips 6.24, ideone.com)',
  53. 111 => 'Clojure (clojure 1.1.0, ideone.com)',
  54. 118 => 'COBOL (open-cobol-1.0, ideone.com)',
  55. 106 => 'COBOL 85 (tinycobol-0.65.9, ideone.com)',
  56. 32 => 'Common Lisp (clisp) (clisp 2.47, ideone.com)',
  57. 102 => 'D (dmd) (dmd-2.042, ideone.com)',
  58. 36 => 'Erlang (erl-5.7.3, ideone.com)',
  59. 124 => 'F# (fsharp-2.0.0, ideone.com)',
  60. 123 => 'Factor (factor-0.93, ideone.com)',
  61. 125 => 'Falcon (falcon-0.9.6.6, ideone.com)',
  62. 107 => 'Forth (gforth-0.7.0, ideone.com)',
  63. 5 => 'Fortran (gfortran-4.3.4, ideone.com)',
  64. 114 => 'Go (gc-2010-07-14, ideone.com)',
  65. 121 => 'Groovy (groovy-1.7, ideone.com)',
  66. 21 => 'Haskell (ghc-6.8.2, ideone.com)',
  67. 16 => 'Icon (iconc 9.4.3, ideone.com)',
  68. 9 => 'Intercal (c-intercal 28.0-r1, ideone.com)',
  69. 10 => 'Java (sun-jdk-1.6.0.17, ideone.com)',
  70. 35 => 'JavaScript (rhino) (rhino-1.6.5, ideone.com)',
  71. 112 => 'JavaScript (spidermonkey) (spidermonkey-1.7, ideone.com)',
  72. 26 => 'Lua (luac 5.1.4, ideone.com)',
  73. 30 => 'Nemerle (ncc 0.9.3, ideone.com)',
  74. 25 => 'Nice (nicec 0.9.6, ideone.com)',
  75. 122 => 'Nimrod (nimrod-0.8.8, ideone.com)',
  76. 43 => 'Objective-C (gcc-4.5.1, ideone.com)',
  77. 8 => 'Ocaml (ocamlopt 3.10.2, ideone.com)',
  78. 119 => 'Oz (mozart-1.4.0, ideone.com)',
  79. 22 => 'Pascal (fpc) (fpc 2.2.0, ideone.com)',
  80. 2 => 'Pascal (gpc) (gpc 20070904, ideone.com)',
  81. 3 => 'Perl (perl 5.12.1, ideone.com)',
  82. 54 => 'Perl 6 (rakudo-2010.08, ideone.com)',
  83. 29 => 'PHP (php 5.2.11, ideone.com)',
  84. 19 => 'Pike (pike 7.6.86, ideone.com)',
  85. 108 => 'Prolog (gnu) (gprolog-1.3.1, ideone.com)',
  86. 15 => 'Prolog (swi) (swipl 5.6.64, ideone.com)',
  87. 4 => 'Python (python 2.6.4, ideone.com)',
  88. 116 => 'Python 3 (python-3.1.2, ideone.com)',
  89. 117 => 'R (R-2.11.1, ideone.com)',
  90. 17 => 'Ruby (ruby-1.9.2, ideone.com)',
  91. 39 => 'Scala (scala-2.8.0.final, ideone.com)',
  92. 33 => 'Scheme (guile) (guile 1.8.5, ideone.com)',
  93. 23 => 'Smalltalk (gst 3.1, ideone.com)',
  94. 40 => 'SQL (sqlite3-3.7.3, ideone.com)',
  95. 38 => 'Tcl (tclsh 8.5.7, ideone.com)',
  96. 62 => 'Text (text 6.10, ideone.com)',
  97. 115 => 'Unlambda (unlambda-2.0.0, ideone.com)',
  98. 101 => 'Visual Basic .NET (mono-2.4.2.3, ideone.com)',
  99. 6 => 'Whitespace (wspace 0.3, ideone.com)',
  100. );
  101. static function get_languages() {
  102. $langs = array();
  103. foreach (self::$supported_languages as $langid => $name) {
  104. $langs[$langid.'_ideone'] = $name;
  105. }
  106. return $langs;
  107. }
  108. /**
  109. * Judge the current task
  110. *
  111. * @return updated task
  112. */
  113. function judge() {
  114. $task = &$this->task;
  115. // create client.
  116. $client = new SoapClient("http://ideone.com/api/1/service.wsdl");
  117. $user = $task->var1;
  118. $pass = $task->var2;
  119. $language = $this->language;
  120. $input = $task->input;
  121. // Get source code
  122. $fs = get_file_storage();
  123. $files = $fs->get_area_files(get_context_instance(CONTEXT_SYSTEM)->id, 'local_onlinejudge', 'tasks', $task->id, 'sortorder, timemodified', false);
  124. $source = '';
  125. foreach ($files as $file) {
  126. $source = $file->get_content();
  127. break;
  128. }
  129. $status_ideone = array(
  130. 0 => ONLINEJUDGE_STATUS_PENDING,
  131. 11 => ONLINEJUDGE_STATUS_COMPILATION_ERROR,
  132. 12 => ONLINEJUDGE_STATUS_RUNTIME_ERROR,
  133. 13 => ONLINEJUDGE_STATUS_TIME_LIMIT_EXCEED,
  134. 15 => ONLINEJUDGE_STATUS_COMPILATION_OK,
  135. 17 => ONLINEJUDGE_STATUS_MEMORY_LIMIT_EXCEED,
  136. 19 => ONLINEJUDGE_STATUS_RESTRICTED_FUNCTIONS,
  137. 20 => ONLINEJUDGE_STATUS_INTERNAL_ERROR
  138. );
  139. // Begin soap
  140. /**
  141. * function createSubmission create a paste.
  142. * @param user is the user name.
  143. * @param pass is the user's password.
  144. * @param source is the source code of the paste.
  145. * @param language is language identifier. these identifiers can be
  146. * retrieved by using the getLanguages methods.
  147. * @param input is the data that will be given to the program on the stdin
  148. * @param run is the determines whether the source code should be executed.
  149. * @param private is the determines whether the paste should be private.
  150. * Private pastes do not appear on the recent pastes page on ideone.com.
  151. * Notice: you can only set submission's visibility to public or private through
  152. * the API (you cannot set the user's visibility).
  153. * @return array(
  154. * error => string
  155. * link => string
  156. * )
  157. */
  158. $webid = $client->createSubmission($user, $pass, $source, $language, $input, true, true);
  159. if ($webid['error'] == 'OK') {
  160. $link = $webid['link'];
  161. } else {
  162. throw new onlinejudge_exception('ideoneerror', $webid['error']);
  163. }
  164. // Get ideone results
  165. $delay = get_config('local_onlinejudge', 'ideonedelay');
  166. while (1) {
  167. sleep($delay);
  168. $status = $client->getSubmissionStatus($user, $pass, $link);
  169. if($status['status'] == 0) {
  170. break;
  171. }
  172. }
  173. $details = $client->getSubmissionDetails($user,$pass,$link,false,false,true,true,true);
  174. $task->stdout = $details['output'];
  175. $task->stderr = $details['stderr'];
  176. $task->compileroutput = $details['cmpinfo'];
  177. $task->memusage = $details['memory'] * 1024;
  178. $task->cpuusage = $details['time'];
  179. $task->infoteacher = get_string('ideoneresultlink', 'local_onlinejudge', $link);
  180. $task->infostudent = get_string('ideonelogo', 'local_onlinejudge');
  181. $task->status = $status_ideone[$details['result']];
  182. if ($task->compileonly) {
  183. if ($task->status != ONLINEJUDGE_STATUS_COMPILATION_ERROR && $task->status != ONLINEJUDGE_STATUS_INTERNAL_ERROR) {
  184. $task->status = ONLINEJUDGE_STATUS_COMPILATION_OK;
  185. }
  186. } else {
  187. if ($task->status == ONLINEJUDGE_STATUS_COMPILATION_OK) {
  188. if ($task->cpuusage > $task->cpulimit) {
  189. $task->status = ONLINEJUDGE_STATUS_TIME_LIMIT_EXCEED;
  190. } else if ($task->memusage > $task->memlimit) {
  191. $task->status = ONLINEJUDGE_STATUS_MEMORY_LIMIT_EXCEED;
  192. } else {
  193. $task->status = $this->diff($details['output'], $task->output);
  194. }
  195. }
  196. }
  197. return $task;
  198. }
  199. }
  200. ?>