Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LinkController.php « Controllers « Http « app - github.com/cydrobolt/polr.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 161ef30ac22f25150bd0907b7d7127a7a1351040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Redirect;

use App\Models\Link;

use App\Helpers\CryptoHelper;
use App\Helpers\LinkHelper;

class LinkController extends Controller {
    /**
     * Show the admin panel, and process admin AJAX requests.
     *
     * @return Response
     */

    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;

        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();

        return $this->formatAndRender($link_ending, $secret_key);
    }

    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->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;

        if (is_int($link->clicks)) {
            $link->clicks += 1;
        }
        else {
            $link->clicks = 1;
        }

        $link->save();

        return redirect()->to($long_url);
    }
}