JsonpCallback.php
935 Bytes
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
<?php
namespace App\Http\Middleware;
use Closure;
use App\Http\Output;
use App\Http\Controllers\LoginController;
use App\Http\Error;
use Config;
class JsonpCallback
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$callback = $request->input('callback', null);
$result = $next($request);
if (!$callback)
return $result;
if (strpos($request->path(), 'api/') !== 0)
return $result;
if (!preg_match('/^[_A-Za-z0-9]+$/', $callback))
return $result;
if (is_array($result)) {
$result = $callback . '(' . json_encode($result) . ')';
} else {
$result->setContent($callback . '(' . $result->content() . ')');
}
return $result;
}
}