This repository has been archived by the owner on Dec 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
264 lines (231 loc) · 8.14 KB
/
index.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
require_once 'config.inc.php'; // for $APIKEY and $APIENDPOINT
require_once 'etherpad-lite-client.php';
$userId = $_SERVER['HTTP_REMOTE_USER'];
$displayName = @$_SERVER['HTTP_DISPLAYNAME'];
if (!$displayName) $displayName = @$_SERVER['HTTP_CN'];
if (!$userId) {
exit("unknown REMOTE_USER, SP shib badly configured");
}
//echo "userId=$userId displayname=$displayName\n<p>";
// error_reporting(E_ALL);
// ini_set('display_errors', '1');
$ether = new EtherpadLiteClient($APIKEY, $APIENDPOINT);
try {
$etherAuthorID = $ether->createAuthorIfNotExistsFor($userId, $displayName)->authorID;
} catch (Exception $e) { myerror($e, "createAuthorIfNotExistsFor Failed"); }
// Get the Params from the URL
$action = @$_GET['action'];
$currentPadId = @$_GET["name"];
if ($action) {
handleAction($ether, $etherAuthorID, $action);
} else if ($currentPadId) {
list($etherGroupID, $_padname) = explode("$",$currentPadId);
createSessionID($ether, $etherGroupID, $etherAuthorID);
}
$pads = getPads($ether, $etherAuthorID);
html_start();
if (@$BANDEAU) echo $BANDEAU;
echo "<div class='main'>";
echo "<div class='actions'>";
if ($currentPadId) {
if (!@$pads[$currentPadId]) {
// this must be a just created pad that is not modified so not listed yet
// fake one
$pads[$currentPadId] = onePad_raw($etherAuthorID, $currentPadId, true);
//unset($pads[$currentPadId]["actions"]);
}
currentPadUi($pads[$currentPadId]);
}
if ($pads) changePadForm($pads, $currentPadId);
newPadForm();
echo "</div>"; // actions
if ($currentPadId) {
iframe($currentPadId);
}
echo "</div>"; // main
echo "</body>";
function newPadForm() {
echo "<form action='/' style='display:inline;'>";
//echo "Nom du « pad »";
echo "<input type='text' name='name'>";
echo "<input type='submit' value='Créer un pad'>";
echo "<input type='hidden' name='action' value='newPad'>";
echo "</form>";
}
function changePadForm($pads, $currentPadId) {
echo "Pads";
echo "<form id='wantedPad' style='display: inline'>";
echo "<select onchange='setFormAction(true)'>";
foreach ($pads as $_id => $pad) {
if ($pad['is_creator']) echo padSelectEntry($pad, $currentPadId) . "<br>\n";
}
foreach ($pads as $_id => $pad) {
if (!$pad['is_creator']) echo padSelectEntry($pad, $currentPadId) . "<br>\n";
}
echo "</select>";
if (!$currentPadId) echo "<input type='submit' value='Choisir'></input>";
echo "</form>";
echo "<script>";
echo "function setFormAction(then_submit) { \n";
echo " var form = $('#wantedPad');";
echo " form.attr('action', $('#wantedPad select').val()); \n";
echo " if (then_submit) form.submit()";
echo "} setFormAction(false);";
echo "</script>";
}
function iframe($name) {
echo "<iframe class='p_proxy' src='/p/$name'></iframe>";
echo "<script> $(function () { var iframe = $('iframe.p_proxy'); var resize = function () { iframe.height($(window).height() - iframe.position().top) }; resize(); $(window).resize(resize); }); </script>";
}
function currentPadUi($pad) {
echo "<div class='currentPad'>";
echo "<ul>";
foreach ($pad['actions'] as $action) {
$actionToText = array('makePublic' => "Rendre public",
'makePrivate' => "Rendre privé",
'deletePad' => "Supprimer");
$text = $actionToText[$action];
$id = $pad['id'];
echo "<li><a href=/?name=$id&action=$action>$text</a></li>";
}
echo "</ul>";
echo "</div>";
}
function padSelectEntry($pad, $currentPadId) {
$selected = $currentPadId && $currentPadId === $pad['id'] ? 'selected' : '';
return "<option $selected value='" . $pad['href'] . "'>" . $pad['nice_name'] . ($pad['is_creator'] ? '' : " (contributeur)") . "</option>";
}
function getPads($ether, $etherAuthorID) {
try {
$padList = $ether->listPadsOfAuthor($etherAuthorID)->padIDs;
} catch (Exception $e) {
echo "\n\nlistPads Failed: ". $e->getMessage();
}
$pads = array();
foreach ($padList as $_key => $padId) {
$pads[$padId] = onePad($ether, $etherAuthorID, $padId);
}
return $pads;
}
function onePad($ether, $etherAuthorID, $padId) {
$isPrivate = $ether->getPublicStatus($padId)->publicStatus === false;
return onePad_raw($etherAuthorID, $padId, $isPrivate);
}
function onePad_raw($etherAuthorID, $padId, $isPrivate) {
list($nice_name, $is_creator) = nice_padname($padId, $etherAuthorID);
$p_prefix = '/ip';
$actions = $is_creator ? array("deletePad", ($isPrivate ? 'makePublic' : 'makePrivate')) : array();
return array('id' => $padId,
'nice_name' => $nice_name,
'is_creator' => $is_creator,
'href' => "$p_prefix/$padId",
'actions' => $actions);
}
function nice_padname($padId, $etherAuthorID) {
list($_group, $padname) = explode("$",$padId);
$s = removePrefixOrNULL($padname, "$etherAuthorID.");
$is_creator = $s !== null;
if ($s === null) {
$s = preg_replace('/^a\.[^.]+\./', '', $padname);
}
$s = preg_replace('/_/', ' ', $s);
return array($s, $is_creator);
}
function handleAction($ether, $etherAuthorID, $action) {
$name = @$_GET["name"];
if ($action == "newPad") // If the request is to create a new pad
{
if (!$name) $name = genRandomString();
$name = preg_replace('/[\$\/]/', '_', $name);
$name = "$etherAuthorID.$name";
$contents = @$_GET["contents"];
$etherGroupID = createEtherGroup($ether, 'everybody');
try {
$padID = $ether->createGroupPad($etherGroupID,$name,$contents)->padID;
$newlocation = "/ip/$padID"; // redirect to the new padID location
} catch (Exception $e) {
myerror($e, "createGroupPad Failed");
}
}
else if ($action == "deletePad") // If teh request is to delete an existing pad
{
try {
$ether->deletePad($name);
$newlocation = '/';
} catch (Exception $e) {
myerror($e, "deletePad Failed");
}
}
else if ($action == "makePublic" || $action == "makePrivate") // If teh request is to delete an existing pad
{
try {
$ether->setPublicStatus($name, ($action == "makePublic" ? "true" : "false"));
$newlocation = "/ip/$name";
} catch (Exception $e) {
myerror($e, "Changing PublicStatus Failed");
}
} else {
exit("invalid action $action");
}
header("Location: $newlocation");
exit;
}
function html_start() {
echo <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<style type="text/css">
.actions{background-color:#fff;padding:14px 10px 10px 10px;}
.currentPad ul { margin: 0; padding: 0; font-size:81.3%}
.currentPad li { float: right; list-style: none; padding-left: 1em; }
a, a:visited { color: #000080; }
form { padding: 0 3em 0 0.5em; }
body { margin: 0; }
.main { margin-left: 0; }
iframe.p_proxy { width: 100%; border: none; }
</style>
</head>
<body>
EOF;
}
function myerror($exception, $msg) {
exit("$msg with message: ``" . $exception->getMessage() . "''");
}
function createEtherGroup($ether, $name) {
try {
return $ether->createGroup()->groupID;
} catch (Exception $e) {
myerror($e, "createGroupIfNotExistsFor Failed");
}
return;
}
function createSessionID($ether, $etherGroupID, $etherAuthorID) {
$validUntil = mktime(0, 0, 0, date("m"), date("d")+1, date("y")); // One day in the future
$etherSessionID = $ether->createSession($etherGroupID, $etherAuthorID, $validUntil)->sessionID;
setcookie("sessionID",$etherSessionID, 0, "/"); // Set a cookie
// echo "New Session ID is $etherSessionID->sessionID\n\n";
}
// A funtion to generate a random name if something doesn't already exist
function genRandomString() {
$length = 10;
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$string = '';
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
function startsWith($hay, $needle) {
return substr($hay, 0, strlen($needle)) === $needle;
}
function removePrefix($s, $prefix) {
return startsWith($s, $prefix) ? substr($s, strlen($prefix)) : $s;
}
function removePrefixOrNULL($s, $prefix) {
return startsWith($s, $prefix) ? substr($s, strlen($prefix)) : NULL;
}
?>