/** * Set HTTP response header. * @param string $header HTTP response header * @param boolean $replace (optional) avoid setting HTTP response header if it would replace an * existing header. This differs from the PHP header() $replace param which adds a header * if it would otherwise replace and existing header. */ function setResponseHeader($header, $replace = true) { /* Use our PHP VM for testability */ global $gallery; $phpVm = $gallery->getPhpVm(); $responseHeaders =& MyOOS_Utilities::_getResponseHeaders(); /* Special case for HTTP status codes. See http://php.net/header */ $key = 'status'; if (strncasecmp($header, 'HTTP/', 5)) { $key = MyOOS_Utilities::strToLower(substr($header, 0, strpos($header, ':'))); } /* Avoid setting HTTP response header if it would replace an existing header */ if (!$replace && (!empty($responseHeaders[$key]) || $key == 'location' && !empty($responseHeaders['status']) && !preg_match('/^HTTP\\/[0-9]\\.[0-9] 3/', $responseHeaders['status']))) { return; } $phpVm->header($header); $responseHeaders[$key] = $header; /* * Special case for the Location: header. Set HTTP status code unless some 3xx status code * is already set. See http://php.net/header */ if ($key == 'location' && (empty($responseHeaders['status']) || !preg_match('/^HTTP\\/[0-9]\\.[0-9] 3/', $responseHeaders['status']))) { $responseHeaders['status'] = 'HTTP/1.0 302 Found'; } }