summaryrefslogtreecommitdiff
path: root/plugins/kolab_files/lib/kolab_files_observer.php
blob: 535fe7957a09641c32d9b16b178725ac052e64a9 (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
<?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':
            if (!$this->file || !($this->fp = @fopen($this->file, 'wb'))) {
                throw new Exception("Cannot open target file '{$this->file}'");
            }
            break;

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

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