-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.php
More file actions
201 lines (166 loc) · 6.69 KB
/
index.php
File metadata and controls
201 lines (166 loc) · 6.69 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
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
<?php
require 'vendor/autoload.php';
use League\OAuth2\Client\Provider\GenericProvider;
session_start();
require_once 'config.php';
$provider = new GenericProvider($providerConfig);
// Check if user is not logged in
if (!isset($_SESSION['user'])) {
// User is not logged in, initiate the OAuth login process
$authorizationUrl = $provider->getAuthorizationUrl();
$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authorizationUrl);
exit;
}
$shortName = explode(" ", $_SESSION['user'])[0];
$dbh = new PDO("pgsql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
// Get current user's ID from session using preferred_username
$currentUserId = null;
$preferredUsername = $_SESSION['preferred_username'] ?? '';
if ($preferredUsername) {
$userQuery = 'SELECT id FROM "Users" WHERE profile::json->>\'username\' = :username';
$userStmt = $dbh->prepare($userQuery);
$userStmt->bindParam(':username', $preferredUsername);
$userStmt->execute();
$userResult = $userStmt->fetch(PDO::FETCH_ASSOC);
$currentUserId = $userResult['id'] ?? null;
}
// Query for public/listed pads
$publicQuery = 'SELECT "Notes".title, "Notes"."updatedAt", "Notes"."shortid", "Users".profile, \'public\' as pad_type
FROM "Notes"
JOIN "Users" ON "Notes"."ownerId" = "Users".id
WHERE (permission = \'freely\' OR permission = \'editable\' OR permission = \'limited\')
AND strpos(content, \'tags: listed\') > 0
ORDER BY "Notes"."updatedAt" DESC';
// Query for private pads of current user
$privateQuery = 'SELECT "Notes".title, "Notes"."updatedAt", "Notes"."shortid", "Users".profile, \'private\' as pad_type
FROM "Notes"
JOIN "Users" ON "Notes"."ownerId" = "Users".id
WHERE "Notes"."ownerId" = :userId
AND permission = \'private\'
ORDER BY "Notes"."updatedAt" DESC';
try {
// Fetch public pads
$publicStmt = $dbh->query($publicQuery);
$publicRows = $publicStmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch private pads if user ID is available
$privateRows = [];
if ($currentUserId) {
$privateStmt = $dbh->prepare($privateQuery);
$privateStmt->bindParam(':userId', $currentUserId);
$privateStmt->execute();
$privateRows = $privateStmt->fetchAll(PDO::FETCH_ASSOC);
}
// Combine all rows
$allRows = array_merge($publicRows, $privateRows);
// Sort by updatedAt DESC
usort($allRows, function($a, $b) {
return strtotime($b['updatedAt']) - strtotime($a['updatedAt']);
});
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
die();
}
function formatDateString($stringDate)
{
$datetime = DateTime::createFromFormat('Y-m-d H:i:s.uP', $stringDate);
if ($datetime) {
return $datetime->format('d.m.Y H:i');
}
return $stringDate;
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pad lister</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css">
<style>
.private-pad {
background-color: rgba(255, 193, 7, 0.1);
}
</style>
</head>
<body>
<div class="container">
<br>
<h6>Willkommen <?= htmlspecialchars($shortName, ENT_QUOTES, 'UTF-8') ?> ✨</h6>
<div style="display: flex; align-items: center; gap: 1.5rem; margin-bottom: 1rem;">
<small>
<label>
<input type="checkbox" id="hideUntitled" checked>
Unbenannte Pads ausblenden
</label>
</small>
<small>
<label>
<input type="checkbox" id="showPrivate">
Meine privaten Pads anzeigen
</label>
</small>
</div>
<table id="padsTable">
<thead>
<tr>
<th>Titel</th>
<th>Owner</th>
<th>Last edit</th>
</tr>
</thead>
<tbody>
<?php foreach ($allRows as $row): ?>
<tr class="<?= $row['pad_type'] === 'private' ? 'private-pad' : '' ?>" data-type="<?= htmlspecialchars($row['pad_type'], ENT_QUOTES, 'UTF-8') ?>">
<td class="pad-title">
<a href="<?= $hedgedocUrl ?>/<?= urlencode($row['shortid']) ?>"><?= htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8') ?></a>
</td>
<td>
<?= htmlspecialchars(json_decode($row['profile'])->username, ENT_QUOTES, 'UTF-8') ?>
</td>
<td>
<?= formatDateString($row['updatedAt']) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if ($showLogout): ?>
<br><br>
<a href="logout.php">Logout</a>
<br><br>
<?php endif; ?>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const hideUntitledCheckbox = document.getElementById('hideUntitled');
const showPrivateCheckbox = document.getElementById('showPrivate');
const table = document.getElementById('padsTable');
const rows = table.querySelectorAll('tbody tr');
function updateVisibility() {
rows.forEach(row => {
const titleCell = row.querySelector('.pad-title a');
const titleText = titleCell ? titleCell.textContent.trim() : '';
const padType = row.getAttribute('data-type');
let shouldHide = false;
// Hide if checkbox checked AND title is exactly 'Untitled'
if (hideUntitledCheckbox.checked && titleText === 'Untitled') {
shouldHide = true;
}
// Hide private pads if showPrivate is not checked
if (padType === 'private' && !showPrivateCheckbox.checked) {
shouldHide = true;
}
row.style.display = shouldHide ? 'none' : '';
});
}
// Initial state - hide private pads by default
updateVisibility();
// Toggle on change
hideUntitledCheckbox.addEventListener('change', updateVisibility);
showPrivateCheckbox.addEventListener('change', updateVisibility);
});
</script>
</body>
</html>