Newer
Older
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
define('RES_OK',0);
define('RES_ERR',-1);
class json_response {
private $_fields = array();
private $_id;
private $_results;
private $_datasetSize;
private $_status = RES_OK;
## @method setID($id)
# Set ID column
#
# @param id ID column name
public function setID($id) {
$this->_id = $id;
}
## @method setStatus($status)
# Set response status
#
# @param status Either RES_OK (default) or RES_ERROR
public function setStatus($status) {
$this->_status = $status;
}
## @method addField($name,$type)
# Add a field to our return results
#
# @param name Field name
# @param type Field type, 'int', 'string', 'float', 'boolean', 'date'
public function addField($name,$type) {
# Build field
$field = array(
'name' => $name,
'type' => $type
);
# Set ISO date format
if ($field['type'] == "date") {
$field['dateFormat'] = "Y-m-d";
}
# Add field to list
array_push($this->_fields,$field);
}
## @method setDatasetSize($size)
# Set how many records are returned in the dataset
#
# @param size Dataset size
public function setDatasetSize($size) {
$this->_datasetSize = $size;
}
## @method parseArrayRef($array)
# Parse in the array of results and fix it up
#
# @param arrayref Array ref containing the results
public function parseArray($array) {
$this->_results = array();
# Loop with array items
foreach ($array as $aitem) {
$item = array();
# Loop with fields we want
foreach ($this->_fields as $field) {
# FIXME - typecast?
$item[$field['name']] = $aitem[$field['name']];
}
array_push($this->_results,$item);
}
}
## @method parseHash($hashref)
# Parse in the hash of results and fix it up
#
# @param hashref Hash ref containing the results
public function parseHash($hash) {
$this->_results = array();
foreach ($this->_fields as $field) {
# FIXME - typecast?
$this_results[$field['name']] = $hash[$field['name']];
}
}
## @method export
# Export response into something we return
#
# @return JSON hash
# @li result - Result code/status
# @li data - Ref containing results
# @li metaData - Metadata containing info about the results being returned
# Metadata contains properties..
# - root: root element, which is always 'data'
# - fields: Optional field description, arrayref of hash refs, name = 'name', type 'type' and 'dateFormat' = 'Y-m-d'
# - id: Optional ID field name
# - totalProperty: Optional property name containing the number of records, always 'datasetSize'
# @li datasetSize Optional, number of records we're rturning
public function export() {
# Build result
$ret = array(
'result' => $this->_status,
# Additional stuff for other things to make life easier
'success' => $this->_status == RES_OK ? 1 : 0,
'metaData' => array(
'successProperty' => 'success'
)
);
# If we have results, add them
if (isset($this->_results)) {
$ret['data'] = $this->_results;
$ret['metaData']['root'] = 'data';
# If we have fields, set them up
if (isset($this->_fields)) {
$ret['metaData']['fields'] = $this->_fields;
}
}
# Check if we have an ID set
if (isset($this->_id)) {
$ret['metaData']['totalProperty'] = 'datasetSize';
$ret['datasetSize'] = $this->_datasetSize;
}
return $ret;
}
}
/*
* AJAX Interface to SMEngine SOAP
*/
# Fire an exception off to Ajax
function ajaxException($msg) {
/*
$res = array(
'success' => FALSE,
'errors' => array(
$msg
)
);
echo json_encode($res);
*/
# echo json_encode($msg);
print_r($_POST);
exit;
}
# PHP exception handler
function exception_handler($exception) {
ajaxException("Exception: ". $exception->getMessage());
}
# Set PHP exception handler
set_exception_handler('exception_handler');
if (!isset($_REQUEST['SOAPUsername']) || empty($_REQUEST['SOAPUsername'])) {
ajaxException("SOAPUsername undefined");
}
if (!is_string($_REQUEST['SOAPUsername'])) {
ajaxException("SOAPUsername is not a string");
}
$username = $_REQUEST['SOAPUsername'];
if (!isset($_REQUEST['SOAPPassword']) || empty($_REQUEST['SOAPPassword'])) {
ajaxEception("SOAPPassword undefined");
}
if (!is_string($_REQUEST['SOAPPassword'])) {
ajaxException("SOAPPassword is not a string");
}
$password = $_REQUEST['SOAPPassword'];
if (!isset($_REQUEST['SOAPAuthType']) || empty($_REQUEST['SOAPAuthType'])) {
ajaxException("SOAPAuthType undefined");
}
if (!is_string($_REQUEST['SOAPAuthType'])) {
ajaxException("SOAPAuthType is not a string");
}
$authType = $_REQUEST['SOAPAuthType'];
if (!isset($_REQUEST['SOAPModule']) || empty($_REQUEST['SOAPModule'])) {
ajaxException("SOAPModule undefined");
}
if (!is_string($_REQUEST['SOAPModule'])) {
ajaxException("SOAPModule is not a string");
}
$module = $_REQUEST['SOAPModule'];
if (!isset($_REQUEST['SOAPFunction']) || empty($_REQUEST['SOAPFunction'])) {
ajaxException("SOAPFunction undefined");
}
if (!is_string($_REQUEST['SOAPFunction'])) {
ajaxException("SOAPFunction is not a string");
}
$function = $_REQUEST['SOAPFunction'];
/*
__search
ID,__search
1:Name,1:Contact,1:Telephone,1:Email
*/
$soapParamTemplate = explode(',',$_REQUEST['SOAPParams']);
$soapParams = array();
foreach ($soapParamTemplate as $param) {
# Special case '__search'
if ($param == "__search") {
# Build hash and push into param list
$search = array(
'Filter' => isset($_REQUEST['filter']) ? $_REQUEST['filter'] : '',
'Start' => $_REQUEST['start'],
'Limit' => $_REQUEST['limit'],
'Sort' => isset($_REQUEST['sort']) ? $_REQUEST['sort'] : '',
'SortDirection' => isset($_REQUEST['dir']) ? $_REQUEST['dir'] : '',
);
array_push($soapParams,$search);
# Special case '__null'
} elseif ($param == "__null") {
array_push($soapParams,NULL);
# Everything else
} else {
# Split off param number if we're using it
$items = explode(':',$param);
# We have a parameter number
if (count($items) > 1) {
$array_pos = $items[0];
$array_item = $items[1];
$array_type = $items[2];
# Initialize array
if (!isset($soapParams[$array_pos])) {
$soapParams[$array_pos] = array();
}
# Check if we have an explicit type
if (isset($array_type)) {
# Check type
if ($array_type == "boolean") {
# Checkboxes/booleans are undefined if false
if (isset($_REQUEST[$array_item])) {
$item_value = 'true';
} else {
$item_value = 'false';
}
# And bitch if we invalid
} else {
ajaxException("Unknown AJAX=>SOAP type: '$array_type'");
}
} else {
$item_value = $_REQUEST[$array_item];
}
# Set item
$soapParams[$array_pos][$array_item] = $item_value;
} else {
array_push($soapParams,$_REQUEST[$items[0]]);
}
}
}
if ($function == "getWiSPUsers") {
$rawData = array (
array(
'ID' => 10,
'AgentID' => 5,
'AgentName' => 'joe agent',
'Username' => 'johnsmith',
'UsageCap' => 1000,
'ClassID' => 7,
'ClassDesc' => 'ClassTest',
'RealmDesc' => 'My Realm',
'Service' => 'My Service',
'AgentDisabled' => FALSE,
'Disabled' => FALSE,
'AgentRef' => 'Reseller ref'
)
);
$numResults = 1;
$res = new json_response;
$res->setID('ID');
$res->addField('ID','int');
$res->addField('AgentID','int');
$res->addField('AgentName','string');
$res->addField('Username','string');
$res->addField('UsageCap','int');
$res->addField('ClassID','int');
$res->addField('ClassDesc','string');
$res->addField('RealmDesc','string');
$res->addField('Service','string');
$res->addField('AgentDisabled','boolean');
$res->addField('Disabled','boolean');
$res->addField('AgentRef','string');
$res->parseArray($rawData);
$res->setDatasetSize($numResults);
echo json_encode($res->export());
}
exit;
# Connect via soap
$soap = new SoapClient(null,
array(
'location' => "http://localhost:1080/?Auth=$authType",
'uri' => $module,
'login' => $username,
'password' => $password
)
);
# Try soap call
try {
$soapRes = $soap->__call($function,$soapParams);
} catch (Exception $e) {
# Build msg string
if (is_soap_fault($e)) {
header("$SERVER_PROTOCOL 500 Internal Server Error");
$msg = "SOAP Fault: ".$e->faultstring;
if (!empty($e->detail)) {
$msg .= " (".$e->detail.")";
}
} else {
header("$SERVER_PROTOCOL 400 Bad Request");
$msg = "Fault: ".$e->getMessage();
}
ajaxException($msg);
}
echo json_encode($soapRes);