Page Menu
Home
GitPull.it
Search
Configure Global Search
Log In
Files
F15175591
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Authored By
Unknown
Size
33 KB
Referenced Files
None
Subscribers
None
View Options
diff --git "a/include/class-mw\\API.php" "b/include/class-mw\\API.php"
index d8b647e..8cfea17 100644
--- "a/include/class-mw\\API.php"
+++ "b/include/class-mw\\API.php"
@@ -1,293 +1,313 @@
<?php
# Boz-MW - Another MediaWiki API handler in PHP
# Copyright (C) 2017, 2018 Valerio Bozzolan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# MediaWiki
namespace mw;
use cli\Log;
/**
* Make HTTP request to a MediaWiki API
*/
class API extends \network\HTTPRequest {
/**
* MediaWiki tokens handler
*
* @var mw\Tokens
*/
private $tokens;
/**
* Default MediaWiki login username
*
* @var string
*/
static $DEFAULT_USERNAME;
/**
* Default MediaWiki login password
*
* @var string
*/
static $DEFAULT_PASSWORD;
/**
* Default MediaWiki API maxlag
*
* @var int
*/
static $DEFAULT_MAXLAG = 5;
/**
* Default MediaWiki API response format
*
* @var string
*/
static $DEFAULT_FORMAT = 'json';
/**
* Inspect POST flag
*
* @var bool
*/
public static $INSPECT_BEFORE_POST = false;
/**
* Username used for the login.
*
* @var string
*/
private $username;
/**
* Constructor
*
* @param $api string API endpoint
*/
public function __construct( $api ) {
parent::__construct( $api, [] );
$this->tokens = new Tokens( $this );
}
/**
* Create an API query with continuation handler
*
* @param $data array Data
* @return mw\APIQuery
*/
public function createQuery( $data ) {
return new APIQuery( $this, $data );
}
/**
* Effectuate an HTTP POST request but only after a login.
*
* @param $data array GET/POST data
* @param $args array Internal arguments
* @override \network\HTTPRequest#post()
* @return mixed
*/
public function post( $data = [], $args = [] ) {
- if( ! $this->isLogged() ) {
+ if( !$this->isLogged() ) {
$this->login();
}
if( static::$INSPECT_BEFORE_POST ) {
print_r( $data );
\cli\Input::askInput( "Press ENTER to submit" );
}
return parent::post( $data, $args );
}
+ /**
+ * Effectuate an HTTP POST (multipart) request but only after a login.
+ *
+ * @param $data array Array of ContentDispositions(s)
+ * @param $args array Internal arguments
+ * @override \network\HTTPRequest#post()
+ * @return mixed
+ */
+ public function postMultipart( $data = [], $args = [] ) {
+ if( !$this->isLogged() ) {
+ $this->login();
+ }
+ if( static::$INSPECT_BEFORE_POST ) {
+ print_r( $data );
+ \cli\Input::askInput( "Press ENTER to submit" );
+ }
+ return parent::postMultipart( $data, $args );
+ }
+
/**
* Fetch response
*
* @param $data array GET/POST data
* @return mixed
*/
public function fetch( $data = [], $args = [] ) {
if( [] === $data ) {
throw \InvalidArgumentException( 'empty data' );
}
return parent::fetch( $data, $args );
}
/**
* Preload some tokens
*
* @return self
*/
public function preloadTokens( $tokens ) {
$this->tokens->preload( $tokens );
return $this;
}
/**
* Get the value of a token
*
* @param $token string Token name
* @return string Token value
*/
public function getToken( $token ) {
return $this->tokens->get( $token );
}
/**
* Invalidate a token
*
* @param $token string Token name
* @return self
*/
public function invalidateToken( $token ) {
$this->tokens->invalidate( $token );
return $this;
}
/**
* Get the username used for the login
*
* @return string|null
*/
public function getUsername() {
return $this->username;
}
/**
* Check if it's already logged in.
*
* @return bool
*/
public function isLogged() {
return $this->getUsername() !== null;
}
/**
* Login into MediaWiki using an username/password pair.
*
* Yes, I'm talking about a bot password.
*/
public function login( $username = null, $password = null ) {
// Can use a default set of credentials
if( ! $username && ! $password ) {
if( $this->isLogged() ) {
return $this;
}
$username = self::$DEFAULT_USERNAME;
$password = self::$DEFAULT_PASSWORD;
}
if( ! $username || ! $password ) {
throw new \Exception( sprintf(
'you must call %1$s#login( $username, $password ) or '.
'set %1$s::$DEFAULT_USERNAME and %1$s::$DEFAULT_PASSWORD ' .
'before POSTing',
__CLASS__
) );
}
// Login
$response = parent::post( [
'action' => 'login',
'lgname' => $username,
'lgpassword' => $password,
'lgtoken' => $this->getToken( Tokens::LOGIN ),
], [
'sensitive' => true
] );
if( ! isset( $response->login->result ) || $response->login->result !== 'Success' ) {
print_r( $response );
throw new \Exception("login failed");
}
$this->username = $response->login->lgusername;
return $this;
}
/**
* Filters the data before using it.
*
* Array elements are imploded by a pipe
* NULL values are unset
*
* @override network\HTTPRequest::onDataReady()
* @param $data array GET/POST data
* @return array
*/
protected function onDataReady( $data ) {
// Some default values
$data = array_replace( [
'maxlag' => self::$DEFAULT_MAXLAG,
'format' => self::$DEFAULT_FORMAT,
], $data );
foreach( $data as $k => $v ) {
if( null === $v ) {
unset( $data[ $k ] );
} elseif( is_array( $v ) ) {
// remove duplicates (API netiquette)
$v = array_unique( $v );
// index alphabetically (API netiquette)
sort( $v, SORT_STRING );
$data[ $k ] = implode( '|', $v );
}
}
if( $this->isLogged() ) {
$data = array_replace( [
'assertuser' => $this->getUsername(),
], $data );
}
return $data;
}
/**
* JSON decode and check formal API errors
*
* @param $response mixed Response
* @param $request_data array GET/POST request data
* @override \network\HTTPRequest#onFetched()
* @throws \mw\API\Exception
*/
- protected function onFetched( $response, $request_data ) {
- $response = json_decode( $response );
+ protected function onFetched( $response_raw, $request_data ) {
+ $response = json_decode( $response_raw );
if( null === $response ) {
- throw new \Exception( 'response not JSON-encoded: ' . $response );
+ Log::debug( $response_raw );
+ throw new \Exception( 'response is not JSON-encoded' );
}
if( isset( $response->warnings ) ) {
foreach( $response->warnings as $subject => $warning ) {
Log::warn( sprintf( '%s: %s', $subject, $warning->{'*'} ) );
}
}
if( isset( $response->error ) ) {
$exception = API\Exception::createFromApiError( $response->error );
if( $exception instanceof API\MaxLagException ) {
// retry after some time when server lags
Log::warn( "Lag! ({$this->api})" );
$response = $this->fetch( $request_data, [
'wait' => self::WAIT_DOS
] );
} else {
throw $exception;
}
}
return $response;
}
}
diff --git "a/include/class-mw\\Site.php" "b/include/class-mw\\Site.php"
index a8ef5f8..3b1227a 100644
--- "a/include/class-mw\\Site.php"
+++ "b/include/class-mw\\Site.php"
@@ -1,321 +1,333 @@
<?php
# Boz-MW - Another MediaWiki API handler in PHP
# Copyright (C) 2017, 2018, 2019 Valerio Bozzolan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# MediaWiki
namespace mw;
class Site {
/**
* A sort of $wgCapitalLinks
*
* @see https://www.mediawiki.org/wiki/Manual:$wgCapitalLinks
*/
const CAPITAL_LINKS = true;
/**
* MediaWiki API
*
* @var API
*/
private $api;
/**
* Site namespaces
*
* @var array Array of namespaces
*/
private $namespaces = [];
/**
* A sort of internal UID
*
* @var null|string E.g. 'enwiki'
*/
private $uid;
/**
* Constructor
*
* @param $url string MediaWiki API URL
*/
public function __construct( $url ) {
$this->api = new API( $url );
}
/**
* Create an API query with continuation handler
*
* @param $data array GET/POST data arguments
* @return mw\APIQuery
*/
public function createQuery( $data ) {
return $this->getApi()->createQuery( $data );
}
/**
* Make an HTTP GET request to the API
*
* @param $data array HTTP GET data
* @return mixed API result
*/
public function fetch( $data ) {
return $this->getApi()->fetch( $data );
}
/**
* Make an HTTP POST request to the API
*
* This method will call the API#login() method.
*
* @param $data array HTTP GET data
* @return mixed API result
*/
public function post( $data ) {
return $this->getApi()->post( $data );
}
+ /**
+ * Make an HTTP POST request to the API
+ *
+ * This method will call the API#login() method.
+ *
+ * @param array $data Array of ContentDisposition(s)
+ * @return mixed API result
+ */
+ public function postMultipart( $data ) {
+ return $this->getApi()->postMultipart( $data );
+ }
+
/**
* Do an API edit request
*
* @param $data array API request data
* @return mixed
* @see https://www.mediawiki.org/wiki/API:Edit
*/
public function edit( $data ) {
return $this->getApi()->post( array_replace( $data, [
'action' => 'edit',
'token' => $this->getToken( \mw\Tokens::CSRF )
] ) );
}
/**
* Check if I'm logged
*
* @return bool
*/
public function isLogged() {
return $this->getApi()->isLogged();
}
/**
* Get the username used for the login (if any)
*
* @return string|null
*/
public function getUsername() {
return $this->getApi()->getUsername();
}
/**
* Preload some tokens
*
* @return self
*/
public function preloadTokens( $tokens ) {
$this->getApi()->preloadTokens( $tokens );
return $this;
}
/**
* Get the value of a token
*
* @param $token string Token name
* @return string Token value
*/
public function getToken( $token ) {
return $this->getApi()->getToken( $token );
}
/**
* Invalidate a token
*
* @param $token string Token name
* @return self
*/
public function invalidateToken( $token ) {
$this->getApi()->invalidateToken( $token );
return $this;
}
/**
* Make a login
*
* @see API#login()
* @param $username string MediaWiki username
* @param $password string MediaWiki password
* @return self
*/
public function login( $username = null, $password = null ) {
$this->getApi()->login( $username, $password );
return $this;
}
/**
* Check if a certain namespace ID is registered
*
* @param int $id Namespace ID
* @return bool
*/
public function hasNamespace( $id ) {
return array_key_exists( $id, $this->namespaces );
}
/**
* Get the namespace related to the specified ID
*
* @param int $id Namespace ID
* @return Ns Corresponding namespace
*/
public function getNamespace( $id ) {
if( ! $this->hasNamespace( $id ) ) {
throw new \Exception( sprintf( 'missing namespace %d', $id ) );
}
return $this->namespaces[ $id ];
}
/**
* Set/overwrite a namespace
*
* @param $namespace Ns Namespace to be set/overwrited
* @return self
*/
public function setNamespace( Ns $namespace ) {
$id = $namespace->getID();
$this->namespaces[ $id ] = $namespace;
return $this;
}
/**
* Find a namespace by it's name
*
* @param $name string
* @return object|false
*/
public function findNamespace( $name ) {
$name = Ns::normalizeName( $name );
foreach( $this->namespaces as $ns ) {
if( $ns->getName() === $name ) {
return $ns;
}
}
return false;
}
/**
* Stupid shortcut for setting multiple namespaces
*
* @param $namespaces array Array of namespaces
* @return self
*/
public function setNamespaces( $namespaces ) {
foreach( $namespaces as $namespace ) {
$this->setNamespace( $namespace );
}
return $this;
}
/**
* Set the UID
*
* @param string E.g. 'enwiki'
* @return self
*/
public function setUID( $uid ) {
$this->uid = $uid;
return $this;
}
/**
* Get internal MediaWiki API object
*
* @return API
*/
public function getApi() {
return $this->api;
}
/**
* Get the UID
*
* @return null|string e.g. 'enwiki'
*/
public function getUID() {
return $this->uid;
}
/**
* Create a Wikitext object
*
* @return Wikitext
*/
public function createWikitext( $wikitext = '' ) {
return new Wikitext( $this, $wikitext );
}
/**
* Create a CompleteTitle object
*
* @param $title string Page title without namespace prefix
* @param $ns int Namespace number
* @return object
*/
public function createTitle( $title, $ns = 0 ) {
return new CompleteTitle( $this, $this->getNamespace( $ns ), new Title( $title, $this ) );
}
/**
* Create a CompleteTitle object
*
* @param $s Page title with namespace prefix
* @return object
*/
public function createTitleParsing( $s ) {
return CompleteTitle::createParsingTitle( $this, $s );
}
/**
* Create a wikilink object
*
* @deprecate Unuseful, just use createTitleParsing()->createWikilink()
* @return object
*/
public function createWikilink( CompleteTitle $title, $alias = null ) {
return $title->createWikilink( $alias );
}
/**
* Check if in this wiki the first case is insensitive
*
* @TODO generalize
* @return boolean
*/
public function hasCapitalLinks() {
return static::CAPITAL_LINKS;
}
/**
* Create from an API URL
*
* @param $url string MediaWiki API URL
* @return self
*/
public static function createFromAPIURL( $url ) {
return new static( $url );
}
}
diff --git "a/include/class-network\\ContentDisposition.php" "b/include/class-network\\ContentDisposition.php"
new file mode 100644
index 0000000..04a24c2
--- /dev/null
+++ "b/include/class-network\\ContentDisposition.php"
@@ -0,0 +1,186 @@
+<?php
+# Boz-MW - Another MediaWiki API handler in PHP
+# Copyright (C) 2019 Valerio Bozzolan
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 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 Affero General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Network
+namespace network;
+
+/**
+ * Describe a Content-Disposition HTTP header during a multipart request
+ */
+class ContentDisposition {
+
+ /**
+ * Constructor
+ *
+ * @param string $name
+ * @param string $content
+ * @param string $filename Just the file name without the path
+ * @param string $content_type
+ */
+ public function __construct( $name, $content, $filename = null, $content_type = null ) {
+ $this->name = $name;
+ $this->content = $content;
+ $this->filename = $filename;
+ $this->contentType = $content_type;
+ }
+
+ /**
+ * Explicit constructor
+ *
+ * @param string $name
+ * @param string $content
+ * @return self
+ */
+ public static function createFromNameContent( $name, $content ) {
+ return new self( $name, $content );
+ }
+
+ /**
+ * Explicit constructor
+ *
+ * @param string $name
+ * @param string $content
+ * @param string $filename Just the file name without the path
+ * @param string $content_type
+ * @return self
+ */
+ public static function createFromNameContentFilenameType( $name, $content, $filename, $content_type ) {
+ return new self( $name, $content, $filename, $content_type );
+ }
+
+ /**
+ * Explicit constructor
+ *
+ * @param string $name Variable name
+ * @param string $url File URL to be downloaded
+ * @param string $content_type
+ * @return self
+ */
+ public static function createFromNameURLTypeFilename( $name, $url, $content_type, $filename ) {
+ $content = file_get_contents( $url );
+ if( !$filename ) {
+ $filename = basename( $url );
+ }
+ return new self( $name, $content, $filename, $content_type );
+ }
+
+
+ /**
+ * Get the multipart request part
+ *
+ * An interesting reference:
+ * https://stackoverflow.com/a/4247082
+ *
+ * @param string $boundary
+ * @return string
+ */
+ public function get( $boundary ) {
+ $headers = [];
+
+ // Content-Disposition
+ $value = "form-data; name=\"{$this->name}\";";
+ if( $this->filename ) {
+ $value .= " filename=\"{$this->filename}\"";
+ }
+ $headers[] = HTTPRequest::header( 'Content-Disposition', $value );
+
+ // Content-Type
+ if( $this->contentType ) {
+ $headers[] = HTTPRequest::header( 'Content-Type', $this->contentType );
+ }
+
+ $content = "--$boundary\r\n";
+ $content .= HTTPRequest::implodeHTTPHeaders( $headers );
+ $content .= "\r\n";
+ $content .= "{$this->content}\r\n";
+
+ return $content;
+ }
+
+ /**
+ * Check if a boundary does not collides
+ *
+ * @param strimg $boundary
+ */
+ public function isBoundaryOK( $boundary ) {
+ return strpos( $this->name, $boundary ) !== -1
+ && strpos( $this->content, $boundary ) !== -1
+ && ( !$this->filename || strpos( $this->filename, $boundary ) !== -1 )
+ && ( !$this->contentType || strpos( $this->contentType, $boundary ) !== -1 );
+ }
+
+ /**
+ * Generate a "Content-Type: multipart/form-data; boundary=$value"
+ *
+ * @return string
+ */
+ public static function generateBoundary() {
+ return 'ASD' . microtime( true );
+ }
+
+ /**
+ * Aggregate some content dispositions with a suitable boumdary
+ *
+ * @param array $dispositions Array of ContentDisposition(s)
+ * @param string $boundary Reference to the generated boundary
+ * @return string
+ * @see https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
+ */
+ public static function aggregate( $dispositions, & $boundary ) {
+
+ // $data should be an array of ContentDisposition
+ foreach( $dispositions as $key => &$value ) {
+ if( !is_object( $value ) ) {
+ $value = ContentDisposition::createFromNameContent( $key, $value );
+ }
+ }
+
+ // generate a safe boundary that never collides with the data
+ $boundary = '';
+ do {
+ $ok = true;
+ $boundary .= self::generateBoundary();
+ foreach( $dispositions as $k => $disposition ) {
+ if( !$disposition->isBoundaryOK( $boundary ) ) {
+ $ok = false;
+ break;
+ }
+ }
+ } while( !$ok );
+
+ // boundary should follow the standard
+ if( strlen( $boundary ) > 69 ) {
+ throw new \Exception( 'cannot generate a suitable boundary (wtf?)' );
+ }
+
+ // I want to test the specifications so this phrase should be perfectly ignored. asd
+ $s = "Hello! This is an ignored preamble for a multipart message. Have a nice read of RFC1341. asd\r\n";
+
+ // merge the dispositions with the safe boundary
+ foreach( $dispositions as $disposition ) {
+ $s .= $disposition->get( $boundary );
+ }
+
+ // close the whole
+ $s .= "--$boundary--\r\n";
+
+ // I want to test the specifications so this phrase should be perfectly ignored. asd
+ $s .= "You know that multipart message? This is its ignored epiloque. Have a nice day! asd\r\n";
+
+ return $s;
+ }
+}
diff --git "a/include/class-network\\HTTPRequest.php" "b/include/class-network\\HTTPRequest.php"
index 48b3c9e..e60c842 100644
--- "a/include/class-network\\HTTPRequest.php"
+++ "b/include/class-network\\HTTPRequest.php"
@@ -1,461 +1,511 @@
<?php
# Boz-MW - Another MediaWiki API handler in PHP
-# Copyright (C) 2017, 2018 Valerio Bozzolan
+# Copyright (C) 2017, 2018, 2019 Valerio Bozzolan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Network
namespace network;
use cli\Log;
use InvalidArgumentException;
/**
* HTTP request handler for GET and POST requests.
*/
class HTTPRequest {
/**
* Class version
*
* To be incremented every time do you notice this number.
*
* @var string
*/
const VERSION = 0.4;
/**
* Source code URL
*
* @var string
*/
const REPO = 'https://github.com/valerio-bozzolan/boz-mw';
/**
* Wait seconds before each GET request.
*/
static $WAIT = 0.2;
/**
* Wait seconds before each POST request
*
* @var float
*/
static $WAIT_POST = 0.2;
/**
* Wait seconds before each lag
*
* @var float
*/
const WAIT_DOS = 5;
/**
* Full HTTP URL to the API endpoint
*
* @var string
*/
protected $api;
/**
* HTTP GET/POST data
*
* @var array
*/
private $data;
/**
* Internal arguments
*
* @param array
*/
private $args;
/**
* Latest HTTP response headers
*
* @var array
*/
private $latestHttpResponseHeaders = [];
/**
* Latest HTTP error status code
*
* @var Status
*/
private $latestHttpResponseStatus;
/**
* HTTP cookies
*
* @var array
*/
private $cookies = [];
/**
* Constructor.
*
* @param $api string API endpoint
* @param $args array Internal arguments
*/
public function __construct( $api, $args = [] ) {
$this->api = $api;
$this->setArgs( $args );
}
/**
* Statical constructor.
*
* @return network\HTTPRequest
*/
public static function factory( $api, $args = [] ) {
return new self( $api, $args );
}
/**
* Get internal arguments.
*
* @return array
*/
public function getArgs() {
return $this->args;
}
/**
* Set internal arguments
*
* method: GET, POST etc.
* user-agent: HTTP user agent
* sensitive: flag to indicate if the data is sensitive and should not be printed as usual in the log
* wait: microseconds to wait after every GET request
* wait-post: microseconds to wait after every POST request
* headers: HTTP headers
*
* @param $args array Internal arguments
* @return self
*/
public function setArgs( $args ) {
$this->args = array_replace( [
'method' => 'GET',
'user-agent' => sprintf( 'boz-mw HTTPRequest.php/%s %s', self::VERSION, self::REPO ),
'sensitive' => false,
+ 'multipart' => false,
'wait' => self::$WAIT,
'wait-post' => self::$WAIT_POST,
'headers' => [],
], $args );
return $this;
}
/**
* Make an HTTP GET query
*
* @param $data array GET data
* @param $args array Internal arguments
* @return mixed Response
*/
public function fetch( $data = [], $args = [] ) {
$args = array_replace( $this->args, $args );
// Eventually post-process the data before using
$data = static::onDataReady( $data );
// HTTP query using file_get_contents()
$url = $this->api;
- $query = http_build_query( $data );
$context = [
- 'http' => []
+ 'http' => [],
];
$context['http']['method'] = $args['method'];
if( $args['user-agent'] ) {
$args['headers'][] = self::header( 'User-Agent', $args['user-agent'] );
}
if( $this->haveCookies() ) {
$args['headers'][] = $this->getCookieHeader();
}
- if( $query ) {
- if( 'POST' === $args['method'] ) {
- $args['headers'][] = self::header('Content-Type', 'application/x-www-form-urlencoded' );
+ $query = '';
+ switch( $args['method'] ) {
+ case 'POST':
+ case 'PUT':
+ // populate the content context
+
+ // the multipart has a data boundary
+ if( $args['multipart'] ) {
+ // get the request body aggregating the content dispositions generating a safe boundary
+ $query = ContentDisposition::aggregate( $data, $boundary );
+
+ // override the content type and set the boundary
+ $args['content-type'] = "multipart/form-data; boundary=$boundary";
+ } else {
+ // normal POST/PUT request
+ $query = http_build_query( $data );
+ }
+
$context['http']['content'] = $query;
- } else {
+ $args['headers'][] = self::header( 'Content-Type', $args['content-type'] );
+
+ Log::sensitive( "{$args['method']} $url $query", "{$args['method']} $url" );
+ break;
+ case 'GET':
+ case 'HEAD':
+ $query = http_build_query( $data );
$url .= "?$query";
- }
- }
- if( 'GET' === $args['method'] ) {
- Log::debug( "GET $url" );
- } else {
- Log::sensitive( "{$args['method']} $url $query", "{$args['method']} $url" );
+ Log::debug( "GET $url" );
+ break;
}
if( $args['headers'] ) {
$context['http']['header'] = self::implodeHTTPHeaders( $args['headers'] );
}
if( $args['wait'] ) {
Log::debug( sprintf(
"Waiting %.2f s.",
$args['wait']
) );
usleep( $args['wait'] * 1000000 );
}
$stream_context = stream_context_create( $context );
$response = file_get_contents( $url, false, $stream_context );
// Here $http_response_header exists magically (PHP merda!)
if( ! isset( $http_response_header ) ) {
throw new Exception( 'undefined http_response_header variable: wrong request?' );
}
$this->loadHTTPResponseHeaders( $http_response_header );
// Check the HTTP status
$status = $this->getLatestHTTPResponseStatus();
if( ! $status->isOK() ) {
if( $status->isClientError() ) {
Log::warn( sprintf(
"waiting because of HTTP status %s: %s",
$status->getCode(),
$status->getMessage()
) );
$args = array_replace( [
'wait' => self::WAIT_DOS
], $args );
return $this->fetch( $data, $args );
}
throw new NotOKException( $status );
}
Log::debug( $status->getHeader() );
return static::onFetched( $response, $data );
}
/**
* Effectuate an HTTP POST.
*
* @param $data array POST data
* @param $args Internal arguments
* @return mixed Response
*/
public function post( $data = [], $args = [] ) {
$args = array_replace(
- [ 'wait' => $this->args['wait-post'] ],
+ // low-priority arguments
+ [
+ 'wait' => $this->args['wait-post'],
+ 'content-type' => 'application/x-www-form-urlencoded',
+ ],
+
+ // medium priority arguments
$args,
+
+ // high priority arguments
[ 'method' => 'POST' ]
);
return $this->fetch( $data, $args );
}
+ /**
+ * Effectuate an HTTP POST with multipart (suitable for files)
+ *
+ * An useful reference:
+ * https://stackoverflow.com/a/4247082
+ *
+ * @param array $data POST data to be converted in content dispositions
+ * @param array $content_disposition Array of ContentDisposition(s)
+ * @param array $args Internal arguments
+ * @return mixed Response
+ */
+ public function postMultipart( $data = [], $args = [] ) {
+ $args = array_replace(
+ // low-priority arguments
+ $args,
+
+ // high priority arguments
+ [
+ 'multipart' => true,
+ 'content-type' => null, // will be filled later
+ ]
+ );
+ return $this->post( $data, $args );
+ }
+
/**
* Get latest HTTP response headers.
*
* @return array
*/
public function getLatestHTTPResponseHeaders() {
return $this->latestHttpResponseHeaders;
}
/**
* Get latest HTTP status
*
* @return Status
*/
private function getLatestHTTPResponseStatus() {
return $this->latestHttpResponseStatus;
}
/**
* Check if cookies are set
*
* @return bool
*/
public function haveCookies() {
return isset( $this->cookies );
}
/**
* Get cookies
*
* @return array
*/
public function getCookies() {
return $this->cookies;
}
/**
* Set an HTTP cookie name => value pair.
*
* @param $name string Cookie name
* @param $value string Cookie value
* @return self
*/
public function setCookie( $name, $value ) {
$this->cookies[ $name ] = $value;
return $this;
}
public function getCookieHeader() {
$cookies = [];
foreach( $this->getCookies() as $name => $value ) {
$cookies[] = urlencode( $name ) . '=' . urlencode( $value );
}
return self::header(
'Cookie',
implode( '; ', $cookies )
);
}
public function getHTTPCookies() {
return $this->cookies;
}
/**
* Set an HTTP cookie in its raw form.
*
* @param $cookie string Cookie text
*/
private function setRawCookie( $cookie ) {
$parts = explode(';', $cookie);
if( isset( $parts[0] ) ) {
$name_value = explode('=', $parts[0] );
if( 2 === count( $name_value ) ) {
list( $name, $value ) = $name_value;
$this->setCookie( $name, $value );
}
}
}
/**
* Load HTTP response headers, filling cookies.
*/
private function loadHTTPResponseHeaders( $http_response_headers ) {
list( $this->latestHttpResponseHeaders, $this->latestHttpResponseStatus )
= self::parseHTTPResponseHeaders( $http_response_headers );
if( isset( $this->latestHttpResponseHeaders['Set-Cookie'] ) ) {
foreach( $this->latestHttpResponseHeaders['Set-Cookie'] as $cookie ) {
$this->setRawCookie( $cookie );
}
}
}
/**
- * Implode HTTP headers
+ * Implode HTTP headers with CRLF
*
* @param $headers array
* @return string
*/
- private function implodeHTTPHeaders( $headers ) {
+ public static function implodeHTTPHeaders( $headers ) {
if( ! $headers ) {
return null;
}
$s = '';
foreach( $headers as $header ) {
$s .= "$header\r\n";
}
return $s;
}
/**
* Group HTTP headers by keys and get the HTTP Status.
*
* @param $http_response_headers array
* @return array The first element contains an associative array of header name and value(s). The second one contains the Status.
*/
private static function parseHTTPResponseHeaders( $http_response_headers ) {
$status = null;
$headers = [];
foreach( $http_response_headers as $header ) {
// Check if it's an header like 'Foo: bar'
$header_parts = explode(':', $header, 2);
if( 2 === count( $header_parts ) ) {
list( $name, $value ) = $header_parts;
if( ! isset( $headers[ $name ] ) ) {
$headers[ $name ] = [];
}
$headers[ $name ][] = ltrim( $value );
} else {
try {
$status = Status::createFromHeader( $header );
} catch( InvalidArgumentException $e ) {
$headers[ $header ] = true;
}
}
}
if( null === $status ) {
throw new \Exception( 'HTTP response without an HTTP status code' );
}
return [ $headers, $status ];
}
/**
* Get an HTTP header string from a name => value pair.
*
* @param $name string HTTP header name
* @param $value string HTTP header value
* @return string HTTP header
*/
- private static function header( $name, $value ) {
+ public static function header( $name, $value ) {
return self::headerRaw( sprintf(
'%s: %s',
$name,
$value
) );
}
/**
* This function simply returns a single header
*
* As you know an header does not contains a line feed or a carriage return.
*
* @param $name string HTTP header
* @return string HTTP header
*/
private static function headerRaw( $header ) {
if( false !== strpos( $header, "\n" ) || false !== strpos( $header, "\r" ) ) {
Log::warn( "wtf header with line feed or carriage return (header injection?)" );
Log::debug( $header );
return str_replace( [ "\n", "\r" ], '', $header );
}
return $header;
}
/**
* Can be overrided to post-process the data before its use
*
* @param $data GET/POST data
* @return array GET/POST data post-processed
*/
protected function onDataReady( $data ) {
return $data;
}
/**
* Callback to be overloaded
*
* This is called every time something is fetched.
*
* @param $response mixed Response
* @param $request_data mixed GET/POST data
* @return mixed Response
*/
protected function onFetched( $response, $request_data ) {
return $response;
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Nov 19, 03:04 (4 h, 44 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2264036
Default Alt Text
(33 KB)
Attached To
Mode
R21 boz-mw: MediaWiki API client
Attached
Detach File
Event Timeline