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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
<?php
namespace callnotifier;
/**
* Fetch location name from OpenGeoDb.
* In case of mobile phone numbers, the provider is named.
*
* It uses a custom table "my_orte" that can be created with
* docs/opengeodb-create-my_orte.sql
*
* Sets "toLocation" or "fromLocation", depending on call type
*
* @link http://opengeodb.org/
*/
class CallMonitor_Detailler_OpenGeoDb implements CallMonitor_Detailler
{
protected $db;
protected $dsn;
protected $username;
protected $password;
protected static $mobile = array(
'0151' => 'Telekom',
'0152' => 'Vodafone D2',
'0157' => 'E-Plus',
'0159' => 'O2',
'0160' => 'Telekom',
'0162' => 'Vodafone D2',
'0163' => 'E-Plus',
'0164' => 'Cityruf (e*message)',
'0170' => 'Telekom',
'0171' => 'Telekom',
'0172' => 'Vodafone D2',
'0173' => 'Vodafone D2',
'0174' => 'Vodafone D2',
'0175' => 'Telekom',
'0176' => 'O2',
'0177' => 'E-Plus',
'0178' => 'E-Plus',
'0179' => 'O2',
);
/**
* Create new detailler object
*
* @param string $dsn PDO connection string, for example
* 'mysql:host=dojo;dbname=opengeodb'
* @param string $username Database username
* @param string $password Database password
*/
public function __construct($dsn, $username, $password)
{
$this->dsn = $dsn;
$this->username = $username;
$this->password = $password;
//check if the credentials are correct
$this->connect();
}
/**
* Connect to the SQL server.
* SQL servers close the connection automatically after some hours,
* and since calls often don't come in every minute, we will have
* disconnects in between.
* Thus, we will reconnect on every location load.
*
* @return void
*/
protected function connect()
{
$this->db = new \PDO(
$this->dsn, $this->username, $this->password,
array(
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_PERSISTENT => true
)
);
}
public function loadCallDetails(CallMonitor_Call $call)
{
if ($call->type == CallMonitor_Call::INCOMING) {
if (!isset($call->fromLocation) || $call->fromLocation === null) {
$call->fromLocation = $this->loadLocation($call->from);
}
} else {
if (!isset($call->toLocation) || $call->toLocation === null) {
$call->toLocation = $this->loadLocation($call->to);
}
}
}
protected function loadLocation($number)
{
if (substr($number, 0, 2) == '01') {
//special number
$prefix = substr($number, 0, 4);
if (isset(self::$mobile[$prefix])) {
return 'Handy: ' . self::$mobile[$prefix];
}
return null;
}
//FIXME: what about international numbers?
//area codes in germany can be 3 to 6 numbers
$this->connect();
$stm = $this->db->query(
'SELECT name FROM my_orte'
. ' WHERE vorwahl = ' . $this->db->quote(substr($number, 0, 3))
. ' OR vorwahl = ' . $this->db->quote(substr($number, 0, 4))
. ' OR vorwahl = ' . $this->db->quote(substr($number, 0, 5))
. ' OR vorwahl = ' . $this->db->quote(substr($number, 0, 6))
. ' ORDER BY einwohner DESC'
);
if ($stm === false) {
throw new \Exception(
implode(' - ', $this->db->errorInfo())
);
}
$res = $stm->fetch();
if ($res === false) {
return null;
}
return $res['name'];
}
}
?>
|