-
Notifications
You must be signed in to change notification settings - Fork 97
/
play.php
171 lines (160 loc) · 5.08 KB
/
play.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
if ( !isset($_COOKIE['secret']) || $_COOKIE['secret'] != '42' ) {
header("Location: index.php");
return;
}
use \Tsugi\Core\LTIX;
use \Tsugi\Util\U;
if ( ! isset($CFG) ) {
if (!defined('COOKIE_SESSION')) define('COOKIE_SESSION', true);
require_once "tsugi/config.php";
$LAUNCH = LTIX::session_start();
}
$BUCKET_RATE = 30; // One compile per 30 seconds
$BUCKET_MAX = 4;
$LOGGED_IN = U::get($_SESSION,'id', null) !== null;
$displayname = U::get($_SESSION,'displayname', null);
$email = U::get($_SESSION,'email', null);
require_once "sandbox/sandbox.php";
require_once "play_util.php";
// If this is a new compile request - post - session - redirect
if ( isset($_POST['new_compile']) ) {
unset($_SESSION['retval']);
$_SESSION['code'] = U::get($_POST, 'code', false);
$_SESSION['input'] = U::get($_POST, 'input', '');
header("Location: play.php");
return;
}
$retval = U::get($_SESSION, 'retval');
if ( is_object($retval) && is_object($retval->pipe) && strlen(U::get($_POST, "emcc_output", '')) > 0 ) {
cc4e_emcc_get_output($retval, $displayname, $email, $_SESSION['id']);
$_SESSION['retval'] = $retval;
}
$sample = U::get($_REQUEST, 'sample');
// if ( ! preg_match('/^[a-zA-Z_0-9]+.md$/', $sample) ) $sample = false;
if ( is_string($sample) ) {
unset($_SESSION['code']);
unset($_SESSION['input']);
unset($_SESSION['retval']);
$code = file_get_contents('book/code/'.$sample);
$retval = null;
$input = null; /* TODO - Get this as well */
$output = null;
} else {
$code = U::get($_SESSION, 'code');
$input = U::get($_SESSION, 'input');
$retval = U::get($_SESSION, 'retval');
if ( $retval == NULL && is_string($code) && strlen($code) > 0 ) {
if ( cc4e_rate_limit($retval) ) {
$retval = $_SESSION['retval']; // reload
} else {
$succinct = preg_replace('/\s+/', ' ', $code);
$note = "play.php ".$displayname.' '.$email.': '.substr($succinct,0, 250);
error_log($note);
$main = null;
$retval = cc4e_emcc($_SESSION['id'], $code, $input, $main, $note);
$retval->assn = 'play.php';
$_SESSION['retval'] = $retval;
if ( isset($retval->js) ) {
header("Location: em_run.php");
return;
}
}
}
}
$lines = $code ? count(explode("\n", $code)) : 15;
echo("<!DOCTYPE html>\n<html>\n<head>\n");
cc4e_play_header($lines);
?>
<style>
.CodeMirror-scroll { max-height: <?= intval(($lines/13)*20) ?>em; }
</style>
</head>
<body>
<p>
<?php if ( $LOGGED_IN ) { ?>
This is the <a href="index.php">www.cc4e.com</a> code playground for writing C programs.
You can also check
<a href="https://status.cc4e.com" target="_blank">recent status</a> of this
compiler page.
<?php
if ( U::get($_REQUEST, "sample", null) != null ) {
echo(' Note that not all of the sample programs in the book compile and run using a modern compiler.');
}
echo("<!-- leaky bucket \n");
$bucket = U::get($_SESSION,"leaky_bucket", null);
if ( is_array($bucket) ) foreach($bucket as $when) {
echo(time() - $when);
echo("\n");
}
echo("-->\n");
} else {
?>
You must be logged in to run your program on this site. There are a number of
free online <a href="compilers.php">C Compilers</a> that you can use.
<?php } ?>
<p>
<form method="post">
<p>
<?php
if ( $LOGGED_IN ) {
echo('<input type="submit" name="new_compile" value="Run Code" id="runcode">');
}
?>
<script>
if ( window.opener ) {
document.write('<button onclick="window.close();">Close</button>');
} else {
document.write('<input type="submit" onclick="window.location=\'index.php\'; return false;" value="Back to CC4E">');
}
</script>
<?php
echo("</p>\n");
$errors = cc4e_play_errors($retval);
cc4e_play_inputs($lines, $code);
if ( $LOGGED_IN ) {
?>
<p>Input to your program:</p>
<p>
<textarea id="myinput" name="input" style="width:100%; border: 1px black solid;">
<?php
if ( is_string($input) ) {
echo(htmlentities($input));
} ?>
</textarea>
</p>
</form>
<?php if ( is_object($retval) && isset($retval->pipe) && is_object($retval->pipe) && is_string($retval->pipe->stdout) && strlen($retval->pipe->stdout) > 0 ) {
$rows = substr_count( $retval->pipe->stdout, "\n" );
if ( $rows < 5 ) $rows = 5;
if ( $rows > 30 ) $rows = 30;
?>
<p>
Program Output:
</p>
<p>
<textarea id="myoutput" name="output" style="width:100%; border: 1px black solid;" rows="<?= $rows ?>">
<?php echo(htmlentities($retval->pipe->stdout)); ?>
</textarea>
</p>
<?php } ?>
<p>
This page uses a server-based compiler called
<a href="https://emscripten.org/" target="_blank">Emscripten</a> that compiles
your code to JavaScript and then executes your code in the browser. You can watch
your browser developer console to monitor how your code is being executed.
If this fails with an unexpected error, please add a note in the
<a href="<?= $CFG->apphome ?>/discussions">Discussions</a>
area.
</p>
<?php
cc4e_play_debug($retval);
} else {
echo("\n</form>\n");
}
?>
<script src="https://static.tsugi.org/js/jquery-1.11.3.js"></script>
<?php
cc4e_play_footer();
?>
</body>