1
0
mirror of https://github.com/cydrobolt/polr.git synced 2024-11-09 11:42:28 +01:00

Refactor use of link lookups in new URL shortening fix #311

This commit is contained in:
Chaoyi Zha 2017-04-11 12:51:40 -04:00
parent 4286d209d5
commit d8dd8d6b0b
2 changed files with 20 additions and 6 deletions

View File

@ -55,10 +55,10 @@ class LinkFactory {
looks like a shortened URL.');
}
if (!$is_secret && (!isset($custom_ending) || $custom_ending === '') && (LinkHelper::longLinkExists($long_url) !== false)) {
if (!$is_secret && (!isset($custom_ending) || $custom_ending === '') && (LinkHelper::longLinkExists($long_url, $creator) !== false)) {
// if link is not specified as secret, is non-custom, and
// already exists in Polr, lookup the value and return
$existing_link = LinkHelper::longLinkExists($long_url);
$existing_link = LinkHelper::longLinkExists($long_url, $creator);
return self::formatLink($existing_link);
}

View File

@ -51,16 +51,30 @@ class LinkHelper {
}
}
static public function longLinkExists($long_url) {
static public function longLinkExists($long_url, $username=false) {
/**
* Provided a long link (string),
* check whether the link is in the DB.
* If a username is provided, only search for links created by the
* user.
* @return boolean
*/
$link = Link::longUrl($long_url)
$link_base = Link::longUrl($long_url)
->where('is_custom', 0)
->where('secret_key', '')
->first();
->where('secret_key', '');
if (is_null($username)) {
// Search for links without a creator only
$link = $link_base->where('creator', '')->first();
}
else if (($username !== false)) {
// Search for links created by $username only
$link = $link_base->where('creator', $username)->first();
}
else {
// Search for links created by any user
$link = $link_base->first();
}
if ($link == null) {
return false;