dir = $dir; $this->url = strtolower(preg_replace('/\/.*$/', '', $_SERVER['SERVER_PROTOCOL'])); if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') $this->url .= 's'; $this->url .= '://' . $_SERVER['SERVER_NAME']; if ($_SERVER['SERVER_PORT'] != 80) $this->url .= ':' . $_SERVER['SERVER_PORT']; $this->url .= $_SERVER['REQUEST_URI']; $this->params = array(); global $_GET; foreach ($_GET as $name => $value) $this->params[$name] = $value; global $_POST; foreach ($_POST as $name => $value) $this->params[$name] = $value; /* Configure */ $config = array(); /* ... defaults */ $config['host'] = IMuSession::getDefaultHost(); $config['port'] = IMuSession::getDefaultPort(); /* ... service-specific */ $this->loadConfig($config); $this->config = $config; if (array_key_exists('trace-file', $this->config)) IMuTrace::setFile($this->config['trace-file']); if (array_key_exists('trace-level', $this->config)) IMuTrace::setLevel($this->config['trace-level']); } /** * @details Remove and return the value for the HTTP parameter specified by * $name from #$params or just return $default if the parameter does not * exists. * * @param string $name * The name of the parameter to remove and return. * @param string $default * The value to return if no value exists for $name. * * @retval mixed * The value of the parameter specified by $name or $default. */ public function extractParam($name, $default = false) { if (! array_key_exists($name, $this->params)) return $default; $value = $this->params[$name]; unset($this->params[$name]); return $value; } /** * @details Return the value for the HTTP parameter specified by $name from * #$params or just return $default if the parameter does not exists. * * @param string $name * The name of the parameter to return. * @param string $default * The value to return if no value exists for $name. * * @retval mixed * The value of the parameter specified by $name or $default. */ public function getParam($name, $default = false) { if (! array_key_exists($name, $this->params)) return $default; return $this->params[$name]; } /** * @details Check for the presence of the HTTP parameter specified by $name * in #$params. * * @param string $name * The name of the parameter to check. * * @retval boolean * True if $name exists, false otherwise. */ public function hasParam($name) { return array_key_exists($name, $this->params); } /** * @details Set the HTTP parameter specified by $name in #$params. * * @param string $name * The name of the parameter to set. * @param string $value * The value to set the parameter specified by $name. */ public function setParam($name, $value) { $this->params[$name] = $value; } /** * @details An empty method definition. The convention is to overridden this * method in a subclass and call it to process web service requests. * * @internal * @note This should probably be abstract or removed. I don't see any * value in this function. It could literally just be a convention to use * process() as the entry point to web service request processing. */ public function process() { /* Do nothing by default */ } /** * @details An IMuSession object. Only accessible after the connect() * method has been called. * @var IMuSession $session * @see connect() */ protected $session; /** * @details Construct an IMuSession object and invoke its @link * IMuSession#connect() connect()@endlink method using the `host` and * `port` parameters of the #$config variable. * * @see $session */ protected function connect() { $this->session = new IMuSession; $this->session->host = $this->config['host']; $this->session->port = $this->config['port']; $this->session->connect(); } /** * @details Disconnect from the IMuSession. * * @see $session */ protected function disconnect() { $this->session->disconnect(); } /** * @details Load configuration options from the files: * - /ws/common/config.php * - /ws/client/config.php * - /ws/local/config.php * * relative to #$dir. * * @param array &$config * @see __construct() */ protected function loadConfig(&$config) { @include $this->dir . '/ws/common/config.php'; @include $this->dir . '/ws/client/config.php'; @include $this->dir . '/ws/local/config.php'; } } /** * @details Write an error to the trace file and throw an IMuException. * @note Optionally, any number of additional parameters can be supplied when * calling this function. Usually these are messages or values that provide * more information about the error that is being raised. * * Usage examples: * @code{.php} * raise(400, 'UpdateNoFiles'); * raise(500, 'MultimediaTempFileOpen', $tempName); * @endcode * * @param int $code * Usually the HTTP error code that should be used in the HTTP response. * @param string $id * A string that identifies the exception. * * @throws IMuException */ function raise($code, $id) { $exception = new IMuException($id); $args = func_get_args(); array_shift($args); array_shift($args); $exception->setArgs($args); $exception->setCode($code); IMuTrace::write(2, 'raising exception %s', $exception); throw $exception; } ?>