add mobile phone number detection
[auerswald-callnotifier.git] / src / callnotifier / CallMonitor / Detailler / OpenGeoDb.php
1 <?php
2 namespace callnotifier;
3
4 class CallMonitor_Detailler_OpenGeoDb implements CallMonitor_Detailler
5 {
6     protected $db;
7     protected static $mobile = array(
8         '0151' => 'Telekom',
9         '0152' => 'Vodafone D2',
10         '0157' => 'E-Plus',
11         '0159' => 'O2',
12         '0160' => 'Telekom',
13         '0162' => 'Vodafone D2',
14         '0163' => 'E-Plus',
15         '0164' => 'Cityruf (e*message)',
16         '0170' => 'Telekom',
17         '0171' => 'Telekom',
18         '0172' => 'Vodafone D2',
19         '0173' => 'Vodafone D2',
20         '0174' => 'Vodafone D2',
21         '0175' => 'Telekom',
22         '0176' => 'O2',
23         '0177' => 'E-Plus',
24         '0178' => 'E-Plus',
25         '0179' => 'O2',
26     );
27
28     public function __construct()
29     {
30         $this->db = new \PDO(
31             'mysql:host=dojo;dbname=opengeodb',
32             'opengeodb-read',
33             'opengeodb',
34             array(
35                 \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
36                 \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
37             )
38         );
39     }
40
41     public function loadCallDetails(CallMonitor_Call $call)
42     {
43         if ($call->type == CallMonitor_Call::INCOMING) {
44             $call->fromLocation = $this->loadLocation($call->from);
45         } else {
46             $call->toLocation = $this->loadLocation($call->to);
47         }
48     }
49
50     protected function loadLocation($number)
51     {
52         if (substr($number, 0, 2) == '01') {
53             //special number
54             $prefix = substr($number, 0, 4);
55             if (isset(self::$mobile[$prefix])) {
56                 return 'Handy: ' . self::$mobile[$prefix];
57             }
58             return null;
59         }
60         //area codes in germany can be 3 to 6 numbers
61         //FIXME: what about international numbers?
62         for ($n = 3; $n <= 6; $n++) {
63             $areacode = substr($number, 0, $n);
64             $name = $this->getNameForAreaCode($areacode);
65             if ($name !== null) {
66                 return $name;
67             }
68         }
69
70         return null;
71     }
72
73     protected function getNameForAreaCode($areacode)
74     {
75         $stm = $this->db->query(
76             'SELECT loc_id FROM geodb_textdata'
77             . ' WHERE text_type = "500400000"'//area code
78             . ' AND text_val = ' . $this->db->quote($areacode)
79         );
80         $res = $stm->fetch();
81         if ($res === false) {
82             //area code does not exist
83             return null;
84         }
85
86         $locId = $res['loc_id'];
87         $stm = $this->db->query(
88             'SELECT text_val FROM geodb_textdata'
89             . ' WHERE text_type = "500100000"'//name
90             . ' AND loc_id = ' . $this->db->quote($locId)
91         );
92         $res = $stm->fetch();
93         if ($res === false) {
94             return null;
95         }
96
97         return $res['text_val'];
98     }
99
100 }
101
102 ?>