1
1
<?php
2
2
/**
3
- * Plugin for http://postimage.org
3
+ * Plugin for http://postimage.org.
4
4
*
5
5
* @release Jun 19, 2014
6
- * @lastupdate Mar 7 , 2015
6
+ * @update May 29 , 2015
7
7
*/
8
8
class ChipVN_ImageUploader_Plugins_Postimage extends ChipVN_ImageUploader_Plugins_Abstract
9
9
{
10
- const FREE_UPLOAD_ENPOINT = 'http://postimage.org/ ' ;
10
+ const FREE_UPLOAD_ENPOINT = 'http://postimage.org/ ' ;
11
11
const ACCOUNT_UPLOAD_ENPOINT = 'http://postimg.org/ ' ;
12
12
13
- const SESSION_LOGIN = 'session_login ' ;
13
+ const SESSION_LOGIN = 'session_login ' ;
14
14
15
15
/**
16
- * Gets upload url endpoint
16
+ * Gets upload url endpoint.
17
17
*
18
18
* @return string
19
19
*/
@@ -59,7 +59,8 @@ protected function doLogin()
59
59
protected function doUpload ()
60
60
{
61
61
$ endpoint = $ this ->getUrlEnpoint ();
62
- $ time = time ();
62
+ $ time = time () * 1000 + mt_rand (1 , 999 );
63
+ $ maxFileSize = 16777216 ;
63
64
64
65
$ this ->resetHttpClient ();
65
66
if ($ this ->useAccount ) {
@@ -70,36 +71,40 @@ protected function doUpload()
70
71
->setReferer ($ endpoint )
71
72
->setParameters (array (
72
73
'upload ' => '@ ' .$ this ->file ,
73
- 'session_upload ' => $ time ,
74
74
'um ' => 'computer ' ,
75
+ 'gallery_id ' => '' ,
75
76
'forumurl ' => $ endpoint ,
76
77
'upload_error ' => '' ,
78
+ 'MAX_FILE_SIZE ' => $ maxFileSize ,
77
79
))
78
80
->setParameters ($ this ->getGeneralParameters ())
79
81
->execute ($ endpoint );
80
82
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__ );
82
103
83
- $ this ->resetHttpClient ();
84
- if ($ this ->useAccount ) {
85
- $ this ->client ->setCookies ($ this ->getCache ()->get (self ::SESSION_LOGIN ));
104
+ $ location = $ this ->client ->getResponseHeaders ('location ' );
86
105
}
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 ' );
99
106
100
- $ this ->checkHttpClientErrors (__METHOD__ );
101
-
102
- return $ this ->getImageFromResult ($ this ->client ->getResponseHeaders ('location ' ));
107
+ return $ this ->getImageFromResult ($ location );
103
108
}
104
109
105
110
/**
@@ -116,7 +121,6 @@ protected function doTransload()
116
121
$ this ->client ->setReferer ($ endpoint )
117
122
->setParameters (array (
118
123
'forumurl ' => $ endpoint ,
119
- 'ui ' => $ ui ,
120
124
'um ' => 'web ' ,
121
125
'url_list ' => $ this ->url ,
122
126
))
@@ -136,7 +140,7 @@ protected function doTransload()
136
140
protected function getGeneralParameters ()
137
141
{
138
142
$ 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 ().'__ ' ;
140
144
141
145
return array (
142
146
'mode ' => 'local ' ,
@@ -157,9 +161,10 @@ protected function getGeneralParameters()
157
161
158
162
/**
159
163
* 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
161
167
*
162
- * @param string $url
163
168
* @return string
164
169
*/
165
170
private function getImageFromResult ($ url )
@@ -174,9 +179,7 @@ private function getImageFromResult($url)
174
179
175
180
$ imageUrl = $ this ->getMatch ('#id="code_2".*?>(https?://\w+\.postimg\.org/\w+/\w+\.\w+)#i ' , $ this ->client );
176
181
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 )) {
180
183
// try to fetch direct link from image page
181
184
// tell postimage that i'm other browser
182
185
// don't hide result with me, lol :v
@@ -189,6 +192,7 @@ private function getImageFromResult($url)
189
192
if (!$ imageUrl ) {
190
193
$ this ->throwException ('%s: Image URL not found. ' , __METHOD__ );
191
194
}
195
+
192
196
return $ imageUrl ;
193
197
}
194
198
}
0 commit comments