-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraded.class.php
More file actions
104 lines (87 loc) · 2.55 KB
/
Copy pathgraded.class.php
File metadata and controls
104 lines (87 loc) · 2.55 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
<?php
class GradedCode {
private static $table = "qtype_code_coderuns";
public $id;
public $runid;
public $output;
private static $debug = false;
private static function debug($var) {
if(!self::$debug) {
return;
}
echo "<pre>";
var_dump($var);
echo "</pre>";
}
private static function debugt($var) {
if(!self::$debug) {
return;
}
echo "<pre>[GRADED] $var</pre>";
}
public static function createRun() {
/** @var moodle_database $DB */
global $DB;
return $DB->insert_record(self::$table, (object) [
"runid" => "0",
"graded" => "{}"
], true);
}
public function __construct($id) {
/** @var moodle_database $DB */
global $DB;
$this->id = $id;
$record = $DB->get_record(self::$table, [
"id" => $id
]);
if($record === false) {
throw new InvalidArgumentException();
}
self::debug($record);
$this->output = (array) json_decode($record->graded);
$this->runid = $record->runid;
}
public static function fieldFromRunID($runid) {
}
/**
* Sets the
* @param string $output
*/
public function setOutput($output) {
/** @var moodle_database $DB */
global $DB;
$DB->update_record(self::$table, (object) [
"id" => $this->id,
"runid" => $output["runid"],
"graded" => json_encode($output)
]);
}
/**
* @param string $raw the raw runid param
* @param string $secret the secret for encryption
* @return bool|int the run id for false on failure
*/
public static function validateRunID($raw, $secret) {
self::debugt("Validating $raw with secret $secret");
$decode = base64_decode($raw);
if($decode == false) {
return false;
}
self::debugt("Value: $decode");
$parts = [];
$result = preg_match("/([0-9]+):([a-f0-9]+)/", $decode, $parts);
self::debugt("Preg result: $result " . implode(";", $parts));
if(!$result) {
return false;
}
$shouldBe = hash_hmac("sha256", $parts[1], $secret);
self::debugt("Compare $shouldBe and $parts[2]");
if($shouldBe != $parts[2]) {
return false;
}
return intval($parts[1]);
}
public static function getRunID($id, $secret) {
return base64_encode("$id:".hash_hmac("sha256", $id, $secret));
}
}