summaryrefslogtreecommitdiff
path: root/plugins/kolab_files/lib/kolab_files_observer.php
blob: d1a1b4eafdcddcef23ac344733660f48e6afb937 (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
<?php

/**
 * Observer for HTTP_Request2 implementing saving response body into a file
 */
class kolab_files_observer implements SplObserver
{
    protected $file;
    protected $fp;

    public function set_file($file)
    {
        $this->file = $file;
    }

    public function update(SplSubject $subject)
    {
        $event = $subject->getLastEvent();

        switch ($event['name']) {
        case 'receivedHeaders':
            $target = $this->dir . DIRECTORY_SEPARATOR . $this->file;
            if (!($this->fp = @fopen($target, 'wb'))) {
                throw new Exception("Cannot open target file '{$target}'");
            }
            break;

        case 'receivedBodyPart':
        case 'receivedEncodedBodyPart':
            fwrite($this->fp, $event['data']);
            break;

        case 'receivedBody':
            fclose($this->fp);
            break;
        }
    }
}