-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_errors.php
More file actions
119 lines (95 loc) · 3.01 KB
/
Copy pathcheck_errors.php
File metadata and controls
119 lines (95 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
//Set up our Moodle session...
define('AJAX_SCRIPT', true);
require_once('../../../config.php');
require_once('lib.php');
//Ensure that only logged in users can check scripts.
//(This allows us to limit the general usage.)
require_login();
//easy way to profile
function getTime()
{
$a = explode (' ',microtime());
return(double) $a[0] + $a[1];
}
/**
* Sends an error summary
*/
function send_errors($errors) {
echo json_encode($errors)."\n";
}
/**
* Creates a table summarizing the result of a script's execution.
*
* @return A string containing the html for the created table.
*/
function summarize_execution($variables) {
ksort($variables);
$html = '';
$html = html_writer::start_tag('div', array('class' => 'code-result'));
//Create a new HTML table, which will store each of the key-value mappings.
$table = new html_table;
$table->attributes = array('class' => 'code-result');
$table->head = array('Variable', 'Sample Value'); // TODO: internationalize
//Fill in each of the name -> value variable mappings.
$table->data = array();
foreach($variables as $name => $value) {
$table->data[] = array(htmlentities($name), htmlentities($value));
}
//Render the table, close the containing div, and return the code for the summary.
$html .= html_writer::table($table);
$html .= html_writer::end_tag('div');
return $html;
}
//don't display PHP errors, just in case
ini_set('display_errors', 1);
if(empty($_POST['script'])) {
die();
}
//get the script to execute
$script = $_POST['script'];
//create a new sandboxed script evaluator
$interpreter = qtype_scripted_language_manager::create_interpreter($_POST['language']);
$error = null;
//record the time at the start of the script's execution
$start_time = getTime();
try {
//Evaluate the given script in a safe evaluation sandbox.
$interpreter->execute($script);
}
//If an error occurs during execution, capture it.
catch(Exception $x) {
$error = $interpreter->error_information($x);
}
//and record the time afterwards
$end_time = getTime();
//if we're not looking for errors related to a given target, display all errors
if(empty($_POST['target']))
{
//Send a summary of any errors which (may have) occurred during
//evaluation of the user script.
send_errors($error);
//If no error occurred,
if(!$error)
{
$variables = $interpreter->summarize_variables();
echo summarize_execution($interpreter->summarize_variables());
}
}
//otherwise, only display errors relevant to the given target
//TODO?
else
{
//evaluate the target expression as well
try
{
$interpreter->evaluate($_POST['target']);
}
catch(Exception $e) {
print_object($e);
}
}
//Print a message summarizing the execution time for the given script.
//This allows a primitive form of benchmarking.
$exec_time = number_format($end_time - $start_time, 3);
echo html_writer::tag('span', get_string('executiontime', 'qtype_scripted', $exec_time), array('class' => 'footnote'));