%"\'\-\/\)\(+]+/', $value, 2); // Stop on any invalid character return $okay[0]; } // Return an implode of all non-empty entries in an array // function implodeNotEmpty($join,$a) { foreach ($a as $key=>$value) { if (empty($value)) unset($a[$key]); } return implode($join,$a); } // Here's how this is set up: // // Fetch a "not found" page from the Drupal site, e.g. http://www.comfsm.fm/?q=node/0 // Replace the following: // Title // Info between

...

// Content between
...
// Dump out the result // // We need to pay attention to whether we have a query issued or not, which // determines whether a blank form or a form and records are displayed. We // just need to examine $_POST for a query. // // Returned content should be a new copy of the form (with defaults from // the current $_POST data) and any matching directory data (maximum rows?) // $ch=curl_init('http://www.comfsm.fm/?q=node/0'); // New CURL resource curl_setopt($ch, CURLOPT_HEADER, false); // Set other options curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); // Limit how long we'll wait for connections curl_setopt($ch, CURLOPT_TIMEOUT, 6000); // and for content curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the content as a string $content=curl_exec($ch); // Fetch content if (!$content) { throw new Exception('Unable to retrieve content'); } $status=curl_getinfo($ch,CURLINFO_HTTP_CODE); // What do we do if the transfer failed? // Fixed replacements in "template" document // $content=preg_replace('/(|<h1 class="title">)[^<]*(<\/title>|<\/h1>)/','${1}COM-FSM Staff and Faculty Directory${2}',$content,2); $department=Array( "Any"=>"Any department", "ACAD"=>"Academic Programs", "ADM"=>"Administrative Services", "AG"=>"Agriculture Division", "AHEC"=>"Area Health Education Center", "BO"=>"Business Office", "BOOK"=>"Bookstore", "BU"=>"Business Division", "CD"=>"Campus Director", "COUN"=>"Counseling Office", "CP"=>"Caribbean and Pacific Consortium Project", "CRE"=>"Cooperative Research and Extension", "DH"=>"Dining Hall", "DISP"=>"Dispensary", "ED"=>"Education Division", "EMSS"=>"Enrollment Management and Student Services", "EN"=>"Languages & Literature", "FAO"=>"Financial Aid Office", "FMI"=>"Fisheries and Maritime Institute", "GEAR"=>"Gaining Early Awareness and Readiness for UG Programs", "HR"=>"Human Resources Office", "HTM"=>"Hotel and Travel Management", "IA"=>"Instructional Affairs", "ICO"=>"Instructional Coordinator's Office", "IEQA"=>"Institutional Effectiveness and Quality Assurance", "IP"=>"Instructional Programs", "IRPO"=>"Institutional Research and Planning Office", "IT"=>"Information Technology", "LAW"=>"Trial Counselors", "LRC"=>"Learning Resource Center", "MITC"=>"Media and Instructional Technology Center", "MS"=>"Math and Science Division", "MTNC"=>"Maintenance Office", "NA"=>"Not Applicable", "NU"=>"Nursing Program", "OAR"=>"Office of Admissions and Records", "PBDC"=>"Pohnpei Business Development Center", "PC"=>"Peer Counseling", "PE"=>"Physical Education", "PH"=>"Public Health", "PRES"=>"President's Office", "RES"=>"Residential and Student Life", "RH"=>"Residence Halls", "SAMH"=>"Substance Abuse and Mental Health Services Administration", "SEC"=>"Security and Safety Office", "SP"=>"Sponsored Programs", "SPRT"=>"Sports and Recreation", "SS"=>"Social Sciences Division", "SSCO"=>"Student Services Coordinator's Office", "ST"=>"Student Services", "TSP"=>"Talent Search Program", "UB"=>"Upward Bound", "VOC"=>"Vocational Education", "VPAS"=>"Vice-President for Administrative Services' Office", "VPIA"=>"Vice-President of Instructional Affairs' Office"); $departmentSelect=''; $current=array_key_exists('departmentWant',$_POST)?$_POST['departmentWant']:'n/a'; foreach ($department as $key=>$value) { $departmentSelect.='<option value="'.$key.'"'. ($current==$key?' selected ':'').'>'.$value.'</option>'; } // Add entry for unknown department (happens during display of search result if missing from LDAP) $department['x']=''; $campus=Array( "Any"=>"Any campus", "C"=>"Chuuk State", "K"=>"Kosrae State", "N"=>"National Campus", "P"=>"Pohnpei State", "Y"=>"Yap State", "F"=>"Yap FMI"); $campusSelect=''; $current=array_key_exists('campusWant',$_POST)?$_POST['campusWant']:'n/a'; foreach ($campus as $key=>$value) { $campusSelect.='<option value="'.$key.'"'. ($current==$key?' selected ':'').'>'.$value.'</option>'; } $form='<form method="post" action="/directory.html" enctype="multipart/form-data"> <table border="0" width="645"> <tr> <td colspan="6"><p>Enter part of a name, title, or other information and press <b>Search Directory</b> to locate COM-FSM staff or faculty members</p></td> </tr> <tr> <td align="right">Name:</td> <td align="left"><input type="text" name="nameWant" value="'. sanePost('nameWant').'" size="17" /></td> <td align="right">Title:</td> <td align="left" colspan="3"><input type="text" name="titleWant" value="'. sanePost('titleWant').'" size="30" /></td> </tr> <tr> <td align="right">E-Mail:</td> <td align="left"><input type="text" name="emailWant" value="'. sanePost('emailWant').'" size="17" /></td> <td align="RIGHT">Division:</td> <td align="LEFT"> <select name="departmentWant" >'.$departmentSelect.'</select> </td> <td align="RIGHT">Campus:</td> <td align="LEFT"> <select name="campusWant" >'.$campusSelect.'</select> </td> </tr> <tr> <td> </td> <td colspan="5"><input type="submit" name="Search" value="Search Directory" /></td> </tr> </table> </form>'; // See if we have a form submission; perform search if we do, using // sanitized $_POST values if (sanePost('Search')=='Search Directory') { $okay=false; $li=ldap_connect('ldap://cdb.nas.palikir/'); if ($li) { ldap_set_option($li, LDAP_OPT_PROTOCOL_VERSION, 3); $okay=ldap_bind($li); } if (!$okay) { $form.='<p>Unable to perform search (LDAP not available)</p>'; } else { // Construct search filter from $_POST data // mail: gsegal@comfsm.fm // title: Director // telephoneNumber: (691) 320-2481 Ext.134 // physicalDeliveryOfficeName: Information Technology Office // departmentNumber: IEQA // l: N $filter=''; if (sanePost('nameWant')) $filter.='(cn=*'.sanePost('nameWant').'*)'; if (sanePost('titleWant')) $filter.='(title=*'.sanePost('titleWant').'*)'; if (sanePost('emailWant')) $filter.='(mail=*'.sanePost('emailWant').'*)'; if (sanePost('phoneWant')) $filter.='(telephoneNumber=*'.sanePost('phoneWant').'*)'; $w=sanePost('departmentWant'); if ($w && $w!='Any') $filter.='(departmentNumber='.$w.')'; $w=sanePost('campusWant'); if ($w && $w!='Any') $filter.='(l='.$w.')'; $result=Array(); if ($filter!='') { $filter='(&'.$filter.')'; $want=Array('sn', 'givenName', 'cn', 'mail', 'title', 'telephoneNumber', 'physicalDeliveryOfficeName', 'departmentNumber','l'); $r=@ldap_list($li,'ou=Directory,dc=comfsm,dc=fm',$filter,$want); if ($r) { // Query is okay; get each value and add to $ld $e=ldap_first_entry($li,$r); while ($e) { $a=ldap_get_attributes($li,$e); // Create empty values for non-required values $a['mail'][]=''; $a['title'][]=''; $a['departmentNumber'][]='x'; // Displays empty department $a['telephoneNumber'][]=''; $a['physicalDeliveryOfficeName'][]=''; $result[$a['sn'][0].' '.$a['givenName'][0]]='<tr><td valign="top">'.implodeNotEmpty('<br />',Array($a['cn'][0],$a['mail'][0])).'</td>'. '<td valign="top">'.implodeNotEmpty('<br />',Array(@$a['title'][0],@$department[$a['departmentNumber'][0]])).'</td>'. '<td valign="top">'.@$a['telephoneNumber'][0].'</td>'. '<td valign="top">'.implodeNotEmpty(', ',Array(@$a['physicalDeliveryOfficeName'][0], @$campus[$a['l'][0]])).'</td></tr>'; $e=ldap_next_entry($li,$e); } ldap_free_result($r); } if (count($result)==0) { $result[]='<tr><td colspan=4>No records matched your query</td></tr>'; } } if (count($result)) { sort($result); $form.='<p>Search Results:</p>'. '<table><thead style="background-color: grey; color: white;"><tr><td>Name / E-Mail address</td><td>Title / Division</td><td>Telephone</td><td>Location</td></tr></thead>'.implode("\n",$result).'</table>'; } } } // Append results to $form // $content=preg_replace('/(<div class="content-middle">)[^<]*(<\/div>)/',"\1$form\2",$content,1); echo $content; ?>