Skip to content

Commit ed50afd

Browse files
committed
Fixes for non-multipart media uploads, see #141
1 parent a0fa478 commit ed50afd

File tree

2 files changed

+76
-47
lines changed

2 files changed

+76
-47
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ codebird-php - changelog
1414
+ #145 Support TON API
1515
+ #120 Support Ads API
1616
+ Support WebP media format
17+
+ Fixes for non-multipart media uploads, see #141
1718

1819
2.7.2 (2015-09-23)
1920
- #135 Invalid HTTP request headers in non-cURL mode

src/codebird.php

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ class Codebird
102102
*/
103103
protected static $_endpoint_oauth = 'https://api.twitter.com/';
104104

105+
/**
106+
* Possible file name parameters
107+
*/
108+
protected static $_possible_files = [
109+
// Tweets
110+
'statuses/update_with_media' => ['media[]'],
111+
'media/upload' => ['media'],
112+
// Accounts
113+
'account/update_profile_background_image' => ['image'],
114+
'account/update_profile_image' => ['image'],
115+
'account/update_profile_banner' => ['banner']
116+
];
117+
105118
/**
106119
* The Request or access token. Used to sign requests
107120
*/
@@ -1639,23 +1652,24 @@ protected function _detectMultipart($method)
16391652
'media/upload',
16401653

16411654
// Users
1642-
'account/update_profile_background_image',
1643-
'account/update_profile_image',
1644-
'account/update_profile_banner'
1655+
// no multipart for these, for now:
1656+
//'account/update_profile_background_image',
1657+
//'account/update_profile_image',
1658+
//'account/update_profile_banner'
16451659
];
16461660
return in_array($method, $multiparts);
16471661
}
16481662

16491663
/**
16501664
* Merge multipart string from parameters array
16511665
*
1652-
* @param array $possible_files List of possible filename parameters
1653-
* @param string $border The multipart border
1654-
* @param array $params The parameters to send along
1666+
* @param string $method_template The method template to call
1667+
* @param string $border The multipart border
1668+
* @param array $params The parameters to send along
16551669
*
16561670
* @return string request
16571671
*/
1658-
protected function _getMultipartRequestFromParams($possible_files, $border, $params)
1672+
protected function _getMultipartRequestFromParams($method_template, $border, $params)
16591673
{
16601674
$request = '';
16611675
foreach ($params as $key => $value) {
@@ -1668,32 +1682,9 @@ protected function _getMultipartRequestFromParams($possible_files, $border, $par
16681682
. 'Content-Disposition: form-data; name="' . $key . '"';
16691683

16701684
// check for filenames
1671-
if (in_array($key, $possible_files)) {
1672-
if (// is it a file, a readable one?
1673-
@file_exists($value)
1674-
&& @is_readable($value)
1675-
) {
1676-
// is it a supported image format?
1677-
$data = @getimagesize($value);
1678-
if ((is_array($data) && in_array($data[2], $this->_supported_media_files))
1679-
|| imagecreatefromwebp($data) // A WebP image! :-) —why won’t getimagesize support this?
1680-
) {
1681-
// try to read the file
1682-
$data = @file_get_contents($value);
1683-
if ($data === false || strlen($data) === 0) {
1684-
continue;
1685-
}
1686-
$value = $data;
1687-
}
1688-
} elseif (// is it a remote file?
1689-
filter_var($value, FILTER_VALIDATE_URL)
1690-
&& preg_match('/^https?:\/\//', $value)
1691-
) {
1692-
$data = $this->_fetchRemoteFile($value);
1693-
if ($data !== false) {
1694-
$value = $data;
1695-
}
1696-
}
1685+
$data = $this->_checkForFiles($method_template, $key, $value);
1686+
if ($data !== false) {
1687+
$value = $data;
16971688
}
16981689

16991690
$request .= "\r\n\r\n" . $value . "\r\n";
@@ -1702,6 +1693,46 @@ protected function _getMultipartRequestFromParams($possible_files, $border, $par
17021693
return $request;
17031694
}
17041695

1696+
/**
1697+
* Check for files
1698+
*
1699+
* @param string $method_template The method template to call
1700+
* @param string $key The parameter name
1701+
* @param array $value The possible file name or URL
1702+
*
1703+
* @return mixed
1704+
*/
1705+
protected function _checkForFiles($method_template, $key, $value) {
1706+
if (!in_array($key, self::$_possible_files[$method_template])) {
1707+
return false;
1708+
}
1709+
if (// is it a file, a readable one?
1710+
@file_exists($value)
1711+
&& @is_readable($value)
1712+
) {
1713+
// is it a supported image format?
1714+
$data = @getimagesize($value);
1715+
if ((is_array($data) && in_array($data[2], $this->_supported_media_files))
1716+
|| imagecreatefromwebp($data) // A WebP image! :-) —why won’t getimagesize support this?
1717+
) {
1718+
// try to read the file
1719+
$data = @file_get_contents($value);
1720+
if ($data !== false && strlen($data) !== 0) {
1721+
return $data;
1722+
}
1723+
}
1724+
} elseif (// is it a remote file?
1725+
filter_var($value, FILTER_VALIDATE_URL)
1726+
&& preg_match('/^https?:\/\//', $value)
1727+
) {
1728+
$data = $this->_fetchRemoteFile($value);
1729+
if ($data !== false) {
1730+
return $data;
1731+
}
1732+
}
1733+
return false;
1734+
}
1735+
17051736

17061737
/**
17071738
* Detect filenames in upload parameters,
@@ -1720,25 +1751,14 @@ protected function _buildMultipart($method, $params)
17201751
}
17211752

17221753
// only check specific parameters
1723-
$possible_files = [
1724-
// Tweets
1725-
'statuses/update_with_media' => 'media[]',
1726-
'media/upload' => 'media',
1727-
// Accounts
1728-
'account/update_profile_background_image' => 'image',
1729-
'account/update_profile_image' => 'image',
1730-
'account/update_profile_banner' => 'banner'
1731-
];
17321754
// method might have files?
1733-
if (! in_array($method, array_keys($possible_files))) {
1755+
if (! in_array($method, array_keys(self::$_possible_files))) {
17341756
return;
17351757
}
17361758

1737-
$possible_files = explode(' ', $possible_files[$method]);
1738-
17391759
$multipart_border = '--------------------' . $this->_nonce();
17401760
$multipart_request =
1741-
$this->_getMultipartRequestFromParams($possible_files, $multipart_border, $params)
1761+
$this->_getMultipartRequestFromParams($method, $multipart_border, $params)
17421762
. '--' . $multipart_border . '--';
17431763

17441764
return $multipart_request;
@@ -2178,6 +2198,13 @@ protected function _callApiPreparationsPost(
21782198
$params = [];
21792199
}
21802200
} else {
2201+
// check for possible files in non-multipart methods
2202+
foreach ($params as $key => $value) {
2203+
$data = $this->_checkForFiles($method_template, $key, $value);
2204+
if ($data !== false) {
2205+
$params[$key] = base64_encode($data);
2206+
}
2207+
}
21812208
if (! $app_only_auth) {
21822209
$authorization = $this->_sign($httpmethod, $url, $params);
21832210
}
@@ -2494,8 +2521,9 @@ protected function _parseApiReplyPrefillHeaders($headers, $reply)
24942521
if (isset($headers['Range'])) {
24952522
$reply['Range'] = $headers['Range'];
24962523
}
2524+
$reply = json_encode($reply);
24972525
}
2498-
return json_encode($reply);
2526+
return $reply;
24992527
}
25002528

25012529
/**

0 commit comments

Comments
 (0)