Issues (177)

controller/EntityController.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * EntityController is responsible for redirecting requests to the /entity address.
5
 */
6
class EntityController extends Controller
7
{
8
    private function redirect303($url)
9
    {
10
        header("HTTP/1.1 303 See Other");
11
        header("Location: $url");
12
    }
13
14
    private function redirectREST($vocab, $uri, $targetFormat)
15
    {
16
        $baseurl = $this->getBaseHref();
17
        $vocid = $vocab->getId();
18
        $query = http_build_query(array('uri'=>$uri, 'format'=>$targetFormat));
19
        $url = $baseurl . "rest/v1/$vocid/data?$query";
20
        $this->redirect303($url);
21
    }
22
23
    private function redirectWeb($vocab, $uri)
24
    {
25
        $baseurl = $this->getBaseHref();
26
        $vocid = $vocab->getId();
27
        $localname = $vocab->getLocalName($uri);
28
29
        if (!$localname) {
30
            $url = $baseurl . "$vocid/";
31
        } else {
32
33
   	        if ($localname !== $uri && $localname === urlencode($localname)) {
34
   	        // the URI can be shortened
35
            $url = $baseurl . "$vocid/page/$localname";
36
            } else {
37
                // must use full URI
38
   	            $query = http_build_query(array('uri'=>$uri));
39
                $url = $baseurl . "$vocid/page/?" . $query;
40
	        }
41
        }
42
        $this->redirect303($url);
43
    }
44
45
    /**
46
     * Perform a HTTP 303 redirect to the content-negotiated URL, either the
47
     * web page for a resource or a REST API URL for retrieving its data.
48
     * @param Request $request
49
     */
50
    public function redirect($request) {
51
        /* determine parameters: URI and (optional) vocabulary */
52
        $request->setUri($request->getQueryParam('uri'));
53
        if ($request->getQueryParam('vocab')) {
54
            // vocabulary explicitly set
55
            $request->setVocab($request->getQueryParam('vocab'));
56
        } else {
57
            // guess vocabulary based on URI
58
            $vocab = $this->model->guessVocabularyFromURI($request->getUri());
59
            if ($vocab === null) {
60
                return $this->returnError('404', 'Not Found', 'Unrecognized URI ' . $request->getUri());
0 ignored issues
show
Are you sure the usage of $this->returnError('404'...' . $request->getUri()) targeting Controller::returnError() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
61
            }
62
            $request->setVocab($vocab->getId());
63
        }
64
65
        // negotiate suitable response format
66
        $restFormats = explode(' ', RestController::SUPPORTED_FORMATS);
67
        $supportedFormats = $restFormats;
68
        // add HTML as supported format (make it the first element so it becomes default)
69
        array_unshift($supportedFormats, 'text/html');
70
        // optional query parameter for forcing a specific format
71
        $requestedFormat = $request->getQueryParam('format');
72
73
        $targetFormat = $this->negotiateFormat($supportedFormats, $request->getServerConstant('HTTP_ACCEPT'), $requestedFormat);
74
75
        if (in_array($targetFormat, $restFormats)) {
76
            $this->redirectREST($request->getVocab(), $request->getUri(), $targetFormat);
77
        } else {
78
            $this->redirectWeb($request->getVocab(), $request->getUri());
79
        }
80
    }
81
}
82