Skip to content

Commit 6cdbb8f

Browse files
committed
only add host header on localhost
- adds isLocalHost method to WbstackMagnusOauth
1 parent 361e7ac commit 6cdbb8f

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

public_html/php/WbstackMagnusOauth.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ public static function getOauthParams(
158158
return $params;
159159
}
160160

161+
public static function isLocalHost(): bool {
162+
return substr($_SERVER['SERVER_NAME'], -10, 10) === '.localhost';
163+
}
164+
161165
/**
162166
* @param string $toolUrlTail Example: "/tools/widar"
163167
* @return mixed
@@ -167,7 +171,7 @@ public static function getSite(
167171
) {
168172
// XXX: this same logic is in quickstatements.php and platform api WikiController backend
169173
$domain = $_SERVER['SERVER_NAME'];
170-
if ( substr($domain,-10, 10) === '.localhost' ){
174+
if ( self::isLocalHost() ){
171175

172176
// localhost development, with a full domain prefixing .localhost
173177
// eg. wiki.addshore.com.localhost
@@ -186,10 +190,10 @@ public static function getSite(
186190
$toolRoot = $domain . $toolUrlTail;
187191

188192
// Directly for config
189-
$publicMwOAuthUrl = 'https://' . $wbRoot . '/w/index.php?title=Special:OAuth';
193+
$publicMwOAuthUrl = 'https://' . $wbRoot . '/w/index.php?title=Special:OAuth'; // TODO this could use the internal network
190194
$mwOAuthUrl = 'https://' . $wbRoot . '/w/index.php?title=Special:OAuth';
191195
$wbPublicHostAndPort = $wbRoot;
192-
$wbApi = 'https://' . $wbRoot . '/w/api.php';
196+
$wbApi = 'https://' . $wbRoot . '/w/api.php'; // TODO this could use the internal network
193197
$wbPageBase = 'https://' . $wbRoot . '/wiki/';
194198
$toolbase = 'https://' . $toolRoot;
195199
}

public_html/php/oauth.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,15 @@ function sign_request( $method, $url, $params = [] ) {
249249
*
250250
*/
251251
function setCurlHttpHeaders( $curlHandle, $headers = [] ) {
252-
$domain = $_SERVER['SERVER_NAME'];
253-
$headers[] = 'HOST: ' . $domain;
254-
curl_setopt( $curlHandle, CURLOPT_HTTPHEADER, $headers );
252+
253+
if( WbstackMagnusOauth::isLocalHost() ) {
254+
$domain = $_SERVER['SERVER_NAME'];
255+
$headers[] = 'HOST: ' . $domain;
256+
}
257+
258+
if( !empty($headers) ) {
259+
curl_setopt( $curlHandle, CURLOPT_HTTPHEADER, $headers );
260+
}
255261
}
256262

257263
/**

0 commit comments

Comments
 (0)