elFinderConnector::output PHP Method

output() protected method

Output json
Author: Dmitry (dio) Levashov
protected output ( array $data ) : void
$data array
return void
    protected function output(array $data)
    {
        if ($this->header) {
            self::sendHeader($this->header);
        }
        if (isset($data['pointer'])) {
            // send optional header
            if (!empty($data['header'])) {
                self::sendHeader($data['header']);
            }
            // clear output buffer
            while (ob_get_level() && ob_end_clean()) {
            }
            $toEnd = true;
            $fp = $data['pointer'];
            if (($this->reqMethod === 'GET' || $this->reqMethod === 'HEAD') && elFinder::isSeekableStream($fp) && array_search('Accept-Ranges: none', headers_list()) === false) {
                header('Accept-Ranges: bytes');
                $psize = null;
                if (!empty($_SERVER['HTTP_RANGE'])) {
                    $size = $data['info']['size'];
                    $start = 0;
                    $end = $size - 1;
                    if (preg_match('/bytes=(\\d*)-(\\d*)(,?)/i', $_SERVER['HTTP_RANGE'], $matches)) {
                        if (empty($matches[3])) {
                            if (empty($matches[1]) && $matches[1] !== '0') {
                                $start = $size - $matches[2];
                            } else {
                                $start = intval($matches[1]);
                                if (!empty($matches[2])) {
                                    $end = intval($matches[2]);
                                    if ($end >= $size) {
                                        $end = $size - 1;
                                    }
                                    $toEnd = $end == $size - 1;
                                }
                            }
                            $psize = $end - $start + 1;
                            header('HTTP/1.1 206 Partial Content');
                            header('Content-Length: ' . $psize);
                            header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
                            fseek($fp, $start);
                        }
                    }
                }
                if (is_null($psize)) {
                    elFinder::rewind($fp);
                }
            } else {
                header('Accept-Ranges: none');
                if (isset($data['info']) && !$data['info']['size']) {
                    if (function_exists('header_remove')) {
                        header_remove('Content-Length');
                    } else {
                        header('Content-Length:');
                    }
                }
            }
            // unlock session data for multiple access
            $this->elFinder->getSession()->close();
            // client disconnect should abort
            ignore_user_abort(false);
            if ($reqMethod !== 'HEAD') {
                if ($toEnd) {
                    fpassthru($fp);
                } else {
                    $out = fopen('php://output', 'wb');
                    stream_copy_to_stream($fp, $out, $psize);
                    fclose($out);
                }
            }
            if (!empty($data['volume'])) {
                $data['volume']->close($data['pointer'], $data['info']['hash']);
            }
            exit;
        } else {
            self::outputJson($data);
            exit(0);
        }
    }