aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/Logger/CallFileTop.php
blob: fca02ab3f8ab7c0b5007e74011428a889e64813e (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace callnotifier;

/**
 * Logs finished calls into a file, latest on top.
 * The date is also show on a extra line, grouping the following calls.
 *
 * Suitable for Dreambox display with CurlyTx
 *
 * Example:
 *
 * 24.08.2012, Friday
 *   07:48 nach Herbert                        00:00:03
 *   08:13 von  02426140162                    00:02:21
 */
class Logger_CallFileTop extends Logger_CallBase
{
    protected $file;

    /**
     * Create a new file call logger.
     *
     * @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;

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

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

        $call = $arData['call'];
        if (!$this->hasValidType($call)) {
            return;
        }
        if (!$this->hasValidMsn($call)) {
            return;
        }

        list($logline, $date) = $this->createLogEntry($call);
        $arLines = file($this->file);
        if (isset($arLines[0]) && $arLines[0] == $date) {
            //same date as previous log entry
            $arLines = array_pad($arLines, -count($arLines) - 1, '');
        } else if (!isset($arLines[0])) {
            //empty file
            $arLines = array_pad($arLines, -count($arLines) - 2, '');
        } else {
            //new date
            $arLines = array_pad($arLines, -count($arLines) - 3, '');
            $arLines[2] = "\n";
        }
        $arLines[0] = $date;
        $arLines[1] = $logline;

        //keep 50 lines only
        array_splice($arLines, 50);

        file_put_contents($this->file, implode('', $arLines));
    }


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

        if ($this->callTypes == 'io') {
            $str .= $prefix;
            $str .= Functions::mb_str_pad($numstr, 20);
        } else {
            $str .= '  ' . Functions::mb_str_pad($numstr, 25);
        }

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

        setlocale(LC_TIME, 'de_DE.utf-8');
        return array($str . "\n", strftime("%x, %A\n", $call->start));
    }

}

?>