1
1
<?php
2
2
/**
3
- * Get API there: https://www.flickr.com/services/apps/create/noncommercial/
3
+ * Get API there: https://www.flickr.com/services/apps/create/noncommercial/.
4
4
*
5
5
* Sometimes yahoo require captcha for login,
6
6
* the plugin can't bypass then it trigger an error message.
9
9
*
10
10
* @update May 04, 2015
11
11
*/
12
-
13
12
class ChipVN_ImageUploader_Plugins_Flickr extends ChipVN_ImageUploader_Plugins_Abstract
14
13
{
15
14
const REQUEST_TOKEN_ENPOINT = 'https://www.flickr.com/services/oauth/request_token ' ;
16
- const AUTH_ENDPOINT = 'https://www.flickr.com/services/oauth/authorize ' ;
15
+ const AUTH_ENDPOINT = 'https://www.flickr.com/services/oauth/authorize ' ;
17
16
const ACCESS_TOKEN_ENDPOINT = 'https://www.flickr.com/services/oauth/access_token ' ;
18
- const API_ENDPOINT = 'https://www.flickr.com/services/rest ' ;
19
- const UPLOAD_ENDPOINT = 'https://www.flickr.com/services/upload/ ' ;
17
+ const API_ENDPOINT = 'https://www.flickr.com/services/rest ' ;
18
+ const UPLOAD_ENDPOINT = 'https://www.flickr.com/services/upload/ ' ;
20
19
21
- const REQUEST_OAUTH_TOKEN = 'request_oauth_token ' ;
22
- const REQUEST_OAUTH_SECRET = 'request_oauth_secret ' ;
23
- const ACCESS_OAUTH_TOKEN = 'access_oauth_token ' ;
24
- const ACCESS_OAUTH_SECRET = 'access_oauth_secret ' ;
20
+ const REQUEST_OAUTH_TOKEN = 'request_oauth_token ' ;
21
+ const REQUEST_OAUTH_SECRET = 'request_oauth_secret ' ;
22
+ const ACCESS_OAUTH_TOKEN = 'access_oauth_token ' ;
23
+ const ACCESS_OAUTH_SECRET = 'access_oauth_secret ' ;
25
24
26
25
/**
27
26
* API secret.
@@ -45,9 +44,10 @@ class ChipVN_ImageUploader_Plugins_Flickr extends ChipVN_ImageUploader_Plugins_A
45
44
protected $ accessSecret ;
46
45
47
46
/**
48
- * Set API secret
47
+ * Set API secret.
48
+ *
49
+ * @param string $secret
49
50
*
50
- * @param string $secret
51
51
* @return void
52
52
*/
53
53
public function setSecret ($ secret )
@@ -58,12 +58,13 @@ public function setSecret($secret)
58
58
/**
59
59
* Set access token.
60
60
*
61
- * @param string $token
61
+ * @param string $token
62
+ *
62
63
* @return void
63
64
*/
64
65
public function setAccessToken ($ token , $ secret )
65
66
{
66
- $ this ->accessToken = $ token ;
67
+ $ this ->accessToken = $ token ;
67
68
$ this ->accessSecret = $ secret ;
68
69
}
69
70
@@ -100,7 +101,8 @@ public function getAccessSecret()
100
101
* This method will direct user to flickr to authorize
101
102
* after success, flickr will direct user back to App url.
102
103
*
103
- * @param string $callback
104
+ * @param string $callback
105
+ *
104
106
* @return array
105
107
*/
106
108
public function getOAuthToken ($ callback = 'http://ptcong.com ' )
@@ -121,95 +123,31 @@ public function getOAuthToken($callback = 'http://ptcong.com')
121
123
/**
122
124
* Direct user to Flickr to get authorisation.
123
125
*
124
- * @param string $callback
126
+ * @param string $callback
127
+ *
125
128
* @return void
126
129
*/
127
130
protected function requestToken ($ callback )
128
131
{
129
132
$ url = $ this ->getRequestTokenUrl ($ callback );
130
133
131
134
if (headers_sent ()) {
132
- echo '<meta http-equiv="refresh" content="0; url= ' .$ url .'"><script type="text/javascript">window.location.href = " ' .$ url .'";</script> ' ;
135
+ echo '<meta http-equiv="refresh" content="0; url= ' .$ url .'"> '
136
+ .'<script type="text/javascript">window.location.href = " ' .$ url .'";</script> ' ;
133
137
} else {
134
138
header ('Location: ' .$ url );
135
139
}
136
140
exit ;
137
141
}
138
142
139
143
/**
140
- * {@inheritdoc}
144
+ * We use OAuth so don't need username, password
145
+ * for authenication.
146
+ *
147
+ * @return bool.
141
148
*/
142
149
protected function doLogin ()
143
150
{
144
- if ($ this ->getAccessToken ()) {
145
- return true ;
146
- }
147
-
148
- // try to automatic bypass yahoo login form
149
- $ requestTokenUrl = $ this ->getRequestTokenUrl ('http://ptcong.com ' );
150
-
151
- // follow to login form
152
- $ formRequest = $ this ->createHttpClient ()
153
- ->setFollowRedirect (true , 3 )
154
- ->execute ($ requestTokenUrl );
155
-
156
- $ text = $ formRequest ;
157
- $ pos = stripos ($ text , 'id="login_form" ' );
158
- $ form = substr ($ text , $ pos );
159
- $ form = substr ($ form , 0 , stripos ($ form , '</fieldset ' ));
160
-
161
- // find login form fields.
162
- preg_match_all ('#name=(?: \'|")(.*?)(?: \'|").*?value=(?: \'|")(.*?)(?: \'|")#is ' , $ form , $ matches );
163
- $ params = array_combine ($ matches [1 ], $ matches [2 ]);
164
- $ params ['login ' ] = $ this ->username ;
165
- $ params ['passwd ' ] = $ this ->password ;
166
- $ params ['passwd_raw ' ] = '' ;
167
- $ params ['.save ' ] = '' ;
168
- // $params['.ws'] = 1; // ajax
169
- ksort ($ params );
170
-
171
- // submit login form
172
- $ authRequest = $ this ->createHttpClient ()
173
- ->setFollowRedirect (true , 4 )
174
- ->setParameters ($ params )
175
- ->setReferer ($ formRequest ->getTarget ())
176
- ->setCookies ($ formRequest ->getResponseArrayCookies ())
177
- ->setMethod ('POST ' )
178
- ->execute ('https://login.yahoo.com/config/login ' );
179
-
180
- if (!strpos ($ authRequest , '/services/auth/ ' )) {
181
- if (stripos ($ authRequest , 'Javascript enabled ' )) {
182
- $ this ->throwException ('%s: Yahoo detected automatic sign in and try to restrict. Please run script get AUTH_TOKEN, AUTH_SECRET then call setAccessToken($token, $secret) before. ' , __METHOD__ );
183
- }
184
- print_r ($ authRequest );
185
- $ this ->throwException ('%s: Cannot reach the Flickr Authorization page or your account is incorrect. ' , __METHOD__ );
186
- }
187
-
188
- // go to flickr to authorize
189
- if (!$ pos = strpos ($ text = $ authRequest , '/services/oauth/authorize.gne ' )) {
190
- $ this ->throwException ('%s: Cannot bypass Flickr authorization. ' , __METHOD__ );
191
- }
192
- $ form = substr ($ text , $ pos );
193
- $ form = substr ($ form , 0 , strpos ($ form , '</form ' ));
194
- $ cookies = $ authRequest ->getCookies ();
195
-
196
- preg_match_all ('#input.*?name="(.*?)".*?value="(.*?)"#is ' , $ form , $ matches );
197
- $ params = array_combine ($ matches [1 ], $ matches [2 ]);
198
-
199
- $ this ->resetHttpClient ()
200
- ->setCookies ($ cookies )
201
- ->setParameters ($ params )
202
- ->setMethod ('POST ' )
203
- ->execute ('https://www.flickr.com//services/oauth/authorize.gne ' );
204
-
205
- $ location = $ this ->client ->getResponseHeaders ('location ' );
206
- if (!($ requestAuthToken = $ this ->getMatch ('#oauth_token=([\w-]+)#i ' , $ location ))
207
- || !($ requestAuthVerifier = $ this ->getMatch ('#oauth_verifier=([\w-]+)#i ' , $ location ))
208
- ) {
209
- $ this ->throwException ('%s: Not found "oauth_token" and "oauth_verifier" . ' , __METHOD__ );
210
- }
211
- $ this ->getOAuthAccessToken ($ requestAuthToken , $ requestAuthVerifier );
212
-
213
151
return true ;
214
152
}
215
153
@@ -247,7 +185,7 @@ protected function doUpload()
247
185
$ this ->throwException ('UPLOAD_PROBLEM: "%s" ' , $ result ['oauth_problem ' ]);
248
186
} else {
249
187
$ error = $ this ->getMatch ('#code="(.+?)"# ' , $ this ->client );
250
- $ msg = $ this ->getMatch ('#msg="(.+?)"# ' , $ this ->client );
188
+ $ msg = $ this ->getMatch ('#msg="(.+?)"# ' , $ this ->client );
251
189
$ this ->throwException ('UPLOAD_PROBLEM: "%s" (%d) ' , $ msg , $ error );
252
190
}
253
191
}
@@ -260,9 +198,10 @@ protected function doUpload()
260
198
261
199
/**
262
200
* Get photo url.
263
- * {@link https://www.flickr.com/services/api/misc.urls.html}
201
+ * {@link https://www.flickr.com/services/api/misc.urls.html}.
202
+ *
203
+ * @param array $info
264
204
*
265
- * @param array $info
266
205
* @return string
267
206
*/
268
207
protected function getPhotoUrl (array $ photo )
@@ -290,15 +229,16 @@ protected function doTransload()
290
229
/**
291
230
* Prepare oauth request data and return url, parameters.
292
231
*
293
- * @param string $url_endpoint
294
- * @param string $method
295
- * @param array $params
232
+ * @param string $url_endpoint
233
+ * @param string $method
234
+ * @param array $params
235
+ *
296
236
* @return array
297
237
*/
298
238
protected function prepareOAuthRequestData ($ url_endpoint , $ method = 'GET ' , $ params = array (), $ secretKey2 = null )
299
239
{
300
240
$ baseString = $ this ->getBaseString ($ url_endpoint , $ method , $ params );
301
- $ params = $ this ->pushSignature ($ params , $ baseString , $ secretKey2 );
241
+ $ params = $ this ->pushSignature ($ params , $ baseString , $ secretKey2 );
302
242
if ($ method == 'GET ' ) {
303
243
$ url = $ url_endpoint .'? ' .http_build_query ($ params );
304
244
} else {
@@ -309,9 +249,10 @@ protected function prepareOAuthRequestData($url_endpoint, $method = 'GET', $para
309
249
}
310
250
311
251
/**
312
- * Get OAuth parameters
252
+ * Get OAuth parameters.
253
+ *
254
+ * @param array $extraParameters
313
255
*
314
- * @param array $extraParameters
315
256
* @return array
316
257
*/
317
258
protected function getParameters (array $ params )
@@ -329,9 +270,10 @@ protected function getParameters(array $params)
329
270
}
330
271
331
272
/**
332
- * Get OAuth base string
273
+ * Get OAuth base string.
274
+ *
275
+ * @param array $parameters
333
276
*
334
- * @param array $parameters
335
277
* @return string
336
278
*/
337
279
protected function getBaseString ($ url , $ method , array $ params )
@@ -342,9 +284,10 @@ protected function getBaseString($url, $method, array $params)
342
284
/**
343
285
* Push OAuth signature.
344
286
*
345
- * @param array $params
346
- * @param string $baseString
347
- * @param string $secretKey2
287
+ * @param array $params
288
+ * @param string $baseString
289
+ * @param string $secretKey2
290
+ *
348
291
* @return void
349
292
*/
350
293
protected function pushSignature (&$ params , $ baseString , $ secretKey2 = null )
@@ -363,7 +306,8 @@ protected function pushSignature(&$params, $baseString, $secretKey2 = null)
363
306
/**
364
307
* Get REQUEST_AUTH_TOKEN url.
365
308
*
366
- * @param string $callback
309
+ * @param string $callback
310
+ *
367
311
* @return string
368
312
*/
369
313
protected function getRequestTokenUrl ($ callback )
@@ -386,7 +330,7 @@ protected function getRequestTokenUrl($callback)
386
330
$ this ->getCache ()->set (self ::REQUEST_OAUTH_TOKEN , $ result ['oauth_token ' ]);
387
331
$ this ->getCache ()->set (self ::REQUEST_OAUTH_SECRET , $ result ['oauth_token_secret ' ]);
388
332
389
- list ($ url ,) = $ this ->prepareOAuthRequestData (self ::AUTH_ENDPOINT , 'GET ' , array (
333
+ list ($ url , ) = $ this ->prepareOAuthRequestData (self ::AUTH_ENDPOINT , 'GET ' , array (
390
334
'oauth_token ' => $ result ['oauth_token ' ],
391
335
'perms ' => 'write ' ,
392
336
));
@@ -397,9 +341,10 @@ protected function getRequestTokenUrl($callback)
397
341
/**
398
342
* Call API to get access token by request auth token, auth verifier.
399
343
*
400
- * @param string $requestAuthToken
401
- * @param string $requestAuthVerifier
402
- * @param null|string $secretKey2 Use REQUEST_OAUTH_SECRET if run in public to get ACCESS_TOKEN
344
+ * @param string $requestAuthToken
345
+ * @param string $requestAuthVerifier
346
+ * @param null|string $secretKey2 Use REQUEST_OAUTH_SECRET if run in public to get ACCESS_TOKEN
347
+ *
403
348
* @return array
404
349
*/
405
350
protected function getOAuthAccessToken ($ requestAuthToken , $ requestAuthVerifier , $ secretKey2 = null )
@@ -429,10 +374,11 @@ protected function getOAuthAccessToken($requestAuthToken, $requestAuthVerifier,
429
374
}
430
375
431
376
/**
432
- * Call Flickr OAuth API
377
+ * Call Flickr OAuth API.
378
+ *
379
+ * @param string $method
380
+ * @param array $params
433
381
*
434
- * @param string $method
435
- * @param array $params
436
382
* @return array
437
383
*
438
384
* @throws Exception
0 commit comments