KeyController.php 2.12 KB
Newer Older
Dominik Hebeler's avatar
Dominik Hebeler committed
1
2
3
4
<?php

namespace App\Http\Controllers;

5
use Cookie;
Dominik Hebeler's avatar
Dominik Hebeler committed
6
use Illuminate\Http\Request;
7
use LaravelLocalization;
Dominik Hebeler's avatar
Dominik Hebeler committed
8
9
10
11
12
13
14
15

class KeyController extends Controller
{
    public function index(Request $request)
    {
        $redirUrl = $request->input('redirUrl', "");

        return view('key')
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
            ->with('title', trans('titles.key'));

    }

    public function setKey(Request $request)
    {
        $redirUrl = $request->input('redirUrl', "");
        $key = $request->input('key', '');

        if ($this->authorizeKey($key)) {
            # Valid Key
            $host = $request->header("X_Forwarded_Host", "");
            if (empty($host)) {
                $host = $request->header("Host", "");
            }

            Cookie::queue('key', $key, 525600, '/meta/', null, false, false);
            return redirect($redirUrl);
        } else {
            return view('key')
                ->with('title', trans('titles.key'))
                ->with('keyValid', false);
        }
    }

    public function removeKey(Request $request)
    {
        $redirUrl = $request->input('redirUrl', "");
        Cookie::queue('key', '', 0, '/meta/', null, false, false);
        $url = LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), action('KeyController@index', ['redirUrl' => $redirUrl]));
        return redirect($url);
    }

    private function authorizeKey($key)
    {
        $postdata = http_build_query(array(
            'dummy' => rand(),
        ));
        $opts = array('http' => array(
            'method' => 'POST',
            'header' => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata,
        ),
        );

        $context = stream_context_create($opts);

        try {
            $link = "https://key.metager3.de/" . urlencode($key) . "/request-permission/api-access";
            $result = json_decode(file_get_contents($link, false, $context));
            if ($result->{'api-access'} == true) {
                return true;
            } else {
                return false;
            }

        } catch (\ErrorException $e) {
            return false;
        }
Dominik Hebeler's avatar
Dominik Hebeler committed
75
76
77

    }
}