<?php
  /*********************************************************************
  *  A very simple chatbox sample.
  *   It demonstrates the use of PHP "Objects-Method Propergation" in use
  *   JS-RS. (Simple Function Propergation is just as simple)
  *  
  *  @author Sam Blum  bs_php@users.sourceforge.net
  *  @doc    http://js-rs.sourceforge.net
  *  @source http://sourceforge.net/projects/js-rs/
  */

  include( "../lib_PHP/PhPage.lib.php" );
  include( "../lib_PHP/Studio.lib.php" );

  // Inclui a Classe JsrsServer
  require("../lib_PHP/JsrsServer.class.php");
  


  /**********************************************************
  * Save chat data so disk and offer to methods to the client:
  *
  *   read()  : Read out the chat text collected so far.
  *
  *   write() : Add text to the chat text.
  */
  class MsgServer {
  
    /*
     *  propriedade: chatFile
     */
    var $chatFile = 'chatBuffer.txt';
    
    // Methods private
    function _tail($lineCount=10) {
      $ret = '';
      if (file_exists($this->chatFile)) {
        $t = file($this->chatFile);
        $t = array_slice($t, -$lineCount);
        $ret = join ('', $t);
      }
      return $ret;
    }
    

    /*
     * Method seek_user  ====================================================
     */
    function seek_user( $txt )
    {
      $ix   = strpos($txt, " :: ");
      $log_ = substr($txt,0, $ix);
      $pas_ = substr($txt,$ix+4);
      $ret  = '0: ' . $txt . ' não cadastrado...';

      $arrUser = readINIfile ( $_SERVER['DOCUMENT_ROOT'] . '/studio/desenv/usuarios.ini', ';' );

      foreach( $arrUser as $ind => $arrDados )
      {
        if ( $arrDados['login']    == $log_ &&
             $arrDados['password'] == $pas_
           )
        {
          $ret = '1:' . $arrDados[ 'user_id' ] . ':' .$arrDados[ 'user_name' ];
        }
      }// End foreach()
      
      $nFile = $_SERVER['DOCUMENT_ROOT'] . "/Studio/desenv/log/".date('Y_m_d')."_USR_".$log_.".log";
      $fp = @fopen($nFile, 'ab+');
      @fclose($fp);

      return $ret;
    }// End function seek_user($txt)




  }// End class MsgServer
  
  // First create the object to be called. (Pseudo static)
  $theMsgServer = new MsgServer();
  
  // Use the static $JsrsServer to 
  // propergate the methodes that are available for the client
  $JsrsServer->propergateMethod($theMsgServer, 'seek_user');

  // Start the JsrsServer to handle the request from the client
  $JsrsServer->start();
?>                                                     	

