add files
[ldaptomb.git] / ldapaddresses2mediatomb.php
1 <?php
2 /**
3 * Provides LDAP address entries on the UPnP server
4 *
5 * @author   Christian Weiske <cweiske@php.net>
6 * @license  LGPL http://www.gnu.org/copyleft/lesser.html
7 */
8 require_once 'Net/LDAP2.php';
9 require_once 'Services/MediaTomb.php';
10 require_once 'config.php';
11
12
13 $config = array (
14     'binddn'    => $GLOBALS['ldaptombConfig']['ldap']['binddn'],
15     'bindpw'    => $GLOBALS['ldaptombConfig']['ldap']['bindpw'],
16     'basedn'    => $GLOBALS['ldaptombConfig']['ldap']['basedn'],
17     'host'      => $GLOBALS['ldaptombConfig']['ldap']['host']
18 );
19
20 $mt = new Services_MediaTomb(
21     $GLOBALS['ldaptombConfig']['mediatomb']['user'],
22     $GLOBALS['ldaptombConfig']['mediatomb']['pass'],
23     $GLOBALS['ldaptombConfig']['mediatomb']['host'],
24     $GLOBALS['ldaptombConfig']['mediatomb']['port']
25 );
26
27
28 $arAttributes = array(
29     'cn'              => '',
30     'homePhone'       => 'Tel: ',
31     'mobile'          => 'Handy: ',
32     'telephoneNumber' => 'Arbeit: ',
33     'street'          => '',
34     'postalCode'      => '',
35     'l'               => '',
36 );
37
38 $ldap = Net_LDAP2::connect($config);
39 if (PEAR::isError($ldap)) {
40     die('Could not connect to LDAP-server: '.$ldap->getMessage());
41 }
42
43 $folderFilter = Net_LDAP2_Filter::create('objectclass', 'equals', 'organizationalUnit');
44 $addressFilter = Net_LDAP2_Filter::combine(
45     'and',
46     array(
47         Net_LDAP2_Filter::create('cn', 'any'),
48         Net_LDAP2_Filter::combine(
49             'or',
50             array(
51                 Net_LDAP2_Filter::create('telephoneNumber', 'any'),
52                 Net_LDAP2_Filter::create('mobile', 'any'),
53                 Net_LDAP2_Filter::create('homePhone', 'any'),
54             )
55         )
56     )
57 );
58
59 $search = $ldap->search(null, $folderFilter, array('scope' => 'one', 'attributes' => array('ou')));
60
61 $mtAddressDir = $mt->getContainerByPath(
62     $GLOBALS['ldaptombConfig']['mediatomb']['addressdir']
63 );
64 if ($mtAddressDir) {
65     //deletes subentries which is what we want - a clean tree
66     $mtAddressDir->delete();
67 }
68
69 foreach ($search as $ldapfolder) {
70     $strFolderName = $ldapfolder->getValue('ou');
71     echo $strFolderName . "\n";
72     $addrSearch = $ldap->search(
73         $ldapfolder->dn(),
74         $addressFilter,
75         array(
76             'scope' => 'one'
77         )
78     );
79     foreach ($addrSearch->sorted(array('cn'), SORT_ASC) as $addrEntry) {
80         $mtEntry = $mt->createContainerByPath(
81             $GLOBALS['ldaptombConfig']['mediatomb']['addressdir']
82             . '/' . $strFolderName
83             . '/' . $addrEntry->getValue('cn')
84         );
85         echo " " . $addrEntry->getValue('cn') . "\n";
86         foreach ($arAttributes as $strLdapKey => $strPrefix) {
87             $val = $addrEntry->getValue($strLdapKey);
88             if (PEAR::isError($val)) {
89                 continue;
90             }
91             $mtEntry->createExternalLink(
92                 $strPrefix . $val, 'http://example.org',
93                 $strPrefix . $val,
94                 $GLOBALS['ldaptombConfig']['mediatomb']['mimetype']
95             );
96             echo '  ' . $strPrefix . $val . "\n";
97         }
98     }
99 }
100 ?>