Skip to content

Commit

Permalink
feat: only redirect if shortlinkUrl is known
Browse files Browse the repository at this point in the history
  • Loading branch information
michtio committed Sep 7, 2022
1 parent 4019923 commit a669899
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions src/services/ShortlinkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ public function handleRedirect(): void
$request = Craft::$app->getRequest();
// Only handle site requests, no live previews or console requests
if ($request->getIsSiteRequest() && !$request->getIsLivePreview() && !$request->getIsConsoleRequest() && !$request->getIsCpRequest()) {
$host = urldecode($request->getHostInfo());
$path = urldecode($request->getUrl());
$url = urldecode($request->getAbsoluteUrl());
$host = urldecode($request->getHostInfo());
$path = urldecode($request->getUrl());
$url = urldecode($request->getAbsoluteUrl());

$baseUrls = [];
$shortlinkUrls = [];
$sites = Craft::$app->getSites()->allSites;
$shortlinks = Shortlink::$settings->shortlinkUrls;

// host returns including trailing slash, make sure we check for that too if it's not site in the SITE_URL
$needle = [
Expand All @@ -106,9 +108,14 @@ public function handleRedirect(): void
$baseUrls[] = $site->baseUrl;
}

// check if our hostname is not one of the existing Craft sites, if so redirect
// TODO add !array_intersect
if(!array_intersect($needle, $baseUrls)) {
// add all shortlinkUrls to an array to check if it's set
foreach($shortlinks as $shortlink) {
$shortlinkUrls[] = $shortlink['shortlinkUrl'];
}

// check if our hostname is not one of the existing Craft sites and a url from our settings, if so redirect
if(!array_intersect($needle, $baseUrls) && array_intersect($needle, $shortlinkUrls)) {

// check if query string should be stripped or not
if (!Shortlink::$settings->redirectQueryString) {
$path = UrlHelper::stripQueryString($path);
Expand Down Expand Up @@ -234,17 +241,7 @@ public function saveShortlink($entry = null, array $post): bool
{
$session = Craft::$app->getSession();

if(!ElementHelper::isDraftOrRevision($entry)) {
$shortlink = ShortlinkElement::findOne(['ownerId' => $entry->id]);
} else {
$shortlink = ShortlinkElement::findOne(['ownerRevisionId' => $entry->id]);
}

if(is_null($shortlink)) {
$shortlink = new ShortlinkElement();
}

//$shortlink = $this->_setShortlinkFromPost();
$shortlink = $this->_setShortlinkFromPost();
$shortlink->shortlinkUri = $post['shortlinkUri'] ?? null;
$shortlink->httpCode = $post['redirectType'] ?? null;
$shortlink->shortlinkStatus = ShortlinkElement::STATUS_ACTIVE;
Expand All @@ -257,8 +254,6 @@ public function saveShortlink($entry = null, array $post): bool
$shortlink->ownerRevisionId = $entry->id;
}

Craft::warning('Shortlink ID: ' . $shortlink->id . ' / Owner ID: ' . $shortlink->ownerId . ' / Owner Revision ID: ' . $shortlink->ownerRevisionId, __METHOD__);

if (!Craft::$app->getElements()->saveElement($shortlink)) {
$session->setError(Craft::t('shortlink', 'Could not save the shortlink.'));
return false;
Expand Down Expand Up @@ -491,18 +486,15 @@ private function _generateUri(string $characters, int $length): string {
* @return ShortlinkElement
* @throws Exception
*/
private function _setShortlinkFromPost(): ShortlinkElement
private function _setShortlinkFromPost(Entry $entry): ShortlinkElement
{
$request = Craft::$app->getRequest();
$shortlinkId = $request->getParam('shortlinkId');

if ($shortlinkId) {
$shortlink = $this->getShortlinkById($shortlinkId);

if (!$shortlink) {
throw new Exception (Craft::t('shortlink', 'No shortlink with the ID “{id}”', ['id' => $shortlinkId]));
}
if(!ElementHelper::isDraftOrRevision($entry)) {
$shortlink = ShortlinkElement::findOne(['ownerId' => $entry->id]);
} else {
$shortlink = ShortlinkElement::findOne(['ownerRevisionId' => $entry->id]);
}

if(is_null($shortlink)) {
$shortlink = new ShortlinkElement();
}

Expand Down

0 comments on commit a669899

Please sign in to comment.