Skip to content

Commit 7e2d16d

Browse files
committed
Revert "Create UserNoteService class"
This reverts commit b31ec1c.
1 parent 47efb7c commit 7e2d16d

5 files changed

Lines changed: 239 additions & 255 deletions

File tree

include/layout.inc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ function make_link(string $url, string $linktext = ''): string
164164
return sprintf("<a href=\"%s\">%s</a>", $url, $linktext ?: $url);
165165
}
166166

167+
// make_popup_link()
168+
// return a hyperlink to something, within the site, that pops up a new window
169+
//
170+
function make_popup_link($url, $linktext = false, $target = false, $windowprops = "", $extras = false) {
171+
return sprintf("<a href=\"%s\" target=\"%s\" onclick=\"window.open('%s','%s','%s');return false;\"%s>%s</a>",
172+
htmlspecialchars($url, ENT_QUOTES | ENT_IGNORE),
173+
($target ?: "_new"),
174+
htmlspecialchars($url, ENT_QUOTES | ENT_IGNORE),
175+
($target ?: "_new"),
176+
$windowprops,
177+
($extras ? ' ' . $extras : ''),
178+
($linktext ?: $url),
179+
);
180+
}
181+
167182
// Print a link for a downloadable file (including filesize)
168183
function download_link($file, $title): void
169184
{
@@ -205,6 +220,20 @@ function clean($var) {
205220
return htmlspecialchars($var, ENT_QUOTES);
206221
}
207222

223+
// Clean out the content of one user note for printing to HTML
224+
function clean_note($text)
225+
{
226+
// Highlight PHP source
227+
$text = highlight_php(trim($text), true);
228+
229+
// Turn urls into links
230+
return preg_replace(
231+
'!((mailto:|(https?|ftp|nntp|news)://).*?)(\s|<|\)|"|\\\\|\'|$)!',
232+
'<a href="\1" rel="nofollow" target="_blank">\1</a>\4',
233+
$text,
234+
);
235+
}
236+
208237
function display_errors($errors): void
209238
{
210239
echo '<div class="errors">';

include/shared-manual.inc

Lines changed: 204 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,185 @@ $PGI = []; $SIDEBAR_DATA = '';
2323
// =============================================================================
2424

2525
use phpweb\I18n\Languages;
26-
use phpweb\UserNotes\UserNoteService;
26+
use phpweb\UserNotes\Sorter;
27+
use phpweb\UserNotes\UserNote;
28+
29+
/**
30+
* Print out all user notes for this manual page
31+
*
32+
* @param array<string, UserNote> $notes
33+
*/
34+
function manual_notes($notes):void {
35+
global $LANG;
36+
37+
// Get needed values
38+
list($filename) = $GLOBALS['PGI']['this'];
39+
40+
// Drop file extension from the name
41+
if (substr($filename, -4) == '.php') {
42+
$filename = substr($filename, 0, -4);
43+
}
44+
45+
$sorter = new Sorter();
46+
$sorter->sort($notes);
47+
48+
$repo = strtolower($LANG);
49+
$addNote = autogen('add_a_note', $LANG);
50+
// Link target to add a note to the current manual page,
51+
// and it's extended form with a [+] image
52+
$addnotelink = '/manual/add-note.php?sect=' . $filename .
53+
'&amp;repo=' . $repo .
54+
'&amp;redirect=' . $_SERVER['BASE_HREF'];
55+
$addnotesnippet = make_link(
56+
$addnotelink,
57+
"+<small>$addNote</small>",
58+
);
59+
60+
$num_notes = count($notes);
61+
$noteCountHtml = '';
62+
if ($num_notes) {
63+
$noteCountHtml = "<span class=\"count\">$num_notes note" . ($num_notes == 1 ? '' : 's') . "</span>";
64+
}
65+
66+
$userContributedNotes = autogen('user_contributed_notes', $LANG);
67+
echo <<<END_USERNOTE_HEADER
68+
<section id="usernotes">
69+
<div class="head">
70+
<span class="action">{$addnotesnippet}</span>
71+
<h3 class="title">$userContributedNotes {$noteCountHtml}</h3>
72+
</div>
73+
END_USERNOTE_HEADER;
74+
75+
// If we have no notes, then inform the user
76+
if ($num_notes === 0) {
77+
$noUserContributedNotes = autogen('no_user_notes', $LANG);
78+
echo "\n <div class=\"note\">$noUserContributedNotes</div>";
79+
} else {
80+
// If we have notes, print them out
81+
echo '<div id="allnotes">';
82+
foreach ($notes as $note) {
83+
manual_note_display($note);
84+
}
85+
echo "</div>\n";
86+
echo "<div class=\"foot\">$addnotesnippet</div>\n";
87+
}
88+
echo "</section>";
89+
}
90+
91+
/**
92+
* Get user notes from the appropriate text dump
93+
*
94+
* @return array<string, UserNote>
95+
*/
96+
function manual_notes_load(string $id): array
97+
{
98+
$hash = substr(md5($id), 0, 16);
99+
$notes_file = $_SERVER['DOCUMENT_ROOT'] . "/backend/notes/" .
100+
substr($hash, 0, 2) . "/$hash";
101+
102+
// Open the note file for reading and get the data (12KB)
103+
// ..if it exists
104+
if (!file_exists($notes_file)) {
105+
return [];
106+
}
107+
$notes = [];
108+
if ($fp = @fopen($notes_file, "r")) {
109+
while (!feof($fp)) {
110+
$line = chop(fgets($fp, 12288));
111+
if ($line == "") { continue; }
112+
@list($id, $sect, $rate, $ts, $user, $note, $up, $down) = explode("|", $line);
113+
$notes[$id] = new UserNote($id, $sect, $rate, $ts, $user, base64_decode($note, true), (int) $up, (int) $down);
114+
}
115+
fclose($fp);
116+
}
117+
return $notes;
118+
}
119+
120+
// Print out one user note entry
121+
function manual_note_display(UserNote $note, $voteOption = true): void
122+
{
123+
if ($note->user) {
124+
$name = "\n <strong class=\"user\"><em>" . htmlspecialchars($note->user) . "</em></strong>";
125+
} else {
126+
$name = "<strong class=\"user\"><em>Anonymous</em></strong>";
127+
}
128+
$name = ($note->id ? "\n <a href=\"#{$note->id}\" class=\"name\">$name</a><a class=\"genanchor\" href=\"#{$note->id}\"> &para;</a>" : "\n $name");
129+
130+
// New date style will be relative time
131+
$date = new DateTime("@{$note->ts}");
132+
$datestr = relTime($date);
133+
$fdatestr = $date->format("Y-m-d h:i");
134+
$text = clean_note($note->text);
135+
136+
// Calculate note rating by up/down votes
137+
$vote = $note->upvotes - $note->downvotes;
138+
$p = floor(($note->upvotes / (($note->upvotes + $note->downvotes) ?: 1)) * 100);
139+
$rate = !$p && !($note->upvotes + $note->downvotes) ? "no votes..." : "$p% like this...";
140+
141+
// Vote User Notes Div
142+
if ($voteOption) {
143+
list($redir_filename) = $GLOBALS['PGI']['this'];
144+
if (substr($redir_filename, -4) == '.php') {
145+
$redir_filename = substr($redir_filename, 0, -4);
146+
}
147+
$rredir_filename = urlencode($redir_filename);
148+
$votediv = <<<VOTEDIV
149+
<div class="votes">
150+
<div id="Vu{$note->id}">
151+
<a href="/manual/vote-note.php?id={$note->id}&amp;page={$rredir_filename}&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a>
152+
</div>
153+
<div id="Vd{$note->id}">
154+
<a href="/manual/vote-note.php?id={$note->id}&amp;page={$rredir_filename}&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a>
155+
</div>
156+
<div class="tally" id="V{$note->id}" title="{$rate}">
157+
{$vote}
158+
</div>
159+
</div>
160+
VOTEDIV;
161+
} else {
162+
$votediv = null;
163+
}
164+
165+
// If the viewer is logged in, show admin options
166+
if (isset($_COOKIE['IS_DEV']) && $note->id) {
167+
168+
$admin = "\n <span class=\"admin\">\n " .
169+
170+
make_popup_link(
171+
'https://main.php.net/manage/user-notes.php?action=edit+' . $note->id,
172+
'<img src="/images/notes-edit@2x.png" height="12" width="12" alt="edit note">',
173+
'admin',
174+
'scrollbars=yes,width=650,height=400',
175+
) . "\n " .
176+
177+
make_popup_link(
178+
'https://main.php.net/manage/user-notes.php?action=reject+' . $note->id,
179+
'<img src="/images/notes-reject@2x.png" height="12" width="12" alt="reject note">',
180+
'admin',
181+
'scrollbars=no,width=300,height=200',
182+
) . "\n " .
183+
184+
make_popup_link(
185+
'https://main.php.net/manage/user-notes.php?action=delete+' . $note->id,
186+
'<img src="/images/notes-delete@2x.png" height="12" width="12" alt="delete note">',
187+
'admin',
188+
'scrollbars=no,width=300,height=200',
189+
) . "\n </span>";
190+
191+
} else {
192+
$admin = '';
193+
}
194+
195+
echo <<<USER_NOTE_TEXT
196+
197+
<div class="note" id="{$note->id}">{$votediv}{$name}{$admin}<div class="date" title="$fdatestr"><strong>{$datestr}</strong></div>
198+
<div class="text" id="Hcom{$note->id}">
199+
{$text}
200+
</div>
201+
</div>
202+
USER_NOTE_TEXT;
203+
204+
}
27205

28206
function manual_navigation_breadcrumbs(array $setup) {
29207
$menu = [];
@@ -120,9 +298,7 @@ function manual_setup($setup): void {
120298
if (substr($filename, -4) == '.php') {
121299
$filename = substr($filename, 0, -4);
122300
}
123-
124-
$userNoteService = new UserNoteService();
125-
$USERNOTES = $userNoteService->load($filename);
301+
$USERNOTES = manual_notes_load($filename);
126302
if ($USERNOTES) {
127303
$note = current($USERNOTES);
128304
$timestamps[] = $note->ts;
@@ -246,14 +422,35 @@ function manual_footer($setup): void {
246422
</div>
247423
CONTRIBUTE;
248424

249-
$userNoteService = new UserNoteService();
250-
$userNoteService->display($USERNOTES);
425+
manual_notes($USERNOTES);
251426
site_footer([
252427
'related_menu' => $__RELATED['toc'],
253428
'related_menu_deprecated' => $__RELATED['toc_deprecated'],
254429
]);
255430
}
256431

432+
// This function takes a DateTime object and returns a formated string of the time difference relative to now
433+
function relTime(DateTime $date) {
434+
$current = new DateTime();
435+
$diff = $current->diff($date);
436+
$units = ["year" => $diff->format("%y"),
437+
"month" => $diff->format("%m"),
438+
"day" => $diff->format("%d"),
439+
"hour" => $diff->format("%h"),
440+
"minute" => $diff->format("%i"),
441+
"second" => $diff->format("%s"),
442+
];
443+
$out = "just now...";
444+
foreach ($units as $unit => $amount) {
445+
if (empty($amount)) {
446+
continue;
447+
}
448+
$out = $amount . " " . ($amount == 1 ? $unit : $unit . "s") . " ago";
449+
break;
450+
}
451+
return $out;
452+
}
453+
257454
function contributors($setup) {
258455
if (!isset($_GET["contributors"])
259456
|| !isset($setup["history"]["contributors"])
@@ -280,6 +477,7 @@ function autogen(string $text, string $lang) {
280477
static $translations = [];
281478

282479
$lang = ($lang === "") ? "en" : $lang;
480+
$lang = strtolower($lang);
283481
if (isset($translations[$lang])) {
284482
if (isset($translations[$lang][$text]) && $translations[$lang][$text] !== "") {
285483
return $translations[$lang][$text];

manual/add-note.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
include __DIR__ . '/spam_challenge.php';
99

1010
use phpweb\UserNotes\UserNote;
11-
use phpweb\UserNotes\UserNoteService;
1211

1312
site_header("Add Manual Note", ['css' => 'add-note.css']);
1413

@@ -147,10 +146,9 @@
147146
if ($error) { echo "<p class=\"formerror\">$error</p>\n"; }
148147

149148
// Print out preview of note
150-
$userNoteService = new UserNoteService();
151149
echo '<p>This is what your entry will look like, roughly:</p>';
152150
echo '<div id="usernotes">';
153-
$userNoteService->displaySingle(new UserNote('', '', '', time(), $user, $note));
151+
manual_note_display(new UserNote('', '', '', time(), $user, $note));
154152
echo '</div><br><br>';
155153
}
156154

manual/vote-note.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<?php
2-
3-
use phpweb\UserNotes\UserNoteService;
4-
52
$_SERVER['BASE_PAGE'] = 'manual/vote-note.php';
63
include_once __DIR__ . '/../include/prepend.inc';
74
include_once __DIR__ . '/../include/posttohost.inc';
@@ -16,10 +13,9 @@
1613
$BACKid = htmlspecialchars($_REQUEST['id'] ?? '');
1714
$link = "/{$BACKpage}#{$BACKid}";
1815
$master_url = "https://main.php.net/entry/user-notes-vote.php";
19-
$notes = new UserNoteService();
2016

2117
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
22-
if (isset($_SERVER['HTTP_X_JSON']) && $_SERVER['HTTP_X_JSON'] == 'On' && !empty($_REQUEST['id']) && !empty($_REQUEST['page']) && ($N = $notes->load($_REQUEST['page'])) && array_key_exists($_REQUEST['id'], $N) && !empty($_REQUEST['vote']) && ($_REQUEST['vote'] === 'up' || $_REQUEST['vote'] === 'down')) {
18+
if (isset($_SERVER['HTTP_X_JSON']) && $_SERVER['HTTP_X_JSON'] == 'On' && !empty($_REQUEST['id']) && !empty($_REQUEST['page']) && ($N = manual_notes_load($_REQUEST['page'])) && array_key_exists($_REQUEST['id'], $N) && !empty($_REQUEST['vote']) && ($_REQUEST['vote'] === 'up' || $_REQUEST['vote'] === 'down')) {
2319
$response = [];
2420
$hash = substr(md5($_REQUEST['page']), 0, 16);
2521
$notes_file = $_SERVER['DOCUMENT_ROOT'] . "/backend/notes/" . substr($hash, 0, 2) . "/$hash";
@@ -55,7 +51,7 @@
5551
echo json_encode($response);
5652
exit;
5753
}
58-
if (!empty($_REQUEST['id']) && !empty($_REQUEST['page']) && ($N = $notes->load($_REQUEST['page'])) && array_key_exists($_REQUEST['id'], $N) && !empty($_REQUEST['vote']) && ($_REQUEST['vote'] === 'up' || $_REQUEST['vote'] === 'down')) {
54+
if (!empty($_REQUEST['id']) && !empty($_REQUEST['page']) && ($N = manual_notes_load($_REQUEST['page'])) && array_key_exists($_REQUEST['id'], $N) && !empty($_REQUEST['vote']) && ($_REQUEST['vote'] === 'up' || $_REQUEST['vote'] === 'down')) {
5955
if (!empty($_POST['challenge']) && !empty($_POST['func']) || empty($_POST['arga']) || empty($_POST['argb'])) {
6056
if (!test_answer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['challenge'])) {
6157
$error = "Incorrect answer! Please try again.";
@@ -100,7 +96,7 @@
10096
site_header("Vote On User Notes");
10197
$headerset = true;
10298

103-
if (!empty($_REQUEST['id']) && !empty($_REQUEST['page']) && ($N = $notes->load($_REQUEST['page'])) && array_key_exists($_REQUEST['id'], $N) && !empty($_REQUEST['vote']) && ($_REQUEST['vote'] === 'up' || $_REQUEST['vote'] === 'down')) {
99+
if (!empty($_REQUEST['id']) && !empty($_REQUEST['page']) && ($N = manual_notes_load($_REQUEST['page'])) && array_key_exists($_REQUEST['id'], $N) && !empty($_REQUEST['vote']) && ($_REQUEST['vote'] === 'up' || $_REQUEST['vote'] === 'down')) {
104100
?>
105101
<div class="container" id="notes-dialog" style="width: 100%; padding-bottom: 15px; margin: auto;">
106102
<div style="width: 100%; margin: auto;"><h1>Voting</h1></div>
@@ -122,7 +118,7 @@
122118
<?php
123119
$backID = htmlspecialchars($_REQUEST['id']);
124120
$backPAGE = htmlspecialchars($_REQUEST['page']);
125-
$notes->displaySingle($N[$_REQUEST['id']], false);
121+
manual_note_display($N[$_REQUEST['id']], false);
126122
?>
127123
</div>
128124
<div style="width: 90%; margin: auto;"><p><a href="<?php echo "/{$backPAGE}#{$backID}"; ?>">&lt;&lt; Back to user notes page</a></p></div>
@@ -175,7 +171,7 @@
175171
<?php
176172
$backID = htmlspecialchars($_REQUEST['id']);
177173
$backPAGE = htmlspecialchars($_REQUEST['page']);
178-
$notes->displaySingle($N[$_REQUEST['id']], false);
174+
manual_note_display($N[$_REQUEST['id']], false);
179175
?>
180176
</div>
181177
<div style="width: 90%; margin: auto;"><p><a href="<?php echo "/{$backPAGE}#{$backID}"; ?>">&lt;&lt; Back to user notes page</a></p></div>

0 commit comments

Comments
 (0)