TeamWox HelpChatPublic ComponentPHP Example

Example of Public Component in PHP

This section contains an extended example of implementation of the public component of the "Chat" module using the PHP language. This example includes implementation of the feature of getting information about online users in service groups.

Comments to the Example

The following line of the example contains the address where requests are sent:

define("TEAMWOX_HOST", "your.domain.com");

You should change the "your.domain.com" address to the address of your own TeamWox system. The internal TeamWox page that is requested in the code is specified in the following line:

$query = "GET /public/chat/groups?json HTTP/1.1\r\n";

The /public/chat/group page is called, and the result is returned in the JSON format. That is the page from which the list of available groups and their online/offline status are requested.

Full Code

The full code of implementation of the public component is given below:

<?php
  define("TEAMWOX_HOST", "your.domain.com");
  /**
   * checking string in the hex format
   *
   * @param string $hex
   * @return boolean true if string is hex, otherwise - false
   */
  function is_hex($hex)
    {
    // processing of string
    $hex = strtolower(trim(ltrim($hex, "0")));
    if (empty($hex))
      {
      $hex = 0;
      }
    $dec = hexdec($hex);
    return ($hex == dechex($dec));
    }
 
  /**
   * junction of chunks http 'transfer-encoding: chunked'
   *
   * @param string $chunk initial string
   * @return string string with joined chunks
   */
  function http_chunked_decode($chunk)
    {
    $pos = 0;
    $len = strlen($chunk);
    $dechunk = null;
    //--- get chunks in loop and join them
    while (($pos < $len) && ($chunk_len_hex = substr($chunk, $pos, ($new_line_at = strpos($chunk, "\n", $pos + 1)) - $pos)))
      {
      if (!is_hex($chunk_len_hex))
        {
        trigger_error('Incorrect number', E_USER_WARNING);
        return $chunk;
        }
      //--- information about next chunk
      $pos = $new_line_at + 1;
      $chunk_len = hexdec(rtrim($chunk_len_hex, "\r\n"));
      //--- junction of chunks
      $dechunk .= substr($chunk, $pos, $chunk_len);
      $pos = strpos($chunk, "\n", $pos + $chunk_len) + 1;
      }
    return $dechunk;
    }
 
  /**
   * The function of getting information about all groups and their statuses
   * @return object
   */
  function MQ_Query_get_chat_group()
    {
    //--- open socket to TEAMWOX_HOST port 443 with 30 seconds timeout
    $socket = fsockopen(TEAMWOX_HOST, 80, $errno, $errstr, 30);
    if (!$socket)
      {
      trigger_error($errstr, E_USER_WARNING);
      return false;
      }
    //--- create request
    $query = "GET /public/chat/groups?json HTTP/1.1\r\n";
    $query .= "Host: " . TEAMWOX_HOST . "\r\n";
    $query .= "User-agent: Web-site\r\n";
    $query .= "Connection: Close\r\n\r\n";
    //--- write request to the socket
    fwrite($socket, $query);
    //--- get answer
    $answer = '';
    while (!feof($socket))
      {
      $answer .= fgets($socket, 4096);
      }
    //--- close socket
    fclose($socket);
    //--- check the answer
    $pos = strpos($answer, "\r\n\r\n");
    if ($pos > 0)
      {
      //--- find body of the answer
      $body = substr($answer, $pos + 4);
      $body = http_chunked_decode($body);
      return json_decode($body);
      }
    return false;
    }
 
  /**
   * The function for displaying of statuses
   * @return void
   */
  function MQ_Print_status()
    {
    $data = MQ_Query_get_chat_group();
    if (empty($data)) return;
    //---
    if (empty($data->groups)) return;
    foreach ($data->groups as $info)
      {
      if (empty($info)) continue;
      //--- display status
      echo $info->name, " ", ($info->online == 1 ? "on-line" : "off line"), "<br>\r\n";
      }
    }
 
  //--- call of the function of displaying of statuses
  MQ_Print_status();
?>