-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
203 lines (167 loc) · 6.69 KB
/
Copy pathupload.php
File metadata and controls
203 lines (167 loc) · 6.69 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
$response = array(); // response object
/**
* Handle file uploads via XMLHttpRequest
*/
class UploadedFileXhr {
private $size = 0;
private $error = 0;
function __construct() {
if (isset($_SERVER["CONTENT_LENGTH"])){
$this->size = (int)$_SERVER["CONTENT_LENGTH"];
} else {
$this->error = UPLOAD_ERR_INI_SIZE;
}
// If size exceeds limit
if($this->size > 12582912) {
$this->error = UPLOAD_ERR_INI_SIZE;
}
}
function getBinary() {
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
fseek($temp, 0, SEEK_SET);
$multiPartImageData = fread($temp, $realSize);
return $multiPartImageData;
}
function getName() {
return $_GET['picfile'];
}
function getSize() {
return $this->size;
}
function getError() {
return $this->error;
}
}
/**
* Handle file uploads via regular form post (uses the $_FILES array)
*/
class UploadedFileForm {
function getBinary() {
$input = fopen($_FILES['picfile']['tmp_name'], "r");
$multiPartImageData = fread($input, $this->getSize());
fclose($input);
return $multiPartImageData;
}
function getName() {
return $_FILES['picfile']['name'];
}
function getSize() {
return $_FILES['picfile']['size'];
}
function getError() {
$_FILES['picfile']['error'];
}
}
// Retreiving the file based on upload type (AJAX or Iframe)
$fileObj = 0;
if(isset($_GET['picfile'])) {
$fileObj = new UploadedFileXhr();
} elseif(isset($_FILES['picfile'])) {
$fileObj = new UploadedFileForm();
}
if ($fileObj) { // file was send from browser
if (!$fileObj->getError() || $fileObj->getError() == UPLOAD_ERR_OK) { // no error
require_once 'eBayApi.php';
// File details
$picNameIn = $fileObj->getName(); // image file to read and upload
$multiPartImageData = $fileObj->getBinary(); // do a binary read of image
// Credentials for eBay image upload API
// Register in http://developer.ebay.com/ to get access to eBay public APIs
// The registration process will generate the following credentials - Developer
// Id, Application Id, Certificate Id and a eBay user token. Please use those
// values below
$devID = '<Your eBay developer Id>';
$appID = '<Your eBay App Id>';
$certID = '<Your eBay cert Id>';
//the token representing the eBay user to assign the call with
$userToken = '<Your eBay user token>';
$siteID = 0; // siteID needed in request - US=0, UK=3, DE=77...
$verb = 'UploadSiteHostedPictures'; // the call being made:
$version = 721; // eBay API version
///Build the request XML request which is first part of multi-part POST
$xmlReq = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
$xmlReq .= '<' . $verb . 'Request xmlns="urn:ebay:apis:eBLBaseComponents">' . "\n";
$xmlReq .= "<Version>$version</Version>\n";
$xmlReq .= "<ErrorLanguage>en_US</ErrorLanguage>\n";
$xmlReq .= "<CustomPictureSet>4000008</CustomPictureSet>\n";
$xmlReq .= "<ExtensionInDays>36500</ExtensionInDays>\n";
$xmlReq .= "<PictureName>$picNameIn</PictureName>\n";
$xmlReq .= "<RequesterCredentials><eBayAuthToken>$userToken</eBayAuthToken></RequesterCredentials>\n";
$xmlReq .= '</' . $verb . 'Request>';
$boundary = "MIME_boundary";
$CRLF = "\r\n";
// The complete POST consists of an XML request plus the binary image separated by boundaries
$firstPart = '';
$firstPart .= "--" . $boundary . $CRLF;
$firstPart .= 'Content-Disposition: form-data; name="XML Payload"' . $CRLF;
$firstPart .= 'Content-Type: text/xml;charset=utf-8' . $CRLF . $CRLF;
$firstPart .= $xmlReq;
$firstPart .= $CRLF;
$secondPart = '';
$secondPart .= "--" . $boundary . $CRLF;
$secondPart .= 'Content-Disposition: form-data; name="dummy"; filename="dummy"' . $CRLF;
$secondPart .= "Content-Transfer-Encoding: binary" . $CRLF;
$secondPart .= "Content-Type: application/octet-stream" . $CRLF . $CRLF;
$secondPart .= $multiPartImageData;
$secondPart .= $CRLF;
$secondPart .= "--" . $boundary . "--" . $CRLF;
$fullPost = $firstPart . $secondPart;
// Create a new eBay session (defined below)
$session = new eBayApi($userToken, $devID, $appID, $certID, false, $version, $siteID, $verb, $boundary);
$respXmlStr = $session->sendHttpRequest($fullPost); // send multi-part request and get string XML response
// Check for errors
if(stristr($respXmlStr, 'HTTP 404') || $respXmlStr == '') {
error_log("########### ".print_r($respXmlStr, true));
// EPS API upload error
$response['error'] = getErrorResp(130, 'EPS API upload error');
} else {
$respXmlObj = simplexml_load_string($respXmlStr); // create SimpleXML object from string for easier parsing
// need SimpleXML library loaded for this
$ack = $respXmlObj->Ack;
$picObj = $respXmlObj->SiteHostedPictureDetails->PictureSetMember;
$data = array();
$data['ack'] = "".$ack;
$data['mainUrl'] = "".$picObj[0]->MemberURL;
$data['thumbNail'] = "".$picObj[1]->MemberURL;
$response['data'] = $data;
}
} elseif ($fileObj->getError() == UPLOAD_ERR_INI_SIZE) {
// File exceeds the allowed size (upload_max_filesize directive in php.ini')
$response['error'] = getErrorResp(120, 'File exceeds the allowed size');
} else {
// File is corrupted
$response['error'] = getErrorResp(110, 'File is corrupted');
}
} else {
// No file to upload
$response['error'] = getErrorResp(100, 'No file to upload');
}
// Set HTTP header to html
header("Content-Type: text/html; charset=utf-8");
// Serialize the response
//$response = array('error'=>getErrorResp(100, 'No file to upload')); // Placeholder To simulate error scenario
$html = json_encode($response);
// Build the script output
if(isset($_POST['cb'])) {
$cb = $_POST['cb'];
$html = '<script>'.$cb.'('.$html.')</script>';
}
// echo out the content
echo $html;
/**
*
* Function to build the error response
*
* @function getErrorResp
* @param $id {String} Error ID
* @param $msg {String} Error Message
*
*/
function getErrorResp($id, $msg) {
return array('id' => $id, 'msg' => $msg);
}
?>