Skip to content

Commit e55d3a3

Browse files
author
ptcong
committed
get image URLs, CS fixes
1 parent 7a5d63a commit e55d3a3

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

Plugins/Postimage.php

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<?php
22
/**
3-
* Plugin for http://postimage.org
3+
* Plugin for http://postimage.org.
44
*
55
* @release Jun 19, 2014
6-
* @lastupdate Mar 7, 2015
6+
* @update May 29, 2015
77
*/
88
class ChipVN_ImageUploader_Plugins_Postimage extends ChipVN_ImageUploader_Plugins_Abstract
99
{
10-
const FREE_UPLOAD_ENPOINT = 'http://postimage.org/';
10+
const FREE_UPLOAD_ENPOINT = 'http://postimage.org/';
1111
const ACCOUNT_UPLOAD_ENPOINT = 'http://postimg.org/';
1212

13-
const SESSION_LOGIN = 'session_login';
13+
const SESSION_LOGIN = 'session_login';
1414

1515
/**
16-
* Gets upload url endpoint
16+
* Gets upload url endpoint.
1717
*
1818
* @return string
1919
*/
@@ -59,7 +59,8 @@ protected function doLogin()
5959
protected function doUpload()
6060
{
6161
$endpoint = $this->getUrlEnpoint();
62-
$time = time();
62+
$time = time() * 1000 + mt_rand(1, 999);
63+
$maxFileSize = 16777216;
6364

6465
$this->resetHttpClient();
6566
if ($this->useAccount) {
@@ -70,36 +71,40 @@ protected function doUpload()
7071
->setReferer($endpoint)
7172
->setParameters(array(
7273
'upload' => '@'.$this->file,
73-
'session_upload' => $time,
7474
'um' => 'computer',
75+
'gallery_id' => '',
7576
'forumurl' => $endpoint,
7677
'upload_error' => '',
78+
'MAX_FILE_SIZE' => $maxFileSize,
7779
))
7880
->setParameters($this->getGeneralParameters())
7981
->execute($endpoint);
8082

81-
$galleryId = (string) $this->client;
83+
if (!$location = $this->client->getResponseHeaders('location')) {
84+
$galleryId = (string) $this->client;
85+
$this->resetHttpClient();
86+
if ($this->useAccount) {
87+
$this->client->setCookies($this->getCache()->get(self::SESSION_LOGIN));
88+
}
89+
$this->client
90+
->setReferer($endpoint)
91+
->setParameters(array(
92+
'upload[]' => '@'.$this->file,
93+
'session_upload' => $time,
94+
'um' => 'computer',
95+
'forumurl' => $endpoint,
96+
'gallery_id' => $galleryId,
97+
'upload_error' => '',
98+
'MAX_FILE_SIZE' => $maxFileSize
99+
))
100+
->setParameters($this->getGeneralParameters())
101+
->execute($endpoint, 'POST');
102+
$this->checkHttpClientErrors(__METHOD__);
82103

83-
$this->resetHttpClient();
84-
if ($this->useAccount) {
85-
$this->client->setCookies($this->getCache()->get(self::SESSION_LOGIN));
104+
$location = $this->client->getResponseHeaders('location');
86105
}
87-
$this->client
88-
->setReferer($endpoint)
89-
->setParameters(array(
90-
'upload[]' => '',
91-
'session_upload' => $time,
92-
'um' => 'computer',
93-
'forumurl' => $endpoint,
94-
'gallery_id' => $galleryId,
95-
'upload_error' => '',
96-
))
97-
->setParameters($this->getGeneralParameters())
98-
->execute($endpoint, 'POST');
99106

100-
$this->checkHttpClientErrors(__METHOD__);
101-
102-
return $this->getImageFromResult($this->client->getResponseHeaders('location'));
107+
return $this->getImageFromResult($location);
103108
}
104109

105110
/**
@@ -116,7 +121,6 @@ protected function doTransload()
116121
$this->client->setReferer($endpoint)
117122
->setParameters(array(
118123
'forumurl' => $endpoint,
119-
'ui' => $ui,
120124
'um' => 'web',
121125
'url_list' => $this->url,
122126
))
@@ -136,7 +140,7 @@ protected function doTransload()
136140
protected function getGeneralParameters()
137141
{
138142
$time = time() * 1000 + mt_rand(0, 999);
139-
$ui = '24__1440__900__true__?__?__'.date('d/m/Y H:i:s', $time).'__'.$this->client->getUserAgent().'__';
143+
$ui = '24__1440__900__true__?__?__'.date('d/m/Y, H:i:s A', $time).'__'.$this->client->getUserAgent().'__';
140144

141145
return array(
142146
'mode' => 'local',
@@ -157,9 +161,10 @@ protected function getGeneralParameters()
157161

158162
/**
159163
* Parse and get image url from result page.
160-
* Eg: http://postimg.org/image/wvznrbllz/d5a5b291/
164+
* Eg: http://postimg.org/image/wvznrbllz/d5a5b291/.
165+
*
166+
* @param string $url
161167
*
162-
* @param string $url
163168
* @return string
164169
*/
165170
private function getImageFromResult($url)
@@ -174,9 +179,7 @@ private function getImageFromResult($url)
174179

175180
$imageUrl = $this->getMatch('#id="code_2".*?>(https?://\w+\.postimg\.org/\w+/\w+\.\w+)#i', $this->client);
176181

177-
if (!$imageUrl
178-
&& $url = $this->getMatch('#id="code_1".*?>(http.*?)<#i', $this->client)
179-
) {
182+
if (!$imageUrl && $url = $this->getMatch('#id="code_1".*?>(http.*?)<#i', $this->client)) {
180183
// try to fetch direct link from image page
181184
// tell postimage that i'm other browser
182185
// don't hide result with me, lol :v
@@ -189,6 +192,7 @@ private function getImageFromResult($url)
189192
if (!$imageUrl) {
190193
$this->throwException('%s: Image URL not found.', __METHOD__);
191194
}
195+
192196
return $imageUrl;
193197
}
194198
}

0 commit comments

Comments
 (0)