LDAP source driver
authorChristian Weiske <cweiske@cweiske.de>
Thu, 13 Feb 2014 19:36:26 +0000 (20:36 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 13 Feb 2014 19:36:26 +0000 (20:36 +0100)
README.rst
data/bdrem.config.php.dist
src/bdrem/Source/Ldap.php [new file with mode: 0644]

index 72f1074ec035b47a8b493caa4fbd7d6b89bd277a..2407b97549bfd366e9ffb37f5a711db046a83a84 100644 (file)
@@ -1,12 +1,27 @@
 *********************************
 bdrem - Birthday reminder by mail
 *********************************
 *********************************
 bdrem - Birthday reminder by mail
 *********************************
-Birthday reminder that sends out mails (Text and HTML).
+Birthday reminder that sends out e-mails.
 
 
-It can also generate tables on your console/shell output and
-normal HTML pages.
+It can also generate ASCII tables on your console/shell and normal HTML pages.
 
 
 
 
+========
+Features
+========
+
+Data sources
+============
+- Any SQL database
+- An LDAP server
+- Birthday reminder files (``.bdf``)
+
+Output formats
+==============
+- ASCII table
+- HTML
+- Email (text + HTML parts)
+
 
 =============
 Configuration
 
 =============
 Configuration
@@ -36,6 +51,19 @@ Use ``dblib`` in the DSN::
     dblib:host=192.168.1.1;dbname=Databasename
 
 
     dblib:host=192.168.1.1;dbname=Databasename
 
 
+============
+Dependencies
+============
+- PHP 5.3 or higher
+- PDO
+- PEAR packages:
+  - Console_CommandLine
+  - Mail
+  - Mail_mime
+  - Console_Table
+  - Net_LDAP2
+
+
 =======
 License
 =======
 =======
 License
 =======
index 076533f2292e52a11881fee1fd513b4efe3a6d5b..6d259e67c9aa8561cc1bc81370cf545b28778caa 100644 (file)
@@ -23,6 +23,18 @@ $source = array(
     )
 );
 
     )
 );
 
+//Source: LDAP
+$source = array(
+    'Ldap',
+    array(
+        'host'   => 'ldap.example.org',
+        'basedn' => 'ou=adressbuch,dc=example,dc=org',
+        'binddn' => 'cn=FIXME,ou=users,dc=example,dc=org',
+        'bindpw' => 'FIXME'
+    )
+);
+
+
 $daysPrev = 3;
 $daysNext = 14;
 $locale = 'de_DE.UTF-8';
 $daysPrev = 3;
 $daysNext = 14;
 $locale = 'de_DE.UTF-8';
diff --git a/src/bdrem/Source/Ldap.php b/src/bdrem/Source/Ldap.php
new file mode 100644 (file)
index 0000000..802d709
--- /dev/null
@@ -0,0 +1,118 @@
+<?php
+namespace bdrem;
+
+/**
+ * Fetch data from an LDAP server.
+ * Works fine with evolutionPerson schema.
+ */
+class Source_Ldap
+{
+    protected $config;
+
+    /**
+     * Create new ldap source
+     *
+     * @param array $config Array of Net_LDAP2 configuration parameters.
+     *                      Some of those you might want to use:
+     *                      - host   - LDAP server host name
+     *                      - basedn - root DN that gets searched
+     *                      - binddn - Username to authenticate with
+     *                      - bindpw - Password for username
+     */
+    public function __construct($config)
+    {
+        $this->config = $config;
+    }
+
+    /**
+     * @param string $strDate Date the events shall be found for, YYYY-MM-DD
+     */
+    public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
+    {
+        //Net_LDAP2 is not E_STRICT compatible
+        error_reporting(error_reporting() & ~E_STRICT);
+
+        $ldap = \Net_LDAP2::connect($this->config);
+        if (\PEAR::isError($ldap)) {
+            throw new \Exception(
+                'Could not connect to LDAP-server: ' . $ldap->getMessage()
+            );
+        }
+
+        $dateAttributes = array(
+            'birthDate'   => 'Birthday',
+            'anniversary' => 'Anniversary',
+        );
+
+        $arDays   = $this->getDates($strDate, $nDaysPrevious, $nDaysNext);
+        $arEvents = array();
+
+        foreach ($dateAttributes as $dateAttribute => $attributeTitle) {
+            $filters = array();
+            foreach ($arDays as $day) {
+                $filters[] = \Net_LDAP2_Filter::create($dateAttribute, 'ends', $day);
+            }
+
+            $filter  = \Net_LDAP2_Filter::combine('or', $filters);
+            $options = array(
+                'scope'      => 'sub',
+                'attributes' => array(
+                    'displayName', 'givenName', 'sn', 'cn', $dateAttribute
+                )
+            );
+
+            $search = $ldap->search(null, $filter, $options);
+            if (!$search instanceof \Net_LDAP2_Search) {
+                throw new \Exception(
+                    'Error searching LDAP: ' . $search->getMessage()
+                );
+            } else if ($search->count() == 0) {
+                continue;
+            }
+
+            while ($entry = $search->shiftEntry()) {
+                $event = new Event(
+                    $this->getNameFromEntry($entry),
+                    $attributeTitle,
+                    $entry->getValue($dateAttribute, 'single')
+                );
+                if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
+                    $arEvents[] = $event;
+                }
+            }
+        }
+
+        return $arEvents;
+    }
+
+    protected function getNameFromEntry(\Net_LDAP2_Entry $entry)
+    {
+        $arEntry = $entry->getValues();
+        if (isset($arEntry['displayName'])) {
+            return $arEntry['displayName'];
+        } else if (isset($arEntry['sn']) && isset($arEntry['givenName'])) {
+            return $arEntry['givenName'] . ' ' . $arEntry['sn'];
+        } else if (isset($arEntry['cn'])) {
+            return $arEntry['cn'];
+        }
+        return null;
+    }
+
+    /**
+     * @return array Values like "-01-24" ("-$month-$day")
+     */
+    protected function getDates($strDate, $nDaysPrevious, $nDaysNext)
+    {
+        $ts = strtotime($strDate) - 86400 * $nDaysPrevious;
+        $numDays = $nDaysPrevious + $nDaysNext;
+
+        $arDays = array();
+        do {
+            $arDays[] = date('-m-d', $ts);
+            $ts += 86400;
+        } while (--$numDays >= 0);
+        return $arDays;
+    }
+
+}
+?>