public function onReceiveData($clientId = null, $data = null) { //print('dati ricevuti'); $response = new \Innomatic\Modules\Server\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 'PING': if ($this->authenticator->authorizeAction($headers['User'], 'ping')) { $response->setBuffer("pong. (from " . $this->bindAddress . ":" . $this->port . ")\n"); } else { $response->sendWarning(\Innomatic\Modules\Server\ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized'); } print "I've been pinged" . "\n"; break; case 'SHUTDOWN': if ($this->authenticator->authorizeAction($headers['User'], 'shutdown')) { $this->serversocket->sendData($clientId, "pinger server: shuting down.\n", true); print "pinger server: shuting down" . "\n"; Carthag::instance()->halt("pinger server: terminated."); } else { $response->sendWarning(\Innomatic\Modules\Server\ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized'); } break; default: $response->sendWarning(\Innomatic\Modules\Server\ModuleServerResponse::SC_BAD_REQUEST, 'Cannot hunderstand command'); } } else { $response->sendWarning(\Innomatic\Modules\Server\ModuleServerResponse::SC_UNAUTHORIZED, 'Authentication needed'); } $this->serversocket->sendData($clientId, $response->getResponse(), true); $this->serversocket->closeConnection(); }
/** * force the ModuleServiceSocketHandler to refresh the registry * * @since 5.1 */ protected function force_refresh() { print "force_refresh" . "\n"; $request = ''; $address = '127.0.0.1'; $context = \Innomatic\Module\Server\ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext'); try { $port = $context->getConfig()->getKey('service_port'); $this->socket->connect($address, $port); $request = 'REFRESH Module/1.1' . "\r\n"; $request .= 'User: admin' . "\r\n"; $request .= 'Password: '******'admin') . "\r\n"; $this->socket->write($request); $result = $this->socket->readAll(); $this->socket->disconnect(); } catch (\Innomatic\Net\Socket\SocketException $e) { print "Cannot refresh: server " . $address . ":" . $port . " down!" . "\n"; } print $result; }
/** * Gets an instance of a local Module object. * * This static method retrieves a Module located in local context, without * using the Module server. Module authentication is needed anyway. * * The method also builds the configuration for the Module. * * @since 5.1 * @param ModuleLocator $locator Module locator object. * @return ModuleObject The Module object. */ public static function getLocalModule(ModuleLocator $locator) { $location = $locator->getLocation(); // Authenticates $authenticator = ModuleServerAuthenticator::instance('ModuleServerAuthenticator'); if (!$authenticator->authenticate($locator->getUsername(), $locator->getPassword())) { throw new ModuleException('Not authorized'); } $context = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext'); // Checks if Module exists $classes_dir = $context->getHome() . 'core/modules/' . $location . '/classes/'; if (!is_dir($classes_dir)) { throw new ModuleException('Module does not exists'); } // Checks if configuration file exists $moduleXml = $context->getHome() . 'core/modules/' . $location . '/setup/module.xml'; if (!file_exists($moduleXml)) { throw new ModuleException('Missing module.xml configuration file'); } $cfg = \Innomatic\Module\Util\ModuleXmlConfig::getInstance($moduleXml); $fqcn = $cfg->getFQCN(); // Builds Module Data Access Source Name $dasn_string = $authenticator->getDASN($locator->getUsername(), $locator->getLocation()); if (strpos($dasn_string, 'context:')) { $module_context = new ModuleContext($location); $dasn_string = str_replace('context:', $module_context->getHome(), $dasn_string); } $cfg->setDASN(new DataAccessSourceName($dasn_string)); // Adds Module classes directory to classpath // TODO: Should add include path only if not already available set_include_path($classes_dir . PATH_SEPARATOR . get_include_path()); require_once $fqcn . '.php'; // Retrieves class name from Fully Qualified Class Name $class = strpos($fqcn, '/') ? substr($fqcn, strrpos($fqcn, '/') + 1) : $fqcn; // Instantiates the new class and returns it return new $class($cfg); }
/** * 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(); }
/** * stops the pinger * (starts the pinger shuting down sequence) * * @see ModulePinger.php * @since 5.1 * @return string Server result string. */ private function stopPinger() { $result = ''; $auth = ModuleServerAuthenticator::instance('ModuleServerAuthenticator'); $context = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext'); $this->socket->connect('127.0.0.1', $context->getConfig()->getKey('pinger_port')); $request = 'SHUTDOWN Module/1.0' . "\r\n"; $request .= 'User: admin' . "\r\n"; $request .= 'Password: '******'admin') . "\r\n"; $this->socket->write($request); $result = $this->socket->readAll(); $this->socket->disconnect(); }
/** * Forces the server to refresh its configuration. * * @since 5.1 * @return string Server result string. */ public function refresh() { try { $result = ''; $auth = ModuleServerAuthenticator::instance('ModuleServerAuthenticator'); $this->socket->connect($this->host, $this->port); $request = 'REFRESH Module/1.0' . "\r\n"; $request .= 'User: admin' . "\r\n"; $request .= 'Password: '******'admin') . "\r\n"; $this->socket->write($request); $result = $this->socket->readAll(); $this->socket->disconnect(); } catch (\Innomatic\Net\Socket\SocketException $e) { $result = 'Module server is down.' . "\n"; } return $result; }
/** * 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(); }