aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2012-08-05 21:33:13 +0200
committerChristian Weiske <cweiske@cweiske.de>2012-08-05 21:33:13 +0200
commit153fb4f6a68fafd7598ed3739795815d12fc0472 (patch)
tree2c9db5cc539e8e561487340932a6aee05e242583 /src
parentebb891c12348e7f1e44161acb605b81b7e799595 (diff)
downloadauerswald-callnotifier-153fb4f6a68fafd7598ed3739795815d12fc0472.tar.gz
auerswald-callnotifier-153fb4f6a68fafd7598ed3739795815d12fc0472.zip
opengeodb and ldap detaillers work!
Diffstat (limited to 'src')
-rw-r--r--src/callnotifier/CLI.php19
-rw-r--r--src/callnotifier/CallMonitor/Detailler/LDAP.php43
-rw-r--r--src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php64
3 files changed, 88 insertions, 38 deletions
diff --git a/src/callnotifier/CLI.php b/src/callnotifier/CLI.php
index 8958f78..16c95b0 100644
--- a/src/callnotifier/CLI.php
+++ b/src/callnotifier/CLI.php
@@ -36,8 +36,23 @@ class CLI
);
$callMonitor = new CallMonitor($this->config, $log);
- $callMonitor->addDetailler(new CallMonitor_Detailler_LDAP());
- $callMonitor->addDetailler(new CallMonitor_Detailler_OpenGeoDb());
+ $callMonitor->addDetailler(
+ new CallMonitor_Detailler_LDAP(
+ array(
+ 'host' => 'ldap.home.cweiske.de',
+ 'basedn' => 'ou=adressbuch,dc=cweiske,dc=de',
+ 'binddn' => 'cn=readonly,ou=users,dc=cweiske,dc=de',
+ 'bindpw' => 'readonly'
+ )
+ )
+ );
+ $callMonitor->addDetailler(
+ new CallMonitor_Detailler_OpenGeoDb(
+ 'mysql:host=dojo;dbname=opengeodb',
+ 'opengeodb-read',
+ 'opengeodb'
+ )
+ );
$handler = new MessageHandler($this->config, $log, $callMonitor);
diff --git a/src/callnotifier/CallMonitor/Detailler/LDAP.php b/src/callnotifier/CallMonitor/Detailler/LDAP.php
index 833fd46..ffcc9e1 100644
--- a/src/callnotifier/CallMonitor/Detailler/LDAP.php
+++ b/src/callnotifier/CallMonitor/Detailler/LDAP.php
@@ -3,8 +3,14 @@ namespace callnotifier;
class CallMonitor_Detailler_LDAP implements CallMonitor_Detailler
{
- public function __construct()
+ public function __construct($ldapConfig)
{
+ $this->ldap = \Net_LDAP2::connect($ldapConfig);
+ if (\PEAR::isError($this->ldap)) {
+ throw new \Exception(
+ 'Could not connect to LDAP-server: ' . $this->ldap->getMessage()
+ );
+ }
}
public function loadCallDetails(CallMonitor_Call $call)
@@ -18,7 +24,40 @@ class CallMonitor_Detailler_LDAP implements CallMonitor_Detailler
protected function loadName($number)
{
- return 'foo';
+ $filter = \Net_LDAP2_Filter::combine(
+ 'or',
+ array(
+ \Net_LDAP2_Filter::create('companyPhone', 'equals', $number),
+ \Net_LDAP2_Filter::create('homePhone', 'equals', $number),
+ \Net_LDAP2_Filter::create('mobile', 'equals', $number),
+ \Net_LDAP2_Filter::create('otherPhone', 'equals', $number),
+ \Net_LDAP2_Filter::create('telephoneNumber', 'equals', $number),
+ )
+ );
+ $options = array(
+ 'scope' => 'sub',
+ 'attributes' => array('displayName', 'givenName', 'sn', 'cn')
+ );
+
+ $search = $this->ldap->search(null, $filter, $options);
+ if (\PEAR::isError($search)) {
+ throw new \Exception(
+ 'Error searching LDAP: ' . $search->getMessage()
+ );
+ }
+ if ($search->count() == 0) {
+ return null;
+ }
+
+ $arEntry = $search->shiftEntry()->getValues();
+ if (isset($arEntry['displayName'])) {
+ return $arEntry['displayName'];
+ } else if (isset($arEntry['sn']) && $arEntry['givenName']) {
+ return $arEntry['givenName'] . ' ' . $arEntry['sn'];
+ } else if (isset($arEntry['cn'])) {
+ return $arEntry['cn'];
+ }
+ return null;
}
}
diff --git a/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php b/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php
index a37ede0..2b48004 100644
--- a/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php
+++ b/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php
@@ -1,6 +1,17 @@
<?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;
@@ -25,12 +36,18 @@ class CallMonitor_Detailler_OpenGeoDb implements CallMonitor_Detailler
'0179' => 'O2',
);
- public function __construct()
+ /**
+ * 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->db = new \PDO(
- 'mysql:host=dojo;dbname=opengeodb',
- 'opengeodb-read',
- 'opengeodb',
+ $dsn, $username, $password,
array(
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
@@ -57,44 +74,23 @@ class CallMonitor_Detailler_OpenGeoDb implements CallMonitor_Detailler
}
return null;
}
- //area codes in germany can be 3 to 6 numbers
- //FIXME: what about international numbers?
- for ($n = 3; $n <= 6; $n++) {
- $areacode = substr($number, 0, $n);
- $name = $this->getNameForAreaCode($areacode);
- if ($name !== null) {
- return $name;
- }
- }
-
- return null;
- }
-
- protected function getNameForAreaCode($areacode)
- {
- $stm = $this->db->query(
- 'SELECT loc_id FROM geodb_textdata'
- . ' WHERE text_type = "500400000"'//area code
- . ' AND text_val = ' . $this->db->quote($areacode)
- );
- $res = $stm->fetch();
- if ($res === false) {
- //area code does not exist
- return null;
- }
- $locId = $res['loc_id'];
+ //FIXME: what about international numbers?
+ //area codes in germany can be 3 to 6 numbers
$stm = $this->db->query(
- 'SELECT text_val FROM geodb_textdata'
- . ' WHERE text_type = "500100000"'//name
- . ' AND loc_id = ' . $this->db->quote($locId)
+ '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'
);
$res = $stm->fetch();
if ($res === false) {
return null;
}
- return $res['text_val'];
+ return $res['name'];
}
}