-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgsxlib.php
556 lines (454 loc) · 15.9 KB
/
gsxlib.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php
/**
* gsxlib/gsxlib.php
* @package gsxlib
* @author Filipp Lepalaan <filipp@mcare.fi>
* https://gsxwsut.apple.com/apidocs/html/WSReference.html?user=asp
* @license
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
class GsxLib
{
private $client;
private $region;
private $session_id;
private $environment;
//IT: https://gsxws%s.apple.com/wsdl/%sAsp/gsx-%sAsp.wsdl
//PROD: https://gsxws2.apple.com/wsdl/%sAsp/gsx-%sAsp.wsdl
private $wsdl = 'https://gsxws%s.apple.com/wsdl/%sAsp/gsx-%sAsp.wsdl';
static $_instance;
const timeout = 30; // session timeout in minutes
public static function getInstance(
$account,
$username,
$password,
$environment = '',
$region = 'emea',
$tz = 'CEST')
{
if(!(self::$_instance instanceof self)) {
self::$_instance = new self(
$account,
$username,
$password,
$environment,
$region,
$tz
);
}
return self::$_instance;
}
private function __clone() {}
private function __construct(
$account,
$username,
$password,
$environment = '',
$region = 'emea',
$tz = 'CEST' )
{
if(!class_exists('SoapClient')) {
throw new GsxException('Looks like your PHP lacks SOAP support');
}
if(!preg_match('/\d+/', $account)) {
throw new GsxException('Invalid Sold-To: ' . $account);
}
$regions = array('am', 'emea', 'apac', 'la');
if(!in_array($region, $regions))
{
$error = 'Region "%s" should be one of: %s';
$error = sprintf($error, $region, implode(', ', $regions));
throw new GsxException($error);
}
$envirs = array('ut', 'it');
if(!empty($environment))
{
if(!in_array($environment, $envirs))
{
$error = 'Environment "%s" should be one of: %s';
$error = sprintf($error, $environment, implode(', ', $envirs));
throw new GsxException($error);
}
} else {
// GSX2...
$environment = '2';
}
$this->wsdl = sprintf($this->wsdl, $environment, $region, $region);
$this->client = new SoapClient(
$this->wsdl, array('exceptions' => TRUE, 'trace' => 1)
);
if(!$this->client) {
throw new GsxException('Failed to create SOAP client.');
}
if(@$_SESSION['_gsxlib_timeout'][$account] > time()) {
// return $this->session_id = $_SESSION['_gsxlib_id'][$account];
}
$a = array(
'AuthenticateRequest' => array(
'userId' => $username,
'password' => $password,
'serviceAccountNo' => $account,
'languageCode' => 'en',
'userTimeZone' => $tz,
)
);
syslog(LOG_ERR, print_r($a, true));
try {
$this->session_id = $this->client
->Authenticate($a)
->AuthenticateResponse
->userSessionId;
} catch(SoapFault $e) {
if(empty($environment)) $environment = 'production';
$error = 'Authentication with GSX failed. Does this account have access to '
.$environment."?\n";
throw new GsxException($error);
}
// there's a session going, put the credentials in there
if(session_id()) {
$_SESSION['_gsxlib_id'][$account] = $this->session_id;
$timeout = time()+(60*self::timeout);
$_SESSION['_gsxlib_timeout'][$account] = $timeout;
}
}
function getClient()
{
return $this->client;
}
function setClient($client)
{
$this->client = $client;
}
/**
* Get current GSX status of repair
* @param mixed $dispatchId
*/
public function repairStatus($dispatchId)
{
$toCheck = array();
if(!is_array($dispatchId)) {
$dispatchId = array($dispatchId);
}
foreach($dispatchId as $id) {
if (self::looksLike($id, 'dispatchId')) {
$toCheck[] = $id;
}
}
if(empty($toCheck)) {
exit('No valid dispatch IDs given');
}
$req = array('RepairStatus' => array(
'repairConfirmationNumbers' => $toCheck
));
return $this->request($req)->repairStatus;
}
public function fetchiOsActivation($query)
{
$like = self::looksLike($query);
$request = array('FetchIOSActivationDetails' => array(
$like => $query
));
return $this->request($request);
}
public function createCarryInRepair($repairData)
{
$resp = $this->client->CreateCarryInRepair(
array('CreateCarryInRequest' => array(
'userSession' => array('userSessionId' => $this->getSessionId()),
'repairData' => $repairData
))
);
return $resp->CreateCarryInResponse;
}
public function createMailInRepair($repairData)
{
}
public function bulkReturnProforma() {}
public function repairLookup($query)
{
$fields = array(
'repairConfirmationNumber' => '',
'customerEmailAddress' => '',
'customerFirstName' => '',
'customerLastName' => '',
'fromDate' => '',
'toDate' => '',
'incompleteRepair' => 'N',
'pendingShipment' => 'N',
'purchaseOrderNumber' => '',
'repairNumber' => '',
'repairStatus' => '',
'repairType' => 'CA',
'serialNumber' => '',
'shipToCode' => '',
'soldToReferenceNumber' => '',
'technicianFirstName' => '',
'technicianLastName' => '',
'unreceivedModules' => 'N',
);
// provide "shortcuts" for dispatch ID and SN
switch(self::looksLike($query)) {
case 'dispatchId':
$query = array('repairConfirmationNumber' => $query);
break;
case 'serialNumber':
$query = array('serialNumber' => $query);
break;
}
$query = array_merge($fields, $query);
$req = array( 'RepairLookupRequest' => array(
'userSession' => array('userSessionId' => $this->session_id),
'lookupRequestData' => $query
));
$response = $this->client->RepairLookup($req)
->RepairLookupResponse;
return $response->lookupResponseData;
}
/**
* List parts pending for return
* Default to Open Carry-Ins
* @param mixed $repairData
* @return mixed
*/
public function partsPendingReturn($repairData = null)
{
$fields = array(
'repairType' => 'CA', // default to Carry In repairs
'repairStatus' => 'Open', // and current ones
'purchaseOrderNumber' => '',
'sroNumber' => '',
'repairConfirmationNumber' => '',
'serialNumber' => '',
'shipToCode' => '',
'customerFirstName' => '',
'customerLastName' => '',
'customerEmailAddress' => '',
'createdFromDate' => '',
'createdToDate' => '',
);
if( !is_array( $repairData )) {
$repairData = array();
}
if(!empty($repairData)) {
foreach($fields as $k => $v) {
if(array_key_exists($k, $repairData)) {
$fields[$k] = $repairData[$f];
}
}
}
$req = array('PartsPendingReturn' => array('repairData' => $fields));
return $this->request($req)->partsPendingResponse;
}
public function fetchDiagnostics($query)
{
if(!is_array($query)) {
$like = self::looksLike($query);
$query = array($like => $query);
}
$req = array('FetchRepairDiagnostic' => array(
'lookupRequestData' => $query
));
return $this->request($req);
}
public function compTiaCodes()
{
try {
$result = $this->client->CompTIACodes(
array('ComptiaCodeLookupRequest' =>
array('userSession' => array('userSessionId' => $this->session_id)))
);
} catch (Exception $e) {
syslog(LOG_ERR, $e->getMessage());
syslog(LOG_ERR, $this->client->__getLastRequest());
syslog(LOG_ERR, $this->client->__getLastResponse());
}
$response = $result->ComptiaCodeLookupResponse;
return $response->comptiaInfo;
}
/**
* Return details for given dispatch ID
* @param string $dispatchId
* @return object lookupResponseData
*/
public function repairDetails($dispatchId)
{
$dispatchId = trim($dispatchId);
if( !self::looksLike( $dispatchId, 'dispatchId' )) {
exit( 'Invalid dispatch ID: ' . $dispatchId );
}
$req = array('RepairDetails' => array('dispatchId' => $dispatchId));
return $this->request($req)->lookupResponseData;
}
/**
* Get PDF label for part return
* @param string $returnOrder order number
* @param string $partNumber code of part being returned
*/
public function returnLabel($returnOrder, $partNumber)
{
if(!self::looksLike($returnOrder, 'returnOrder')) {
exit('Invalid order number: ' . $returnOrder);
}
if(!self::looksLike($partNumber, 'partNumber')) {
exit('Invalid part number: ' . $partNumber);
}
$req = array('ReturnLabel' => array(
'returnOrderNumber' => $returnOrder,
'partNumber' => $partNumber
));
return $this->request($req)->returnLabelData;
}
/**
* a shortcut for looking up part information
* @param mixed $query
* @return [bool|string]
*/
public function partsLookup($query = null)
{
if(is_array($query)) {
$req = array('PartsLookup' => array(
'lookupRequestData' => $query
));
} else {
$query = trim($query);
$what = self::looksLike($query);
if(!$what) {
throw new GsxException('Invalid search term for part lookup: '.$query);
}
$query = array($what => $query);
}
$req = array('PartsLookup' => array(
'lookupRequestData' => $query
));
$result = $this->request($req)->parts;
// always return an array
return (is_array($result)) ? $result : array($result);
}
/**
* A shortcut for checking warranty status of device
*/
public function warrantyStatus($serialNumber)
{
if(!$this->isValidSerialNumber($serialNumber)) {
exit('Invalid serial number: ' . $serialNumber);
}
$req = array('WarrantyStatus' => array(
'unitDetail' => array('serialNumber' => $serialNumber)
));
return $this->request($req)->warrantyDetailInfo;
}
public function productModel($serialNumber)
{
if(!$this->isValidSerialNumber($serialNumber)) {
exit('Invalid serial number: ' . $serialNumber);
}
$req = array( 'FetchProductModelRequest' => array(
'userSession' => array(
'userSessionId' => $this->session_id
),
'productModelRequest' => array(
'serialNumber' => $serialNumber
)
));
$response = $this->client
->FetchProductModel($req)
->FetchProductModelResponse;
return $response->productModelResponse;
}
public function onsiteDispatchDetail($query)
{
if(!self::looksLike($query, 'dispatchId')) {
exit( "Invalid dispatch ID: $query" );
}
$req = array('OnsiteDispatchDetailRequest' => array(
'userSession' => array('userSessionId' => $this->session_id),
'lookupRequestData' => array(
'dispatchId' => $query,
'dispatchSentFromDate' => '',
'dispatchSentToDate' => ''
)
));
$response = $this->client->OnsiteDispatchDetail($req)
->OnsiteDispatchDetailResponse;
return $response->onsiteDispatchDetails;
}
public function isValidSerialNumber($serialNumber)
{
$serialNumber = trim( $serialNumber );
// SNs should never start with an S, but they're often coded into barcodes
// and since an "old- ormat" SN + S would still qualify as a "new format" SN,
// we strip it here and not in self::looksLike
$serialNumber = ltrim($serialNumber, 'sS');
return self::looksLike($serialNumber, 'serialNumber');
}
/**
* return the GSX user session ID
* I still keep the property private since it should not be modified
* outside the constructor
* @return string GSX session ID
*/
public function getSessionId()
{
return $this->session_id;
}
/**
* Do the actual SOAP request
*/
public function request($req)
{
$result = FALSE;
// split the request name and data
list($r, $p) = each($req);
// add session info
$p['userSession'] = array('userSessionId' => $this->session_id);
$request = array($r.'Request' => $p);
try {
$result = $this->client->$r($request);
$resp = "{$r}Response";
return $result->$resp;
} catch(SoapFault $e) {
throw new GsxException($e->getMessage(), $this->client->__getLastRequest());
}
return $result;
}
/**
* Try to "categorise" a string
* About identifying serial numbers - before 2010, Apple had a logical
* serial number format, with structure, that you could id quite reliably.
* unfortunately, it's no longer the case
* @param string $string string to check
*/
static function looksLike($string, $what = null)
{
$result = false;
$rex = array(
'partNumber' => '/^([a-z]{1,2})?\d{3}\-\d{4}$/i',
'serialNumber' => '/^[a-z0-9]{11,12}$/i',
'eeeCode' => '/^[A-Z0-9]{3,4}$/', // only match ALL-CAPS!
'returnOrder' => '/^7\d{9}$/',
'repairNumber' => '/^\d{12}$/',
'dispatchId' => '/^G\d{9}$/i',
'alternateDeviceId' => '/^\d{15}$/',
'diagnosticEventNumber' => '/^\d{23}$/'
// 12715075303192012074604
);
foreach ($rex as $k => $v) {
if (preg_match($v, $string)) {
$result = $k;
}
}
return ($what) ? ($result == $what) : $result;
}
}
class GsxException extends Exception
{
function __construct($message, $request = null)
{
$this->request = $request;
$this->message = $message;
}
}