SettingsController.php 8.99 KB
Newer Older
1 2 3 4
<?php

namespace App\Http\Controllers;

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

class SettingsController extends Controller
{
    public function index(Request $request)
    {
        $fokus = $request->input('fokus', '');
Dominik Hebeler's avatar
Dominik Hebeler committed
15
        $fokusName = "";
16 17 18
        if (empty($fokus)) {
            return redirect('/');
        } else {
Dominik Hebeler's avatar
Dominik Hebeler committed
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
            $fokusName = trans('index.foki.' . $fokus);
        }

        $langFile = MetaGer::getLanguageFile();
        $langFile = json_decode(file_get_contents($langFile));

        $sumas = $this->getSumas($fokus);

        # Parse the Parameter Filter
        $filters = [];
        $filteredSumas = false;
        foreach ($langFile->filter->{"parameter-filter"} as $name => $filter) {
            $values = $filter->values;
            $cookie = Cookie::get($fokus . "_setting_" . $filter->{"get-parameter"});
            foreach ($sumas as $suma => $sumaInfo) {
                if (!$filteredSumas && $sumaInfo["filtered"]) {
                    $filteredSumas = true;
                }
                if (!$sumaInfo["filtered"] && $sumaInfo["enabled"] && !empty($filter->sumas->{$suma})) {
                    if (empty($filters[$name])) {
                        $filters[$name] = $filter;
                        unset($filters[$name]->values);
                    }
                    if (empty($filters[$name]->values)) {
                        $filters[$name]->values = (object) [];
                    }
                    foreach ($filter->sumas->{$suma}->values as $key => $value) {
                        $filters[$name]->values->$key = $values->$key;
                    }
                }

            }
        }

        $url = $request->input('url', '');

        # Check if any setting is active
        $cookies = Cookie::get();
        $settingActive = false;
        foreach ($cookies as $key => $value) {
            if (\starts_with($key, [$fokus . "_engine_", $fokus . "_setting_"])) {
                $settingActive = true;
            }
62 63 64
        }

        return view('settings.index')
Dominik Hebeler's avatar
Dominik Hebeler committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
            ->with('title', trans('titles.settings', ['fokus' => $fokusName]))
            ->with('fokus', $request->input('fokus', ''))
            ->with('fokusName', $fokusName)
            ->with('filteredSumas', $filteredSumas)
            ->with('sumas', $sumas)
            ->with('filter', $filters)
            ->with('settingActive', $settingActive)
            ->with('url', $url);
    }

    private function getSumas($fokus)
    {
        $langFile = MetaGer::getLanguageFile();
        $langFile = json_decode(file_get_contents($langFile));

        $sumasFoki = $langFile->foki->{$fokus}->sumas;

        $sumas = [];
        foreach ($sumasFoki as $suma) {
84 85 86 87
            if ((!empty($langFile->sumas->{$suma}->disabled) && $langFile->sumas->{$suma}->disabled) ||
                (!empty($langFile->sumas->{$suma}->{"auto-disabled"}) && $langFile->sumas->{$suma}->{"auto-disabled"})) {
                continue;
            }
Dominik Hebeler's avatar
Dominik Hebeler committed
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
            $sumas[$suma]["display-name"] = $langFile->sumas->{$suma}->{"display-name"};
            $sumas[$suma]["filtered"] = false;
            if (Cookie::get($fokus . "_engine_" . $suma) === "off") {
                $sumas[$suma]["enabled"] = false;
            } else {
                $sumas[$suma]["enabled"] = true;
            }
        }

        foreach ($langFile->filter->{"parameter-filter"} as $name => $filter) {
            $values = $filter->values;
            $cookie = Cookie::get($fokus . "_setting_" . $filter->{"get-parameter"});
            foreach ($sumas as $suma => $sumaInfo) {
                if ($cookie !== null && (empty($filter->sumas->{$suma}) || (!empty($filter->sumas->{$suma}) && empty($filter->sumas->{$suma}->values->$cookie)))) {
                    $sumas[$suma]["filtered"] = true;
                }
            }
        }
        return $sumas;
    }

    public function disableSearchEngine(Request $request)
    {
        $suma = $request->input('suma', '');
        $fokus = $request->input('fokus', '');
        $url = $request->input('url', '');

        if (empty($suma) || empty($fokus)) {
            abort(404);
        }

        # Only disable this engine if it's not the last
        $sumas = $this->getSumas($fokus);
        $sumaCount = 0;
        foreach ($sumas as $name => $sumainfo) {
            if (!$sumainfo["filtered"] && $sumainfo["enabled"]) {
                $sumaCount++;
            }
        }
        $langFile = MetaGer::getLanguageFile();
        $langFile = json_decode(file_get_contents($langFile));

        if ($sumaCount > 1 && in_array($suma, $langFile->foki->{$fokus}->sumas)) {
131 132 133
            $path = \Request::path();
            $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
            Cookie::queue($fokus . "_engine_" . $suma, "off", 525600, $cookiePath, null, false, false);
Dominik Hebeler's avatar
Dominik Hebeler committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        }

        return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings', ["fokus" => $fokus, "url" => $url])));

    }

    public function enableSearchEngine(Request $request)
    {
        $suma = $request->input('suma', '');
        $fokus = $request->input('fokus', '');
        $url = $request->input('url', '');

        if (empty($suma) || empty($fokus)) {
            abort(404);
        }

        if (Cookie::get($fokus . "_engine_" . $suma) !== null) {
151 152 153
            $path = \Request::path();
            $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
            Cookie::queue($fokus . "_engine_" . $suma, "", 0, $cookiePath, null, false, false);
Dominik Hebeler's avatar
Dominik Hebeler committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        }

        return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings', ["fokus" => $fokus, "url" => $url])));

    }

    public function enableFilter(Request $request)
    {
        $fokus = $request->input('fokus', '');
        $url = $request->input('url', '');
        if (empty($fokus)) {
            abort(404);
        }

        $newFilters = $request->except(["fokus", "url"]);

        $langFile = MetaGer::getLanguageFile();
        $langFile = json_decode(file_get_contents($langFile));

        foreach ($newFilters as $key => $value) {
            if ($value === "") {
175 176 177
                $path = \Request::path();
                $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
                Cookie::queue($fokus . "_setting_" . $key, "", 0, $cookiePath, null, false, false);
Dominik Hebeler's avatar
Dominik Hebeler committed
178 179 180 181
            } else {
                # Check if this filter and its value exists:
                foreach ($langFile->filter->{"parameter-filter"} as $name => $filter) {
                    if ($key === $filter->{"get-parameter"} && !empty($filter->values->$value)) {
182 183 184
                        $path = \Request::path();
                        $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
                        Cookie::queue($fokus . "_setting_" . $key, $value, 525600, $cookiePath, null, false, false);
Dominik Hebeler's avatar
Dominik Hebeler committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
                        break;
                    }
                }
            }
        }

        return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings', ["fokus" => $fokus, "url" => $url])));

    }

    public function deleteSettings(Request $request)
    {
        $fokus = $request->input('fokus', '');
        $url = $request->input('url', '');
        if (empty($fokus)) {
            abort(404);
        }

        $cookies = Cookie::get();
        foreach ($cookies as $key => $value) {
            if (\starts_with($key, [$fokus . "_engine_", $fokus . "_setting_"])) {
206 207 208
                $path = \Request::path();
                $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
                Cookie::queue($key, "", 0, $cookiePath, null, false, false);
Dominik Hebeler's avatar
Dominik Hebeler committed
209 210 211 212
            }
        }

        return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings', ["fokus" => $fokus, "url" => $url])));
213
    }
214 215 216 217 218 219 220 221 222 223 224

    public function allSettingsIndex(Request $request)
    {
        $sumaFile = MetaGer::getLanguageFile();
        $sumaFile = json_decode(file_get_contents($sumaFile));

        return view('settings.allSettings')
            ->with('title', trans('titles.allSettings'))
            ->with('url', $request->input('url', ''))
            ->with('sumaFile', $sumaFile);
    }
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

    public function removeOneSetting(Request $request)
    {
        $key = $request->input('key', '');
        $path = \Request::path();
        $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
        Cookie::queue($key, "", 0, $cookiePath, null, false, false);

        return redirect($request->input('url', 'https://metager.de'));

    }

    public function removeAllSettings(Request $request)
    {
        $path = \Request::path();
        $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);

        foreach (Cookie::get() as $key => $value) {
            Cookie::queue($key, "", 0, $cookiePath, null, false, false);
        }
        return redirect($request->input('url', 'https://metager.de'));
    }
247
}