@@ -102,6 +102,19 @@ class Codebird
102
102
*/
103
103
protected static $ _endpoint_oauth = 'https://api.twitter.com/ ' ;
104
104
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
+
105
118
/**
106
119
* The Request or access token. Used to sign requests
107
120
*/
@@ -1639,23 +1652,24 @@ protected function _detectMultipart($method)
1639
1652
'media/upload ' ,
1640
1653
1641
1654
// 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'
1645
1659
];
1646
1660
return in_array ($ method , $ multiparts );
1647
1661
}
1648
1662
1649
1663
/**
1650
1664
* Merge multipart string from parameters array
1651
1665
*
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
1655
1669
*
1656
1670
* @return string request
1657
1671
*/
1658
- protected function _getMultipartRequestFromParams ($ possible_files , $ border , $ params )
1672
+ protected function _getMultipartRequestFromParams ($ method_template , $ border , $ params )
1659
1673
{
1660
1674
$ request = '' ;
1661
1675
foreach ($ params as $ key => $ value ) {
@@ -1668,32 +1682,9 @@ protected function _getMultipartRequestFromParams($possible_files, $border, $par
1668
1682
. 'Content-Disposition: form-data; name=" ' . $ key . '" ' ;
1669
1683
1670
1684
// 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 ;
1697
1688
}
1698
1689
1699
1690
$ request .= "\r\n\r\n" . $ value . "\r\n" ;
@@ -1702,6 +1693,46 @@ protected function _getMultipartRequestFromParams($possible_files, $border, $par
1702
1693
return $ request ;
1703
1694
}
1704
1695
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
+
1705
1736
1706
1737
/**
1707
1738
* Detect filenames in upload parameters,
@@ -1720,25 +1751,14 @@ protected function _buildMultipart($method, $params)
1720
1751
}
1721
1752
1722
1753
// 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
- ];
1732
1754
// method might have files?
1733
- if (! in_array ($ method , array_keys ($ possible_files ))) {
1755
+ if (! in_array ($ method , array_keys (self :: $ _possible_files ))) {
1734
1756
return ;
1735
1757
}
1736
1758
1737
- $ possible_files = explode (' ' , $ possible_files [$ method ]);
1738
-
1739
1759
$ multipart_border = '-------------------- ' . $ this ->_nonce ();
1740
1760
$ multipart_request =
1741
- $ this ->_getMultipartRequestFromParams ($ possible_files , $ multipart_border , $ params )
1761
+ $ this ->_getMultipartRequestFromParams ($ method , $ multipart_border , $ params )
1742
1762
. '-- ' . $ multipart_border . '-- ' ;
1743
1763
1744
1764
return $ multipart_request ;
@@ -2178,6 +2198,13 @@ protected function _callApiPreparationsPost(
2178
2198
$ params = [];
2179
2199
}
2180
2200
} 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
+ }
2181
2208
if (! $ app_only_auth ) {
2182
2209
$ authorization = $ this ->_sign ($ httpmethod , $ url , $ params );
2183
2210
}
@@ -2494,8 +2521,9 @@ protected function _parseApiReplyPrefillHeaders($headers, $reply)
2494
2521
if (isset ($ headers ['Range ' ])) {
2495
2522
$ reply ['Range ' ] = $ headers ['Range ' ];
2496
2523
}
2524
+ $ reply = json_encode ($ reply );
2497
2525
}
2498
- return json_encode ( $ reply) ;
2526
+ return $ reply ;
2499
2527
}
2500
2528
2501
2529
/**
0 commit comments