Skip to content

Commit 87d67eb

Browse files
committed
Create UserNoteService class
1 parent b03a982 commit 87d67eb

File tree

5 files changed

+248
-238
lines changed

5 files changed

+248
-238
lines changed

include/layout.inc

-36
Original file line numberDiff line numberDiff line change
@@ -157,28 +157,6 @@ function make_link (string $url, string $linktext = ''): string
157157
return sprintf("<a href=\"%s\">%s</a>", $url, $linktext ?: $url);
158158
}
159159

160-
// make_popup_link()
161-
// return a hyperlink to something, within the site, that pops up a new window
162-
//
163-
function make_popup_link ($url, $linktext = false, $target = false, $windowprops = "", $extras = false) {
164-
return sprintf("<a href=\"%s\" target=\"%s\" onclick=\"window.open('%s','%s','%s');return false;\"%s>%s</a>",
165-
htmlspecialchars($url, ENT_QUOTES | ENT_IGNORE),
166-
($target ?: "_new"),
167-
htmlspecialchars($url, ENT_QUOTES | ENT_IGNORE),
168-
($target ?: "_new"),
169-
$windowprops,
170-
($extras ? ' ' . $extras : ''),
171-
($linktext ?: $url)
172-
);
173-
}
174-
175-
// print_popup_link()
176-
// print a hyperlink to something, within the site, that pops up a new window
177-
//
178-
function print_popup_link($url, $linktext = false, $windowprops = "", $target = false, $extras = false) {
179-
echo make_popup_link($url, $linktext, $windowprops, $target, $extras);
180-
}
181-
182160
// Print a link for a downloadable file (including filesize)
183161
function download_link($file, $title)
184162
{
@@ -234,20 +212,6 @@ function clean($var) {
234212
return htmlspecialchars($var, ENT_QUOTES);
235213
}
236214

237-
// Clean out the content of one user note for printing to HTML
238-
function clean_note($text)
239-
{
240-
// Highlight PHP source
241-
$text = highlight_php(trim($text), true);
242-
243-
// Turn urls into links
244-
return preg_replace(
245-
'!((mailto:|(https?|ftp|nntp|news)://).*?)(\s|<|\)|"|\\\\|\'|$)!',
246-
'<a href="\1" rel="nofollow" target="_blank">\1</a>\4',
247-
$text
248-
);
249-
}
250-
251215
function display_errors($errors)
252216
{
253217
echo '<div class="errors">';

include/shared-manual.inc

+6-196
Original file line numberDiff line numberDiff line change
@@ -24,178 +24,7 @@ $PGI = []; $SIDEBAR_DATA = '';
2424

2525
require_once __DIR__ . '/../autoload.php';
2626

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

20029
function manual_navigation_breadcrumbs(array $setup) {
20130
$menu = [];
@@ -293,7 +122,9 @@ function manual_setup($setup) {
293122
if (substr($filename, -4) == '.php') {
294123
$filename = substr($filename, 0, -4);
295124
}
296-
$USERNOTES = manual_notes_load($filename);
125+
126+
$userNoteService = new UserNoteService();
127+
$USERNOTES = $userNoteService->load($filename);
297128
if ($USERNOTES) {
298129
$note = current($USERNOTES);
299130
$timestamps[] = $note->ts;
@@ -384,34 +215,13 @@ CHANGE_LANG;
384215
function manual_footer() {
385216
global $USERNOTES, $__RELATED;
386217

387-
manual_notes($USERNOTES);
218+
$userNoteService = new UserNoteService();
219+
$userNoteService->display($USERNOTES);
388220
$config = [
389221
'related_menu' => $__RELATED['toc'],
390222
'related_menu_deprecated' => $__RELATED['toc_deprecated']
391223
];
392224
site_footer($config);
393225
}
394226

395-
// This function takes a DateTime object and returns a formated string of the time difference relative to now
396-
function relTime(DateTime $date) {
397-
$current = new DateTime;
398-
$diff = $current->diff($date);
399-
$units = ["year" => $diff->format("%y"),
400-
"month" => $diff->format("%m"),
401-
"day" => $diff->format("%d"),
402-
"hour" => $diff->format("%h"),
403-
"minute" => $diff->format("%i"),
404-
"second" => $diff->format("%s"),
405-
];
406-
$out = "just now...";
407-
foreach ($units as $unit => $amount) {
408-
if (empty($amount)) {
409-
continue;
410-
}
411-
$out = $amount . " " . ($amount == 1 ? $unit : $unit . "s") . " ago";
412-
break;
413-
}
414-
return $out;
415-
}
416-
417227
/* vim: set et ts=4 sw=4: */

manual/add-note.php

+3-1
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

@@ -140,9 +141,10 @@
140141
if ($error) { echo "<p class=\"formerror\">$error</p>\n"; }
141142

142143
// Print out preview of note
144+
$userNoteService = new UserNoteService();
143145
echo '<p>This is what your entry will look like, roughly:</p>';
144146
echo '<div id="usernotes">';
145-
manual_note_display(new UserNote('', '', '', time(), $user, $note));
147+
$userNoteService->displaySingle(new UserNote('', '', '', time(), $user, $note));
146148
echo '</div><br><br>';
147149
}
148150

manual/vote-note.php

+9-5
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.";
@@ -102,7 +106,7 @@
102106
site_header("Vote On User Notes");
103107
$headerset = true;
104108

105-
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')) {
109+
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')) {
106110
?>
107111
<div class="container" id="notes-dialog" style="width: 100%; padding-bottom: 15px; margin: auto;">
108112
<div style="width: 100%; margin: auto;"><h1>Voting</h1></div>
@@ -124,7 +128,7 @@
124128
<?php
125129
$backID = htmlspecialchars($_REQUEST['id']);
126130
$backPAGE = htmlspecialchars($_REQUEST['page']);
127-
manual_note_display($N[$_REQUEST['id']], false);
131+
$notes->displaySingle($N[$_REQUEST['id']], false);
128132
?>
129133
</div>
130134
<div style="width: 90%; margin: auto;"><p><a href="<?php echo "/{$backPAGE}#{$backID}"; ?>">&lt;&lt; Back to user notes page</a></p></div>
@@ -177,7 +181,7 @@
177181
<?php
178182
$backID = htmlspecialchars($_REQUEST['id']);
179183
$backPAGE = htmlspecialchars($_REQUEST['page']);
180-
manual_note_display($N[$_REQUEST['id']], false);
184+
$notes->displaySingle($N[$_REQUEST['id']], false);
181185
?>
182186
</div>
183187
<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)