make it easier to build urls with parameters

This commit is contained in:
Spine
2023-03-12 18:19:55 +00:00
parent 3dae28f662
commit bf9ffdd4f3
14 changed files with 43 additions and 13 deletions

View File

@@ -22,8 +22,21 @@ abstract class BaseObject extends Base {
return $this->id;
}
public function url(): string {
return htmlentities($this->location());
public function publicLocation(): string {
return SITE_URL . '/' . $this->location();
}
public function publicUrl(string|null $param = null): string {
return SITE_URL . '/' . $this->url($param);
}
public function url(string|null $param = null): string {
$location = $this->location();
if (isset($param)) {
$location = preg_replace('/#.*$/', '', $location);
$location .= str_contains($location, '?') ? "&$param" : "?$param";
}
return htmlentities($location);
}
public function pkName(): string {

View File

@@ -21,7 +21,7 @@ class ReportType extends \Gazelle\BaseObject {
public function pkName(): string { return "torrent_report_configuration_id"; }
public function tableName(): string { return 'torrent_report_configuration'; }
public function url(): string {
public function url(string|null $param = null): string {
return htmlentities($this->location());
}

View File

@@ -22,7 +22,7 @@ $comment->setBody($body)->setEditedUserID($Viewer->id())->modify();
if ((bool)($_POST['pm'] ?? false) && !$comment->isAuthor($Viewer->id())) {
// Send a PM to the user to notify them of the edit
$id = $comment->id();
$url = SITE_URL . "/comments.php?action=jump&postid=$id";
$url = $comment->publicUrl('action=jump');
$moderator = "[url=" . $Viewer->url() . "]" . $Viewer->username() . "[/url]";
(new Gazelle\Manager\User)-> sendPM($comment->userId(), 0,
"Your comment #$id has been edited",

View File

@@ -21,14 +21,14 @@ if (is_null($user) || $user->classLevel() > $Viewer->classLevel()) {
error(403);
}
$url = SITE_URL . '/' . $comment->url();
$url = $comment->publicUrl();
$comment->setBody(trim($_POST['body']))->modify();
$Length = trim($_POST['length']);
$Reason = trim($_POST['reason']);
$PrivateMessage = trim($_POST['privatemessage']);
if ($Length !== 'verbal') {
$Time = (int)$Length * (7 * 24 * 60 * 60);
$Time = (int)$Length * (7 * 86_400);
$WarnTime = Time::offset($Time);
$userMan->warn($user->id(), $Time, "$url - $Reason", $Viewer->username());
$subject = 'You have received a warning';

View File

@@ -6,7 +6,7 @@ function notify ($Viewer, $Channel, $Message) {
Irc::sendMessage($Channel,
$Message . " error by "
. ($Viewer
? SITE_URL . "/" . $Viewer->location() . " (" . $Viewer->username() . ")"
? $Viewer->publicLocation() . " (" . $Viewer->username() . ")"
: $_SERVER['REMOTE_ADDR']
)
. " (" . geoip($_SERVER['REMOTE_ADDR']) . ")"

View File

@@ -69,7 +69,7 @@ $threadId = $thread->id();
if ($needPoll) {
(new Gazelle\Manager\ForumPoll)->create($threadId, $question, $answerList);
if ($forum->id() == STAFF_FORUM_ID) {
Irc::sendMessage(MOD_CHAN, "Poll created by {$Viewer->username()}: \"$question\" " . SITE_URL . "/forums.php?action=viewthread&threadid=$threadId");
Irc::sendMessage(MOD_CHAN, "Poll created by {$Viewer->username()}: \"$question\" " . $thread->publicLocation());
}
}

View File

@@ -412,7 +412,7 @@ if (!$newRequest) {
Gazelle\Util\Irc::sendMessage(
'#requests',
$request->text() . " - " . SITE_URL . "/" . $request->location() . " - " . implode(' ', $tags)
$request->text() . " - " . $request->publicLocation() . " - " . implode(' ', $tags)
);
}

View File

@@ -36,7 +36,7 @@ if ($Viewer->permitted('admin_manage_blog')) {
if ($_REQUEST['action'] == 'takenewblog') {
$blog = $blogMan->create($Viewer, $title, $body);
Irc::sendMessage(MOD_CHAN, "New staff blog: " . $blog->title()
. " - " . SITE_URL . '/' . $blog->location()
. " - " . $blog->publicLocation()
);
} else {
$blog->setUpdate('Title', $title)

View File

@@ -42,7 +42,7 @@ if (!($_REQUEST['usetoken'] ?? 0) && $torrent->uploaderId() != $userId) {
} else {
Irc::sendMessage(
STATUS_CHAN,
SITE_URL . "/" . $Viewer->location()
$Viewer->publicLocation()
. " (" . $Viewer->username() . ")"
. " (" . geoip($_SERVER['REMOTE_ADDR']) . ")"
. " accessing "

View File

@@ -14,7 +14,7 @@ if (!isset($_REQUEST['id'])) {
$ownProfile = ($user->id() == $Viewer->id());
if (!$ownProfile && !$Viewer->permitted('users_edit_profiles')) {
Gazelle\Util\Irc::sendMessage(ADMIN_CHAN, 'User ' . $Viewer->label()
. ' tried to edit ' . SITE_URL . "/" . $user->location()
. ' tried to edit ' . $user->publicLocation()
);
error(403);
}

View File

@@ -84,6 +84,11 @@ class RequestTest extends TestCase {
$this->assertEquals("$artistName $title [2018]", $this->request->text(), 'request-text');
$this->assertEquals(1, $this->request->releaseType(), 'request-release-type-id');
$location = 'requests.php?action=view&id=' . $this->request->id();
$this->assertEquals($location, $this->request->location(), 'request-location');
$this->assertEquals(SITE_URL . "/$location", $this->request->publicLocation(), 'request-public-location');
$this->assertEquals(htmlentities($location), $this->request->url(), 'request-url');
$this->assertEquals(SITE_URL . '/' . htmlentities($location), $this->request->publicUrl(), 'request-public-url');
$this->assertEquals('Album', $this->request->releaseTypeName(), 'request-release-type-name');
$this->assertEquals(0, $this->request->fillerId(), 'request-unfilled-filler-id');
$this->assertEquals(0, $this->request->torrentId(), 'request-unfilled-torrent-id');

View File

@@ -24,6 +24,12 @@ class StaffBlogTest extends TestCase {
$this->assertIsString($blog->created(), 'staff-blog-created');
$this->assertIsInt($blog->epoch(), 'staff-blog-epoch');
$location = 'staffblog.php#blog' . $blog->id();
$this->assertEquals($location, $blog->location(), 'staff-blog-location');
$this->assertEquals(SITE_URL . "/$location", $blog->publicLocation(), 'staff-blog-public-location');
$this->assertEquals($location, $blog->url(), 'staff-blog-url');
$this->assertEquals(SITE_URL . "/$location", $blog->publicUrl(), 'staff-blog-public-url');
$this->assertEquals(0, $manager->readBy($mod), 'staff-blog-not-read-by-mod');
$this->assertGreaterThan(0, $manager->catchup($mod), 'staff-blog-viewed-by-mod');
$this->assertGreaterThan(0, $manager->readBy($mod), 'staff-blog-now-read-by-mod');

View File

@@ -104,7 +104,7 @@ class TextTest extends TestCase {
);
$this->assertEquals(
"<a href=\"" . SITE_URL . "/collages.php?id={$collage->id()}\">{$collage->name()}</a>",
Text::full_format(SITE_URL . '/' . $collage->url()),
Text::full_format($collage->publicUrl()),
'text-collage-url'
);
$this->assertEquals(1, $collage->remove(), 'text-remove-collage');

View File

@@ -33,6 +33,12 @@ class UserCreateTest extends TestCase {
$this->assertStringContainsString($adminComment, $this->user->staffNotes(), 'user-create-staff-notes');
$this->assertTrue($this->user->isUnconfirmed(), 'user-create-unconfirmed');
$location = "user.php?id={$this->user->id()}";
$this->assertEquals($location, $this->user->location(), 'user-location');
$this->assertEquals(SITE_URL . "/$location", $this->user->publicLocation(), 'user-public-location');
$this->assertEquals($location, $this->user->url(), 'user-url');
$this->assertEquals(SITE_URL . "/$location", $this->user->publicUrl(), 'user-public-url');
$login = new Gazelle\Login;
$watch = new Gazelle\LoginWatch($_SERVER['REMOTE_ADDR']);
$watch->clearAttempts();