/**
  * Actions executed when receiving data.
  *
  * The current implementation of the server recognizes the following
  * commands:
  *
  * - SHUTDOWN: the shutdown procedure is called;
  * - STATUS: a message about the status of the server is sent;
  * - REFRESH: refreshes the loaded Module configuration;
  * - INVOKE: a command is requested to be executed.
  *
  * @since 5.1
  */
 public function onReceiveData($clientId = null, $data = null)
 {
     $response = new ModuleServerResponse();
     $raw_request = explode("\n", $data);
     $headers = array();
     $body = '';
     $body_start = false;
     $command_line = '';
     foreach ($raw_request as $line) {
         $line = trim($line);
         if (!$body_start and $line == '') {
             $body_start = true;
             continue;
         }
         if ($body_start) {
             $body .= $line . "\n";
         } else {
             if (strlen($command_line)) {
                 $headers[substr($line, 0, strpos($line, ':'))] = trim(substr($line, strpos($line, ':') + 1));
             } else {
                 $command_line = $line;
             }
         }
     }
     if (!isset($headers['User'])) {
         $headers['User'] = '';
     }
     if (!isset($headers['Password'])) {
         $headers['Password'] = '';
     }
     if ($this->authenticator->authenticate($headers['User'], $headers['Password'])) {
         $command = explode(' ', $command_line);
         switch ($command[0]) {
             case 'SHUTDOWN':
                 if ($this->authenticator->authorizeAction($headers['User'], 'shutdown')) {
                     $this->serversocket->sendData($clientId, "Shutdown requested.\n", true);
                     $this->logAccess($clientId, $headers['User'], $command_line);
                     $this->serversocket->shutDown();
                     return;
                 } else {
                     $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized');
                 }
                 break;
             case 'STATUS':
                 if ($this->authenticator->authorizeAction($headers['User'], 'status')) {
                     $response->setBuffer("Module server is up.\n");
                 } else {
                     $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized');
                 }
                 break;
             case 'REFRESH':
                 if ($this->authenticator->authorizeAction($headers['User'], 'status')) {
                     $this->authenticator->parseConfig();
                     $response->setBuffer("Module server configuration reloaded.\n");
                 } else {
                     $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized');
                 }
                 break;
             case 'INVOKE':
                 if ($this->authenticator->authorizeModule($headers['User'], $command[1])) {
                     $request = new ModuleServerRequest();
                     $request->setCommand($command_line);
                     $request->setHeaders($headers);
                     $request->setPayload($body);
                     $server = new ModuleServerXmlRpcProcessor();
                     $server->process($request, $response);
                 } else {
                     $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Module not authorized');
                 }
                 break;
         }
     } else {
         $response->sendWarning(ModuleServerResponse::SC_UNAUTHORIZED, 'Authentication needed');
     }
     $this->serversocket->sendData($clientId, $response->getResponse(), true);
     $this->logAccess($clientId, $headers['User'], $command_line);
     $this->serversocket->closeConnection();
 }
 /**
  * Actions executed when receiving data.
  *
  * The current implementation of the server recognizes the following
  * commands:
  *
  * - SHUTDOWN: the shutdown procedure is called;
  * - STATUS: a message about the status of the server is sent;
  * - REFRESH: refreshes the loaded Module configuration;
  * - INVOKE: a command is requested to be executed.
  *
  * @since 5.1
  */
 public function onReceiveData($clientId = null, $data = null)
 {
     $response = new ModuleServerResponse();
     $raw_request = explode("\n", $data);
     $headers = array();
     $body = '';
     $body_start = false;
     $command_line = '';
     foreach ($raw_request as $line) {
         $line = trim($line);
         if (!$body_start and $line == '') {
             $body_start = true;
             continue;
         }
         if ($body_start) {
             $body .= $line . "\n";
         } else {
             if (strlen($command_line)) {
                 $headers[substr($line, 0, strpos($line, ':'))] = trim(substr($line, strpos($line, ':') + 1));
             } else {
                 $command_line = $line;
             }
         }
     }
     if (!isset($headers['User'])) {
         $headers['User'] = '';
     }
     if (!isset($headers['Password'])) {
         $headers['Password'] = '';
     }
     if ($this->authenticator->authenticate($headers['User'], $headers['Password'])) {
         $command = explode(' ', $command_line);
         switch ($command[0]) {
             case 'GET_REGISTRY':
                 //OK
                 if ($this->authenticator->authorizeAction($headers['User'], 'get_registry')) {
                     $this->logAccess($clientId, $headers['User'], $command_line);
                     $context = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext');
                     if ($file_content = file_get_contents($context->getHome() . 'core/conf/modules-netregistry.xml')) {
                         $response->setBuffer($file_content);
                     } else {
                         $response->setBuffer("Net Registry not found");
                     }
                 } else {
                     $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized');
                 }
                 break;
             case 'SHUTDOWN':
                 if ($this->authenticator->authorizeAction($headers['User'], 'shutdown')) {
                     $this->serversocket->sendData($clientId, "Shutdown requested.\n", true);
                     $this->logAccess($clientId, $headers['User'], $command_line);
                     $this->serversocket->shutDown();
                     return;
                 } else {
                     $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized');
                 }
                 break;
             case 'STATUS':
                 if ($this->authenticator->authorizeAction($headers['User'], 'status')) {
                     $response->setBuffer("Module: services-extension up and ready.\n");
                 } else {
                     $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized');
                 }
                 break;
             case 'REFRESH':
                 //refresh configuration and net-registry (following ini-file parameters)
                 if ($this->authenticator->authorizeAction($headers['User'], 'status')) {
                     $this->authenticator->parseConfig();
                     $registryHandler = new ModuleRegistryHandler();
                     $registryHandler->parseRegistry();
                     $response->setBuffer("Module: services-extension registry reloaded successfully.\n");
                     print "Module: services-extension registry reloaded successfully.\n";
                 } else {
                     $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized');
                 }
                 break;
             case 'INVOKE':
                 if ($this->authenticator->authorizeModule($headers['User'], $command[1])) {
                     $request = new ModuleServerRequest();
                     $request->setCommand($command_line);
                     $request->setHeaders($headers);
                     $request->setPayload($body);
                     $server = new ModuleServerXmlRpcProcessor();
                     $server->process($request, $response);
                 } else {
                     $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Module not authorized');
                 }
                 break;
         }
     } else {
         $response->sendWarning(ModuleServerResponse::SC_UNAUTHORIZED, 'Authentication needed');
     }
     $this->serversocket->sendData($clientId, $response->getResponse(), true);
     $this->logAccess($clientId, $headers['User'], $command_line);
     $this->serversocket->closeConnection();
 }