@@ -45,6 +45,16 @@ class Codebird
45
45
*/
46
46
private static $ _instance = null ;
47
47
48
+ /**
49
+ * The OAuth consumer key of your registered app
50
+ */
51
+ private static $ _oauth_consumer_key = null ;
52
+
53
+ /**
54
+ * The corresponding consumer secret
55
+ */
56
+ private static $ _oauth_consumer_secret = null ;
57
+
48
58
/**
49
59
* The API endpoint to use
50
60
*/
@@ -61,16 +71,6 @@ class Codebird
61
71
*/
62
72
private $ _endpoint_upload = 'https://upload.twitter.com/1/ ' ;
63
73
64
- /**
65
- * The OAuth consumer key of your registered app
66
- */
67
- private $ _oauth_consumer_key = null ;
68
-
69
- /**
70
- * The corresponding consumer secret
71
- */
72
- private $ _oauth_consumer_secret = null ;
73
-
74
74
/**
75
75
* The Request or access token. Used to sign requests
76
76
*/
@@ -91,10 +91,15 @@ class Codebird
91
91
*/
92
92
private $ _statuses_public_timeline_cache = array ('timestamp ' => false , 'data ' => false );
93
93
94
+ /**
95
+ * The file formats that Twitter accepts as image uploads
96
+ */
97
+ private $ _supported_media_files = array (IMAGETYPE_GIF , IMAGETYPE_JPEG , IMAGETYPE_PNG );
98
+
94
99
/**
95
100
* The current Codebird version
96
101
*/
97
- private $ _version = '2.0.3006.2126 ' ;
102
+ private $ _version = '2.1.3007.0006 ' ;
98
103
99
104
/**
100
105
* Returns singleton class instance
@@ -111,27 +116,27 @@ public static function getInstance()
111
116
}
112
117
113
118
/**
114
- * Gets the current Codebird version
119
+ * Sets the OAuth consumer key and secret (App key)
115
120
*
116
- * @return string The version number
121
+ * @param string $key OAuth consumer key
122
+ * @param string $secret OAuth consumer secret
123
+ *
124
+ * @return void
117
125
*/
118
- public function getVersion ( )
126
+ public static function setConsumerKey ( $ key , $ secret )
119
127
{
120
- return $ this ->_version ;
128
+ self ::$ _oauth_consumer_key = $ key ;
129
+ self ::$ _oauth_consumer_secret = $ secret ;
121
130
}
122
131
123
132
/**
124
- * Sets the OAuth consumer key and secret (App key)
125
- *
126
- * @param string $key OAuth consumer key
127
- * @param string $secret OAuth consumer secret
133
+ * Gets the current Codebird version
128
134
*
129
- * @return void
135
+ * @return string The version number
130
136
*/
131
- public function setConsumerKey ( $ key , $ secret )
137
+ public function getVersion ( )
132
138
{
133
- $ this ->_oauth_consumer_key = $ key ;
134
- $ this ->_oauth_consumer_secret = $ secret ;
139
+ return $ this ->_version ;
135
140
}
136
141
137
142
/**
@@ -221,6 +226,9 @@ public function __call($fn, $params)
221
226
$ sign = $ this ->_detectSign ($ method2 );
222
227
$ multipart = $ this ->_detectMultipart ($ method2 );
223
228
229
+ // geek-geek: Now allowing to specify filenames as params
230
+ $ this ->_detectFilenames ($ method2 , $ apiparams );
231
+
224
232
return $ this ->_callApi ($ httpmethod , $ method , $ apiparams , $ sign , $ multipart );
225
233
}
226
234
@@ -325,13 +333,13 @@ private function _url($data)
325
333
*/
326
334
private function _sha1 ($ data )
327
335
{
328
- if ($ this -> _oauth_consumer_secret == null ) {
336
+ if (self :: $ _oauth_consumer_secret == null ) {
329
337
throw new Exception ('To generate a hash, the consumer secret must be set. ' );
330
338
}
331
339
if (!function_exists ('hash_hmac ' )) {
332
340
throw new Exception ('To generate a hash, the PHP hash extension must be available. ' );
333
341
}
334
- return base64_encode (hash_hmac ('sha1 ' , $ data , $ this -> _oauth_consumer_secret . '& '
342
+ return base64_encode (hash_hmac ('sha1 ' , $ data , self :: $ _oauth_consumer_secret . '& '
335
343
. ($ this ->_oauth_token_secret != null ? $ this ->_oauth_token_secret : '' ), true ));
336
344
}
337
345
@@ -363,11 +371,11 @@ private function _nonce($length = 8)
363
371
*/
364
372
private function _sign ($ httpmethod , $ method , $ params = array (), $ multipart = false )
365
373
{
366
- if ($ this -> _oauth_consumer_key == null ) {
374
+ if (self :: $ _oauth_consumer_key == null ) {
367
375
throw new Exception ('To generate a signature, the consumer key must be set. ' );
368
376
}
369
377
$ sign_params = array (
370
- 'consumer_key ' => $ this -> _oauth_consumer_key ,
378
+ 'consumer_key ' => self :: $ _oauth_consumer_key ,
371
379
'version ' => '1.0 ' ,
372
380
'timestamp ' => time (),
373
381
'nonce ' => $ this ->_nonce (),
@@ -505,6 +513,7 @@ private function _detectMethod($method)
505
513
'statuses/destroy/:id ' ,
506
514
'statuses/retweet/:id ' ,
507
515
'statuses/update ' ,
516
+ 'statuses/update_with_media ' ,
508
517
// Direct Messages
509
518
'direct_messages/new ' ,
510
519
// Friends & Followers
@@ -598,6 +607,73 @@ private function _detectMultipart($method)
598
607
return in_array ($ method , $ multiparts );
599
608
}
600
609
610
+ /**
611
+ * Detects filenames in upload parameters
612
+ *
613
+ * @param string $method The API method to call
614
+ * @param byref array $params The parameters to send along
615
+ *
616
+ * @return void
617
+ */
618
+ private function _detectFilenames ($ method , &$ params )
619
+ {
620
+ // well, files will only work in multipart methods
621
+ if (! $ this ->_detectMultipart ($ method )) {
622
+ return ;
623
+ }
624
+
625
+ // only check specific parameters
626
+ $ possible_files = array (
627
+ // Tweets
628
+ 'statuses/update_with_media ' => 'media[] ' ,
629
+ // Accounts
630
+ 'account/update_profile_background_image ' => 'image ' ,
631
+ 'account/update_profile_image ' => 'image '
632
+ );
633
+ // method might have files?
634
+ if (! in_array ($ method , array_keys ($ possible_files ))) {
635
+ return ;
636
+ }
637
+
638
+ // check for filenames
639
+ $ possible_files = explode (' ' , $ possible_files [$ method ]);
640
+ foreach ($ possible_files as $ possible_file ) {
641
+ // is this parameter set currently?
642
+ if (! isset ($ params [$ possible_file ])) {
643
+ continue ;
644
+ }
645
+ // is it an array?
646
+ if (is_array ($ params [$ possible_file ])) {
647
+ throw new Exception ('Using URL-encoded parameters is not supported for uploading media. ' );
648
+ continue ;
649
+ }
650
+ // is it a file, a readable one?
651
+ if (! @file_exists ($ params [$ possible_file ])
652
+ || ! @is_readable ($ params [$ possible_file ])
653
+ ) {
654
+ continue ;
655
+ }
656
+ // is it a valid image?
657
+ if (! $ data = @getimagesize ($ params [$ possible_file ])) {
658
+ continue ;
659
+ }
660
+ // is it a supported image format?
661
+ if (! in_array ($ data [2 ], $ this ->_supported_media_files )) {
662
+ continue ;
663
+ }
664
+ // try to read the file
665
+ ob_start ();
666
+ readfile ($ params [$ possible_file ]);
667
+ $ data = ob_get_contents ();
668
+ ob_end_clean ();
669
+ if (strlen ($ data ) == 0 ) {
670
+ continue ;
671
+ }
672
+ $ params [$ possible_file ] = $ data ;
673
+ }
674
+ }
675
+
676
+
601
677
/**
602
678
* Builds the complete API endpoint url
603
679
*
0 commit comments