Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the use of localhost workarounds #17

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions classes/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,6 @@ public function getMultipleURLsInParallel ( $urls , $batch_size = 50 ) {
curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch[$key], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch[$key], CURLOPT_SSL_VERIFYHOST, false);

WbstackMagnusOauth::setCurlHttpHeaders( $ch[$key] );

curl_multi_add_handle($mh,$ch[$key]);
}

Expand Down
59 changes: 9 additions & 50 deletions public_html/php/WbstackMagnusOauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ public static function getOauthParams(
return $params;
}

public static function isLocalHost(): bool {
return substr($_SERVER['SERVER_NAME'], -10, 10) === '.localhost';
}

/**
* @param string $toolUrlTail Example: "/tools/widar"
* @return mixed
Expand All @@ -171,34 +167,15 @@ public static function getSite(
) {
// XXX: this same logic is in quickstatements.php and platform api WikiController backend
$domain = $_SERVER['SERVER_NAME'];
if ( self::isLocalHost() ){

// localhost development, with a full domain prefixing .localhost
// eg. wiki.addshore.com.localhost
$wbRoot = $domain;
$toolRoot = 'http://' . $domain . $toolUrlTail;

// Directly for config
$publicMwOAuthUrl = 'http://' . $domain . '/w/index.php?title=Special:OAuth';
$mwOAuthUrl = 'http://' . self::platformIngressHostAndPort . '/w/index.php?title=Special:OAuth';
$wbPublicHostAndPort = $wbRoot;
$wbApi = 'http://' . self::platformIngressHostAndPort . '/w/api.php';
$wbPageBase = $wbRoot . '/wiki/';
$toolbase = $toolRoot;
$entityBase = 'http://' . $wbRoot . '/entity/';
} else {
$wbRoot = $domain;
$toolRoot = $domain . $toolUrlTail;

// Directly for config
$publicMwOAuthUrl = 'https://' . $wbRoot . '/w/index.php?title=Special:OAuth'; // TODO this could use the internal network
$mwOAuthUrl = 'https://' . $wbRoot . '/w/index.php?title=Special:OAuth';
$wbPublicHostAndPort = $wbRoot;
$wbApi = 'https://' . $wbRoot . '/w/api.php'; // TODO this could use the internal network
$wbPageBase = 'https://' . $wbRoot . '/wiki/';
$toolbase = 'https://' . $toolRoot;
$entityBase = 'https://' . $wbRoot . '/entity/';
}
$wbRoot = $domain;
$toolRoot = $domain . $toolUrlTail;
$publicMwOAuthUrl = 'https://' . $wbRoot . '/w/index.php?title=Special:OAuth'; // TODO this could use the internal network
$mwOAuthUrl = 'https://' . $wbRoot . '/w/index.php?title=Special:OAuth';
$wbPublicHostAndPort = $wbRoot;
$wbApi = 'https://' . $wbRoot . '/w/api.php'; // TODO this could use the internal network
$wbPageBase = 'https://' . $wbRoot . '/wiki/';
$toolbase = 'https://' . $toolRoot;
$entityBase = 'https://' . $wbRoot . '/entity/';

$site = [
'oauth' => [
Expand All @@ -223,22 +200,4 @@ public static function getSite(
$site = json_decode(json_encode($site));
return $site;
}

/**
* Set the HTTP Headers for the curl handle
*
* Sets the HOST parameter when internally talking to wbstack platform ingress
*
*/
public static function setCurlHttpHeaders( $curlHandle, $headers = [] ) {

if( WbstackMagnusOauth::isLocalHost() ) {
$domain = $_SERVER['SERVER_NAME'];
$headers[] = 'HOST: ' . $domain;
}

if( !empty($headers) ) {
curl_setopt( $curlHandle, CURLOPT_HTTPHEADER, $headers );
}
}
}
8 changes: 1 addition & 7 deletions public_html/php/oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ function fetchAccessToken() {
curl_setopt( $ch, CURLOPT_URL, $url );
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->gUserAgent );

curl_setopt( $ch, CURLOPT_HEADER, 0 );
WbstackMagnusOauth::setCurlHttpHeaders( $ch );

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$data = curl_exec( $ch );

Expand Down Expand Up @@ -285,7 +282,6 @@ function doAuthorizationRedirect($callback='') {
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->gUserAgent );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
WbstackMagnusOauth::setCurlHttpHeaders( $ch );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$data = curl_exec( $ch );
if ( !$data ) {
Expand Down Expand Up @@ -503,12 +499,10 @@ function doApiQuery( $post, &$ch = null , $mode = '' , $iterations_left = 5 , $l
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_fields );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [ $header ] );
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->gUserAgent );

curl_setopt( $ch, CURLOPT_HEADER, 0 );
WbstackMagnusOauth::setCurlHttpHeaders( $ch, [ $header ] );

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );

$data = curl_exec( $ch );
Expand Down
3 changes: 0 additions & 3 deletions public_html/php/wikidata.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ protected function getMultipleURLsInParallel ( $urls ) {
curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch[$key], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch[$key], CURLOPT_SSL_VERIFYHOST, false);

WbstackMagnusOauth::setCurlHttpHeaders( $ch[$key] );

curl_multi_add_handle($mh,$ch[$key]);
}

Expand Down