Kerio APIs Client Library for PHP 1.4.0.234
  • Namespace
  • Class
  • Tree
  • Deprecated

Namespaces

  • None
  • PHP

Classes

  • KerioApi
  • KerioApiSocket
  • KerioConnectApi
  • KerioControlApi
  • KerioDirectoryApi
  • KerioOperatorApi
  • KerioWorkspaceApi
  • SamepageApi

Interfaces

  • KerioApiInterface
  • KerioApiSocketInterface

Exceptions

  • KerioApiException
  1 <?php
  2 /**
  3  * This file is part of the kerio-api-php.
  4  *
  5  * Copyright (c) Kerio Technologies s.r.o.
  6  *
  7  * For the full copyright and license information, please view
  8  * the file license.txt that was distributed with this source code
  9  * or visit Developer Zone. (http://www.kerio.com/developers)
 10  *
 11  * Do not modify this source code.
 12  * Any changes may be overwritten by a new version.
 13  */
 14 
 15 require_once(dirname(__FILE__) . '/KerioApiSocketInterface.php');
 16 
 17 /**
 18  * Kerio API Socket Class.
 19  *
 20  * This class implements basic methods used in HTTP communication.
 21  *
 22  * @copyright   Copyright &copy; 2012-2012 Kerio Technologies s.r.o.
 23  * @license     http://www.kerio.com/developers/license/sdk-agreement
 24  * @version     1.4.0.234
 25  */
 26 class KerioApiSocket implements KerioApiSocketInterface {
 27 
 28     /**
 29      * Socket buffer size
 30      */
 31     const BUFFER_SIZE = 10240;
 32 
 33     /**
 34      * Socket handler
 35      * @var resource
 36      */
 37     private $socketHandler = '';
 38 
 39     /**
 40      * Communication timeout
 41      * @var integer
 42      */
 43     private $timeout = 10;
 44 
 45     /**
 46      * Server hostname
 47      * @var string
 48      */
 49     private $hostname = '';
 50 
 51     /**
 52      * Server port
 53      * @var integer
 54      */
 55     private $port = '';
 56 
 57     /**
 58      * SSL encryption
 59      * @var string
 60      */
 61     private $cipher = 'ssl://';
 62 
 63     /**
 64      * Headers
 65      * @var string
 66      */
 67     private $headers = '';
 68 
 69     /**
 70      * Body
 71      * @var string
 72      */
 73     private $body = '';
 74 
 75     /**
 76      * Socker error message
 77      * @var string
 78      */
 79     private $errorMessage = '';
 80 
 81     /**
 82      * Socker error code
 83      * @var integer
 84      */
 85     private $errorCode = 0;
 86 
 87     /**
 88      * Class constructor.
 89      *
 90      * @param   string  Hostname
 91      * @param   integer Port
 92      * @param   integer Timeout, optional
 93      * @return  boolean True on success
 94      */
 95     public function KerioApiSocket($hostname, $port, $timeout = '') {
 96         /* Set host */
 97         $this->hostname = $hostname;
 98         $this->port = $port;
 99 
100         /* Set timeout */
101         if (is_int($timeout)) {
102             $this->timeout = $timeout;
103         }
104 
105         /* Open socket to server */
106         $this->open();
107         return ($this->socketHandler) ? TRUE : FALSE;
108     }
109 
110     /**
111      * Class desctructor.
112      *
113      * @param   void
114      * @return  void
115      */
116     public function __destruct() {
117         $this->close();
118     }
119 
120     /**
121      * Open socket to server.
122      *
123      * @param   void
124      * @return  void
125      */
126     protected function open() {
127         $errstr = "";
128         $errno  = "";
129         $context = stream_context_create();
130         stream_context_set_option($context, "ssl", "allow_self_signed", true);
131         stream_context_set_option($context, "ssl", "verify_peer", false);
132         stream_context_set_option($context, "ssl", "verify_peer_name", false);
133         $this->socketHandler = @stream_socket_client($this->cipher . $this->hostname . ':' .  $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $context);
134         $this->errorCode = $errno;
135         $this->errorMessage = $errstr;
136     }
137 
138     /**
139      * Close socket to server.
140      *
141      * @param   void
142      * @return  void
143      */
144     protected function close() {
145         @fclose($this->socketHandler);
146         unset($this->socketHandler);
147     }
148 
149     /**
150      * Send data to socket.
151      *
152      * @see class/KerioApiSocketInterface::send()
153      * @param   string  Data to socket
154      * @return  string  Data from socket
155      * @throws  KerioApiException
156      */
157     public function send($data) {
158         if ($this->checkConnection()) {
159             @fwrite($this->socketHandler, $data);
160             return $this->read();
161         }
162         else {
163             throw new KerioApiException(sprintf("Cannot connect to %s using port %d.", $this->hostname, $this->port));
164         }
165     }
166 
167     /**
168      * Read data from socket.
169      *
170      * @param   void
171      * @return  string  HTTP data from socket
172      * @throws  KerioApiExceptions
173      */
174     protected function read() {
175         if ($this->socketHandler) {
176             $response = '';
177             while (FALSE === feof($this->socketHandler)) {
178                 $response .= fgets($this->socketHandler, self::BUFFER_SIZE);
179             }
180             
181             list($this->headers, $this->body) = explode("\r\n\r\n", $response);
182             
183             if (FALSE !== strpos(strtolower($this->headers), 'transfer-encoding: chunked')) {
184                 $this->unchunkHttp();
185             }
186             
187             return $response;
188         }
189         else {
190             throw new KerioApiException('Cannot read data from server, connection timeout.');
191         }
192     }
193 
194     /**
195      * Unchunk HTTP/1.1 body.
196      * 
197      * @param   void
198      * @return  void
199      */
200     private function unchunkHttp() {
201         $body = $this->body;
202         for ($new = ''; !empty($body); $str = trim($body)) {
203             $pos  = strpos($body, "\r\n");
204             $len  = hexdec(substr($body, 0, $pos));
205             $new .= substr($body, $pos + 2, $len);
206             $body = substr($body, $pos + 2 + $len);
207         }
208         $this->body = $new;
209     }
210 
211     /**
212      * Set connection encryption to ssl://
213      * 
214      * @param   boolen  True if ssl:// is used
215      * @return  void
216      */
217     public function setEncryption($boolean) {
218         $this->cipher = ($boolean) ? 'ssl://' : '';
219     }
220 
221     /**
222      * Check connection to server.
223      *
224      * @param   void
225      * @return  boolean True on success
226      */
227     public final function checkConnection() {
228         if ($this->checkHost()) {
229             $socket = @fsockopen($this->hostname, $this->port, $errno, $errstr, $this->timeout);
230             $this->errorCode = $errno;
231             $this->errorMessage = $errstr;
232             return ($socket) ? TRUE : FALSE;
233         }
234         else {
235             return FALSE;
236         }
237     }
238 
239     /**
240      * Check if DNS host is valid.
241      *
242      * @param   void
243      * @return  boolean True on success
244      */
245     public final function checkHost() {
246         return gethostbyname($this->hostname) ? TRUE : FALSE;
247     }
248 
249     /**
250      * Get headers.
251      * 
252      * @param   void
253      * @return  string
254      */
255     public final function getHeaders() {
256         return $this->headers;
257     }
258 
259     /**
260      * Get body.
261      *
262      * @param   void
263      * @return  string
264      */
265      public final function getBody() {
266         return $this->body;
267     }
268 
269     /**
270      * Get socker error message.
271      *
272      * @param   void
273      * @return  string
274      */
275     public final function getErrorMessage() {
276         return $this->errorMessage;
277     }
278 
279     /**
280      * Get socket error code.
281      *
282      * @param   void
283      * @return  integer
284      */
285     public final function getErrorCode() {
286         return $this->errorCode;
287     }
288 }
289 
290 
Kerio APIs Client Library for PHP 1.4.0.234 API documentation generated by ApiGen 2.8.0