webapp/app/Http/Controllers/LinksController.php

34 lines
941 B
PHP

<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Laravel\Lumen\Routing\Controller as BaseController;
class LinksController extends BaseController
{
public function add(Request $request)
{
/** @var User $user */
$user = $request->user()->first();
$email = $user->email;
$link = $request->input('u');
$link = filter_var($link, FILTER_VALIDATE_URL);
if ($link === false) {
return response()->json(['error' => 'Invalid link.'], 422);
}
$title = $request->input('t') ?: '';
$email_sent = mail($email, 'New link : '.$title, $link);
if ($email_sent) {
return response()->json(['info' => 'Email sent.']);
}
return response()->json(['error' => 'An error occured while sending email'], 500);
}
}