aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/Logger/CallFile.php
blob: 850e840f0c072c160a8dc3a64cd744d53fad50e0 (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
84
85
86
87
88
89
90
<?php
namespace callnotifier;

class Logger_CallFile extends Logger_CallBase
{
    protected $file;
    protected $fileHdl;
    protected $callTypes;
    protected $msns;

    /**
     * Create a new file call logger. It logs finished calls into a file.
     *
     * @param string $file      Path to the file to log the calls in.
     * @param string $callTypes Which types of call to log:
     *                          - "i" - incoming calls only
     *                          - "o" - outgoing calls only
     *                          - "io" - both incoming and outgoing calls
     * @param array  $msns      Array of MSN (Multi Subscriber Number) that
     *                          calls to shall get logged.
     *                          If the array is empty, calls to all MSNs get
     *                          logged.
     */
    public function __construct(
        $file,
        $callTypes = 'io',
        $msns = array()
    ) {
        $this->file      = $file;
        $this->callTypes = $callTypes;
        $this->msns      = (array)$msns;

        $this->fileHdl = fopen($this->file, 'a');
        if (!$this->fileHdl) {
            throw new \Exception(
                'Cannot open call log file for writing: ' . $this->file
            );
        }
    }

    public function log($type, $arData)
    {
        if ($type != 'finishedCall') {
            return;
        }

        $call = $arData['call'];

        //check if call type matches
        if ($call->type == CallMonitor_Call::INCOMING && $this->callTypes == 'o') {
            return;
        }
        if ($call->type == CallMonitor_Call::OUTGOING && $this->callTypes == 'i') {
            return;
        }

        if ($call->type == CallMonitor_Call::INCOMING) {
            $msn = $call->to;
        } else {
            $msn = $call->from;
        }
        if (count($this->msns) > 0 && !in_array($msn, $this->msns)) {
            //msn shall not be logged
            return;
        }

        fwrite($this->fileHdl, $this->createLogEntry($call));
    }


    protected function createLogEntry(CallMonitor_Call $call)
    {
        $this->addUnsetVars($call);
        $str = date('Y-m-d H:i:s', $call->start);
        if ($call->type == CallMonitor_Call::INCOMING) {
            $str .= ' ' . $call->to
                . ' von  ' . $this->getNumberString($call, 'from');
        } else {
            $str .= ' ' . $call->from
                . ' nach ' . $this->getNumberString($call, 'to');
        }

        $str .= ', Dauer ' . date('H:i:s', $call->end - $call->start - 3600);

        return $str . "\n";
    }

}

?>