diff --git a/httprl.module b/httprl.module index ad0e9a2..312abf7 100755 --- a/httprl.module +++ b/httprl.module @@ -648,3 +648,69 @@ function httprl_parse_data(&$result) { } } +/** + * Output text, close connection, continue processing in the background. + * + * @param $output + * string - Text to output to open connection. + * @param $wait + * bool - Wait 1 second? + * @param $content_type + * string - Content type header. + * @param $length + * int - Content length. + */ +function httprl_background_processing($output, $wait = TRUE, $content_type = "text/html; charset=utf-8", $length = 0) { + if (headers_sent()) { + return FALSE; + } + + // Calculate Content Length + if ($length == 0) { + $output .= "\n"; + $length = (httprl_strlen($output)-1); + } + // Prime php for background operations + $loop = 0; + while (ob_get_level() && $loop < 25) { + ob_end_clean(); + $loop++; + } + header("Connection: close"); + ignore_user_abort(); + + // Output headers & data + ob_start(); + header("Content-type: " . $content_type); + header("Expires: Sun, 19 Nov 1978 05:00:00 GMT"); + header("Cache-Control: no-cache"); + header("Cache-Control: must-revalidate"); + header("Content-Length: " . $length); + header("Connection: close"); + print($output); + ob_end_flush(); + flush(); + // wait for 1 second + if ($wait) { + sleep(1); + } + + // text returned and connection closed. + // Do background processing. Time taken after should not effect page load times. + return TRUE; +} + +/** + * Get the length of a string in bytes + * + * @param $string + * get string length + */ +function httprl_strlen($string) { + if (function_exists('mb_strlen')) { + return mb_strlen($string, '8bit'); + } + else { + return strlen($string); + } +}