diff --git a/.gitignore b/.gitignore index b6fa401..ab95405 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ env.*.php env .env.php .env +.env.bak .env.example vendor/ composer.phar diff --git a/app/Factories/LinkFactory.php b/app/Factories/LinkFactory.php new file mode 100644 index 0000000..173e0fc --- /dev/null +++ b/app/Factories/LinkFactory.php @@ -0,0 +1,100 @@ +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); + + return $formatted_link; + } + +} diff --git a/app/Factories/UserFactory.php b/app/Factories/UserFactory.php index 9d13efa..480c3c0 100644 --- a/app/Factories/UserFactory.php +++ b/app/Factories/UserFactory.php @@ -4,7 +4,8 @@ use Hash; use App\Models\User; use App\Helpers\CryptoHelper; -class UserFactory { + +class UserFactory { public static function createUser($username, $email, $password, $active=0, $ip='127.0.0.1') { $hashed_password = Hash::make($password); diff --git a/app/Helpers/ApiHelper.php b/app/Helpers/ApiHelper.php new file mode 100644 index 0000000..d645c8c --- /dev/null +++ b/app/Helpers/ApiHelper.php @@ -0,0 +1,12 @@ +first(); if ($link == null) { - return false; + return $link; } else { return true; @@ -73,6 +74,14 @@ static public function validateEnding($link_ending) { return $is_alphanum; } + static public function processPostClick($link) { + /** + * Given a Link model instance, process post click operations. + * @param Link model instance $link + * @return boolean + */ + } + static public function findSuitableEnding() { /** * Provided an in-use link ending (string), diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index 30acfcb..60ec371 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -1,5 +1,6 @@ input('api_key'); + $user = User::where('active', 1) + ->where('api_key', $api_key) + ->where('api_active', true) + ->first(); + + $api_limited_reached = ApiHelper::checkUserApiQuota($user->username); + } + + protected static function encodeResponse($result, $action, $response_type='json') { + $response = { + "action" => $action, + "result" => $result + } + + if ($response_type == 'json') { + return json_encode($response); + } + else if ($response_type == 'plain_text') { + return $result; + } + } +} diff --git a/app/Http/Controllers/Api/ApiLinkController.php b/app/Http/Controllers/Api/ApiLinkController.php new file mode 100644 index 0000000..5e6465f --- /dev/null +++ b/app/Http/Controllers/Api/ApiLinkController.php @@ -0,0 +1,38 @@ +input('response_type'); + $ard = self::getApiUserInfo($request); + + /* */ + $long_url = $request->input('url'); + $is_secret = $request->input('is_secret'); + $custom_ending = $request->input('custom_ending'); + + $formatted_link = LinkFactory::createLink(); + + return self::encodeResponse($formatted_link, 'shorten', $response_type); + } + + public static function lookupLink(Request $request) { + $response_type = $request->input('response_type'); + $ard = self::getApiUserInfo($request); + + /* */ + $url_ending = $request->input('url_ending'); + $link_or_false = LinkHelper::linkExists($url_ending); + + if ($link_or_false) { + return $link_or_false; + } + else { + abort(404, "Link not found."); + } + + } +} diff --git a/app/Http/Controllers/ApiController.php b/app/Http/Controllers/ApiController.php deleted file mode 100644 index e69de29..0000000 diff --git a/app/Http/Controllers/LinkController.php b/app/Http/Controllers/LinkController.php index 161ef30..35e773e 100644 --- a/app/Http/Controllers/LinkController.php +++ b/app/Http/Controllers/LinkController.php @@ -4,7 +4,7 @@ use Illuminate\Http\Redirect; use App\Models\Link; - +use App\Factories\LinkFactory; use App\Helpers\CryptoHelper; use App\Helpers\LinkHelper; @@ -19,78 +19,25 @@ private function renderError($message) { return redirect(route('index'))->with('error', $message); } - private function formatAndRender($link_ending, $secret_ending=False) { - $short_url = env('APP_PROTOCOL') . env('APP_ADDRESS') . '/' . $link_ending; - if ($secret_ending) { - $short_url .= '/' . $secret_ending; - } - return view('shorten_result', ['short_url' => $short_url]); - } - - public function performShorten(Request $request) { $this->request = $request; $long_url = $request->input('link-url'); $custom_ending = $request->input('custom-ending'); $is_secret = ($request->input('options') == "s" ? true : false); - $creator = session('username'); - - $is_already_short = LinkHelper::checkIfAlreadyShortened($long_url); - if ($is_already_short) { - return $this->renderError('Sorry, but your link already\ - looks like a shortened URL.'); - } - - if (!$is_secret && $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 $this->formatAndRender($existing_link); - } - - if ($custom_ending) { - // has custom ending - $ending_conforms = LinkHelper::validateEnding($custom_ending); - if (!$ending_conforms) { - return $this->renderError('Sorry, but custom endings\ - can only contain alphanumeric characters'); - } - - $ending_in_use = LinkHelper::linkExists($custom_ending); - if ($ending_in_use) { - return $this->renderError('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 = $request->ip(); - $link->is_custom = $custom_ending != null; + $creator = session('username'); - if ($creator) { - // if user is logged in, save user as creator - $link->creator = $creator; - } + $link_ip = $request->ip(); - if ($is_secret) { - $rand_bytes_num = intval(env('POLR_SECRET_BYTES')); - $secret_key = CryptoHelper::generateRandomHex($rand_bytes_num); - $link->secret_key = $secret_key; + try { + $short_url = LinkFactory::createLink($long_url, $is_secret, $custom_ending, $link_ip, $creator); } - else { - $secret_key = false; + catch (\Exception $e) { + return self::renderError($e->getMessage()); } - $link->save(); - - return $this->formatAndRender($link_ending, $secret_key); + return view('shorten_result', ['short_url' => $short_url]); } public function performRedirect(Request $request, $short_url, $secret_key=false) { @@ -109,8 +56,6 @@ public function performRedirect(Request $request, $short_url, $secret_key=false) ]); } - - if ($link_secret_key) { if (!$secret_key) { // if we do not receieve a secret key @@ -137,6 +82,8 @@ public function performRedirect(Request $request, $short_url, $secret_key=false) $link->save(); + LinkHelper::processPostClick($link); + return redirect()->to($long_url); } } diff --git a/app/Http/routes.php b/app/Http/routes.php index a0a791e..949162b 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -42,5 +42,7 @@ $app->post('/api/v2/link_avail_check', ['as' => 'api_link_check', 'uses' => 'AjaxController@checkLinkAvailability']); $app->post('/api/v2/admin/toggle_api_active', ['as' => 'api_toggle_api_active', 'uses' => 'AjaxController@toggleAPIActive']); $app->post('/api/v2/admin/generate_new_api_key', ['as' => 'api_generate_new_api_key', 'uses' => 'AjaxController@generateNewAPIKey']); - $app->post('/api/v2/admin/delete_user', ['as' => 'api_generate_new_api_key', 'uses' => 'AjaxController@deleteUser']); + +$app->post('/api/v2/action/shorten', ['as' => 'api_shorten_url', 'uses' => 'Api\ApiLinkController@shortenLink']); +$app->post('/api/v2/action/lookup', ['as' => 'api_lookup_url', 'uses' => 'Api\ApiLinkController@lookupLink']); diff --git a/database/migrations/2015_11_04_015813_create_link_table.php b/database/migrations/2015_11_04_015813_create_link_table.php index f2e32c2..78be1e5 100644 --- a/database/migrations/2015_11_04_015813_create_link_table.php +++ b/database/migrations/2015_11_04_015813_create_link_table.php @@ -28,6 +28,7 @@ public function up() $table->boolean('is_disabled')->default(0); $table->boolean('is_custom')->default(0); + $table->boolean('is_api')->default(0); $table->timestamps(); }); diff --git a/resources/views/layouts/base.blade.php b/resources/views/layouts/base.blade.php index e20bf4c..a87b175 100644 --- a/resources/views/layouts/base.blade.php +++ b/resources/views/layouts/base.blade.php @@ -57,16 +57,16 @@ diff --git a/resources/views/snippets/user_table.blade.php b/resources/views/snippets/user_table.blade.php index 9644a8d..19430de 100644 --- a/resources/views/snippets/user_table.blade.php +++ b/resources/views/snippets/user_table.blade.php @@ -30,7 +30,7 @@ - username)btn-disabled @endif' data-user-id='{{$user->id}}'> Delete diff --git a/util/restore_stock_env.sh b/util/restore_stock_env.sh new file mode 100644 index 0000000..a5bc73d --- /dev/null +++ b/util/restore_stock_env.sh @@ -0,0 +1,3 @@ +mv .env .env.bak +wget https://raw.githubusercontent.com/cydrobolt/polr/2.0-dev/.env +echo "Done!"