/**
 * This function build a JSON-RPC server based in the specification version 1.0
 * http://json-rpc.org/wiki/specification
 *
 * @author Basilio Briceno <*****@*****.**>
 * @param object $object
 * @return boolean
 */
function tlalokes_jsonrpc_server_handle ( &$object )
{
  if ( !tlalokes_jsonrpc_server_check() ) {
    return false;
  }

  // reads the input data
  $request = json_decode( file_get_contents( 'php://input' ), true );

  // executes the task on local object
  try {
    $result = call_user_func_array( array( $object, $request['method'] ),
                                    $request['params'] );
    if ( $result ) {
      $response = array ( 'id' => $request['id'],
                          'result' => $result,
                          'error' => null );
    } else {
      $response = array ( 'id' => $request['id'],
                          'result' => NULL,
                          'error' => 'Unknown method or incorrect parameters' );
    }
    unset( $result );
  } catch ( Exception $e ) {
    $response = array ( 'id' => $request['id'],
                        'result' => null,
                        'error' => $e->getMessage() );
  }

  // output the response
  if ( isset( $request['id'] ) && $request['id'] ) {
    // notifications don't want response
    header( 'content-type: text/javascript' );
    echo json_encode( $response );
  }

  // finish
  exit;
}
/**
 * Checks if web services are declares in the controller and loads them
 *
 * @author Basilio Briceno <*****@*****.**>
 * @param TlalokesRegistry $reg
 */
function tlalokes_receiver_webservices(&$reg)
{
    // reflect Annotations in method
    require 'ReflectionAnnotatedClass.php';
    $ref = new ReflectionAnnotatedClass($reg->conf['current']['controller']);
    //require 'ControllerDefinition.php';
    if ($ref->hasAnnotation('ControllerDefinition')) {
        // JSON
        if ($ref->getAnnotation('ControllerDefinition')->json) {
            // check request
            if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                var_dump($_POST);
                $reg->webservice = true;
                $obj = new $reg->conf['current']['controller']($reg);
                unset($reg);
                echo json_encode($obj->response);
                exit;
            }
        }
        // JSON-RPC
        if ($ref->getAnnotation('ControllerDefinition')->jsonrpc) {
            require 'tlalokes_jsonrpc.php';
            // check request
            if (tlalokes_jsonrpc_server_check()) {
                $reg->json = true;
                $obj = new $reg->conf['current']['controller']($reg);
                unset($reg);
                tlalokes_jsonrpc_server_handle($obj);
            }
        }
        // SOAP
        if ($ref->getAnnotation('ControllerDefinition')->soap) {
            if ($_SERVER['REQUEST_METHOD'] == 'POST' || $_SERVER['CONTENT_TYPE'] == 'application/soap+xml') {
                // set URI for the service
                $uri = 'http://' . $_SERVER['HTTP_HOST'] . $reg->conf->path->uri . preg_replace('/^\\/(.*).php/', '$1', $_SERVER['SCRIPT_NAME']) . '/' . $reg->conf['current']['controller'];
                // set service and handle it
                $server = new SoapServer(null, array('uri' => $uri));
                $server->setClass($reg->conf['current']['controller'] . 'Ctl', $reg);
                $server->handle();
                unset($ref);
                exit;
            }
        }
    }
}