summaryrefslogtreecommitdiffstats
path: root/library/SSRS/Object/ReportOutput.php
blob: 1b603517981bb28d1a59fc0c97c64cdb416a341c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php

namespace SSRS\Object;

use SSRS\Report;
use SSRS\Report\CachedStreamResource;
use SSRS\Object\ExecutionInfo;

class ReportOutput extends ObjectAbstract {

    public function __construct($data = null, ExecutionInfo $executionInfo = null) {
        parent::__construct($data);
        $this->executionInfo = $executionInfo;
    }

    public function preCacheStreams(Report $report, $localCachePath, $format = 'HTML4.0') {
        $this->verifyCachePath($localCachePath);

        $rootPath = rtrim($localCachePath, '/');

        $streamIds = $this->getStreamIds();
        foreach ($streamIds as $streamId) {
            $path = $rootPath . '/' . $streamId;
            $stream = $report->renderStream($format, $streamId);

            $cachedResource = new CachedStreamResource($streamId, $path);
            $cachedResource->store($stream);
        }

        return $this;
    }

    public function getStreamIds() {
        $ids = is_array($this->StreamIds->string) ? $this->StreamIds->string : array($this->StreamIds->string);
        return array_filter($ids);
    }

    public function download($filename) {
        header("Cache-control: max-age=3600, must-revalidate");
        header("Pragma: public");
        header("Expires: -1");
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header("Content-Type: " . $this->MimeType);
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: " . strlen($this->Result));

        echo($this->Result);
        exit(0);
    }

    public function resultClean() {
        $output = (string) $this->Result;
        $clean = $this->convertReplacementRootUrls($output);

        return $clean;
    }

    protected function convertReplacementRootUrls($output) {
        $executionId = ($this->executionInfo) ? $this->executionInfo->getExecutionId() : null;

        return preg_replace_callback('#href="//php-ssrs//([^\?]+)([^"]+)"#', function($results) use($executionId) {
            return 'href="' . html_entity_decode(urldecode($results[2])) . '&executionId=' . $executionId . '"';
        }, $output);
    }

    public function __toString() {
        return $this->resultClean();
    }

    protected function verifyCachePath($path) {
        if (false === file_exists($path) || false === is_dir($path)) {
            throw new \RuntimeException('Stream cache path does not exist');
        }

        if (false === is_writable($path)) {
            throw new \RuntimeException('Stream cache path is not writeable');
        }

        return $this;
    }

}