Skip to content

Commit b31ec1c

Browse files
kamil-tekieladerickr
authored andcommitted
Create UserNoteService class
1 parent 5f7c6aa commit b31ec1c

File tree

5 files changed

+255
-261
lines changed

5 files changed

+255
-261
lines changed

include/layout.inc

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -164,21 +164,6 @@ 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-
182167
// Print a link for a downloadable file (including filesize)
183168
function download_link($file, $title): void
184169
{
@@ -220,20 +205,6 @@ function clean($var) {
220205
return htmlspecialchars($var, ENT_QUOTES);
221206
}
222207

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-
237208
function display_errors($errors): void
238209
{
239210
echo '<div class="errors">';

include/shared-manual.inc

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

2525
use phpweb\I18n\Languages;
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/[email protected]" 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/[email protected]" 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/[email protected]" 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-
}
26+
use phpweb\UserNotes\UserNoteService;
20527

20628
function manual_navigation_breadcrumbs(array $setup) {
20729
$menu = [];
@@ -298,7 +120,9 @@ function manual_setup($setup): void {
298120
if (substr($filename, -4) == '.php') {
299121
$filename = substr($filename, 0, -4);
300122
}
301-
$USERNOTES = manual_notes_load($filename);
123+
124+
$userNoteService = new UserNoteService();
125+
$USERNOTES = $userNoteService->load($filename);
302126
if ($USERNOTES) {
303127
$note = current($USERNOTES);
304128
$timestamps[] = $note->ts;
@@ -422,62 +246,18 @@ function manual_footer($setup): void {
422246
</div>
423247
CONTRIBUTE;
424248

425-
manual_notes($USERNOTES);
249+
$userNoteService = new UserNoteService();
250+
$userNoteService->display($USERNOTES);
426251
site_footer([
427252
'related_menu' => $__RELATED['toc'],
428253
'related_menu_deprecated' => $__RELATED['toc_deprecated'],
429254
]);
430255
}
431256

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-
454-
function contributors($setup) {
455-
if (!isset($_GET["contributors"])
456-
|| !isset($setup["history"]["contributors"])
457-
|| count($setup["history"]["contributors"]) < 1) {
458-
return;
459-
}
460-
461-
$contributorList = "<li>" . implode("</li><li>", $setup["history"]["contributors"]) . "</li>";
462-
463-
echo <<<CONTRIBUTORS
464-
<div class="book">
465-
<h1 class="title">Output Buffering Control</h1>
466-
The following have authored commits that contributed to this page:
467-
<ul>
468-
$contributorList
469-
</ul>
470-
</div>
471-
CONTRIBUTORS;
472-
manual_footer($setup);
473-
exit;
474-
}
475-
476257
function autogen(string $text, string $lang) {
477258
static $translations = [];
478259

479260
$lang = ($lang === "") ? "en" : $lang;
480-
$lang = strtolower($lang);
481261
if (isset($translations[$lang])) {
482262
if (isset($translations[$lang][$text]) && $translations[$lang][$text] !== "") {
483263
return $translations[$lang][$text];

manual/add-note.php

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

1010
use phpweb\UserNotes\UserNote;
11+
use phpweb\UserNotes\UserNoteService;
1112

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

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

148149
// Print out preview of note
150+
$userNoteService = new UserNoteService();
149151
echo '<p>This is what your entry will look like, roughly:</p>';
150152
echo '<div id="usernotes">';
151-
manual_note_display(new UserNote('', '', '', time(), $user, $note));
153+
$userNoteService->displaySingle(new UserNote('', '', '', time(), $user, $note));
152154
echo '</div><br><br>';
153155
}
154156

manual/vote-note.php

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

1721
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
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')) {
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')) {
1923
$response = [];
2024
$hash = substr(md5($_REQUEST['page']), 0, 16);
2125
$notes_file = $_SERVER['DOCUMENT_ROOT'] . "/backend/notes/" . substr($hash, 0, 2) . "/$hash";
@@ -51,7 +55,7 @@
5155
echo json_encode($response);
5256
exit;
5357
}
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')) {
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')) {
5559
if (!empty($_POST['challenge']) && !empty($_POST['func']) || empty($_POST['arga']) || empty($_POST['argb'])) {
5660
if (!test_answer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['challenge'])) {
5761
$error = "Incorrect answer! Please try again.";
@@ -96,7 +100,7 @@
96100
site_header("Vote On User Notes");
97101
$headerset = true;
98102

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')) {
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')) {
100104
?>
101105
<div class="container" id="notes-dialog" style="width: 100%; padding-bottom: 15px; margin: auto;">
102106
<div style="width: 100%; margin: auto;"><h1>Voting</h1></div>
@@ -118,7 +122,7 @@
118122
<?php
119123
$backID = htmlspecialchars($_REQUEST['id']);
120124
$backPAGE = htmlspecialchars($_REQUEST['page']);
121-
manual_note_display($N[$_REQUEST['id']], false);
125+
$notes->displaySingle($N[$_REQUEST['id']], false);
122126
?>
123127
</div>
124128
<div style="width: 90%; margin: auto;"><p><a href="<?php echo "/{$backPAGE}#{$backID}"; ?>">&lt;&lt; Back to user notes page</a></p></div>
@@ -171,7 +175,7 @@
171175
<?php
172176
$backID = htmlspecialchars($_REQUEST['id']);
173177
$backPAGE = htmlspecialchars($_REQUEST['page']);
174-
manual_note_display($N[$_REQUEST['id']], false);
178+
$notes->displaySingle($N[$_REQUEST['id']], false);
175179
?>
176180
</div>
177181
<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)