Skip to content

Commit 6fccce0

Browse files
author
ptcong
committed
update 5.2.15
1 parent 2605895 commit 6fccce0

File tree

3 files changed

+442
-109
lines changed

3 files changed

+442
-109
lines changed

Plugins/Flickr.php

Lines changed: 54 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Get API there: https://www.flickr.com/services/apps/create/noncommercial/
3+
* Get API there: https://www.flickr.com/services/apps/create/noncommercial/.
44
*
55
* Sometimes yahoo require captcha for login,
66
* the plugin can't bypass then it trigger an error message.
@@ -9,19 +9,18 @@
99
*
1010
* @update May 04, 2015
1111
*/
12-
1312
class ChipVN_ImageUploader_Plugins_Flickr extends ChipVN_ImageUploader_Plugins_Abstract
1413
{
1514
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';
1716
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/';
2019

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';
2524

2625
/**
2726
* API secret.
@@ -45,9 +44,10 @@ class ChipVN_ImageUploader_Plugins_Flickr extends ChipVN_ImageUploader_Plugins_A
4544
protected $accessSecret;
4645

4746
/**
48-
* Set API secret
47+
* Set API secret.
48+
*
49+
* @param string $secret
4950
*
50-
* @param string $secret
5151
* @return void
5252
*/
5353
public function setSecret($secret)
@@ -58,12 +58,13 @@ public function setSecret($secret)
5858
/**
5959
* Set access token.
6060
*
61-
* @param string $token
61+
* @param string $token
62+
*
6263
* @return void
6364
*/
6465
public function setAccessToken($token, $secret)
6566
{
66-
$this->accessToken = $token;
67+
$this->accessToken = $token;
6768
$this->accessSecret = $secret;
6869
}
6970

@@ -100,7 +101,8 @@ public function getAccessSecret()
100101
* This method will direct user to flickr to authorize
101102
* after success, flickr will direct user back to App url.
102103
*
103-
* @param string $callback
104+
* @param string $callback
105+
*
104106
* @return array
105107
*/
106108
public function getOAuthToken($callback = 'http://ptcong.com')
@@ -121,95 +123,31 @@ public function getOAuthToken($callback = 'http://ptcong.com')
121123
/**
122124
* Direct user to Flickr to get authorisation.
123125
*
124-
* @param string $callback
126+
* @param string $callback
127+
*
125128
* @return void
126129
*/
127130
protected function requestToken($callback)
128131
{
129132
$url = $this->getRequestTokenUrl($callback);
130133

131134
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>';
133137
} else {
134138
header('Location: '.$url);
135139
}
136140
exit;
137141
}
138142

139143
/**
140-
* {@inheritdoc}
144+
* We use OAuth so don't need username, password
145+
* for authenication.
146+
*
147+
* @return bool.
141148
*/
142149
protected function doLogin()
143150
{
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-
213151
return true;
214152
}
215153

@@ -247,7 +185,7 @@ protected function doUpload()
247185
$this->throwException('UPLOAD_PROBLEM: "%s"', $result['oauth_problem']);
248186
} else {
249187
$error = $this->getMatch('#code="(.+?)"#', $this->client);
250-
$msg = $this->getMatch('#msg="(.+?)"#', $this->client);
188+
$msg = $this->getMatch('#msg="(.+?)"#', $this->client);
251189
$this->throwException('UPLOAD_PROBLEM: "%s" (%d)', $msg, $error);
252190
}
253191
}
@@ -260,9 +198,10 @@ protected function doUpload()
260198

261199
/**
262200
* 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
264204
*
265-
* @param array $info
266205
* @return string
267206
*/
268207
protected function getPhotoUrl(array $photo)
@@ -290,15 +229,16 @@ protected function doTransload()
290229
/**
291230
* Prepare oauth request data and return url, parameters.
292231
*
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+
*
296236
* @return array
297237
*/
298238
protected function prepareOAuthRequestData($url_endpoint, $method = 'GET', $params = array(), $secretKey2 = null)
299239
{
300240
$baseString = $this->getBaseString($url_endpoint, $method, $params);
301-
$params = $this->pushSignature($params, $baseString, $secretKey2);
241+
$params = $this->pushSignature($params, $baseString, $secretKey2);
302242
if ($method == 'GET') {
303243
$url = $url_endpoint.'?'.http_build_query($params);
304244
} else {
@@ -309,9 +249,10 @@ protected function prepareOAuthRequestData($url_endpoint, $method = 'GET', $para
309249
}
310250

311251
/**
312-
* Get OAuth parameters
252+
* Get OAuth parameters.
253+
*
254+
* @param array $extraParameters
313255
*
314-
* @param array $extraParameters
315256
* @return array
316257
*/
317258
protected function getParameters(array $params)
@@ -329,9 +270,10 @@ protected function getParameters(array $params)
329270
}
330271

331272
/**
332-
* Get OAuth base string
273+
* Get OAuth base string.
274+
*
275+
* @param array $parameters
333276
*
334-
* @param array $parameters
335277
* @return string
336278
*/
337279
protected function getBaseString($url, $method, array $params)
@@ -342,9 +284,10 @@ protected function getBaseString($url, $method, array $params)
342284
/**
343285
* Push OAuth signature.
344286
*
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+
*
348291
* @return void
349292
*/
350293
protected function pushSignature(&$params, $baseString, $secretKey2 = null)
@@ -363,7 +306,8 @@ protected function pushSignature(&$params, $baseString, $secretKey2 = null)
363306
/**
364307
* Get REQUEST_AUTH_TOKEN url.
365308
*
366-
* @param string $callback
309+
* @param string $callback
310+
*
367311
* @return string
368312
*/
369313
protected function getRequestTokenUrl($callback)
@@ -386,7 +330,7 @@ protected function getRequestTokenUrl($callback)
386330
$this->getCache()->set(self::REQUEST_OAUTH_TOKEN, $result['oauth_token']);
387331
$this->getCache()->set(self::REQUEST_OAUTH_SECRET, $result['oauth_token_secret']);
388332

389-
list($url,) = $this->prepareOAuthRequestData(self::AUTH_ENDPOINT, 'GET', array(
333+
list($url, ) = $this->prepareOAuthRequestData(self::AUTH_ENDPOINT, 'GET', array(
390334
'oauth_token' => $result['oauth_token'],
391335
'perms' => 'write',
392336
));
@@ -397,9 +341,10 @@ protected function getRequestTokenUrl($callback)
397341
/**
398342
* Call API to get access token by request auth token, auth verifier.
399343
*
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+
*
403348
* @return array
404349
*/
405350
protected function getOAuthAccessToken($requestAuthToken, $requestAuthVerifier, $secretKey2 = null)
@@ -429,10 +374,11 @@ protected function getOAuthAccessToken($requestAuthToken, $requestAuthVerifier,
429374
}
430375

431376
/**
432-
* Call Flickr OAuth API
377+
* Call Flickr OAuth API.
378+
*
379+
* @param string $method
380+
* @param array $params
433381
*
434-
* @param string $method
435-
* @param array $params
436382
* @return array
437383
*
438384
* @throws Exception

0 commit comments

Comments
 (0)