This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
xls_image_upload.php
168 lines (121 loc) · 4.06 KB
/
xls_image_upload.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
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
<?php
/*
LightSpeed Web Store
NOTICE OF LICENSE
This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to support@lightspeedretail.com <mailto:support@lightspeedretail.com>
* so we can send you a copy immediately.
DISCLAIMER
* Do not edit or add to this file if you wish to upgrade Web Store to newer
* versions in the future. If you wish to customize Web Store for your
* needs please refer to http://www.lightspeedretail.com for more information.
* @copyright Copyright (c) 2011 Xsilva Systems, Inc. http://www.lightspeedretail.com
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/** THIS SCRIPT IS USED BY THE LIGHTSPEED SOFTWARE TO POST IMAGES FROM THE POS TO THE WEB SERVER, DO NOT ALTER **/
require_once('xls_ws_service.php');
$Importer = new XLSWService('XLSWService', '');
$PassKey = null;
function sendAuthRequest($msg='') {
if (!$msg)
$msg = 'Must be authenticated to access this functionality';
header('WWW-Authenticate: Basic realm="WS Image Uploads"');
header('HTTP/1.0 401 Unauthorized');
echo $msg;
exit(0);
}
function checkAuthentication() {
global $Importer;
global $PassKey;
$PassKey = $_SERVER['HTTP_PASSKEY'];
return true;
}
function errorInParams($msg) {
header('HTTP/1.0 422 Unprocessable Entity');
echo $msg;
exit(0);
}
function errorInImport($msg, $errCode) {
header('HTTP/1.0 400 Bad Request');
echo $msg;
echo $errCode;
exit(0);
}
function successResponse($msg='Success!') {
header('HTTP/1.0 200 OK');
header('Content-type: text/plain');
echo $msg;
exit(0);
}
function errorConflict($msg, $errCode) {
header('HTTP/1.0 409 Conflict');
echo $msg;
echo $errCode;
exit(0);
}
function getDestination() {
if (isset($_SERVER['ORIG_PATH_INFO']))
$strPath=$_SERVER['ORIG_PATH_INFO'];
elseif (isset($_SERVER['PATH_INFO']))
$strPath = $_SERVER['PATH_INFO'];
else
return errorInParams('No path info details present');
$matches = array();
if (!preg_match('@/product/(\d+)/index/([0-5])/@',$strPath, $matches))
return errorInParams('Badly formed path:' . $strPath);
$pid = $matches[1];
$idx = $matches[2];
$destination = array(
'product_id' => $pid,
'image_index' => $idx
);
return $destination;
}
function getImageData() {
$ctx=stream_context_create(array(
'http'=>array('timeout' => ini_get('max_input_time'))
));
$rawPostData = file_get_contents('php://input',false,$ctx);
if (empty($rawPostData)) {
return errorInParams('No image data posted?');
}
return $rawPostData;
}
function handlePost() {
global $Importer;
global $PassKey;
if (! checkAuthentication())
die("In handlePost but not authenticated?");
$destination = getDestination();
if (! ($destination && is_array($destination)))
die("Could not get destination of POST ?");
$imageData = getImageData();
if (! $imageData)
die("Could not get image data?");
if ($destination['image_index'] > 0) {
$additionalImgIdx = $destination['image_index'] - 1;
$resp = $Importer->add_additional_product_image_at_index($PassKey, $destination['product_id'], $imageData, $additionalImgIdx);
if ($resp != XLSWService::OK) {
return errorConflict(
'Problem adding additional image ' . $destination['image_index'] . ' to product ' . $destination['product_id'],
$resp
);
}
} elseif ($destination['image_index'] == 0) {
// save master product image
$resp = $Importer->save_product_image($PassKey, $destination['product_id'], $imageData);
if ($resp != XLSWService::OK)
return errorConflict('Problem saving image for product ' . $destination['product_id'], $resp);
} else {
return errorInParams("Image index specified is neither > 0 nor == 0 ??");
}
unset($imageData);
return successResponse("Image saved for product " . $destination['product_id']);
}
handlePost();
?>