Subversion Repositories munaweb

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
<?php
2
/*  © 2013 eBay Inc., All Rights Reserved */
3
/* Licensed under CDDL 1.0 -  http://opensource.org/licenses/cddl1.php */
4
?>
5
<?php
6
// Configuration class to handle settings
7
class eBaySession {
8
	protected $properties;
9
 
10
	const URL_PRODUCTION = 'https://api.ebay.com/wsapi';
11
	const URL_SANDBOX    = 'https://api.sandbox.ebay.com/wsapi';
12
 
13
 	public function __construct($dev, $app, $cert) {
14
		$this->properties = array(
15
			'dev'      => null,
16
			'app'      => null,
17
			'cert'     => null,
18
			'wsdl'     => null,
19
			'options'  => null,
20
			'token'    => null,
21
			'userid'   => null,
22
			'password' => null,
23
			'site'     => null,
24
			'location' => null,
25
			'ns'       => null,
26
			'version'  => null,
27
			'runame'   => null,
28
		);
29
 
30
		$this->dev = $dev;
31
		$this->app = $app;
32
		$this->cert = $cert;
33
 
34
#		$this->wsdl = 'https://developer.ebay.com/webservices/latest/eBaySvc.wsdl';
35
		$this->wsdl = '../php/eBaySvc.wsdl';
36
		$xml = new DOMDocument();
37
		$xml->load($this->wsdl);
38
		$this->version = $xml->getElementsByTagName('Version')->item(0)->nodeValue;
39
		$this->options = array('trace' => true,
40
		                       'exceptions' => false,
41
		                       'classmap' => array(/* 'UserType' => 'eBayUserType', */
42
		                                           'GetSearchResultsResponseType' => 'eBayGetSearchResultsResponseType',
43
		                                           'SearchResultItemArrayType' => 'eBaySearchResultItemArrayType',
44
		                                           'SearchResultItemType' => 'eBaySearchResultItemType',
45
		                                           'AmountType' => 'eBayAmountType',
46
		                                           'FeeType' => 'eBayFeeType',
47
		                                           'FeesType' => 'eBayFeesType',
48
		                                           'PaginatedItemArrayType' => 'eBayPaginatedItemArrayType',
49
		                                           'ItemArrayType' => 'eBayItemArrayType',
50
		                                           'ItemType' => 'eBayItemType',
51
		                                           'NameValueListArrayType' => 'eBayNameValueListArrayType',
52
		                                           'NameValueListType' => 'eBayNameValueListType',
53
		                                           'PictureDetailsType' => 'eBayPictureDetailsType',
54
		                                          ),
55
#		                       'compression' => SOAP_COMPRESSION_ACCEPT,
56
		                      );
57
 
58
		$this->ns = 'urn:ebay:apis:eBLBaseComponents';
59
	}
60
 
61
	public function __set($property, $value) {
62
		 if (array_key_exists($property, $this->properties)) {
63
		    $this->properties[$property] = $value;
64
		 }
65
	}
66
 
67
	public function __get($property) {
68
		 if (array_key_exists($property, $this->properties)) {
69
		    return $this->properties[$property];
70
		 } else {
71
		    return null;
72
		 }
73
	}
74
 
75
	public function __isset($property) {
76
		 return array_key_exists($property, $this->properties);
77
	}
78
 
79
}
80
 
81
// Main class for communication with eBay Web services via SOAP
82
class eBaySOAP extends SoapClient {
83
	protected $headers = null;
84
	protected $session = null;
85
 
86
	public function __construct(eBaySession $session) {
87
		$this->session = $session;
88
		$this->__setHeaders();
89
		parent::__construct($session->wsdl, $session->options);
90
	}
91
 
92
	protected function __setHeaders() {
93
		$eBayAuth = $this->__geteBayAuth($this->session);
94
		$header_body = new SoapVar($eBayAuth, SOAP_ENC_OBJECT);
95
		$headers = array(new SOAPHeader($this->session->ns, 'RequesterCredentials', $header_body));
96
 
97
		$this->headers = $headers;
98
	}
99
 
100
	protected function __geteBayAuth(eBaySession $session) {
101
		$credentials = array();
102
		$eBayAuth = array();
103
 
104
		$credentials['AppId'] = new SoapVar($session->app, XSD_STRING, null, null, null, $session->ns);
105
		$credentials['DevId'] = new SoapVar($session->dev, XSD_STRING, null, null, null, $session->ns);
106
		$credentials['AuthCert'] = new SoapVar($session->cert, XSD_STRING, null, null, null, $session->ns);
107
		if (isset($session->userid) && ($session->userid != null)) {
108
			$credentials['Username'] = new SoapVar($session->userid, XSD_STRING, null, null, null, $session->ns);
109
		}
110
		if (isset($session->password) && ($session->password != null)) {
111
			$credentials['Password'] = new SoapVar($session->password, XSD_STRING, null, null, null, $session->ns);
112
		}
113
 
114
		if (isset($session->token)) {
115
			$eBayAuth['eBayAuthToken'] = new SoapVar($session->token, XSD_STRING, null, null, null, $session->ns);
116
		}
117
		$eBayAuth['Credentials'] = new SoapVar($credentials, SOAP_ENC_OBJECT, null, null, null, $session->ns);
118
 
119
		return $eBayAuth;
120
	}
121
 
122
 	public function __call($function, $args) {
123
		if (empty($args[0]['Version'])) {
124
			$args[0]['Version'] = $this->session->version;
125
		}
126
 
127
		$callname = $function;
128
		$siteid = $this->session->site;
129
		$version = $args[0]['Version'];
130
		$appid = $this->session->app;
131
		$Routing = 'default'; // XXX: hardcoded
132
 
133
		$query_string = http_build_query(array('callname' => $callname, 'siteid' => $siteid, 'version' => $version, 'appid' => $appid, 'Routing' => $Routing));
134
	 	$location = "{$this->session->location}?{$query_string}";
135
 
136
 		return $this->__soapCall($function, $args, array('location' => $location), $this->headers);
137
 	}
138
}
139
 
140
class eBayPlatformNotifications {
141
	protected $session = null;
142
	protected $debug;
143
 
144
	public function __construct(eBaySession $session, $debug = false) {
145
		$this->session = $session;
146
		$this->debug = $debug;
147
	}
148
 
149
	protected function carp($string) {
150
		$me = get_class($this);
151
		if ($this->debug) { error_log("$me: $string"); }
152
	}
153
 
154
	protected function notificationLog($string) {
155
		$me = get_class($this);
156
		$datestamp = "[" . date("d-M-Y H:i:s") . "] ";
157
		file_put_contents('notification.log', $datestamp . $string . "\n", FILE_APPEND);
158
	}
159
 
160
	protected function errorLog($string) {
161
		$me = get_class($this);
162
		error_log("$me: $string");
163
		$this->errorMail($me, $string);
164
	}
165
 
166
	protected function errorMail($notification, $message) {
167
    global $fromEmail;
168
    global $toEmail;
169
 
156 - 170
    $subject = "eBay Webhook Alert";
2 - 171
    $headers = "MIME-Version: 1.0" . "\n";
172
    $headers .= "Content-type:text/html;charset=UTF-8" . "\n";
173
    $headers .= "From: eBay Webhook <$fromEmail>" . "\n";
174
    $message = "
175
    <html>
176
      <head>
177
        <title>eBay Webhook Alert</title>
178
      </head>
179
      <body>
180
        <h1>Alert from " . $notification . "</h1>
181
        <table>
182
          <tr>
183
            <th>Message</th>
184
          </tr>
185
          <tr>
186
            <td>" . $message . "</td>
187
          </tr>
188
        </table>
189
      </body>
190
    </html>
191
    ";
192
 
193
 
194
    mail($toEmail,$subject,$message,$headers,"-f".$fromEmail);
195
	}
196
 
197
	protected function CalculateSignature($Timestamp) {
198
		$DevID = $this->session->dev;
199
		$AppID = $this->session->app;
200
		$Cert  = $this->session->cert;
201
 
202
		$hash = "{$Timestamp}{$DevID}{$AppID}{$Cert}";
203
		$this->carp($hash);
204
		// Not quite sure why we need the pack('H*'), but we do
205
		$Signature = base64_encode(pack('H*', md5($hash)));
206
		return $Signature;
207
	}
208
}
209
 
210
 
211
 
212
// General utility class. Currently not used.
213
class eBayUtils {
214
	static public function findByName($values, $name) {
215
		foreach($values as $value) {
216
			if ($value->Name == $name) {
217
				return $value;
218
			}
219
		}
220
	}
221
}
222
 
223
// The following classes are used in the classmap array
224
// Right now, they are largely experiments to see how I can make it easier to use the API
225
class eBayFeesType implements ArrayAccess {
226
	public function offsetExists($offset) {
227
		foreach ($this->Fee as $value) {
228
			if ($value->Name == $offset) {
229
				return true;
230
			}
231
		}
232
 
233
		return false;
234
	}
235
 
236
	public function offsetGet($offset) {
237
		foreach ($this->Fee as $value) {
238
			if ($value->Name == $offset) {
239
				return $value;
240
			}
241
		}
242
	}
243
 
244
	public function offsetSet($offset, $value) {
245
		return true;
246
	}
247
 
248
	public function offsetUnset($offset) {
249
		return true;
250
	}
251
 
252
}
253
 
254
// The following classes are used in the classmap array
255
// Right now, they are largely experiments to see how I can make it easier to use the API
256
class eBayNameValueListType implements ArrayAccess {
257
	public function offsetExists($offset) {
258
		return true;
259
	}
260
 
261
	public function offsetGet($offset) {
262
		foreach ($this->Fee as $value) {
263
			if ($value->Name == $offset) {
264
				return $value;
265
			}
266
		}
267
	}
268
 
269
	public function offsetSet($offset, $value) {
270
		return true;
271
	}
272
 
273
	public function offsetUnset($offset) {
274
		return true;
275
	}
276
 
277
}
278
 
279
 
280
class eBayFeeType {
281
	public function __toString() {
282
		return (string) $this->Fee->_;
283
	}
284
}
285
class eBayPaginatedItemArrayType implements IteratorAggregate {
286
	public function getIterator( ) {
287
        return $this->ItemArray;
288
    }
289
 
290
}
291
 
292
 
293
class eBayItemArrayType implements IteratorAggregate {
294
	public function getIterator( ) {
295
        return new ArrayObject($this->Item);
296
    }
297
 
298
}
299
 
300
 
301
class eBayGetSearchResultsResponseType implements IteratorAggregate {
302
	public function getIterator( ) {
303
        return $this->SearchResultItemArray;
304
    }
305
 
306
}
307
 
308
class eBaySearchResultItemArrayType implements IteratorAggregate {
309
	public function getIterator() {
310
		// put this in __wakeUp()
311
		if (!is_array($this->SearchResultItem)) {
312
			$this->SearchResultItem = array($this->SearchResultItem);
313
		}
314
 
315
        return new ArrayObject($this->SearchResultItem);
316
    }
317
}
318
 
319
class eBayPictureDetailsType implements IteratorAggregate {
320
	public function getIterator() {
321
		// put this in __wakeUp()
322
		if (!is_array($this->PictureURL)) {
323
			$this->PictureURL = array($this->PictureURL);
324
		}
325
 
326
        return new ArrayObject($this->PictureURL);
327
    }
328
}
329
 
330
 
331
 
332
class eBayNameValueListArrayType implements IteratorAggregate, ArrayAccess {
333
	public function getIterator() {
334
		// put this in __wakeUp()
335
		if (!is_array($this->NameValueList)) {
336
			$this->NameValueList = array($this->NameValueList);
337
		}
338
 
339
        return new ArrayObject($this->NameValueList);
340
    }
341
 
342
	public function offsetExists($offset) {
343
		foreach ($this as $NameValueList) {
344
			if ($NameValueList->Name == $offset) {
345
				return true;
346
			}
347
		}
348
 
349
		return false;
350
	}
351
 
352
	public function offsetGet($offset) {
353
		/*
354
		  May need is_object check because we can get:
355
			 <NameValueList xsi:nil="true"/>
356
		  Instead of normal:
357
			  <NameValueList>
358
			   <Name>Year</Name>
359
			   <Value>19000000</Value>
360
			  </NameValueList>
361
 
362
		  ext/soap will create an empty array element for the nilled out element.
363
		  I think that's actually correct, but it would be better for eBay not to return anything here.
364
		  However, I am not 100% sure. This is tricky.
365
		  Note: nilled out elements can have attributes.
366
		  Also, I'm unsure if an element has to be explicitly labled as nillable in the schema if you want to nil it out.
367
		*/
368
 
369
		foreach ($this as $NameValueList) {
370
			if ($NameValueList->Name == $offset) {
371
				if ($NameValueList->Name == 'Year') {
372
					// eBay returns this as YYYYMMDD, but MMDD is always 0000
373
					// Because cars come out in 2006, not 20060000
374
					// So, trim off stupid 0000 at end
375
					$value = substr($NameValueList->Value, 0, 4);
376
				} else {
377
					$value = $NameValueList->Value;
378
				}
379
 
380
				return $value;
381
			}
382
		}
383
 
384
		return null;
385
	}
386
 
387
	public function offsetSet($offset, $value) {
388
		return true;
389
	}
390
 
391
	public function offsetUnset($offset) {
392
		return true;
393
	}
394
 
395
}
396
 
397
 
398
class eBaySearchResultItemType {
399
	public function __toString() {
400
		return $this->Item->Title;
401
	}
402
}
403
 
404
class eBayAmountType {
405
	public function __toString() {
406
		return (string) $this->_;
407
	}
408
}
409
 
410
class eBayItemType {
411
	public function __toString() {
412
		return (string) $this->Title;
413
	}
414
}
415
 
416
class xsDateTime {
417
	public function __toString() {
418
		return $this->Item->Title;
419
	}
420
 
421
}
422
?>