ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
NotAllowed.php
Go to the documentation of this file.
1 <?php
9 namespace Slim\Handlers;
10 
13 use Slim\Http\Body;
15 
23 {
35  {
36  if ($request->getMethod() === 'OPTIONS') {
37  $status = 200;
38  $contentType = 'text/plain';
39  $output = $this->renderPlainOptionsMessage($methods);
40  } else {
41  $status = 405;
42  $contentType = $this->determineContentType($request);
43  switch ($contentType) {
44  case 'application/json':
45  $output = $this->renderJsonNotAllowedMessage($methods);
46  break;
47 
48  case 'text/xml':
49  case 'application/xml':
50  $output = $this->renderXmlNotAllowedMessage($methods);
51  break;
52 
53  case 'text/html':
54  $output = $this->renderHtmlNotAllowedMessage($methods);
55  break;
56  default:
57  throw new UnexpectedValueException('Cannot render unknown content type ' . $contentType);
58  }
59  }
60 
61  $body = new Body(fopen('php://temp', 'r+'));
62  $body->write($output);
63  $allow = implode(', ', $methods);
64 
65  return $response
66  ->withStatus($status)
67  ->withHeader('Content-type', $contentType)
68  ->withHeader('Allow', $allow)
69  ->withBody($body);
70  }
71 
78  protected function renderPlainOptionsMessage($methods)
79  {
80  $allow = implode(', ', $methods);
81 
82  return 'Allowed methods: ' . $allow;
83  }
84 
91  protected function renderJsonNotAllowedMessage($methods)
92  {
93  $allow = implode(', ', $methods);
94 
95  return '{"message":"Method not allowed. Must be one of: ' . $allow . '"}';
96  }
97 
104  protected function renderXmlNotAllowedMessage($methods)
105  {
106  $allow = implode(', ', $methods);
107 
108  return "<root><message>Method not allowed. Must be one of: $allow</message></root>";
109  }
110 
117  protected function renderHtmlNotAllowedMessage($methods)
118  {
119  $allow = implode(', ', $methods);
120  $output = <<<END
121 <html>
122  <head>
123  <title>Method not allowed</title>
124  <style>
125  body{
126  margin:0;
127  padding:30px;
128  font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;
129  }
130  h1{
131  margin:0;
132  font-size:48px;
133  font-weight:normal;
134  line-height:48px;
135  }
136  </style>
137  </head>
138  <body>
139  <h1>Method not allowed</h1>
140  <p>Method not allowed. Must be one of: <strong>$allow</strong></p>
141  </body>
142 </html>
143 END;
144 
145  return $output;
146  }
147 }
getMethod()
Retrieves the HTTP method of the request.
Representation of an incoming, server-side HTTP request.
foreach($paths as $path) $request
Definition: asyncclient.php:32
renderXmlNotAllowedMessage($methods)
Render XML not allowed message.
Definition: NotAllowed.php:104
withStatus($code, $reasonPhrase='')
Return an instance with the specified status code and, optionally, reason phrase. ...
renderPlainOptionsMessage($methods)
Render PLAIN message for OPTIONS response.
Definition: NotAllowed.php:78
"color:#CC0000 style
Definition: example_001.php:92
determineContentType(ServerRequestInterface $request)
Determine which content type we know about is wanted using Accept header.
renderHtmlNotAllowedMessage($methods)
Render HTML not allowed message.
Definition: NotAllowed.php:117
$sc Method
__invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods)
Invoke error handler.
Definition: NotAllowed.php:34
Representation of an outgoing, server-side response.
font size
Definition: langcheck.php:162
renderJsonNotAllowedMessage($methods)
Render JSON not allowed message.
Definition: NotAllowed.php:91
html()
Body.
Definition: Body.php:19
if($path[strlen($path) - 1]==='/') if(is_dir($path)) if(!file_exists($path)) if(preg_match('#\.php$#D', mb_strtolower($path, 'UTF-8'))) $contentType
Definition: module.php:144
Slim Framework (https://slimframework.com)
Default Slim application not allowed handler.
Definition: NotAllowed.php:22
$response
Abstract Slim application handler.