Skip to content

Commit 8c411ae

Browse files
author
Phan Thanh Cong
committed
update new cacher
1 parent a176f86 commit 8c411ae

File tree

7 files changed

+80
-44
lines changed

7 files changed

+80
-44
lines changed

Manager.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,31 @@ class ChipVN_ImageUploader_Manager
1717
* Create a plugin for uploading.
1818
*
1919
* @param string $plugin
20-
* @return ChipVN_ImageUploaderPlugins_Plugin
20+
* @return ChipVN_ImageUploader_Plugins_Abstract
2121
*/
2222
public static function make($plugin)
2323
{
24-
$class = 'ChipVN_ImageUploader_Plugins_' . ucfirst($plugin);
24+
$prefix = 'ChipVN_ImageUploader_Plugins_';
25+
foreach (array($prefix.'Abstract', $class = $prefix.ucfirst($plugin)) as $name) {
26+
if (!class_exists($name, false)) {
27+
require self::getClassFile($name);
28+
}
29+
}
2530

26-
return new $class;
31+
return new $class();
32+
}
33+
34+
/**
35+
* Gets class file.
36+
*
37+
* @param string $class
38+
* @return string
39+
*/
40+
protected static function getClassFile($class)
41+
{
42+
return strtr($class, array(
43+
'ChipVN' => dirname(dirname(__FILE__)),
44+
'_' => DIRECTORY_SEPARATOR,
45+
)).'.php';
2746
}
2847
}

Plugins/Abstract.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public function getCache()
8888
if (!$this->cache) {
8989
$this->cache = ChipVN_Cache_Manager::make('Session');
9090
}
91-
// update group use real identifier
92-
$this->cache->setOption('group', $this->getIdentifier());
91+
// ensure don't overrides session of other accounts
92+
$this->cache->setOption('prefix', $this->getIdentifier());
9393

9494
return $this->cache;
9595
}
@@ -102,11 +102,11 @@ public function getCache()
102102
*/
103103
public function setCache($cache = '', array $options = array())
104104
{
105-
if ($cache instanceof ChipVN_Cache_Adapter_Interface) {
105+
if ($cache instanceof ChipVN_Cache_Adapter_Abstract) {
106106
$this->cache = $cache;
107107
} elseif (is_string($cache)) {
108108
$this->cache = ChipVN_Cache_Manager::make($cache, $options + array(
109-
'group' => $this->getIdentifier(),
109+
'prefix' => $this->getIdentifier(),
110110
));
111111
}
112112

@@ -225,7 +225,7 @@ final public function transload($url)
225225
*/
226226
final public function getIdentifier()
227227
{
228-
return md5($this->getName().$this->username.$this->password);
228+
return substr(md5($this->getName().$this->username.$this->password), 0, 5);
229229
}
230230

231231
/**

Plugins/Flickr.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* the plugin can't bypass then it trigger an error message.
77
* You should run script get AUTH_TOKEN, AUTH_SECRET
88
* then call setAccessToken($token, $secret) before.
9+
*
10+
* @lastupdate Jan 20, 2015
911
*/
1012

1113
class ChipVN_ImageUploader_Plugins_Flickr extends ChipVN_ImageUploader_Plugins_Abstract

Plugins/Imageshack.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
* Register an API here: {@link https://imageshack.com/contact/api}.
55
* You must login and have an API for uploading, transloading.
66
*
7-
* @update Jul 10, 2014
7+
* @lastupdate Jan 20, 2015
88
*/
99

1010
class ChipVN_ImageUploader_Plugins_Imageshack extends ChipVN_ImageUploader_Plugins_Abstract
1111
{
1212
const LOGIN_ENDPOINT = 'https://imageshack.com/rest_api/v2/user/login';
1313
const UPLOAD_ENPOINT = 'https://imageshack.com/rest_api/v2/images';
1414

15+
const SESSION_LOGIN = 'session_login';
16+
1517
/**
1618
* Get API endpoint URL.
1719
*
@@ -29,7 +31,7 @@ protected function getApiURL($path)
2931
protected function doLogin()
3032
{
3133
// session_login is array
32-
if (!$this->getCache()->get('session_login')) {
34+
if (!$this->getCache()->has(self::SESSION_LOGIN)) {
3335
$this->resetHttpClient()
3436
->setReferer('https://imageshack.com/')
3537
->setParameters(array(
@@ -44,14 +46,15 @@ protected function doLogin()
4446
$result = json_decode($this->client, true);
4547

4648
if (!empty($result['result']['userid'])) {
47-
$this->getCache()->set('session_login', $result['result']);
49+
$this->getCache()->set(self::SESSION_LOGIN, $result['result']);
4850
} else {
4951
if (isset($result['error']['error_message'])) {
5052
$message = $result['error']['error_message'];
5153
} else {
5254
$message = 'Login failed.';
5355
}
54-
$this->getCache()->deleteGroup($this->getIdentifier());
56+
$this->getCache()->delete(self::SESSION_LOGIN);
57+
5558
$this->throwException('%s: %s.', __METHOD__, $message); // $this->client
5659
}
5760
}
@@ -85,13 +88,13 @@ protected function doTransload()
8588
*/
8689
private function sendRequest(array $param)
8790
{
88-
if (!$this->getCache()->get('session_login') || empty($this->apiKey)) {
91+
if (!$this->getCache()->has(self::SESSION_LOGIN) || empty($this->apiKey)) {
8992
$this->throwException(
9093
'You must be loggedin and have an API key. Register API here: https://imageshack.com/contact/api'
9194
);
9295
}
9396

94-
$session = $this->getCache()->get('session_login');
97+
$session = $this->getCache()->get(self::SESSION_LOGIN);
9598

9699
$this->resetHttpClient()
97100
->setSubmitMultipart()

Plugins/Imgur.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@
33
* You may upload to your account or without account.
44
*
55
* Update Oct 06, 2014: Use API ver 3
6+
*
7+
* @lastupdate Jan 20, 2015
68
*/
79

810
class ChipVN_ImageUploader_Plugins_Imgur extends ChipVN_ImageUploader_Plugins_Abstract
911
{
10-
const AUTH_ENDPOINT = 'https://api.imgur.com/oauth2/authorize';
11-
const TOKEN_ENDPOINT = 'https://api.imgur.com/oauth2/token';
12-
const UPLOAD_ENPOINT = 'https://api.imgur.com/3/image';
12+
const AUTH_ENDPOINT = 'https://api.imgur.com/oauth2/authorize';
13+
const TOKEN_ENDPOINT = 'https://api.imgur.com/oauth2/token';
14+
const UPLOAD_ENPOINT = 'https://api.imgur.com/3/image';
15+
const START_SESSION_ENDPOINT = 'http://imgur.com/upload/start_session';
16+
17+
const ACCESS_TOKEN = 'access_token';
18+
const REFRESH_TOKEN = 'refresh_token';
1319

14-
const ACCESS_TOKEN = 'access_token';
15-
const REFRESH_TOKEN = 'refresh_token';
20+
const FREE_SESSION_ID = 'free_session_id';
21+
const FREE_SESSION = 'free_session';
1622

1723
/**
1824
* Client secret.
@@ -36,7 +42,7 @@ public function setSecret($secret)
3642
*/
3743
protected function doLogin()
3844
{
39-
if (!$this->getCache()->get(self::ACCESS_TOKEN)) {
45+
if (!$this->getCache()->has(self::ACCESS_TOKEN)) {
4046
if ($this->refreshToken()) {
4147
return true;
4248
}
@@ -263,21 +269,21 @@ private function handleJsonData($file, $method, $result, $errorMessage = null)
263269
*/
264270
private function getFreeSession()
265271
{
266-
if (!$this->getCache()->get('free_sid')) {
267-
$this->resetHttpClient()->execute('http://imgur.com/upload/start_session');
272+
if (!$this->getCache()->has(self::FREE_SESSION_ID)) {
273+
$this->resetHttpClient()->execute(self::START_SESSION_ENDPOINT);
268274

269275
$this->checkHttpClientErrors(__METHOD__);
270276
$result = json_decode($this->client, true);
271277

272278
if (isset($result['sid'])) {
273-
$this->getCache()->set('free_sid', $result['sid']);
274-
$this->getCache()->set('free_session', $this->client->getResponseCookies());
279+
$this->getCache()->set(self::FREE_SESSION_ID, $result['sid']);
280+
$this->getCache()->set(self::FREE_SESSION, $this->client->getResponseCookies());
275281
} else {
276282
$this->throwException('%s: Cannot get free IMGURSESSION.', __METHOD__);
277283
}
278284
}
279285

280-
return array($this->getCache()->get('free_sid'), $this->getCache()->get('free_session'));
286+
return array($this->getCache()->get(self::FREE_SESSION_ID), $this->getCache()->get(self::FREE_SESSION));
281287
}
282288

283289
/**

Plugins/Picasa.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
* You must loggedin for uploading.
44
* This plugin doesn't support transloading.
55
*
6-
* @lastupdate May 27, 2014
6+
* @lastupdate Jan 20, 2015
77
*/
88

99
class ChipVN_ImageUploader_Plugins_Picasa extends ChipVN_ImageUploader_Plugins_Abstract
1010
{
11-
const API_ENDPOINT = 'https://picasaweb.google.com/data/feed/api';
12-
const PATH_USER = 'user';
13-
const PATH_ALBUMID = 'albumid';
11+
const CLIENT_LOGIN_ENDPOINT = 'https://www.google.com/accounts/ClientLogin';
12+
const API_ENDPOINT = 'https://picasaweb.google.com/data/feed/api';
13+
const PATH_USER = 'user';
14+
const PATH_ALBUMID = 'albumid';
15+
16+
const SESSION_LOGIN = 'session_login';
1417

1518
/*
1619
* AlbumId to archive image.
@@ -34,7 +37,7 @@ protected function doLogin()
3437
// normalize username
3538
$this->username = preg_replace('#@gmail\.com#i', '', $this->username);
3639

37-
if (!$this->getCache()->get('session_login')) {
40+
if (!$this->getCache()->has(self::SESSION_LOGIN)) {
3841
$this->resetHttpClient()
3942
->setParameters(array(
4043
'accountType' => 'HOSTED_OR_GOOGLE',
@@ -43,21 +46,21 @@ protected function doLogin()
4346
'source' => self::POWERED_BY,
4447
'service' => 'lh2',
4548
))
46-
->execute('https://www.google.com/accounts/ClientLogin', 'POST');
49+
->execute(self::CLIENT_LOGIN_ENDPOINT, 'POST');
4750

4851
$this->checkHttpClientErrors(__METHOD__);
4952

5053
if ($cookie = $this->getMatch('#Auth=([a-z0-9_\-]+)#i', $this->client)) {
51-
$this->getCache()->set('session_login', $cookie, 900);
54+
$this->getCache()->set(self::SESSION_LOGIN, $cookie, 900);
5255
} elseif (
5356
($error = $this->getMatch('#Error=(.+)#i', $this->client))
5457
&& ($info = $this->getMatch('#Info=(.+)#i', $this->client))
5558
) {
56-
$this->getCache()->deleteGroup($this->getIdentifier());
59+
$this->getCache()->delete(self::SESSION_LOGIN);
5760

5861
$this->throwException('%s: Error=%s. Info=%s', __METHOD__, $error, $info);
5962
} else {
60-
$this->getCache()->deleteGroup($this->getIdentifier());
63+
$this->getCache()->delete(self::SESSION_LOGIN);
6164

6265
$this->throwException('%s: Login failed.', __METHOD__);
6366
}
@@ -177,7 +180,7 @@ private function getAlbumEndpoint($albumId)
177180
*/
178181
private function checkPermission($method)
179182
{
180-
if (!$this->getCache()->get('session_login')) {
183+
if (!$this->getCache()->has(self::SESSION_LOGIN)) {
181184
$this->throwException('You must be logged in before call the method "%s"', $method);
182185
}
183186
}
@@ -190,7 +193,7 @@ private function checkPermission($method)
190193
private function getGeneralHeaders()
191194
{
192195
return array(
193-
"Authorization: GoogleLogin auth=".$this->getCache()->get('session_login'),
196+
"Authorization: GoogleLogin auth=".$this->getCache()->get(self::SESSION_LOGIN),
194197
"MIME-Version: 1.0",
195198
);
196199
}

Plugins/Postimage.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@
33
* Plugin for http://postimage.org
44
*
55
* @release Jun 19, 2014
6-
* @update Nov 07, 2014
6+
* @lastupdate Jan 20, 2015
77
*/
88
class ChipVN_ImageUploader_Plugins_Postimage extends ChipVN_ImageUploader_Plugins_Abstract
99
{
1010
const FREE_UPLOAD_ENPOINT = 'http://postimage.org/';
1111
const ACCOUNT_UPLOAD_ENPOINT = 'http://postimg.org/';
12+
13+
const SESSION_LOGIN = 'session_login';
14+
1215
/**
1316
* Gets upload url endpoint
1417
*
1518
* @return string
1619
*/
1720
private function getUrlEnpoint()
1821
{
19-
return $this->getCache()->get('session_login')
22+
return $this->getCache()->has(self::SESSION_LOGIN)
2023
? self::ACCOUNT_UPLOAD_ENPOINT
2124
: self::FREE_UPLOAD_ENPOINT;
2225
}
@@ -26,7 +29,7 @@ private function getUrlEnpoint()
2629
*/
2730
protected function doLogin()
2831
{
29-
if (!$this->getCache()->get('session_login')) {
32+
if (!$this->getCache()->has(self::SESSION_LOGIN)) {
3033
$this->resetHttpClient()
3134
->setParameters(array(
3235
'login' => $this->username,
@@ -40,9 +43,9 @@ protected function doLogin()
4043
if ($this->client->getResponseStatus() == 302
4144
&& $this->client->getResponseArrayCookies('userlogin') != 'deleted'
4245
) {
43-
$this->getCache()->set('session_login', $this->client->getResponseArrayCookies());
46+
$this->getCache()->set(self::SESSION_LOGIN, $this->client->getResponseArrayCookies());
4447
} else {
45-
$this->getCache()->deleteGroup($this->getIdentifier());
48+
$this->getCache()->delete(self::SESSION_LOGIN);
4649
$this->throwException('%s: Login failed.', __METHOD__);
4750
}
4851
}
@@ -60,7 +63,7 @@ protected function doUpload()
6063

6164
$this->resetHttpClient();
6265
if ($this->useAccount) {
63-
$this->client->setCookies($this->getCache()->get('session_login'));
66+
$this->client->setCookies($this->getCache()->get(self::SESSION_LOGIN));
6467
}
6568
$this->client
6669
->setSubmitMultipart()
@@ -79,7 +82,7 @@ protected function doUpload()
7982

8083
$this->resetHttpClient();
8184
if ($this->useAccount) {
82-
$this->client->setCookies($this->getCache()->get('session_login'));
85+
$this->client->setCookies($this->getCache()->get(self::SESSION_LOGIN));
8386
}
8487
$this->client
8588
->setReferer($endpoint)
@@ -108,7 +111,7 @@ protected function doTransload()
108111

109112
$this->resetHttpClient();
110113
if ($this->useAccount) {
111-
$this->client->setCookies($this->getCache()->get('session_login'));
114+
$this->client->setCookies($this->getCache()->get(self::SESSION_LOGIN));
112115
}
113116
$this->client->setReferer($endpoint)
114117
->setParameters(array(

0 commit comments

Comments
 (0)