* * The syntax includes the PHP file per include an puts the result into * the wiki page. * * @license GNU_GPL_v2 * @author Darren Hemphill */ if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'syntax.php'); require_once('ldapsearch.php'); class syntax_plugin_ldapsearch extends DokuWiki_Syntax_Plugin { var $ldapsearch_conf; function getInfo(){ return array( 'author' => 'Baseline IT', 'email' => 'info@baseline-remove-this-it.co.za', 'date' => '2009-04-11', 'name' => 'LDAP Search', 'desc' => 'Allows you to do an inline LDAP search using LDAP URL syntax or configured searches', 'url' => 'http://wiki.splitbrain.org/plugins:ldapsearch', ); } function getType(){ return 'substition'; } function getSort(){ return 1; } function connectTo($mode) { $searchTriggerPattern = '\[ *ldapsearch .*?\]'; $this->Lexer->addSpecialPattern($searchTriggerPattern ,$mode,'plugin_ldapsearch'); } function handle($match, $state, $pos, &$handler) { $align = -1; // default left alignment $alignSyntax = '\[( *)[^ ]+.*[^ ]+( *)\]'; $alignMatches = array(); if ( preg_match("/$alignSyntax/",$match,&$alignMatches) ) { if ( $alignMatches[2] && $alignMatches[1] ) { $align = 0; // float center } elseif ( $alignMatches[1] ) { $align = 1; // float right } elseif ( $alignMatches[2] ) { $align = 2; // float left } } $urlSyntax = 'ldap:\/\/([\w\.-_]+):([0-9]+)\/([^\?]*)\?([^\?]*)\?(base|one|sub)\?([^\]]+)'; $paramSyntax = ' (\w+)=([^\s\]]+)'; // build the ldapsearch_conf here only once if(!$this->ldapsearch_conf) { $this->build_ldapsearch_conf(); } switch ($state) { case DOKU_LEXER_SPECIAL : { $matches = array(); if(preg_match("/$urlSyntax/",$match,&$matches)) { $ldapDetails = array( 'hostname' => $matches[1], 'port' => $matches[2], 'basedn' => $matches[3], 'attributes' => explode(",",$matches[4]), 'scope' => $matches[5], 'filter' => $matches[6], ); $data = array( 'result' => null, 'ldapDetails' => $ldapDetails, 'align' => $align ); return array($state, $data); } elseif(preg_match_all("/$paramSyntax/",$match,&$matches, PREG_SET_ORDER)) { $ldapDetails = array(); foreach($matches as $pair) { $ldapDetails[$pair[1]] = $pair[2]; } // return null if no name specified if(!$ldapDetails['search']) { return null; } if($this->getConf('allow_overrides')) { // allow overrides foreach (array('hostname','port','basedn','attributes','scope','binddn','bindpassword') as $key) { if(!$ldapDetails[$key]) { $ldapDetails[$key] = $this->ldapsearch_conf[$ldapDetails['search']][$key]; } } } // explode attributes $ldapDetails['attributes'] = explode(',',$ldapDetails['attributes']); // on its way $data = array( 'result' => null, 'ldapDetails' => $ldapDetails, 'align' => $align ); return array($state, $data); } else { return null; } } default: return array($state); } } function build_ldapsearch_conf() { $set_index = array(); foreach(explode('|',$this->getConf('name')) as $name) { $this->ldapsearch_conf[$name] = array(); array_push($set_index,$name); } foreach(array('hostname','port','basedn','attributes','scope','binddn','bindpassword') as $param) { $count = 0; foreach(explode('|',$this->getConf($param)) as $value) { $this->ldapsearch_conf[$set_index[$count]][$param] = $value; $count++; } } //print_r($this->ldapsearch_conf); } /* function ldapsearch_search($ldapDetails) { //print_r($ldapDetails); if(!$ldapDetails['port']) { $ldapDetails['port'] = 389; } if($ldap_handle = ldap_connect($ldapDetails['hostname'],$ldapDetails['port'])) { ldap_set_option($ldap_handle, LDAP_OPT_PROTOCOL_VERSION, 3) ; if(ldap_bind($ldap_handle,$ldapDetails['binddn'],$ldapDetails['bindpassword'])) { $value = ""; if($ldapDetails['scope'] == 'sub') { $results = ldap_search($ldap_handle,$ldapDetails['basedn'],$ldapDetails['filter'],$ldapDetails['attributes']); $info = ldap_get_entries($ldap_handle, $results); //$value = $info[0][strtolower($ldapDetails['attributes'][0])][0]; $value = $info[0]; } elseif($ldapDetails['scope'] == 'one') { $results = ldap_list($ldap_handle, $ldapDetails['basedn'], $ldapDetails['filter'],$ldapDetails['attributes']); $info = ldap_get_entries($ldap_handle, $results); //$value = $info[0][strtolower($ldapDetails['attributes'][0])][0]; $value = $info[0]; } elseif($ldapDetails['scope'] == 'base') { $results = ldap_read($ldap_handle, $ldapDetails['basedn'], $ldapDetails['filter'],$ldapDetails['attributes']); $info = ldap_get_entries($ldap_handle, $results); //$value = $info[strtolower($ldapDetails['attributes'][0])][0]; $value = $info; } else { $value = "Unknown scope ".$ldapDetails['scope']."\n"; } ldap_unbind($ldap_handle); return $value; } else { return "Failed to bind to LDAP on ".$ldapDetails['hostname'].":".$ldapDetails['port']."\n"; } } else { return "Failed to connect to LDAP on ".$ldapDetails['hostname'].":".$ldapDetails['port']."\n"; } } */ function render($mode, &$renderer, $indata) { if($mode == 'xhtml'){ list($state, $data) = $indata; $result = $data['result']; $ldapDetails = $data['ldapDetails']; $align = $this->alignHTML($data['align']); switch ($state) { case DOKU_LEXER_SPECIAL : { //error_log("render $match\n",3,"/tmp/mylog.txt"); $content = ldapsearch_search($ldapDetails); foreach ( $ldapDetails['attributes'] as $attribute ) { $att = strtolower($attribute); if ( $att == 'vcard' ) { // inject display name instead of vcard $content[$att][0] = $content['displayName'][0]; } $this->render_ldap_attribute($renderer, $ldapDetails, $att, $content, $align); } break; } } return true; } // unsupported $mode return false; } function render_ldap_attribute(&$renderer, $ldapDetails, $attribute, $content, $align) { $attcontent = $content[$attribute][0]; if ( $attribute == 'jpegphoto' ) { //$renderer->doc .= ''.$ldapDetails['filter'].''; $ldapDetails['attributes'] = array( $attribute ); $ldapDetails['binddn'] = ''; // disable login, as password would become world-readable in URL $ldapDetails['bindpassword'] = ''; $renderer->doc .= ''.$attribute.''; } elseif ( $attribute == 'vcard' ) { $ldapDetails['attributes'] = array( $attribute ); $ldapDetails['binddn'] = ''; // disable login, as password would become world-readable in URL $ldapDetails['bindpassword'] = ''; //$renderer->doc .= ''.$ldapDetails['filter'].''; $link = array(); $link['class'] = 'vcard'; $link['more'] = 'rel="nofollow"'; $link['title'] = $content['displayname'][0]." ($attribute)"; $link['name'] = $renderer->_xmlEntities($content['displayname'][0]); $link['url'] = DOKU_URL.'lib/plugins/ldapsearch/get.php?'.$this->data_to_query_string("$attribute.vcf",'text/x-vCard',$ldapDetails); $renderer->doc .= $renderer->_formatLink($link); } elseif ( preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>', $attcontent) ) { $renderer->doc .= $renderer->emaillink($attcontent, $attcontent); } elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i', $attcontent) ) { $renderer->doc .= $renderer->externallink($attcontent, $attcontent); } else { $renderer->doc .= $attcontent; } } function alignHTML($align) { switch ($align) { case 0 : { $alignHTML = 'center'; break; } case 1 : { $alignHTML = 'right'; break; } case 2 : { $alignHTML = 'left'; break; } default : { $alignHTML = ''; break; } } return $alignHTML; } function data_to_query_string($name,$mimetype,$data) { $data = serialize(array($name,$mimetype,$data)); $sum = md5($data.$this->getConf('secret')); return 'data='.urlencode(base64_encode($data)).'&sum='.urlencode($sum); } } ?>