public function performRedirect(Request $request, $short_url, $secret_key = false) { $link = Link::where('short_url', $short_url)->first(); if ($link == null) { return abort(404); } $link_secret_key = $link->secret_key; if ($link->is_disabled == 1) { return view('error', ['message' => 'Sorry, but this link has been disabled by an administrator.']); } if ($link_secret_key) { if (!$secret_key) { // if we do not receieve a secret key // when we are expecting one, return a 404 return abort(404); } else { if ($link_secret_key != $secret_key) { // a secret key is provided, but it is incorrect return abort(404); } } } $long_url = $link->long_url; $clicks = intval($link->clicks); if (is_int($clicks)) { $clicks += 1; } $link->clicks = $clicks; $link->save(); LinkHelper::processPostClick($link); return redirect()->to($long_url); }
public function testLinkExists() { $link = LinkFactory::createLink('http://example.com/ci', true, null, '127.0.0.1', false, true); // assert that existent link ending returns true $this->assertNotEquals(LinkHelper::linkExists($link->short_url), false); // assert that nonexistent link ending returns false $this->assertEquals(LinkHelper::linkExists('nonexistent'), false); }
public static function findSuitableEnding() { /** * Provided an in-use link ending (string), * find the next available base-32/62 ending. * @return string */ $base = env('POLR_BASE'); $link = Link::where('is_custom', 0)->orderBy('created_at', 'desc')->first(); if ($link == null) { $base10_val = 0; $base_x_val = 0; } else { $latest_link_ending = $link->short_url; $base10_val = BaseHelper::toBase10($latest_link_ending, $base); $base10_val++; } $base_x_val = null; while (LinkHelper::linkExists($base_x_val) || $base_x_val == null) { $base_x_val = BaseHelper::toBase($base10_val, $base); $base10_val++; } return $base_x_val; }
public static function lookupLink(Request $request) { $response_type = $request->input('response_type'); $user = self::getApiUserInfo($request); /* */ $url_ending = $request->input('url_ending'); // * required if (!self::checkRequiredArgs([$url_ending])) { abort(400, "Missing required arguments."); } // "secret" key required for lookups on secret URLs $url_key = $request->input('url_key'); $link = LinkHelper::linkExists($url_ending); if ($link['secret_key']) { if ($url_key != $link['secret_key']) { abort(401, "Invalid URL code for secret URL."); } } if ($link) { return self::encodeResponse(['long_url' => $link['long_url'], 'created_at' => $link['created_at'], 'clicks' => $link['clicks']], 'lookup', $response_type, $link['long_url']); } else { abort(404, "Link not found."); } }
public static function createLink($long_url, $is_secret = false, $custom_ending = null, $link_ip = '127.0.0.1', $creator = false, $return_object = false) { /** * Given parameters needed to create a link, generate appropriate ending and * return formatted link. * * @param string $custom_ending * @param boolean (optional) $is_secret * @param string (optional) $custom_ending * @param string $link_ip * @param string $creator * @return string $formatted_link */ $is_already_short = LinkHelper::checkIfAlreadyShortened($long_url); if ($is_already_short) { throw new \Exception('Sorry, but your link already looks like a shortened URL.'); } if (!$is_secret && !$custom_ending && ($existing_link = LinkHelper::longLinkExists($long_url))) { // if link is not specified as secret, is non-custom, and // already exists in Polr, lookup the value and return return self::formatLink($existing_link); } if ($custom_ending) { // has custom ending $ending_conforms = LinkHelper::validateEnding($custom_ending); if (!$ending_conforms) { throw new \Exception('Sorry, but custom endings can only contain alphanumeric characters'); } $ending_in_use = LinkHelper::linkExists($custom_ending); if ($ending_in_use) { throw new \Exception('Sorry, but this URL ending is already in use.'); } $link_ending = $custom_ending; } else { // no custom ending $link_ending = LinkHelper::findSuitableEnding(); } $link = new Link(); $link->short_url = $link_ending; $link->long_url = $long_url; $link->ip = $link_ip; $link->is_custom = $custom_ending != null; if ($creator) { // if user is logged in, save user as creator $link->creator = $creator; } if ($is_secret) { $rand_bytes_num = intval(env('POLR_SECRET_BYTES')); $secret_key = CryptoHelper::generateRandomHex($rand_bytes_num); $link->secret_key = $secret_key; } else { $secret_key = false; } $link->save(); $formatted_link = self::formatLink($link_ending, $secret_key); if ($return_object) { return $link; } return $formatted_link; }
public function toggleLink(Request $request) { self::ensureAdmin(); $link_ending = $request->input('link_ending'); $link = LinkHelper::linkExists($link_ending); if (!$link) { abort(404, 'Link not found.'); } $current_status = $link->is_disabled; $new_status = 1; if ($current_status == 1) { // if currently disabled, then enable $new_status = 0; } $link->is_disabled = $new_status; $link->save(); return $new_status ? "Enable" : "Disable"; }