1: <?php
2:
3: namespace tschiemer\Aspsms\Xml;
4: use \tschiemer\Aspsms as Aspsms;
5:
6: if ( ! function_exists('curl_init'))
7: {
8: throw new \Exception('CURL extension required for Aspsms\HttpClient');
9: }
10:
11: 12: 13: 14: 15: 16: 17: 18: 19:
20: class XmlClient extends Aspsms\AbstractClient
21: {
22: const ENCODING = 'ISO-8859-1';
23:
24: 25: 26: 27: 28:
29: var $servers = array(
30: 'http://xml1.aspsms.com:5061/xmlsvr.asp',
31: 'http://xml1.aspsms.com:5098/xmlsvr.asp',
32: 'http://xml2.aspsms.com:5061/xmlsvr.asp',
33: 'http://xml2.aspsms.com:5098/xmlsvr.asp'
34: );
35:
36: 37: 38: 39: 40:
41: var $options = array(
42: 'encodingIn' => 'UTF-8',
43: 'encodingOut'=> 'UTF-8'
44: );
45:
46: 47: 48: 49: 50:
51: var $curlOpt = array(
52: CURLOPT_USERAGENT => 'aspsms-php v1 xml:1',
53: CURLOPT_SSL_VERIFYPEER => FALSE
54: );
55:
56: 57: 58:
59: var $requestDOM;
60:
61: 62: 63:
64: var $responseDOM;
65:
66: 67: 68:
69: var $entities = array();
70:
71: 72: 73: 74: 75: 76: 77:
78:
79: 80: 81: 82: 83:
84: var $requests = array(
85: 'getCredits' => array(
86: 'action' => 'ShowCredits',
87: 'param' => array(
88: 'UserKey' => '',
89: 'Password' => ''
90: )),
91:
92: 'sendText' => array(
93: 'action' => 'SendTextSMS',
94: 'param' => array(
95: 'UserKey' => '',
96: 'Password' => '',
97: 'Recipients'=> '',
98: 'Originator'=> '',
99: 'MessageText' => '',
100: 'DeferredDeliveryTime' => '',
101: 'FlashingSMS'=> '',
102: 'TimeZone' => '',
103: 'URLBufferedMessageNotification' => '',
104: 'URLDeliveryNotification' => '',
105: 'URLNonDeliveryNotification' => '',
106: 'AffiliateId' => ''
107: )),
108: 'sendWapPush' => array(
109: 'action' => 'SendWAPPushSMS',
110: 'param' => array(
111: 'UserKey' => '',
112: 'Password' => '',
113: 'Recipients'=> '',
114: 'Originator'=> '',
115: 'WapDescription' => '',
116: 'WapURL' => '',
117: 'DeferredDeliveryTime' => '',
118: 'FlashingSMS'=> '',
119: 'TimeZone' => '',
120: 'URLBufferedMessageNotification' => '',
121: 'URLDeliveryNotification' => '',
122: 'URLNonDeliveryNotification' => '',
123: 'AffiliateId' => ''
124: )),
125:
126: 'getDeliveryStatus' => array(
127: 'action' => 'InquireDeliveryNotifications',
128: 'param' => array(
129: 'UserKey' => '',
130: 'Password' => '',
131: 'TransactionReferenceNumbers'=> ''
132: )),
133:
134: 'checkOriginator' => array(
135: 'action' => 'CheckOriginatorAuthorization',
136: 'param' => array(
137: 'UserKey' => '',
138: 'Password' => '',
139: 'Originator'=> ''
140: )),
141: 'sendOriginatorCode' => array(
142: 'action' => 'SendOriginatorUnlockCode',
143: 'param' => array(
144: 'UserKey' => '',
145: 'Password' => '',
146: 'Originator'=> ''
147: )),
148: 'unlockOriginator' => array(
149: 'action' => 'UnlockOriginator',
150: 'param' => array(
151: 'UserKey' => '',
152: 'Password' => '',
153: 'Originator'=> '',
154: 'OriginatorUnlockCode'=>'',
155: 'AffiliateId'=> ''
156: )),
157: 'sendPicture' => array(),
158: 'sendLogo' => array(),
159: 'sendGroupLogo' => array(),
160: 'sendRingtone' => array(
161: 'action' => 'SendRingtone',
162: 'param' => array(
163: 'UserKey' => '',
164: 'Password' => '',
165: 'Originator'=> '',
166: 'AffiliateId'=> '',
167: 'Recipients'=> '',
168: 'URLBinaryFile' => ''
169: )),
170: 'sendVCard' => array(
171: 'action' => 'SendVCard',
172: 'param' => array(
173: 'UserKey' => '',
174: 'Password' => '',
175: 'Originator'=> '',
176: 'AffiliateId'=> '',
177: 'Recipients'=> '',
178: 'VCard' => array()
179: )),
180: 'sendBinaryData' => array()
181: );
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202: public function __construct()
203: {
204: if (isset($options['servers']))
205: {
206: $this->servers = $options['servers'];
207: }
208:
209: if (isset($options['encodingIn']))
210: {
211: $this->options['encodingIn'] = $options['encodingIn'];
212: }
213: if (isset($options['encodingOut']))
214: {
215: $this->options['encodingOut'] = $options['encodingOut'];
216: }
217:
218: if (isset($options['curl']))
219: {
220: foreach($options['curl'] as $k => $v)
221: {
222: $this->curlOpt[$k] = $v;
223: }
224: }
225:
226: $entities = array(
227: chr(38) => '&',
228: chr(60) => '<',
229: chr(62) => '>'
230: );
231: for($i = 128; $i < 256; $i++)
232: {
233: $entities[chr($i)] = sprintf('&#%03d;',$i);
234: }
235: $this->entities = array(
236: 'plain' => array_keys($entities),
237: 'xml' => array_values($entities)
238: );
239: }
240:
241: 242: 243: 244: 245: 246: 247:
248: public function send($request) {
249:
250:
251: $this->request = $request;
252:
253:
254: $requestName = $request->getRequestName();
255:
256:
257: $cfg =& $this->requests[$requestName];
258:
259:
260: $actionName = $cfg['action'];
261:
262:
263: $this->response = new Aspsms\Response($request);
264:
265:
266: $this->requestDOM = new \DOMDocument('1.0','ISO-8859-1');
267: $this->requestDOM->appendChild(new \DOMElement('aspsms'));
268:
269: if (isset($cfg['param']))
270: {
271: foreach($request->extractArray($cfg['param']) as $k => $v)
272: {
273: if (method_exists($this, 'set_'.$k))
274: {
275: $this->{'set_'.$k}($v);
276: }
277: else
278: {
279: $this->set_default($k,$v);
280: }
281: }
282: }
283:
284: $this->requestDOM->firstChild->appendChild(new \DOMElement('Action',$actionName));
285:
286:
287: $xml = $this->requestDOM->saveXML();
288:
289: var_dump($xml);
290:
291:
292: $ch = curl_init($this->servers[0]);
293:
294: curl_setopt_array($ch, $this->curlOpt);
295:
296: curl_setopt_array($ch, array(
297: CURLOPT_POST => TRUE,
298: CURLOPT_RETURNTRANSFER => TRUE,
299: CURLOPT_POSTFIELDS => $xml
300: ));
301:
302:
303: $result = curl_exec($ch);
304:
305: curl_close($ch);
306:
307: if (is_string($result) and preg_match('/<\?xml version="((?:\d|\.)+)"\?>/',$result,$m))
308: {
309: var_dump($result);
310:
311: $this->responseDOM = new \DOMDocument($m[1],self::ENCODING);
312: $this->responseDOM->loadXML($result);
313:
314: $this->response->statusCode(
315: $this->responseDOM->getElementsByTagName('ErrorCode')->item(0)->textContent
316: );
317:
318: $this->response->statusDescription(
319: $this->responseDOM->getElementsByTagName('ErrorDescription')->item(0)->textContent
320: );
321:
322: }
323: else
324: {
325: var_dump($result);
326: die("failed curl request\n");
327: }
328:
329:
330:
331: if (method_exists($this, 'post_'.$actionName))
332: {
333: $this->{'post_'.$actionName}();
334: }
335: else
336: {
337: $this->post_default();
338: }
339: }
340:
341: 342: 343: 344: 345: 346:
347: public function encodeIn($value)
348: {
349: if ($this->options['encodingIn'] !== self::ENCODING)
350: {
351: $value = mb_convert_encoding($value, self::ENCODING, $this->options['encodingIn']);
352: }
353: return str_replace($this->entities['plain'], $this->entities['xml'], $value);
354: }
355:
356: 357: 358: 359: 360: 361:
362: public function encodeOut($value)
363: {
364: if ($this->options['encodingOut'] !== self::ENCODING)
365: {
366: $value = mb_convert_encoding($value, $this->options['encodingOut'], self::ENCODING);
367: }
368: return $value;
369: }
370:
371: public function set_default($key,$value)
372: {
373:
374: {
375: $node = new \DOMElement($key, $this->encodeIn($value));
376: $this->requestDOM->firstChild->appendChild($node);
377: }
378: }
379:
380: public function set_UserKey($value)
381: {
382: $this->set_default('Userkey', $value);
383: }
384:
385: public function set_Recipients($value)
386: {
387: if (strlen($value) == 0)
388: {
389: return;
390: }
391:
392: $recipientList = explode(';',$value);
393: foreach($recipientList as $recipient)
394: {
395: $phone_track = explode(':',$recipient);
396:
397: $recipientNode = new \DOMElement('Recipient');
398:
399: $this->requestDOM->firstChild->appendChild($recipientNode);
400:
401: $recipientNode->appendChild(new \DOMElement('PhoneNumber', $phone_track[0]));
402:
403: if (count($phone_track) == 1)
404: {
405: $recipientNode->appendChild(new \DOMElement('TransRefNumber'));
406: }
407: else
408: {
409: $recipientNode->appendChild(new \DOMElement('TransRefNumber', $phone_track[1]));
410: }
411: }
412: }
413:
414: public function set_MessageText($value)
415: {
416: $this->set_default('MessageData', $value);
417: }
418:
419: public function set_TransactionReferenceNumbers($value)
420: {
421: if (strlen($value) == 0)
422: {
423: return;
424: }
425:
426: $list = explode(';',$value);
427: foreach($list as $refNr)
428: {
429: $this->requestDOM->firstChild->appendChild(new \DOMElement('TransRefNumber', $refNr));
430: }
431: }
432:
433: public function set_VCard($value)
434: {
435: if (empty($value) or ! is_array($value))
436: {
437: return;
438: }
439:
440: $vcard = new \DOMElement('VCard');
441:
442: $this->requestDOM->firstChild->appendChild($vcard);
443:
444: $vcard->appendChild(new \DOMElement('VName', $value['name']));
445: $vcard->appendChild(new \DOMElement('VPhoneNumber', $value['phoneNr']));
446: }
447:
448: 449: 450:
451: public function post_default()
452: {
453: $this->response->result = $this->response->statusCode() == 1;
454: }
455:
456: 457: 458:
459: public function post_ShowCredits()
460: {
461: $this->response->result = floatval($this->responseDOM->getElementsByTagName('Credits')->item(0)->textContent);
462: }
463: }
464:
465: