aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/Logger/CallBase.php
blob: 2a1d3e8a22bc533c401be8143201b7144c53fd4d (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
116
<?php
namespace callnotifier;

abstract class Logger_CallBase implements Logger
{
    protected $callTypes;
    protected $msns;

    public $debug = false;

    /**
     * Create a new call logger.
     *
     * @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($callTypes = 'io', $msns = array())
    {
        $this->callTypes = $callTypes;
        $this->msns      = (array)$msns;
    }

    /**
     * Check if the call type (incoming or outgoing) shall be logged.
     *
     * @return boolean True if it should be logged, false if not
     */
    protected function hasValidType($call)
    {
        if ($call->type == CallMonitor_Call::INCOMING && $this->callTypes == 'o') {
            $this->debug('No valid call type (requested: o)');
            return false;
        }
        if ($call->type == CallMonitor_Call::OUTGOING && $this->callTypes == 'i') {
            $this->debug('No valid call type (requested: i)');
            return false;
        }

        return true;
    }

    /**
     * Check if the MSN shall be logged
     *
     * @return boolean True if it should be logged, false if not
     */
    protected function hasValidMsn($call)
    {
        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
            $this->debug('No valid MSN (requested: ' . $msn . ')');
            return false;
        }

        return true;
    }

    protected function addUnsetVars($call)
    {
        static $expectedVars = array(
            'toName', 'fromName', 'toLocation', 'fromLocation'
        );
        foreach ($expectedVars as $varName) {
            if (!isset($call->$varName)) {
                $call->$varName = null;
            }
        }
    }


    protected function getNumberString($call, $type)
    {
        $varNumber   = $type;
        $varName     = $type . 'Name';
        $varLocation = $type . 'Location';

        if ($call->$varName !== null) {
            return $call->$varName;
        }

        $str = $this->getNumber($call->$varNumber);
        if ($call->$varLocation !== null) {
            $str .= ' aus ' . $call->$varLocation;
        }
        return $str;
    }

    protected function getNumber($number)
    {
        if ($number == '') {
            $number = '*anonym*';
        }
        return str_pad($number, 12, ' ', STR_PAD_RIGHT);
    }

    protected function debug($msg)
    {
        if (!$this->debug) {
            return;
        }
        echo $msg . "\n";
    }
}

?>