MailController.php 11.1 KB
Newer Older
1
2
3
4
<?php

namespace App\Http\Controllers;

5
6
use App\Mail\Kontakt;
use App\Mail\Sprachdatei;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response;
9
use LaravelLocalization;
10
use Mail;
Dominik Hebeler's avatar
Dominik Hebeler committed
11
use Log;
12
use Validator;
Dominik Hebeler's avatar
Dominik Hebeler committed
13
14
use \IBAN;
use \IBANCountry;
15
16
17
18
19
20
21
22
23

class MailController extends Controller
{
    /**
     * Load Startpage accordingly to the given URL-Parameter and Mobile
     *
     * @param  int  $id
     * @return Response
     */
Dominik Hebeler's avatar
Dominik Hebeler committed
24
25
    public function contactMail(Request $request)
    {
26
        # Nachricht, die wir an den Nutzer weiterleiten:
Dominik Hebeler's avatar
Dominik Hebeler committed
27
        $messageType = ""; # [success|error]
28
        $returnMessage = '';
29
30
31
32

        # Wir benötigen 3 Felder von dem Benutzer wenn diese nicht übermittelt wurden, oder nicht korrekt sind geben wir einen Error zurück
        $validator = Validator::make(
            [
Dominik Hebeler's avatar
Dominik Hebeler committed
33
                'email' => $request->input('email'),
Dominik Hebeler's avatar
Dominik Hebeler committed
34
                'pcsrf' => $request->input('pcsrf'),
35
36
            ],
            [
Dominik Hebeler's avatar
Dominik Hebeler committed
37
                'email' => 'required|email',
Dominik Hebeler's avatar
Dominik Hebeler committed
38
                'pcsrf' => ['required', 'string', new \App\Rules\PCSRF],
39
40
41
            ]
        );

Dominik Hebeler's avatar
Dominik Hebeler committed
42
        if ($validator->fails()) {
43
44
45
46
47
            return view('kontakt.kontakt')->with('formerrors', $validator)->with('title', trans('titles.kontakt'))->with('navbarFocus', 'kontakt');
        }

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

Dominik Hebeler's avatar
Dominik Hebeler committed
48
        $replyTo = $request->input('email', 'noreply@metager.de');
Dominik Hebeler's avatar
Dominik Hebeler committed
49
        if ($replyTo === "") {
50
            $replyTo = "noreply@metager.de";
Dominik Hebeler's avatar
Dominik Hebeler committed
51
        } else {
52
53
54
            $replyTo = $request->input('email');
        }

Dominik Hebeler's avatar
Dominik Hebeler committed
55
        if (!$request->filled('message') || !$request->filled('subject')) {
Dominik Hebeler's avatar
Dominik Hebeler committed
56
            $messageType = "error";
57
            $returnMessage = trans('kontakt.error.1');
Dominik Hebeler's avatar
Dominik Hebeler committed
58
        } else {
59
            # Wir versenden die Mail des Benutzers an uns:
60
            $mailto = "support@metager.org";
Dominik Hebeler's avatar
Dominik Hebeler committed
61
            if (LaravelLocalization::getCurrentLocale() === "de") {
62
63
                $mailto = "support@suma-ev.de";
            }
64
            $message = $request->input('message');
65
            $subject = $request->input('subject');
66
            Mail::to($mailto)
67
                ->send(new Kontakt($name, $replyTo, $subject, $message));
68

69
            $returnMessage = trans('kontakt.success.1');
Dominik Hebeler's avatar
Dominik Hebeler committed
70
            $messageType = "success";
71
72
73
        }

        return view('kontakt.kontakt')
Dominik Hebeler's avatar
Dominik Hebeler committed
74
            ->with('title', 'Kontakt')
75
            ->with('js', ['lib.js'])
Dominik Hebeler's avatar
Dominik Hebeler committed
76
            ->with($messageType, $returnMessage);
77
    }
Dominik Hebeler's avatar
Dominik Hebeler committed
78
79
80

    public function donation(Request $request)
    {
Davide Aprea's avatar
Davide Aprea committed
81
82
83
84
85
86
87
88
89
90
91
92
        $name = '';
        if($request->input('person') === 'private') {
            $firstname = $request->input('firstname');
            $lastname = $request->input('lastname');
            if($firstname !== '' || $lastname !== '') {
                $name = $firstname . ' ' . $lastname;
            }
        } elseif($request->input('person') === 'company') {
            $company = $request->input('companyname');
            $name = $company;
        }

Dominik Hebeler's avatar
Dominik Hebeler committed
93
        $data = [
Davide Aprea's avatar
Davide Aprea committed
94
            'name' => $name,
Dominik Hebeler's avatar
Dominik Hebeler committed
95
96
97
            'iban' => $request->input('iban', ''),
            'bic' => $request->input('bic', ''),
            'email' => $request->input('email', ''),
98
            'betrag' => $request->input('amount', ''),
Phil Höfer's avatar
Phil Höfer committed
99
            'frequency' => $request->input('frequency', ''),
Dominik Hebeler's avatar
Dominik Hebeler committed
100
101
            'nachricht' => $request->input('Nachricht', ''),
        ];
Davide Aprea's avatar
Davide Aprea committed
102
    
Dominik Hebeler's avatar
Dominik Hebeler committed
103
104
105
        $iban = $request->input('iban', '');
        $bic = $request->input('bic', '');
        $email = $request->input('email', '');
106
        $frequency = $request->input('frequency', '');
Phil Höfer's avatar
Phil Höfer committed
107
        $betrag = $request->input('amount', '');
Dominik Hebeler's avatar
Dominik Hebeler committed
108
        $nachricht = $request->input('Nachricht', '');
109

Phil Höfer's avatar
Phil Höfer committed
110
        # Allow custom amounts
111
        if ($betrag == "custom" && $request->filled('custom-amount')) {
Phil Höfer's avatar
Phil Höfer committed
112
113
114
115
            $betrag = $request->input('custom-amount', '');
            $data['betrag'] = $betrag;
        }

Dominik Hebeler's avatar
Dominik Hebeler committed
116
117
118
119
120
121
122
123
124
        # Check for valid frequency
        $validFrequencies = [
            "once",
            "monthly",
            "quarterly",
            "six-monthly",
            "annual",
        ];

Dominik Hebeler's avatar
Dominik Hebeler committed
125
126
        # Der enthaltene String wird dem Benutzer nach der Spende ausgegeben
        $messageToUser = "";
Dominik Hebeler's avatar
Dominik Hebeler committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
        $messageType = ""; # [success|error]

        # Check the IBAN
        $iban = new IBAN($iban);
        $bic = $request->input('Bankleitzahl', '');
        $country = new IBANCountry($iban->Country());
        $isSEPA = filter_var($country->IsSEPA(), FILTER_VALIDATE_BOOLEAN);

        # Check the amount
        $validBetrag = is_numeric($betrag) && $betrag > 0;

        # Validate Email
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $email = "anonymous@suma-ev.de";
        }
Davide Aprea's avatar
Davide Aprea committed
142
143
144
145
        if($name === ''){
            $messageToUser = trans('spende.error.name');
            $messageType = "error";
        } elseif (!$iban->Verify()) {
Dominik Hebeler's avatar
Dominik Hebeler committed
146
            $messageToUser = trans('spende.error.iban');
Dominik Hebeler's avatar
Dominik Hebeler committed
147
            $messageType = "error";
148
        } elseif (!$isSEPA && $bic === '') {
Dominik Hebeler's avatar
Dominik Hebeler committed
149
            $messageToUser = trans('spende.error.bic');
Dominik Hebeler's avatar
Dominik Hebeler committed
150
            $messageType = "error";
151
        } elseif (!$validBetrag) {
Dominik Hebeler's avatar
Dominik Hebeler committed
152
153
154
155
            $messageToUser = trans('spende.error.amount');
            $messageType = "error";
        } elseif (!in_array($frequency, $validFrequencies)) {
            $messageToUser = trans('spende.error.frequency');
Dominik Hebeler's avatar
Dominik Hebeler committed
156
            $messageType = "error";
Dominik Hebeler's avatar
Dominik Hebeler committed
157
        } else {
Dominik Hebeler's avatar
Dominik Hebeler committed
158

Dominik Hebeler's avatar
Dominik Hebeler committed
159
160
161
            # The value has to have a maximum of 2 decimal digits
            $betrag = round($betrag, 2, PHP_ROUND_HALF_DOWN);

Davide Aprea's avatar
Davide Aprea committed
162
163
164
            # Generating personalised key for donor
            $key = app('App\Models\Key')->generateKey($betrag);

165
166
167
168
169
170
171
            # Folgende Felder werden vom Spendenformular als Input übergeben:
            # Name
            # Telefon
            # email
            # Kontonummer ( IBAN )
            # Bankleitzahl ( BIC )
            # Nachricht
Dominik Hebeler's avatar
Dominik Hebeler committed
172
173
174
175
176
177
178
179

            $message = "\r\nName: " . $name;
            $message .= "\r\nIBAN: " . $iban->HumanFormat();
            if ($bic !== "") {
                $message .= "\r\nBIC: " . $bic;
            }

            $message .= "\r\nBetrag: " . $betrag;
Dominik Hebeler's avatar
Dominik Hebeler committed
180
            $message .= "\r\nHäufigkeit: " . trans('spende.frequency.' . $frequency);
Dominik Hebeler's avatar
Dominik Hebeler committed
181
182
            $message .= "\r\nNachricht: " . $nachricht;

Davide Aprea's avatar
Davide Aprea committed
183
184
185
186
            if($key){
                $message .= "\r\nSchlüssel:" . $key;
            }

Dominik Hebeler's avatar
Dominik Hebeler committed
187
188
189
190
191
192
193
            try {
                Mail::to("spenden@suma-ev.de")
                    ->send(new \App\Mail\Spende($email, $message, $name));

                $messageType = "success";
                $messageToUser = "Herzlichen Dank!! Wir haben Ihre Spendenbenachrichtigung erhalten.";
            } catch (\Swift_TransportException $e) {
Dominik Hebeler's avatar
Dominik Hebeler committed
194
                Log::error($e->getMessage());
Dominik Hebeler's avatar
Dominik Hebeler committed
195
196
                $messageType = "error";
                $messageToUser = 'Beim Senden Ihrer Spendenbenachrichtigung ist ein Fehler auf unserer Seite aufgetreten. Bitte schicken Sie eine E-Mail an: office@suma-ev.de, damit wir uns darum kümmern können.';
Dominik Hebeler's avatar
Dominik Hebeler committed
197
198
199
            }
        }

200
        if ($messageType === "error") {
201
            return view('spende.spende')
202
                ->with('title', 'Kontakt')
Dominik Hebeler's avatar
Dominik Hebeler committed
203
204
                ->with($messageType, $messageToUser)
                ->with('data', $data);
205
        } else {
Dominik Hebeler's avatar
Dominik Hebeler committed
206
            $data['iban'] = $iban->HumanFormat();
Davide Aprea's avatar
Davide Aprea committed
207
            $data['key'] = $key;
208
            $data = base64_encode(serialize($data));
209
            return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route("danke", ['data' => $data])));
210
        }
Dominik Hebeler's avatar
Dominik Hebeler committed
211
    }
212

213
    #Ueberprueft ob ein bereits vorhandener Eintrag bearbeitet worden ist
214
    public static function isEdited($k, $v, $filename)
215
    {
216
        try {
Dominik Hebeler's avatar
Dominik Hebeler committed
217
            $temp = include resource_path() . "/" . $filename;
218
            foreach ($temp as $key => $value) {
Dominik Hebeler's avatar
Dominik Hebeler committed
219
                if ($k === $key && $v !== $value) {
220
221
                    return true;
                }
Dominik Hebeler's avatar
Dominik Hebeler committed
222
            }
223
224
225
        } catch (\ErrorException $e) {
            #Datei existiert noch nicht
            return true;
226
227
228
229
        }
        return false;
    }

Dominik Hebeler's avatar
Dominik Hebeler committed
230
    public function sendLanguageFile(Request $request, $from, $to, $exclude = "", $email = "")
231
232
233
234
    {
        $filename = $request->input('filename');
        # Wir erstellen nun zunächst den Inhalt der Datei:
        $data = [];
Dominik Hebeler's avatar
Dominik Hebeler committed
235
        $new = 0;
236
        $emailAddress = "";
237
        $editedKeys = "";
238
239
240
241
        foreach ($request->all() as $key => $value) {
            if ($key === "filename" || $value === "") {
                continue;
            }
Dominik Hebeler's avatar
Dominik Hebeler committed
242
            if ($key === "email") {
243
244
245
                $emailAddress = $value;
                continue;
            }
246
            $key = base64_decode($key);
247
248
249
            if (strpos($key, "_new_") === 0 && $value !== "") {
                $new++;
                $key = substr($key, strpos($key, "_new_") + 5);
Dominik Hebeler's avatar
Dominik Hebeler committed
250
                $editedKeys = $editedKeys . "\n" . $key;
251
            } elseif ($this->isEdited($key, $value, $filename)) {
252
                $new++;
Dominik Hebeler's avatar
Dominik Hebeler committed
253
                $editedKeys = $editedKeys . "\n" . $key;
254
            }
255

256
257
258
259
260
261
262
263
264
265
266
267
268
            $key = trim($key);
            if (!strpos($key, "#")) {
                $data[$key] = $value;
            } else {
                $ref = &$data;
                do {
                    $ref = &$ref[substr($key, 0, strpos($key, "#"))];
                    $key = substr($key, strpos($key, "#") + 1);
                } while (strpos($key, "#"));
                $ref = &$ref[$key];
                $ref = $value;
            }
        }
269

270
        $output = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
271
272
273
        $output = preg_replace("/\{/si", "[", $output);
        $output = preg_replace("/\}/si", "]", $output);
        $output = preg_replace("/\": ([\"\[])/si", "\"\t=>\t$1", $output);
Dominik Hebeler's avatar
Dominik Hebeler committed
274

275
276
        $output = "<?php\n\nreturn $output;\n";

277
278
        $message = "Moin moin,\n\nein Benutzer hat eine Sprachdatei aktualisiert.\nBearbeitet wurden die Einträge: $editedKeys\n\nSollten die Texte so in Ordnung sein, ersetzt, oder erstellt die Datei aus dem Anhang in folgendem Pfad:\n$filename\n\nFolgend zusätzlich der Inhalt der Datei:\n\n$output";

279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
        # Wir haben nun eine Mail an uns geschickt, welche die entsprechende Datei beinhaltet.
        # Nun müssen wir den Nutzer eigentlich nur noch zurück leiten und die Letzte bearbeitete Datei ausschließen:
        $ex = [];
        if ($exclude !== "") {
            try {
                $ex = unserialize(base64_decode($exclude));
            } catch (\ErrorException $e) {
                $ex = [];
            }

            if (!isset($ex["files"])) {
                $ex["files"] = [];
            }
        }
        if (!isset($ex["new"])) {
            $ex["new"] = 0;
        }
        $ex['files'][] = basename($filename);
        $ex["new"] += $new;
298
299

        if ($new > 0) {
Dominik Hebeler's avatar
Dominik Hebeler committed
300
            if ($emailAddress !== "") {
Aria Givi's avatar
Aria Givi committed
301
                Mail::to("dev@suma-ev.de")
Dominik Hebeler's avatar
Dominik Hebeler committed
302
                    ->send(new Sprachdatei($message, $output, basename($filename), $emailAddress));
303
            } else {
Aria Givi's avatar
Aria Givi committed
304
                Mail::to("dev@suma-ev.de")
Dominik Hebeler's avatar
Dominik Hebeler committed
305
                    ->send(new Sprachdatei($message, $output, basename($filename)));
306
            }
307
        }
308
        $ex = base64_encode(serialize($ex));
309
310

        return redirect(url('languages/edit', ['from' => $from, 'to' => $to, 'exclude' => $ex, 'email' => $emailAddress]));
311
    }
Dominik Hebeler's avatar
Dominik Hebeler committed
312
}