1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\PODataLaravel\Controllers; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Http\Response; |
8
|
|
|
use AlgoWeb\PODataLaravel\Controllers\Controller as BaseController; |
9
|
|
|
use Illuminate\Support\Facades\App; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
use POData\OperationContext\ServiceHost as ServiceHost; |
12
|
|
|
use POData\SimpleDataService as DataService; |
13
|
|
|
use POData\OperationContext\Web\Illuminate\IlluminateOperationContext as OperationContextAdapter; |
14
|
|
|
use voku\helper\AntiXSS; |
15
|
|
|
|
16
|
|
|
class ODataController extends BaseController |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Display a listing of the resource. |
20
|
|
|
* |
21
|
|
|
* @return \Illuminate\Http\Response |
22
|
|
|
*/ |
23
|
|
|
public function index(Request $request, $dump = false) |
24
|
|
|
{ |
25
|
|
|
$dump = $dump || $this->getIsDumping(); |
26
|
|
|
//$antiXss = new AntiXSS(); |
|
|
|
|
27
|
|
|
$op = new OperationContextAdapter($request); |
28
|
|
|
$host = new ServiceHost($op, $request); |
29
|
|
|
$host->setServiceUri("/odata.svc/"); |
30
|
|
|
|
31
|
|
|
$query = App::make('odataquery'); |
32
|
|
|
$meta = App::make('metadata'); |
33
|
|
|
|
34
|
|
|
$service = new DataService($query, $meta, $host); |
35
|
|
|
$service->handleRequest(); |
36
|
|
|
|
37
|
|
|
$odataResponse = $op->outgoingResponse(); |
38
|
|
|
|
39
|
|
|
if (true === $dump) { |
40
|
|
|
// iff XTest header is set, containing class and method name |
41
|
|
|
// dump outgoing odataResponse, metadata, and incoming request |
42
|
|
|
$xTest = $request->header('XTest'); |
43
|
|
|
$date = Carbon::now(0); |
44
|
|
|
$timeString = $date->toTimeString(); |
45
|
|
|
$xTest = (null !== $xTest) ? $xTest |
46
|
|
|
: $request->method() . ";" . str_replace("/", "-", $request->path()) . ";" . $timeString . ";"; |
47
|
|
|
if (null != $xTest) { |
48
|
|
|
$reflectionClass = new \ReflectionClass('Illuminate\Http\Request'); |
49
|
|
|
$reflectionProperty = $reflectionClass->getProperty('userResolver'); |
50
|
|
|
$reflectionProperty->setAccessible(true); |
51
|
|
|
$reflectionProperty->setValue($request, null); |
52
|
|
|
$reflectionProperty = $reflectionClass->getProperty('routeResolver'); |
53
|
|
|
$reflectionProperty->setAccessible(true); |
54
|
|
|
$reflectionProperty->setValue($request, null); |
55
|
|
|
$cerealRequest = serialize($request); |
56
|
|
|
$cerealMeta = serialize($meta); |
57
|
|
|
$cerealResponse = serialize($odataResponse); |
58
|
|
|
Storage::put($xTest.'request', $cerealRequest); |
59
|
|
|
Storage::put($xTest.'metadata', $cerealMeta); |
60
|
|
|
Storage::put($xTest.'response', $cerealResponse); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$content = $odataResponse->getStream(); |
65
|
|
|
//$content = $antiXss->xss_clean($content); |
|
|
|
|
66
|
|
|
$headers = $odataResponse->getHeaders(); |
67
|
|
|
$responseCode = $headers[\POData\Common\ODataConstants::HTTPRESPONSE_HEADER_STATUS_CODE]; |
68
|
|
|
$responseCode = isset($responseCode) ? intval($responseCode) : 200; |
69
|
|
|
$response = new Response($content, $responseCode); |
|
|
|
|
70
|
|
|
$response->setStatusCode($headers["Status"]); |
71
|
|
|
|
72
|
|
|
foreach ($headers as $headerName => $headerValue) { |
73
|
|
|
if (!is_null($headerValue)) { |
74
|
|
|
$response->headers->set($headerName, $headerValue); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return $response; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
protected function getIsDumping() |
84
|
|
|
{ |
85
|
|
|
return true === env('APP_DUMP_REQUESTS', false); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.