2020-12-31 16:16:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Exceptions\LegacyExitException;
|
|
|
|
use App\Exceptions\LegacyRedirectException;
|
2021-02-19 16:15:50 +01:00
|
|
|
use Illuminate\Http\Request;
|
2021-02-22 14:36:36 +01:00
|
|
|
use Illuminate\Http\Response;
|
2020-12-31 16:16:52 +01:00
|
|
|
use Illuminate\Support\Facades\Redirect;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
|
|
|
|
class LegacyController extends Controller
|
|
|
|
{
|
2021-03-22 20:45:14 +01:00
|
|
|
public function index(Request $request, $path)
|
2020-12-31 16:16:52 +01:00
|
|
|
{
|
2021-03-22 20:45:14 +01:00
|
|
|
//$path = substr($request->getPathInfo(), 1);
|
2021-02-19 16:15:50 +01:00
|
|
|
|
2021-03-15 10:43:14 +01:00
|
|
|
// Gestione dell'output
|
|
|
|
$output = self::simulate($path);
|
|
|
|
$response = response($output);
|
|
|
|
|
|
|
|
// Fix content-type per contenuti non HTML
|
|
|
|
if (ends_with($path, '.js')) {
|
|
|
|
$response = $response->header('Content-Type', 'application/javascript');
|
|
|
|
} elseif (string_contains($path, 'pdfgen.php')) {
|
|
|
|
$response = $response->header('Content-Type', 'application/pdf');
|
|
|
|
}
|
|
|
|
// Correzione header per API
|
|
|
|
elseif (self::isApiRequest($path)) {
|
2021-03-22 20:45:14 +01:00
|
|
|
$output = json_decode($output, true);
|
|
|
|
$response = $response->header('Content-Type', 'application/json')
|
|
|
|
->setStatusCode($output['status']);
|
2021-03-15 10:43:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function isApiRequest($path)
|
|
|
|
{
|
2021-02-19 16:15:50 +01:00
|
|
|
// Fix per redirect all'API
|
|
|
|
$api_request = false;
|
|
|
|
if (in_array($path, ['api', 'api/', 'api/index.php'])) {
|
|
|
|
$api_request = true;
|
|
|
|
}
|
|
|
|
|
2021-03-15 10:43:14 +01:00
|
|
|
return $api_request;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function simulate($path)
|
|
|
|
{
|
|
|
|
$base_path = base_path('legacy');
|
|
|
|
|
|
|
|
// Fix per redirect all'API
|
|
|
|
$api_request = self::isApiRequest($path);
|
|
|
|
if ($api_request) {
|
|
|
|
$path = 'api/index.php';
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:15:50 +01:00
|
|
|
// Ricerca del file interessato
|
2020-12-31 16:16:52 +01:00
|
|
|
$file = realpath($base_path.'/'.$path);
|
2021-02-19 11:54:03 +01:00
|
|
|
if (strpos($file, $base_path) === false) {
|
2020-12-31 16:16:52 +01:00
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:15:50 +01:00
|
|
|
// Inclusione diretta del file
|
2020-12-31 16:16:52 +01:00
|
|
|
ob_start();
|
|
|
|
try {
|
|
|
|
require $file;
|
2021-02-19 11:54:03 +01:00
|
|
|
} catch (LegacyExitException $e) {
|
|
|
|
} catch (LegacyRedirectException $e) {
|
2020-12-31 16:16:52 +01:00
|
|
|
return Redirect::to($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:15:50 +01:00
|
|
|
// Gestione dell'output
|
2020-12-31 16:16:52 +01:00
|
|
|
$output = ob_get_clean();
|
2021-02-19 14:39:39 +01:00
|
|
|
|
2021-03-15 10:43:14 +01:00
|
|
|
return $output;
|
2020-12-31 16:16:52 +01:00
|
|
|
}
|
|
|
|
}
|