-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpad.php
159 lines (147 loc) · 5.77 KB
/
pad.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
<?php
/**
* Scratchpad to quickly try PHP code
*
* See https://github.com/port8000/pad
* Copyright 2012 Port 8000 UG (haftungsbeschraenkt)
* This code is licensed under both the MIT and GPL license.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*/
require_once 'error_handler.php';
define('HISTSIZE', 99);
$___c = '';
if (isset($_POST['c'])) {
$___c = $_POST['c'];
error_log("PHP Pad here. I'm about to eval() some code in file ".__FILE__);
}
$___i = Null;
$___s = true;
$___db = new PDO('sqlite:'.dirname(__FILE__).'/pad.sqlite');
$___db->query('CREATE TABLE IF NOT EXISTS pad (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
code TEXT NOT NULL,
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
success INTEGER DEFAULT 0
)');
if (isset($_GET['___k']) && is_numeric($_GET['___k'])) {
/* a history item is selected to be deleted */
$___db->prepare('DELETE FROM pad WHERE "id" = ?')
->execute(array($_GET['___k']));
header('Location: '.$_SERVER['PHP_SELF']);
die();
}
if (isset($_GET['___f']) && strlen($_GET['___f']) > 1 &&
! preg_match('/[^a-zA-Z0-9_]/', $_GET['___f'])) {
/* a function definition is requested */
header('Content-type: application/json; charset=utf-8');
require_once 'functions.php';
$result = array();
foreach ($functions as $name => $synopsis) {
if (strpos($name, $_GET['___f']) === 0) {
$result[$name] = $synopsis;
}
}
die(json_encode($result));
}
$___o = $___db->query('SELECT * FROM pad
ORDER BY created DESC LIMIT '.HISTSIZE)->fetchAll(PDO::FETCH_ASSOC);
if ($___c) {
foreach ($___o as $___d) {
if ($___d['code'] === $___c) {
$___i = False;
}
}
unset($___d);
if ($___i !== False) {
$q = $___db->prepare('INSERT INTO pad (code) VALUES (?)');
$q->execute(array($___c));
$___i = $___db->lastInsertId();
unset($q);
} else {
$___i = NULL;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Codepad</title>
<style><?php include "static/pad.css" ?></style>
<script type="module"><?php include "static/pad.js" ?></script>
<link rel="icon" href="data:image/svg+xml,%3Csvg%20xmlns=%22http%3A//www.w3.org/2000/svg%22%20viewBox=%220%200%2032%2032%22%3E%0A%3Crect%20x=%220%22%20y=%220%22%20width=%2232%22%20height=%2232%22%20fill=%22red%22/%3E%0A%3C/svg%3E">
</head>
<body>
<div class="history" tabindex="-1">
<?php if ($___i):
printf('<div class="history__item" tabindex="0">
<a class="history__delete" href="?___k='.$___i.'" title="delete this entry">×</a>
<pre title="%s">%s%s</pre></div>',
htmlspecialchars($___c, ENT_QUOTES, 'UTF-8'),
htmlspecialchars(substr($___c, 0, 40), ENT_QUOTES, 'UTF-8'),
strlen($___c) > 40? '...' : '');
endif ?>
<?php foreach ($___o as $___d) {
printf('<div class="history__item success_%s" tabindex="0">
<a class="history__delete" href="?___k='.$___d['id'].'" title="delete this entry">×</a>
<pre title="%s">%s%s</pre></div>',
$___d['success'],
htmlspecialchars($___d['code'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars(substr(trim(preg_replace('/\r?\n([ \t]*\r?\n)+/', "\n", $___d['code'])), 0, 40), ENT_QUOTES, 'UTF-8'),
strlen($___d['code']) > 40? '...' : '');
}
unset($___o);
unset($___d);?>
</div>
<div class="content">
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') ?>">
<textarea placeholder="Enter PHP code here (without leading <?php)"
rows="20" cols="78" name="c" id="c" autofocus tabindex="1"
class="code code--<?php
echo ($___c)? 'filled' : 'empty'?>"><?php
echo htmlspecialchars(($___c? $___c : (isset($_GET['c'])? $_GET['c'] : '')), ENT_QUOTES, 'UTF-8');
?></textarea>
<br>
<button type="submit" id="run" tabindex="2">run code</button>
<small>(or press <kbd>Ctrl+Enter</kbd>)</small>
</form>
<div class="stage"><?php
if ($___c) {
try {
if (function_exists('untaint')) {
untaint($___c);
}
$___r = eval($___c);
if ($___r === False) {
$___s = False;
$e = error_get_last();
___e($e['type'], $e['message'], $e['file'], $e['line']);
} elseif ($___r !== Null) {
if (! is_string($___r)) {
$___r = print_r($___r, true);
}
echo '<hr/>RETURNED VALUE:<br>' . htmlspecialchars($___r, ENT_QUOTES, 'UTF-8');
}
} catch (Exception $e) {
echo '<div class="error"><span class="error__type">EXCEPTION THROWN</span><div class="error__backtrace">';
var_dump($e);
echo '</div></div>';
$___s = false;
}
if ($___i !== NULL && $___s === True) {
$q = $___db->prepare('UPDATE pad SET success = ? WHERE id = ?');
$q->execute(array($___s, $___i));
}
} else {
?>Please enter PHP code above. No starting <code><?php</code> is needed.<?php
} ?>
</div>
<p><small title="If you can see this, there was no uncaught fatal error above."><code>EOF</code></small></p>
</div>
<?php if ($___i !== NULL): ?>
<script>last_state = <?php echo $___s? '1' : '0' ?>;</script>
<?php endif ?>
</body>
</html>