![]() |
|---|
| [ Index ] |
Source Code Reference for V1.00 |
[Summary view] [Print] [Text view]
1 <?php /* $Id: contacts_ldap.php 157 2008-04-13 16:11:42Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/modules/system/contacts_ldap.php $ */ 2 if (!defined('W2P_BASE_DIR')) { 3 die('You should not access this file directly.'); 4 } 5 6 $AppUI->savePlace(); 7 8 $canEdit = !getDenyEdit($m); 9 $canRead = !getDenyRead($m); 10 if (!$canRead) { 11 $AppUI->redirect('m=public&a=access_denied'); 12 } 13 14 $sql_table = 'contacts'; 15 16 //Modify this mapping to match your LDAP->contact structure 17 //For instance, of you want the contact_phone2 field to be populated out of, say telephonenumber2 then you would just modify 18 // "physicaldeliveryofficename" => "contact_phone2", 19 // or 20 // "telephonenumber2" => "contact_phone2", 21 22 $sql_ldap_mapping = array('givenname' => 'first_name', 'sn' => 'last_name', 'title' => 'job', 'o' => 'company', 'ou' => 'department', 'personaltitle' => 'title', 'employeetype' => 'type', 'mail' => 'email', 'telephonenumber' => 'phone', 'homephone' => 'phone2', 'fax' => 'fax', 'mobile' => 'mobile', 'postaladdress' => 'address1', 'l' => 'city', 'st' => 'state', 'postalcode' => 'zip', 'c' => 'country', 'comment' => 'notes'); 23 24 $titleBlock = new CTitleBlock('Import Contacts from LDAP Directory', '', 'admin', ''); 25 $titleBlock->addCrumb('?m=system', 'system admin'); 26 $titleBlock->show(); 27 28 if (isset($_POST['server'])) { 29 $AppUI->setState('LDAPServer', $_POST['server']); 30 } 31 $server = $AppUI->getState('LDAPServer', ''); 32 33 if (isset($_POST['bind_name'])) { 34 $AppUI->setState('LDAPBindName', $_POST['bind_name']); 35 } 36 $bind_name = $AppUI->getState('LDAPBindName', ''); 37 38 $bind_password = w2PgetParam($_POST, 'bind_password', ''); 39 40 if (isset($_POST['port'])) { 41 $AppUI->setState('LDAPPort', $_POST['port']); 42 } 43 $port = $AppUI->getState('LDAPPort', '389'); 44 45 if (isset($_POST['dn'])) { 46 $AppUI->setState('LDAPDN', $_POST['dn']); 47 } 48 $dn = $AppUI->getState('LDAPDN', ''); 49 //$dn = 'OU=USA,O=MINEBEA'; 50 51 if (isset($_POST['filter'])) { 52 $AppUI->setState('LDAPFilter', $_POST['filter']); 53 } 54 $filter = $AppUI->getState('LDAPFilter', '(objectclass=Person)'); 55 //$filter = '(objectclass=dominoPerson)'; 56 57 $import = w2PgetParam($_POST, 'import'); 58 $test = w2PgetParam($_POST, 'test'); 59 60 $AppUI->setState('LDAPProto', w2PgetParam($_POST, 'ldap_proto')); 61 $proto = $AppUI->getState('LDAPProto', '3'); 62 63 ?> 64 <form method="post"> 65 <table border="0" cellpadding="2" cellspacing="1" width="100%" class="std"> 66 <tr> 67 <td align="right" nowrap="nowrap"><?php echo $AppUI->_('Server'); ?>:</td> 68 <td><input type="text" class="text" name="server" value="<?php echo $server; ?>" size="50" /></td> 69 </tr> 70 <tr> 71 <td align="right" nowrap="nowrap"><?php echo $AppUI->_('Port'); ?>:</td> 72 <td><input type="text" class="text" name="port" value="<?php echo $port; ?>" size="4" /></td> 73 </tr> 74 <tr> 75 <td align="right" nowrap="nowrap"><?php echo $AppUI->_('Protocol'); ?>:</td> 76 <td><?php 77 echo $AppUI->_('Version 2') . ' <input type="radio" name="ldap_proto" value="2"'; 78 if ($proto == '2') { 79 echo ' checked="checked"'; 80 } 81 echo ' /> ' . $AppUI->_('Version 3') . ' <input type="radio" name="ldap_proto" value="3"'; 82 if ($proto == '3') { 83 echo ' checked="checked"'; 84 } 85 echo ' />'; 86 ?> 87 </td> 88 </tr> 89 <tr> 90 <td align="right" nowrap="nowrap"><?php echo $AppUI->_('Bind Name'); ?>:</td> 91 <td><input type="text" class="text" name="bind_name" value="<?php echo $bind_name; ?>" size="50" /></td> 92 </tr> 93 <tr> 94 <td align="right" nowrap="nowrap"><?php echo $AppUI->_('Bind Password'); ?>:</td> 95 <td><input type="password" class="text" name="bind_password" value="<?php echo $bind_password; ?>" size="25" /></td> 96 </tr> 97 <tr> 98 <td align="right" nowrap="nowrap"><?php echo $AppUI->_('Base DN'); ?>:</td> 99 <td><input type="text" class="text" name="dn" value="<?php echo $dn; ?>" size="100" /></td> 100 </tr> 101 <tr> 102 <td align="right" nowrap="nowrap"><?php echo $AppUI->_('Filter'); ?>:</td> 103 <td><input type="text" class="text" name="filter" value="<?php echo $filter; ?>" size="100" /></td> 104 </tr> 105 <tr> 106 <td colspan="2" align="right"><input type="submit" name="test" value="<?php echo $AppUI->_('Test Connection and Query'); ?>" /><input type="submit" name="import" value="<?php echo $AppUI->_('Import Contacts'); ?>" /></td> 107 </tr> 108 <tr> 109 <td colspan="2"> 110 <pre> 111 <?php 112 $s = '<b>'; 113 if (isset($test)) { 114 $s .= $test; 115 } 116 if (isset($import)) { 117 $s .= $import; 118 } 119 $s .= '</b><hr />'; 120 if (isset($test) || isset($import)) { 121 if (function_exists('ldap_connect')) { 122 $ds = @ldap_connect($server, $port); 123 } else { 124 $s .= '<span style="color:red;font-weight:bold;">ldap_connect function is not installed.</span><br />'; 125 $ds = false; 126 } 127 128 129 if (!$ds) { 130 if (function_exists('ldap_error')) { 131 $s .= ldap_error($ds); 132 } else { 133 $s .= '<span style="color:red;font-weight:bold;">ldap_connect failed.</span><br />'; 134 } 135 } else { 136 $s .= 'ldap_connect succeeded.<br />'; 137 } 138 139 if (function_exists('ldap_set_option')) { 140 @ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $proto); 141 } else { 142 $s .= '<span style="color:red;font-weight:bold;">ldap_set_option function is not installed.</span><br />'; 143 } 144 145 if (!function_exists('ldap_bind') || !@ldap_bind($ds, $bind_name, $bind_password)) { 146 $s .= '<span style="color:red;font-weight:bold;">ldap_bind failed.</span><br />'; 147 if (function_exists('ldap_error')) { 148 $s .= ldap_error($ds); 149 } 150 } else { 151 $s .= 'ldap_bind successful.<br />'; 152 } 153 154 $return_types = array(); 155 foreach ($sql_ldap_mapping as $ldap => $sql) { 156 $return_types[] = $ldap; 157 } 158 159 $s .= 'basedn: ' . $dn . '<br />'; 160 $s .= 'expression: ' . $filter . '<br />'; 161 162 if (function_exists('ldap_search')) { 163 $sr = @ldap_search($ds, $dn, $filter, $return_types); 164 } else { 165 $sr = false; 166 } 167 168 if ($sr) { 169 $s .= 'Search completed Sucessfully.<br />'; 170 } else { 171 $s .= '<span style="color:red;font-weight:bold;">ldap_search failed.</span><br />'; 172 if (function_exists('ldap_error')) { 173 $s .= 'Search Error: [' . ldap_errno($ds) . '] ' . ldap_error($ds) . '<br />'; 174 } 175 } 176 177 $s .= '</pre>'; 178 179 // $s .= "Result Count:".(ldap_count_entries($ds,$sr)); 180 if (function_exists('ldap_get_entries')) { 181 $info = @ldap_get_entries($ds, $sr); 182 } else { 183 $s .= '<span style="color:red;font-weight:bold;">ldap_get_entries is not installed.</span><br />'; 184 $info = array(); 185 } 186 187 if (!$info['count']) { 188 $s .= 'No contacts were found.'; 189 } else { 190 $s .= 'Total Contacts Found:' . $info['count'] . '<hr />'; 191 $s .= '<table border="0" cellpadding="1" cellspacing="0" width="98%" class="std">'; 192 if (isset($test)) { 193 foreach ($sql_ldap_mapping as $ldap => $sql) { 194 $s .= '<th>' . $sql . '</th>'; 195 } 196 } else { 197 $q = new DBQuery; 198 $q->addTable($sql_table); 199 $q->addQuery('contact_id, contact_first_name, contact_last_name'); 200 $contacts = $q->loadList(); 201 $q->clear(); 202 203 foreach ($contacts as $contact) { 204 $contact_list[$contact['contact_first_name'] . ' ' . $contact['contact_last_name']] = $contact['contact_id']; 205 } 206 unset($contacts); 207 } 208 209 for ($i = 0, $i_cmp = $info['count']; $i < $i_cmp; $i++) { 210 $pairs = array(); 211 $s .= '<tr>'; 212 foreach ($sql_ldap_mapping as $ldap_name => $sql_name) { 213 unset($val); 214 if (isset($info[$i][$ldap_name][0])) { 215 $val = clean_value($info[$i][$ldap_name][0]); 216 } 217 if ($val && $ldap_name == 'postaladdress') { 218 $val = str_replace('$', "\r", $val); 219 } 220 if (isset($val)) { 221 //if an email address is not specified in Domino you get a crazy value for this field that looks like FOO/BAR%NAME@domain.com This'll filter those values out. 222 if (isset($test) && $ldap_name == 'mail' && substr_count($val, '%') > 0) { 223 $s .= '<td><span style="color:#880000;">' . $AppUI->_('bad email address') . '</span></td>'; 224 continue; 225 } 226 $pairs['contact_' . $sql_name] = $val; 227 if (isset($test)) { 228 $s .= '<td>' . $val . '</td>'; 229 } 230 } else { 231 if (isset($test)) { 232 $s .= '<td>-</td>'; 233 } 234 } 235 } 236 237 if (isset($import)) { 238 $pairs['contact_order_by'] = $pairs['contact_first_name'] . ' ' . $pairs['contact_last_name']; 239 //Check to see if this value already exists. 240 if (isset($contact_list[$pairs['contact_first_name'] . ' ' . $pairs['contact_last_name']])) { 241 //if it does, remove the old one. 242 $pairs['contact_id'] = $contact_list[$pairs['contact_first_name'] . ' ' . $pairs['contact_last_name']]; 243 244 //Try to find a matching company name in the system, if not them set contact_company to 0 245 $q = new DBQuery; 246 $q->addQuery('company_id'); 247 $q->addTable('companies'); 248 $q->addWhere('company_name LIKE \'' . w2Phtmlspecialchars(trim($pairs['contact_company'])) . '\''); 249 $company_id = $q->loadResult(); 250 $pairs['contact_company'] = $company_id ? $company_id : 0; 251 $q->clear(); 252 253 //Try to find a matching department name in the system, if not them set contact_department to 0 254 $q = new DBQuery; 255 $q->addQuery('dept_id'); 256 $q->addTable('departments'); 257 $q->addWhere('dept_name LIKE \'' . w2Phtmlspecialchars(trim($pairs['contact_department'])) . '\''); 258 $dept_id = $q->loadResult(); 259 $pairs['contact_department'] = $dept_id ? $dept_id : 0; 260 $q->clear(); 261 262 $q = new DBQuery; 263 $q->updateArray($sql_table, $pairs, 'contact_id'); 264 $q->clear(); 265 $s .= '<td><span style="color:#880000;">There is a duplicate record for ' . $pairs['contact_first_name'] . ' ' . $pairs['contact_last_name'] . ', the record has been updated.</span></td>'; 266 } else { 267 //If the contact has no name, go to the next 268 if (!trim($pairs['contact_first_name'] . ' ' . $pairs['contact_last_name'])) { 269 continue; 270 } 271 $s .= '<td>Adding ' . $pairs['contact_first_name'] . ' ' . $pairs['contact_last_name'] . '.</td>'; 272 273 //Try to find a matching company name in the system, if not them set contact_company to 0 274 $q = new DBQuery; 275 $q->addQuery('company_id'); 276 $q->addTable('companies'); 277 $q->addWhere('company_name LIKE \'' . w2Phtmlspecialchars(trim($pairs['contact_company'])) . '\''); 278 $company_id = $q->loadResult(); 279 $pairs['contact_company'] = $company_id ? $company_id : 0; 280 $q->clear(); 281 282 //Try to find a matching department name in the system, if not them set contact_department to 0 283 $q = new DBQuery; 284 $q->addQuery('dept_id'); 285 $q->addTable('departments'); 286 $q->addWhere('dept_name LIKE \'' . w2Phtmlspecialchars(trim($pairs['contact_department'])) . '\''); 287 $dept_id = $q->loadResult(); 288 $pairs['contact_department'] = $dept_id ? $dept_id : 0; 289 $q->clear(); 290 291 $q = new DBQuery; 292 $q->insertArray($sql_table, $pairs); 293 $q->clear(); 294 } 295 } 296 $s .= '</tr>'; 297 298 /* 299 for ($ii=0, $ii_cmp=$info[$i]['count']; $ii<$ii_cmp; $ii++){ 300 $data = $info[$i][$ii]; 301 for ($iii=0, $iii_cmp=$info[$i][$data]['count']; $iii<$iii_cmp; $iii++) { 302 echo $data.': '.$info[$i][$data][$iii]; 303 } 304 } 305 */ 306 } 307 $s .= '</table>'; 308 } 309 if (function_exists('ldap_close')) { 310 ldap_close($ds); 311 } else { 312 $s .= '<span style="color:red;font-weight:bold;">ldap_close is not installed.</span>'; 313 } 314 } 315 echo $s; 316 317 function clean_value($str) { 318 $bad_values = array("'"); 319 return str_replace($bad_values, '', $str); 320 } 321 ?> 322 </td> 323 </tr> 324 </table>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Jan 8 03:00:03 2009 | Cross-referenced by PHPXref 0.7 |