Skip to content

Commit 24c1cb0

Browse files
committed
refactor: use RedirectResponse class instead of header()
1 parent 03b8523 commit 24c1cb0

File tree

12 files changed

+64
-31
lines changed

12 files changed

+64
-31
lines changed

phpmyfaq/add.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use phpMyFAQ\Question;
2323
use phpMyFAQ\Strings;
2424
use phpMyFAQ\Translation;
25+
use Symfony\Component\HttpFoundation\RedirectResponse;
2526
use Symfony\Component\HttpFoundation\Request;
2627

2728
if (!defined('IS_VALID_PHPMYFAQ')) {
@@ -33,14 +34,14 @@
3334

3435
// Check user permissions
3536
if (-1 === $user->getUserId() && !$faqConfig->get('records.allowNewFaqsForGuests')) {
36-
header('Location:' . $faqSystem->getSystemUri($faqConfig) . '?action=login');
37-
exit;
37+
$redirect = new RedirectResponse($faqSystem->getSystemUri($faqConfig) . '?action=login');
38+
$redirect->send();
3839
}
3940

4041
// Check permission to add new faqs
4142
if (-1 !== $user->getUserId() && !$user->perm->hasPermission($user->getUserId(), 'addfaq')) {
42-
header('Location:' . $faqSystem->getSystemUri($faqConfig));
43-
exit;
43+
$redirect = new RedirectResponse($faqSystem->getSystemUri($faqConfig));
44+
$redirect->send();
4445
}
4546

4647
$captcha = Captcha::getInstance($faqConfig);

phpmyfaq/ask.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use phpMyFAQ\Helper\CategoryHelper as HelperCategory;
2222
use phpMyFAQ\Strings;
2323
use phpMyFAQ\Translation;
24-
use phpMyFAQ\User\CurrentUser;
24+
use Symfony\Component\HttpFoundation\RedirectResponse;
2525
use Symfony\Component\HttpFoundation\Request;
2626

2727
if (!defined('IS_VALID_PHPMYFAQ')) {
@@ -31,7 +31,8 @@
3131

3232
// Check user permissions
3333
if ((-1 === $user->getUserId() && !$faqConfig->get('records.allowQuestionsForGuests'))) {
34-
header('Location:' . $faqSystem->getSystemUri($faqConfig) . '?action=login');
34+
$redirect = new RedirectResponse($faqSystem->getSystemUri($faqConfig) . '?action=login');
35+
$redirect->send();
3536
}
3637

3738
$request = Request::createFromGlobals();

phpmyfaq/index.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,11 @@
201201
$action = 'main';
202202
$ssoLogout = $faqConfig->get('security.ssoLogoutRedirect');
203203
if ($faqConfig->get('security.ssoSupport') && !empty($ssoLogout)) {
204-
header('Location: ' . $ssoLogout);
204+
$redirect = new RedirectResponse($ssoLogout);
205205
} else {
206-
header('Location: ' . $faqConfig->getDefaultUrl());
206+
$redirect = new RedirectResponse($faqConfig->getDefaultUrl());
207207
}
208+
$redirect->send();
208209
}
209210

210211
//

phpmyfaq/register.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use phpMyFAQ\Captcha\Captcha;
2020
use phpMyFAQ\Captcha\Helper\CaptchaHelper;
2121
use phpMyFAQ\Translation;
22+
use Symfony\Component\HttpFoundation\RedirectResponse;
2223
use Symfony\Component\HttpFoundation\Request;
2324

2425
if (!defined('IS_VALID_PHPMYFAQ')) {
@@ -29,8 +30,8 @@
2930
$request = Request::createFromGlobals();
3031

3132
if (!$faqConfig->get('security.enableRegistration')) {
32-
header('Location:' . $faqSystem->getSystemUri($faqConfig));
33-
exit();
33+
$redirect = new RedirectResponse($faqSystem->getSystemUri($faqConfig));
34+
$redirect->send();
3435
}
3536

3637
try {

phpmyfaq/services/twitter/callback.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use Abraham\TwitterOAuth\TwitterOAuth;
2020
use phpMyFAQ\Filter;
21+
use Symfony\Component\HttpFoundation\RedirectResponse;
2122

2223
//
2324
// Prepend and start the PHP session
@@ -42,8 +43,8 @@
4243

4344
if (isset($oAuthToken) && $requestToken['oauth_token'] !== $oAuthToken) {
4445
$_SESSION['oauth_status'] = 'oldtoken';
45-
header('Location: ./clearsessions.php');
46-
exit;
46+
$redirect = new RedirectResponse('./clearsessions.php');
47+
$redirect->send();
4748
}
4849

4950
$connection = new TwitterOAuth(
@@ -61,7 +62,9 @@
6162
$_SESSION['access_token'] = $accessToken;
6263
$_SESSION['status'] = 'verified';
6364

64-
header('Location: ./index.php');
65+
$redirect = new RedirectResponse('./index.php');
66+
$redirect->send();
6567
} else {
66-
header('Location: ./clearsessions.php');
68+
$redirect = new RedirectResponse('./clearsessions.php');
69+
$redirect->send();
6770
}

phpmyfaq/services/twitter/clearsessions.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
88
* obtain one at https://mozilla.org/MPL/2.0/.
99
*
10-
* @package phpMyFAQ
11-
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
10+
* @package phpMyFAQ
11+
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
1212
* @copyright 2010-2023 phpMyFAQ Team
13-
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14-
* @link https://www.phpmyfaq.de
15-
* @since 2010-09-18
13+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
* @link https://www.phpmyfaq.de
15+
* @since 2010-09-18
1616
*/
1717

18+
use Symfony\Component\HttpFoundation\RedirectResponse;
19+
1820
//
1921
// Prepend and start the PHP session
2022
//
@@ -27,5 +29,7 @@
2729
require PMF_ROOT_DIR . '/src/Bootstrap.php';
2830

2931
session_destroy();
32+
session_start();
3033

31-
header('Location: ./connect.php');
34+
$redirect = new RedirectResponse('./connect.php');
35+
$redirect->send();

phpmyfaq/services/twitter/index.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
use Abraham\TwitterOAuth\TwitterOAuth;
2020
use Abraham\TwitterOAuth\TwitterOAuthException;
21+
use phpMyFAQ\Configuration;
22+
use Symfony\Component\HttpFoundation\RedirectResponse;
2123

2224
//
2325
// Prepend and start the PHP session
@@ -30,12 +32,15 @@
3032
//
3133
require PMF_ROOT_DIR . '/src/Bootstrap.php';
3234

35+
$faqConfig = Configuration::getConfigurationInstance();
36+
3337
if (
3438
empty($_SESSION['access_token']) ||
3539
empty($_SESSION['access_token']['oauth_token']) ||
3640
empty($_SESSION['access_token']['oauth_token_secret'])
3741
) {
38-
header('Location: ./clearsessions.php');
42+
$redirect = new RedirectResponse('./clearsessions.php');
43+
$redirect->send();
3944
}
4045

4146
$accessToken = $_SESSION['access_token'];
@@ -55,5 +60,6 @@
5560
$content = $connection->get('account/verify_credentials');
5661

5762
if (isset($content->screen_name)) {
58-
header('Location: ../../admin/index.php');
63+
$redirect = new RedirectResponse('../../admin/index.php');
64+
$redirect->send();
5965
}

phpmyfaq/services/twitter/redirect.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818

1919
use Abraham\TwitterOAuth\TwitterOAuth;
20+
use phpMyFAQ\Configuration;
21+
use Symfony\Component\HttpFoundation\RedirectResponse;
2022

2123
//
2224
// Prepend and start the PHP session
@@ -29,6 +31,8 @@
2931
//
3032
require PMF_ROOT_DIR . '/src/Bootstrap.php';
3133

34+
$faqConfig = Configuration::getConfigurationInstance();
35+
3236
$connection = new TwitterOAuth(
3337
$faqConfig->get('socialnetworks.twitterConsumerKey'),
3438
$faqConfig->get('socialnetworks.twitterConsumerSecret')
@@ -45,7 +49,8 @@
4549
switch ($connection->getLastHttpCode()) {
4650
case 200:
4751
$url = $connection->url('oauth/authorize', ['oauth_token' => $requestToken['oauth_token']]);
48-
header('Location: ' . $url);
52+
$redirect = new RedirectResponse($url);
53+
$redirect->send();
4954
break;
5055
default:
5156
echo 'Could not connect to Twitter. Refresh the page or try again later.';

phpmyfaq/setup/update.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use phpMyFAQ\Setup\Update;
2828
use phpMyFAQ\Strings;
2929
use phpMyFAQ\System;
30+
use Symfony\Component\HttpFoundation\RedirectResponse;
3031

3132
const COPYRIGHT = '&copy; 2001-2023 <a target="_blank" href="//www.phpmyfaq.de/">phpMyFAQ Team</a>';
3233
const IS_VALID_PHPMYFAQ = null;
@@ -48,8 +49,8 @@
4849
$query = [];
4950

5051
if (!file_exists(PMF_ROOT_DIR . '/config/database.php')) {
51-
header('Location: index.php');
52-
exit();
52+
$redirect = new RedirectResponse('./index.php');
53+
$redirect->send();
5354
}
5455

5556
$dbConfig = new DatabaseConfiguration(PMF_ROOT_DIR . '/config/database.php');

phpmyfaq/src/Bootstrap.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use phpMyFAQ\Database;
2525
use phpMyFAQ\Core\Exception;
2626
use phpMyFAQ\Init;
27+
use Symfony\Component\HttpFoundation\RedirectResponse;
2728
use Symfony\Component\HttpFoundation\Request;
2829

2930
//
@@ -92,8 +93,8 @@
9293
// Check if config/database.php exist -> if not, redirect to installer
9394
//
9495
if (!file_exists(PMF_CONFIG_DIR . '/database.php')) {
95-
header('Location: ./setup/index.php');
96-
exit();
96+
$redirect = new RedirectResponse('./setup/index.php');
97+
$redirect->send();
9798
}
9899

99100
//

phpmyfaq/src/phpMyFAQ/Auth/AuthAzureActiveDirectory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public function isValidLogin(string $login, array $optionalData = []): int
118118
/**
119119
* Method to authorize against Azure AD
120120
*
121+
* @throws \Exception
121122
*/
122123
public function authorize(): void
123124
{
@@ -156,12 +157,16 @@ public function logout(): void
156157

157158
$user->getUserByLogin($user->getLogin());
158159
$user->deleteFromSession(true);
159-
header('Location: ' . self::AAD_LOGOUT_URL);
160+
161+
$redirect = new RedirectResponse(self::AAD_LOGOUT_URL);
162+
$redirect->send();
160163
}
161164

162165
/**
163166
* Method to generate code verifier and code challenge for oAuth login.
164167
* See RFC7636 for details.
168+
*
169+
* @throws \Exception
165170
*/
166171
private function createOAuthChallenge(): void
167172
{

phpmyfaq/ucp.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@
1515
* @since 2012-01-12
1616
*/
1717

18+
use phpMyFAQ\Configuration;
1819
use phpMyFAQ\Services\Gravatar;
1920
use phpMyFAQ\Session\Token;
2021
use phpMyFAQ\Strings;
2122
use phpMyFAQ\Translation;
2223
use phpMyFAQ\User\CurrentUser;
2324
use phpMyFAQ\User\TwoFactor;
2425
use RobThree\Auth\TwoFactorAuthException;
26+
use Symfony\Component\HttpFoundation\RedirectResponse;
2527

2628
if (!defined('IS_VALID_PHPMYFAQ')) {
2729
http_response_code(400);
2830
exit();
2931
}
3032

33+
$faqConfig = Configuration::getConfigurationInstance();
34+
3135
if ($user->isLoggedIn()) {
3236
try {
3337
$faqSession->userTracking('user_control_panel', $user->getUserId());
@@ -97,7 +101,7 @@
97101
]
98102
);
99103
} else {
100-
// Redirect to login
101-
header('Location: ' . $faqConfig->getDefaultUrl());
102-
exit();
104+
// Redirect to login page
105+
$redirect = new RedirectResponse($faqConfig->getDefaultUrl());
106+
$redirect->send();
103107
}

0 commit comments

Comments
 (0)