Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
outils
question2answer
Commits
7df10de7
Commit
7df10de7
authored
Jan 17, 2019
by
Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle Method Not Allowed in router
parent
e572049e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
81 additions
and
29 deletions
+81
-29
page.php
qa-include/app/page.php
+26
-22
qa-lang-main.php
qa-include/lang/qa-lang-main.php
+1
-0
ExceptionHandler.php
qa-src/Exceptions/ExceptionHandler.php
+13
-1
MethodNotAllowedException.php
qa-src/Http/Exceptions/MethodNotAllowedException.php
+36
-0
PageNotFoundException.php
qa-src/Http/Exceptions/PageNotFoundException.php
+0
-2
Router.php
qa-src/Http/Router.php
+5
-4
No files found.
qa-include/app/page.php
View file @
7df10de7
...
...
@@ -19,8 +19,6 @@
More about this license: http://www.question2answer.org/license.php
*/
use
Q2A\Exceptions\ExceptionHandler
;
if
(
!
defined
(
'QA_VERSION'
))
{
// don't allow this page to be requested directly from browser
header
(
'Location: ../../'
);
exit
;
...
...
@@ -181,33 +179,39 @@ function qa_get_request_content()
$routing
=
qa_page_routing
();
qa_controller_routing
();
$
route
=
qa_service
(
'router'
)
->
match
(
$requestlower
)
;
$
qa_content
=
[]
;
if
(
$route
!==
null
)
{
try
{
// use new Controller system
qa_set_template
(
$route
->
getOption
(
'template'
));
$controllerClass
=
$route
->
getController
();
$ctrl
=
new
$controllerClass
();
try
{
$route
=
qa_service
(
'router'
)
->
match
(
$requestlower
);
if
(
$route
!==
null
)
{
qa_set_template
(
$route
->
getOption
(
'template'
));
$controllerClass
=
$route
->
getController
();
$ctrl
=
new
$controllerClass
();
$qa_content
=
$ctrl
->
executeAction
(
$route
->
getAction
(),
$route
->
getParameters
());
}
catch
(
Exception
$e
)
{
$qa_content
=
(
new
ExceptionHandler
())
->
handle
(
$e
);
}
}
elseif
(
isset
(
$routing
[
$requestlower
])
)
{
qa_set_template
(
$firstlower
);
$qa_content
=
require
QA_INCLUDE_DIR
.
$routing
[
$requestlower
];
}
catch
(
\Exception
$e
)
{
$qa_content
=
(
new
\Q2A\Exceptions\ExceptionHandler
)
->
handle
(
$e
);
}
}
elseif
(
isset
(
$routing
[
$firstlower
.
'/'
]))
{
qa_set_template
(
$firstlower
);
$qa_content
=
require
QA_INCLUDE_DIR
.
$routing
[
$firstlower
.
'/'
];
if
(
empty
(
$qa_content
))
{
if
(
isset
(
$routing
[
$requestlower
]))
{
qa_set_template
(
$firstlower
);
$qa_content
=
require
QA_INCLUDE_DIR
.
$routing
[
$requestlower
];
}
elseif
(
is_numeric
(
$requestparts
[
0
]))
{
qa_set_template
(
'question'
);
$qa_content
=
require
QA_INCLUDE_DIR
.
'pages/question.php'
;
}
elseif
(
isset
(
$routing
[
$firstlower
.
'/'
]))
{
qa_set_template
(
$firstlower
);
$qa_content
=
require
QA_INCLUDE_DIR
.
$routing
[
$firstlower
.
'/'
]
;
}
else
{
qa_set_template
(
strlen
(
$firstlower
)
?
$firstlower
:
'qa'
);
// will be changed later
$qa_content
=
require
QA_INCLUDE_DIR
.
'pages/default.php'
;
// handles many other pages, including custom pages and page modules
}
elseif
(
is_numeric
(
$requestparts
[
0
]))
{
qa_set_template
(
'question'
);
$qa_content
=
require
QA_INCLUDE_DIR
.
'pages/question.php'
;
}
else
{
qa_set_template
(
strlen
(
$firstlower
)
?
$firstlower
:
'qa'
);
// will be changed later
$qa_content
=
require
QA_INCLUDE_DIR
.
'pages/default.php'
;
// handles many other pages, including custom pages and page modules
}
}
if
(
$firstlower
==
'admin'
)
{
...
...
qa-include/lang/qa-lang-main.php
View file @
7df10de7
...
...
@@ -87,6 +87,7 @@ return array(
'highest_users'
=>
'Top scoring users'
,
'hot_qs_in_x'
=>
'Hot questions in ^'
,
'hot_qs_title'
=>
'Hot questions'
,
'http_status_405'
=>
'Error: 405 Method Not Allowed'
,
'image_not_read'
=>
'The image could not be read. Please upload one of: ^'
,
'image_too_big_x_pc'
=>
'This image is too big. Please scale to ^% then try again.'
,
'in_category_x'
=>
'in ^'
,
...
...
qa-src/Exceptions/ExceptionHandler.php
View file @
7df10de7
...
...
@@ -20,6 +20,7 @@ namespace Q2A\Exceptions;
use
Exception
;
use
Q2A\Http\Exceptions\PageNotFoundException
;
use
Q2A\Http\Exceptions\MethodNotAllowedException
;
class
ExceptionHandler
{
...
...
@@ -34,6 +35,8 @@ class ExceptionHandler
$this
->
handleFatalErrorException
(
$exception
);
}
elseif
(
$exception
instanceof
PageNotFoundException
)
{
return
$this
->
handlePageNotFoundException
(
$exception
);
}
elseif
(
$exception
instanceof
MethodNotAllowedException
)
{
return
$this
->
handleMethodNotAllowedException
(
$exception
);
}
elseif
(
$exception
instanceof
ErrorMessageException
)
{
return
$this
->
handleErrorMessageException
(
$exception
);
}
else
{
...
...
@@ -48,7 +51,7 @@ class ExceptionHandler
private
function
handlePageNotFoundException
(
PageNotFoundException
$exception
)
{
header
(
'HTTP/1.
0
404 Not Found'
);
header
(
'HTTP/1.
1
404 Not Found'
);
$qa_content
=
$this
->
handleErrorMessageException
(
$exception
);
$qa_content
[
'suggest_next'
]
=
qa_html_suggest_qs_tags
(
qa_using_tags
());
...
...
@@ -56,6 +59,15 @@ class ExceptionHandler
return
$qa_content
;
}
private
function
handleMethodNotAllowedException
(
MethodNotAllowedException
$exception
)
{
header
(
'HTTP/1.1 405 Method Not Allowed'
);
$qa_content
=
$this
->
handleErrorMessageException
(
$exception
);
return
$qa_content
;
}
private
function
handleErrorMessageException
(
ErrorMessageException
$exception
)
{
$qa_content
=
qa_content_prepare
();
...
...
qa-src/Http/Exceptions/MethodNotAllowedException.php
0 → 100644
View file @
7df10de7
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
namespace
Q2A\Http\Exceptions
;
use
Q2A\Exceptions\ErrorMessageException
;
class
MethodNotAllowedException
extends
ErrorMessageException
{
/**
* @param string $message
*/
public
function
__construct
(
$message
=
null
)
{
if
(
$message
===
null
)
{
$message
=
qa_lang_html
(
'main/http_status_405'
);
}
parent
::
__construct
(
$message
);
}
}
qa-src/Http/Exceptions/PageNotFoundException.php
View file @
7df10de7
...
...
@@ -23,8 +23,6 @@ use Q2A\Exceptions\ErrorMessageException;
class
PageNotFoundException
extends
ErrorMessageException
{
/**
* PageNotFoundException constructor.
*
* @param string $message
*/
public
function
__construct
(
$message
=
null
)
...
...
qa-src/Http/Router.php
View file @
7df10de7
...
...
@@ -18,6 +18,8 @@
namespace
Q2A\Http
;
use
Q2A\Http\Exceptions\MethodNotAllowedException
;
class
Router
{
/** @var Route[] */
...
...
@@ -61,12 +63,11 @@ class Router
public
function
match
(
$request
)
{
foreach
(
$this
->
routes
as
$route
)
{
if
(
$route
->
getHttpMethod
()
!==
$this
->
httpMethod
)
{
continue
;
}
$pathRegex
=
$this
->
buildPathRegex
(
$route
->
getRoutePath
());
if
(
preg_match
(
$pathRegex
,
$request
,
$matches
))
{
if
(
$route
->
getHttpMethod
()
!==
$this
->
httpMethod
)
{
throw
new
MethodNotAllowedException
;
}
$route
->
setParameters
(
array_slice
(
$matches
,
1
));
return
$route
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment