Skip to content

Commit 08909f3

Browse files
committed
added option to enable fileUpload using curl @ syntax
1 parent 7d538d3 commit 08909f3

File tree

3 files changed

+85
-18
lines changed

3 files changed

+85
-18
lines changed

examples/example.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
require '../src/facebook.php';
44

5-
// Create our Application instance.
5+
// Create our Application instance (replace this with your appId and secret).
66
$facebook = new Facebook(array(
7-
'appId' => '254752073152',
8-
'secret' => '904270b68a2cc3d54485323652da4d14',
7+
'appId' => '117743971608120',
8+
'secret' => '943716006e74d9b9283d4d5d8ab93204',
99
'cookie' => true,
1010
));
1111

src/facebook.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Facebook
7979
/**
8080
* Version.
8181
*/
82-
const VERSION = '2.0.6';
82+
const VERSION = '2.1.1';
8383

8484
/**
8585
* Default options for curl.
@@ -145,6 +145,11 @@ class Facebook
145145
*/
146146
protected $baseDomain = '';
147147

148+
/**
149+
* Indicates if the CURL based @ syntax for file uploads is enabled.
150+
*/
151+
protected $fileUploadSupport = false;
152+
148153
/**
149154
* Initialize a Facebook Application.
150155
*
@@ -153,6 +158,7 @@ class Facebook
153158
* - secret: the application secret
154159
* - cookie: (optional) boolean true to enable cookie support
155160
* - domain: (optional) domain for the cookie
161+
* - fileUpload: (optional) boolean indicating if file uploads are enabled
156162
*
157163
* @param Array $config the application configuration
158164
*/
@@ -165,6 +171,9 @@ public function __construct($config) {
165171
if (isset($config['domain'])) {
166172
$this->setBaseDomain($config['domain']);
167173
}
174+
if (isset($config['fileUpload'])) {
175+
$this->setFileUploadSupport($config['fileUpload']);
176+
}
168177
}
169178

170179
/**
@@ -243,6 +252,25 @@ public function getBaseDomain() {
243252
return $this->baseDomain;
244253
}
245254

255+
/**
256+
* Set the file upload support status.
257+
*
258+
* @param String $domain the base domain
259+
*/
260+
public function setFileUploadSupport($fileUploadSupport) {
261+
$this->fileUploadSupport = $fileUploadSupport;
262+
return $this;
263+
}
264+
265+
/**
266+
* Get the file upload support status.
267+
*
268+
* @return String the base domain
269+
*/
270+
public function useFileUploadSupport() {
271+
return $this->fileUploadSupport;
272+
}
273+
246274
/**
247275
* Get the data from a signed_request token
248276
*
@@ -538,7 +566,11 @@ protected function makeRequest($url, $params, $ch=null) {
538566
}
539567

540568
$opts = self::$CURL_OPTS;
541-
$opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
569+
if ($this->useFileUploadSupport()) {
570+
$opts[CURLOPT_POSTFIELDS] = $params;
571+
} else {
572+
$opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
573+
}
542574
$opts[CURLOPT_URL] = $url;
543575

544576
// disable the 'Expect: 100-continue' behaviour. This causes CURL to wait

tests/tests.php

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77

88
class FacebookTest extends PHPUnit_Framework_TestCase
99
{
10-
const APP_ID = '254752073152';
11-
const SECRET = '904270b68a2cc3d54485323652da4d14';
10+
const APP_ID = '117743971608120';
11+
const SECRET = '943716006e74d9b9283d4d5d8ab93204';
1212

1313
private static $VALID_EXPIRED_SESSION = array(
14-
'access_token' => '254752073152|2.I_eTFkcTKSzX5no3jI4r1Q__.3600.1273359600-1677846385|uI7GwrmBUed8seZZ05JbdzGFUpk.',
15-
'expires' => '1273359600',
16-
'secret' => '0d9F7pxWjM_QakY_51VZqw__',
17-
'session_key' => '2.I_eTFkcTKSzX5no3jI4r1Q__.3600.1273359600-1677846385',
18-
'sig' => '9f6ae89510b30dddb3f864f3caf32fb3',
14+
'access_token' => '117743971608120|2.vdCKd4ZIEJlHwwtrkilgKQ__.86400.1281049200-1677846385|NF_2DDNxFBznj2CuwiwabHhTAHc.',
15+
'expires' => '1281049200',
16+
'secret' => 'u0QiRGAwaPCyQ7JE_hiz1w__',
17+
'session_key' => '2.vdCKd4ZIEJlHwwtrkilgKQ__.86400.1281049200-1677846385',
18+
'sig' => '7a9b063de0bef334637832166948dcad',
1919
'uid' => '1677846385',
2020
);
2121

22-
private static $VALID_SIGNED_REQUEST = 'ZcZocIFknCpcTLhwsRwwH5nL6oq7OmKWJx41xRTi59E.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOiIxMjczMzU5NjAwIiwib2F1dGhfdG9rZW4iOiIyNTQ3NTIwNzMxNTJ8Mi5JX2VURmtjVEtTelg1bm8zakk0cjFRX18uMzYwMC4xMjczMzU5NjAwLTE2Nzc4NDYzODV8dUk3R3dybUJVZWQ4c2VaWjA1SmJkekdGVXBrLiIsInNlc3Npb25fa2V5IjoiMi5JX2VURmtjVEtTelg1bm8zakk0cjFRX18uMzYwMC4xMjczMzU5NjAwLTE2Nzc4NDYzODUiLCJ1c2VyX2lkIjoiMTY3Nzg0NjM4NSJ9';
23-
private static $NON_TOSSED_SIGNED_REQUEST = 'laEjO-az9kzgFOUldy1G7EyaP6tMQEsbFIDrB1RUamE.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiJ9';
22+
private static $VALID_SIGNED_REQUEST = '1sxR88U4SW9m6QnSxwCEw_CObqsllXhnpP5j2pxD97c.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEyODEwNTI4MDAsIm9hdXRoX3Rva2VuIjoiMTE3NzQzOTcxNjA4MTIwfDIuVlNUUWpub3hYVVNYd1RzcDB1U2g5d19fLjg2NDAwLjEyODEwNTI4MDAtMTY3Nzg0NjM4NXx4NURORHBtcy1nMUM0dUJHQVYzSVdRX2pYV0kuIiwidXNlcl9pZCI6IjE2Nzc4NDYzODUifQ';
23+
private static $NON_TOSSED_SIGNED_REQUEST = 'c0Ih6vYvauDwncv0n0pndr0hP0mvZaJPQDPt6Z43O0k.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiJ9';
2424

2525
public function testConstructor() {
2626
$facebook = new Facebook(array(
@@ -49,6 +49,20 @@ public function testConstructorWithCookie() {
4949
'Expect Cookie support to be on.');
5050
}
5151

52+
public function testConstructorWithFileUpload() {
53+
$facebook = new Facebook(array(
54+
'appId' => self::APP_ID,
55+
'secret' => self::SECRET,
56+
'fileUpload' => true,
57+
));
58+
$this->assertEquals($facebook->getAppId(), self::APP_ID,
59+
'Expect the App ID to be set.');
60+
$this->assertEquals($facebook->getApiSecret(), self::SECRET,
61+
'Expect the API secret to be set.');
62+
$this->assertTrue($facebook->useFileUploadSupport(),
63+
'Expect file upload support to be on.');
64+
}
65+
5266
public function testSetAppId() {
5367
$facebook = new Facebook(array(
5468
'appId' => self::APP_ID,
@@ -104,6 +118,18 @@ public function testIgnoreDeleteSetCookie() {
104118
'Expect Cookie to not exist.');
105119
}
106120

121+
public function testSetFileUploadSupport() {
122+
$facebook = new Facebook(array(
123+
'appId' => self::APP_ID,
124+
'secret' => self::SECRET,
125+
));
126+
$this->assertFalse($facebook->useFileUploadSupport(),
127+
'Expect file upload support to be off.');
128+
$facebook->setFileUploadSupport(true);
129+
$this->assertTrue($facebook->useFileUploadSupport(),
130+
'Expect file upload support to be on.');
131+
}
132+
107133
public function testSetNullSession() {
108134
$facebook = new Facebook(array(
109135
'appId' => self::APP_ID,
@@ -345,7 +371,8 @@ public function testGraphAPIWithOnlyParams() {
345371
'secret' => self::SECRET,
346372
));
347373

348-
$response = $facebook->api('/platform/feed', array('limit' => 1));
374+
$response = $facebook->api('/platform/feed',
375+
array('limit' => 1, 'access_token' => ''));
349376
$this->assertEquals(1, count($response['data']), 'should get one entry');
350377
$this->assertTrue(
351378
strstr($response['paging']['next'], 'limit=1') !== false,
@@ -613,15 +640,23 @@ public function testSignedToken() {
613640
$payload = $facebook->publicParseSignedRequest(self::$VALID_SIGNED_REQUEST);
614641
$this->assertNotNull($payload, 'Expected token to parse');
615642
$session = $facebook->publicCreateSessionFromSignedRequest($payload);
616-
foreach (array('uid', 'access_token') as $key) {
617-
$this->assertEquals($session[$key], self::$VALID_EXPIRED_SESSION[$key]);
618-
}
643+
$this->assertEquals($session['uid'], self::$VALID_EXPIRED_SESSION['uid']);
619644
$this->assertEquals($facebook->getSignedRequest(), null);
620645
$_REQUEST['signed_request'] = self::$VALID_SIGNED_REQUEST;
621646
$this->assertEquals($facebook->getSignedRequest(), $payload);
622647
unset($_REQUEST['signed_request']);
623648
}
624649

650+
public function testSignedTokenInQuery() {
651+
$facebook = new Facebook(array(
652+
'appId' => self::APP_ID,
653+
'secret' => self::SECRET,
654+
));
655+
$_REQUEST['signed_request'] = self::$VALID_SIGNED_REQUEST;
656+
$this->assertNotNull($facebook->getSession());
657+
unset($_REQUEST['signed_request']);
658+
}
659+
625660
public function testNonTossedSignedtoken() {
626661
$facebook = new FBPublic(array(
627662
'appId' => self::APP_ID,

0 commit comments

Comments
 (0)