initial import
[auerswald-compact-ldap.git] / ldap2csv.php
1 <?php
2 /**
3  * Fetches all entries from the LDAP server and outputs a CSV file
4  * compatible with a Auerswald COMpact 3000's address book upload.
5  *
6  * PHP version 5
7  *
8  * @category Tools
9  * @package  auerswald-compact-ldap
10  * @author   Christian Weiske <cweiske@cweiske.de>
11  * @license  http://www.gnu.org/licenses/agpl.html AGPL v3 or later
12  */
13 if (!is_file(__DIR__ . '/config.php')) {
14     echo "Copy config.php.dist to config.php and adjust it.\n";
15     exit(1);
16 }
17 require_once __DIR__ . '/config.php';
18 require_once 'Net/LDAP2.php';
19
20 $ldap = Net_LDAP2::connect($ldapcfg);
21 if (Net_LDAP2::isError($ldap)) {
22     die('Could not connect to LDAP-server: ' . $ldap->getMessage() . "\n");
23 }
24
25 $numberfields = array(
26     'companyPhone',
27     'facsimileTelephoneNumber',
28     'homeFacsimileTelephoneNumber',
29     'homePhone',
30     'mobile',
31     'otherPhone',
32     'pager',
33     'telephoneNumber',
34 );
35
36 $search = $ldap->search(
37     null, null, 
38     array(
39         'scope' => 'sub',
40         'attributes' => array_merge(
41             array('displayName', 'cn',),
42             $numberfields
43         )
44     )
45 );
46 if (Net_LDAP2::isError($search)) {
47     die('Error searching: ' . $search->getMessage() . "\n");
48 }
49
50 echo "Kurzwahl;Rufnummer;Name\n";
51 while ($entry = $search->shiftEntry()) {
52     $a = $entry->getValues();
53     if (isset($a['displayName'])) {
54         $name = $a['displayName'];
55     } else {
56         $name = $a['cn'];
57     }
58
59     foreach ($numberfields as $nfield) {
60         if (isset($a[$nfield])) {
61             echo sprintf(";%s;%s\n", $a[$nfield], $name);
62         }
63     }
64 }
65 ?>