diff --git a/syntax/book.php b/syntax/book.php --- a/syntax/book.php +++ b/syntax/book.php @@ -1,247 +1,305 @@ */ // must be run within Dokuwiki if(!defined('DOKU_INC')) die(); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'syntax.php'); class syntax_plugin_davcard_book extends DokuWiki_Syntax_Plugin { protected $hlp = null; // Load the helper plugin public function syntax_plugin_davcard_book() { $this->hlp =& plugin_load('helper', 'davcard'); } /** * What kind of syntax are we? */ function getType(){ return 'substition'; } /** * What about paragraphs? */ function getPType(){ return 'normal'; } /** * Where to sort in? */ function getSort(){ return 165; } /** * Connect pattern to lexer */ function connectTo($mode) { $this->Lexer->addSpecialPattern('\{\{davcardbook>[^}]*\}\}',$mode,'plugin_davcard_book'); } /** * Handle the match */ function handle($match, $state, $pos, Doku_Handler $handler){ global $ID; $options = trim(substr($match,14,-2)); $options = explode(',', $options); $data = array('name' => $ID, 'description' => $this->getLang('created_by_davcard'), 'id' => array(), + 'filter' => array(), ); foreach($options as $option) { list($key, $val) = explode('=', $option); $key = strtolower(trim($key)); $val = trim($val); switch($key) { + case 'filter': + list($k, $v) = explode(':', strtolower($val), 2); + $data['filter'][$k] = $v; + break; case 'id': if(!in_array($val, $data['id'])) $data['id'][] = $val; break; default: $data[$key] = $val; } } // Handle the default case when the user didn't enter a different ID if(empty($data['id'])) { $data['id'] = array($ID); } // Only update the addressbook name/description if the ID matches the page ID. // Otherwise, the addressbook is included in another page and we don't want // to interfere with its data. if(in_array($ID, $data['id'])) { if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER'])) $username = $_SERVER['REMOTE_USER']; else $username = uniqid('davcard-'); $this->hlp->setAddressbookNameForPage($data['name'], $data['description'], $ID, $username); } $meta = p_get_metadata($ID, 'plugin_davcard'); if(is_null($meta)) $meta = array(); $meta['addressbooks'] = $data; // Add webdavclient information so that we can disable caching if need be foreach($data['id'] as $addrbkid) { if(strpos($addrbkid, 'webdav://') === 0) { $connectionId = str_replace('webdav://', '', $addrbkid); if(!is_array($meta['webdavclient'])) $meta['webdavclient'] = array(); if(!in_array($addrbkid, $meta['webdavclient'])) $meta['webdavclient'][] = $connectionId; } } p_set_metadata($ID, array('plugin_davcard' => $meta)); return $data; } /** * Create output */ function render($format, Doku_Renderer $R, $data) { global $ID; if($format !== 'xhtml') return false; - - $R->doc .= '
'.$this->getLang('add_new').'
'; - + if(in_array($ID, $data['id'])) + { + $R->doc .= '
'.$this->getLang('add_new').'
'; + } // FIXME: Add new is not yet permission checked and does not support // included address books! $R->doc .= '
'; $R->doc .= ''; $R->doc .= ''; foreach($data['id'] as $id) { $write = false; if(strpos($id, 'webdav://') === 0) { $wdc =& plugin_load('helper', 'webdavclient'); if(is_null($wdc)) { echo $this->getLang('no_wdc'); continue; } $connectionId = str_replace('webdav://', '', $id); $settings = $wdc->getConnection($connectionId); if($settings === false) { echo $this->getLang('settings_not_found'); continue; } if($settings['type'] !== 'contacts') { echo $this->getLang('wrong_type'); continue; } $entries = $wdc->getAddressbookEntries($connectionId); $write = $settings['write']; } else { $acl = auth_quickaclcheck($id); if($acl > AUTH_READ) { $write = true; } elseif($acl < AUTH_READ) { continue; } else { $write = false; } $addressbookid = $this->hlp->getAddressbookIdForPage($id); $entries = $this->hlp->getAddressbookEntries($addressbookid); } foreach($entries as $entry) { $contactdata = $this->hlp->parseVcard($entry['contactdata'], $entry['uri']); + if(!$this->contactFilterMatch($data['filter'], $contactdata)) + continue; $R->doc .= ''; } } $R->doc .= '
'.$this->getLang('name').''.$this->getLang('address').''.$this->getLang('phone').''.$this->getLang('email').'
'.$entry['formattedname'].''; if(count($contactdata['addr']) > 0) { $R->doc .= ''; foreach($contactdata['addr'] as $dat) { if(isset($dat['type'])) $type = $dat['type']; else $type = 'other'; $R->doc .= ''.$this->getLang('adr'.strtolower($type)).''; if($dat['address'][2] != '') { $R->doc .= ''.$dat['address'][2].'
'; } if($dat['address'][5] != '') { $R->doc .= ''.$dat['address'][5].' '; } if($dat['address'][3] != '') { $R->doc .= ''.$dat['address'][3].'
'; } if($dat['address'][6] != '') { $R->doc .= ''.$dat['address'][6].''; } } $R->doc .= '
'; } $R->doc .= '
'; if(count($contactdata['tel']) > 0) { $R->doc .= ''; foreach($contactdata['tel'] as $dat) { if(isset($dat['type'])) $type = $dat['type']; else $type = 'other'; $R->doc .= ''.$this->getLang('tel'.strtolower($type)).' '; $R->doc .= $dat['number'].'
'; } $R->doc .= '
'; } $R->doc .= '
'; if(count($contactdata['mail']) > 0) { foreach($contactdata['mail'] as $dat) { $R->doc .= '
'; } } $R->doc .= '
'; $R->doc .= '
'; } - + private function contactFilterMatch($filter, $contactdata) + { + if(empty($filter)) + return true; + + foreach($filter as $type => $params) + { + $params = '/'.$params.'/i'; + switch($type) + { + case 'name': + if(preg_match($params, $contactdata['formattedname']) !== 1) + return false; + break; + case 'mail': + $found = false; + foreach($contactdata['mail'] as $dat) + { + if(preg_match($params, $dat['mail']) === 1) + $found = true; + } + if(!$found) + return false; + break; + case 'address': + $found = false; + foreach($contactdata['addr'] as $dat) + { + foreach($dat['address'] as $da) + { + if(preg_match($params, $da) === 1) + $found = true; + } + } + if(!$found) + return false; + break; + case 'tel': + $found = false; + foreach($contactdata['tel'] as $dat) + { + if(preg_match($params, $dat['number']) === 1) + $found = true; + } + if(!$found) + return false; + break; + } + } + return true; + } } // vim:ts=4:sw=4:et:enc=utf-8: