1 | <?php
|
---|
2 | /**
|
---|
3 | * This file contains the code for the TCP SOAP server.
|
---|
4 | *
|
---|
5 | * PHP versions 4 and 5
|
---|
6 | *
|
---|
7 | * LICENSE: This source file is subject to version 2.02 of the PHP license,
|
---|
8 | * that is bundled with this package in the file LICENSE, and is available at
|
---|
9 | * through the world-wide-web at http://www.php.net/license/2_02.txt. If you
|
---|
10 | * did not receive a copy of the PHP license and are unable to obtain it
|
---|
11 | * through the world-wide-web, please send a note to license@php.net so we can
|
---|
12 | * mail you a copy immediately.
|
---|
13 | *
|
---|
14 | * @category Web Services
|
---|
15 | * @package SOAP
|
---|
16 | * @author Shane Caraveo <Shane@Caraveo.com> Port to PEAR and more
|
---|
17 | * @copyright 2003-2005 The PHP Group
|
---|
18 | * @license http://www.php.net/license/2_02.txt PHP License 2.02
|
---|
19 | * @link http://pear.php.net/package/SOAP
|
---|
20 | */
|
---|
21 |
|
---|
22 | require_once 'SOAP/Server.php';
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * SOAP Server Class that implements a TCP SOAP Server.
|
---|
26 | * http://www.pocketsoap.com/specs/smtpbinding/
|
---|
27 | *
|
---|
28 | * This class overrides the default HTTP server, providing the ability to
|
---|
29 | * accept socket connections and execute SOAP calls.
|
---|
30 | *
|
---|
31 | * TODO:
|
---|
32 | * use Net_Socket
|
---|
33 | * implement some security scheme
|
---|
34 | * implement support for attachments
|
---|
35 | *
|
---|
36 | * @access public
|
---|
37 | * @package SOAP
|
---|
38 | * @author Shane Caraveo <shane@php.net>
|
---|
39 | */
|
---|
40 | class SOAP_Server_TCP extends SOAP_Server {
|
---|
41 |
|
---|
42 | var $headers = array();
|
---|
43 | var $localaddr;
|
---|
44 | var $port;
|
---|
45 | var $listen;
|
---|
46 | var $reuse;
|
---|
47 |
|
---|
48 | function SOAP_Server_TCP($localaddr = '127.0.0.1', $port = 10000,
|
---|
49 | $listen = 5, $reuse = true)
|
---|
50 | {
|
---|
51 | parent::SOAP_Server();
|
---|
52 | $this->localaddr = $localaddr;
|
---|
53 | $this->port = $port;
|
---|
54 | $this->listen = $listen;
|
---|
55 | $this->reuse = $reuse;
|
---|
56 | }
|
---|
57 |
|
---|
58 | function run()
|
---|
59 | {
|
---|
60 | if (($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0) {
|
---|
61 | return $this->_raiseSoapFault('socket_create() failed. Reason: ' . socket_strerror($sock));
|
---|
62 | }
|
---|
63 | if ($this->reuse &&
|
---|
64 | !@socket_setopt($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
|
---|
65 | return $this->_raiseSoapFault('socket_setopt() failed. Reason: ' . socket_strerror(socket_last_error($sock)));
|
---|
66 | }
|
---|
67 | if (($ret = socket_bind($sock, $this->localaddr, $this->port)) < 0) {
|
---|
68 | return $this->_raiseSoapFault('socket_bind() failed. Reason: ' . socket_strerror($ret));
|
---|
69 | }
|
---|
70 | if (($ret = socket_listen($sock, $this->listen)) < 0) {
|
---|
71 | return $this->_raiseSoapFault('socket_listen() failed. Reason: ' . socket_strerror($ret));
|
---|
72 | }
|
---|
73 |
|
---|
74 | while (true) {
|
---|
75 | $data = null;
|
---|
76 | if (($msgsock = socket_accept($sock)) < 0) {
|
---|
77 | $this->_raiseSoapFault('socket_accept() failed. Reason: ' . socket_strerror($msgsock));
|
---|
78 | break;
|
---|
79 | }
|
---|
80 | while ($buf = socket_read($msgsock, 8192)) {
|
---|
81 | if (!$buf = trim($buf)) {
|
---|
82 | continue;
|
---|
83 | }
|
---|
84 | $data .= $buf;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if ($data) {
|
---|
88 | $response = $this->service($data);
|
---|
89 | /* Write to the socket. */
|
---|
90 | if (!socket_write($msgsock, $response, strlen($response))) {
|
---|
91 | return $this->_raiseSoapFault('Error sending response data reason ' . socket_strerror());
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | socket_close ($msgsock);
|
---|
96 | }
|
---|
97 |
|
---|
98 | socket_close ($sock);
|
---|
99 | }
|
---|
100 |
|
---|
101 | function service(&$data)
|
---|
102 | {
|
---|
103 | /* TODO: we need to handle attachments somehow. */
|
---|
104 | return $this->parseRequest($data, $attachments);
|
---|
105 | }
|
---|
106 | }
|
---|