forked from neilopet/PHP-Filehost-Uploaders
-
Notifications
You must be signed in to change notification settings - Fork 2
/
uploading.handler.php
134 lines (119 loc) · 5.12 KB
/
uploading.handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
require_once 'AbstractCurly.class.php';
/**
* Uploading
* An object used to upload files to Uploading.com
* @author Neil Opet <neil.opet@gmail.com>
*/
interface UploadingInterface
{
public function Upload( $callback ); // Upload file
}
final class Uploading extends AbstractCurly implements UploadingInterface
{
private
$SID,
$upload_url,
$cookies,
$username,
$password,
$filePath;
function __construct( $username, $password, $path_to_file )
{
if (file_exists($path_to_file))
$this->filePath = $path_to_file;
$this->username = $username;
$this->password = $password;
}
private function Login()
{
$postData = http_build_query(array(
'email' => $this->username,
'password' => $this->password,
'remember' => 'on'
));
parent::curly_init('http://uploading.com/general/login_form/?JsHttpRequest=' . time() . '-xml');
$this->options[CURLOPT_HEADER] = TRUE;
$this->options[CURLOPT_HTTPHEADER] = array('Content-Type: application/octet-stream; charset=UTF-8', 'Content-Length: ' . strlen($postData));
$response = parent::curly_post($postData, function(){});
$lines = explode("\n", $response);
$cookies = array();
foreach($lines as $line)
{
if (preg_match('/Set-Cookie: (?<cname>[a-z0-9\_\-]+)=(?<cval>[a-z0-9\=\_\-\%\.]+)/i', $line, $matches))
{
$this->cookies[] = $matches['cname'] . '=' . $matches['cval'];
if ($matches['cname'] == 'SID')
$this->SID = urldecode($matches['cval']);
}
}
return TRUE;
}
public function Upload( $callback )
{
if ($this->Login())
{
if (!isset($this->SID))
die('Uploading.com - No session.');
// get upload url
parent::curly_init('http://uploading.com/');
$this->options[CURLOPT_HTTPHEADER] = array('Cookie: ' . implode('; ', $this->cookies)); # must be set each time
$response = parent::curly_get(array(''=>''), function(){});
if (preg_match("/upload_url: 'http:\/\/(?<upload_url>.*)',/", $response, $matches))
{
$this->upload_url = isset($matches['upload_url']) ? ('http://' . $matches['upload_url']) : NULL;
}
if (!isset($this->upload_url))
die('No upload URL returned.');
// create file placeholder
parent::curly_init('http://uploading.com/files/generate/?JsHttpRequest');
$this->options[CURLOPT_HTTPHEADER] = array('Cookie: ' . implode('; ', $this->cookies)); # must be set each time
$json_obj = json_decode(parent::curly_post(array(
'name' => basename($this->filePath),
'size' => filesize($this->filePath)
), function(){}));
// this needs the post header custom created because of duplicate "file" fields
parent::curly_init($this->upload_url);
// additional headers required for upload
$boundary = substr(strtr(base64_encode(md5(microtime(TRUE) . 'asdfasdf')), array('+'=>'','/'=>'','='=>'')), 0, 30);
$this->options[CURLOPT_HTTPHEADER] = array
(
'Accept: text/*',
'Content-Type: multipart/form-data; boundary=----------' . $boundary,
'User-Agent: Shockwave Flash',
'Connection: Keep-Alive',
'Cache-Control: no-cache'
);
$headers = array
(
'------------' . $boundary,
'Content-Disposition: form-data; name="Filename"',
'',
basename($this->filePath),
'------------' . $boundary,
'Content-Disposition: form-data; name="folder_id"',
'',
'0',
'------------' . $boundary,
'Content-Disposition: form-data; name="SID"',
'',
$this->SID,
'------------' . $boundary,
'Content-Disposition: form-data; name="file"',
'',
$json_obj->file_id,
'------------' . $boundary,
'Content-Disposition: form-data; name="file"; filename="' . basename($this->filePath) . '"',
'Content-Type: application/octet-stream',
'',
file_get_contents($this->filePath),
'------------' . $boundary,
'Content-Disposition: form-data; name="Upload"',
'',
'Submit Query',
'------------' . $boundary . '--',
);
return (substr(parent::curly_post(implode("\r\n", $headers), $callback), 0, 3) == 'new' ? $json_obj->link : FALSE);
}
}
}