Page MenuHomePhabricator

No OneTemporary

diff --git a/action/cache.php b/action/cache.php
--- a/action/cache.php
+++ b/action/cache.php
@@ -1,44 +1,44 @@
<?php
/**
* DokuWiki DAVCal PlugIn - Ajax component
*/
if(!defined('DOKU_INC')) die();
-class action_plugin_davcal_cache extends DokuWiki_Action_Plugin {
+class action_plugin_davcard_cache extends DokuWiki_Action_Plugin {
function __construct() {
}
function register(Doku_Event_Handler $controller) {
$controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handle_parser_cache_use');
}
function handle_parser_cache_use(Doku_Event $event, $param)
{
$cache = &$event->data;
if(!isset($cache->page)) return;
//purge only xhtml cache
if($cache->mode != "xhtml") return;
$meta = p_get_metadata($cache->page, 'plugin_davcard');
if($meta === null)
return;
// Force re-caching if the webdavclient has synced
if(isset($meta['webdavclient']))
{
$wdc =& plugin_load('helper', 'webdavclient');
if(is_null($wdc))
return;
foreach($meta['webdavclient'] as $connectionId)
{
$cache->depends['files'][] = $wdc->getLastSyncChangeFileForConnection($connectionId);
}
}
}
}
diff --git a/conf/default.php b/conf/default.php
--- a/conf/default.php
+++ b/conf/default.php
@@ -1,8 +1,8 @@
<?php
/**
* Default settings for the davcard plugin
*
* @author Andreas Boehler <dev@aboehler.at>
*/
-
+$conf['default_client_id'] = '';
diff --git a/conf/metadata.php b/conf/metadata.php
--- a/conf/metadata.php
+++ b/conf/metadata.php
@@ -1,7 +1,8 @@
<?php
/**
* Options for the davcard plugin
*
* @author Andreas Boehler <dev@aboehler.at>
*/
+$meta['default_client_id'] = array('string');
diff --git a/helper.php b/helper.php
--- a/helper.php
+++ b/helper.php
@@ -1,379 +1,411 @@
<?php
/**
* Helper Class for the DAVCard plugin
* This helper does the actual work.
*
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
class helper_plugin_davcard extends DokuWiki_Plugin {
protected $sqlite = null;
/**
* Constructor to load the configuration
*/
public function helper_plugin_davcard() {
$this->sqlite =& plugin_load('helper', 'sqlite');
global $conf;
if(!$this->sqlite)
{
if($conf['allowdebug'])
dbglog('This plugin requires the sqlite plugin. Please install it.');
msg('This plugin requires the sqlite plugin. Please install it.');
return;
}
if(!$this->sqlite->init('davcard', DOKU_PLUGIN.'davcard/db/'))
{
if($conf['allowdebug'])
dbglog('Error initialising the SQLite DB for DAVCard');
return;
}
}
-
- public function getContactByStructuredName($id, $firstname = '', $lastname = '')
+
+ private function getContactByDetails($id, $type, $params = array())
{
if(strpos($id, 'webdav://') === 0)
{
$wdc =& plugin_load('helper', 'webdavclient');
if(is_null($wdc))
return $this->getLang('no_wdc');
$connectionId = str_replace('webdav://', '', $id);
$settings = $wdc->getConnection($connectionId);
if($settings === false)
- return $this->getLang('settings_not_found');
+ return array('formattedname' => $this->getLang('settings_not_found'));
if($settings['type'] !== 'contacts')
- return $this->getLang('wrong_type');
+ return array('formattedname' => $this->getLang('wrong_type'));
$entries = $wdc->getAddressbookEntries($connectionId);
foreach($entries as $entry)
{
- $contactdata = explode(';', $entry['structuredname']);
- if(count($contactdata) != 5) // There MUST be five entries
- continue;
- if(($lastname != '') && ($contactdata[0] === $lastname) || $lastname === '')
+ switch($type)
{
- // last name matched or no last name given
- if(($firstname != '') && ($contactdata[1] === $firstname) || $firstname === '')
- {
- // first name matched too or no first name given
- $info = $this->parseVcard($entry['contactdata']);
- return $info;
- }
+ case 'structuredname':
+ $contactdata = explode(';', $entry['structuredname']);
+ if(count($contactdata) != 5) // There MUST be five entries
+ continue;
+ if(($params['lastname'] != '') &&
+ ($contactdata[0] === $params['lastname'])
+ || $params['lastname'] === '')
+ {
+ // last name matched or no last name given
+ if(($params['firstname'] != '') &&
+ ($contactdata[1] === $params['firstname'])
+ || $params['firstname'] === '')
+ {
+ // first name matched too or no first name given
+ $info = $this->parseVcard($entry['contactdata'], $entry['uri']);
+ return $info;
+ }
+ }
+ break;
+ case 'formattedname':
+ if(trim($entry['formattedname']) == $params['formattedname'])
+ {
+ $info = $this->parseVcard($entry['contactdata'], $entry['uri']);
+ return $info;
+ }
+ break;
+ case 'email':
+ $info = $this->parseVcard($entry['contactdata'], $entry['uri']);
+ foreach($info['mail'] as $key => $mail)
+ {
+ if($mail === $params['email'])
+ return $info;
+ }
+ break;
}
}
}
- return false;
+ return array('formattedname' => $this->getLang('contact_not_found'));
+ }
+
+ public function getContactByStructuredName($id, $firstname = '', $lastname = '')
+ {
+ return $this->getContactByDetails($id, 'structuredname', array('firstname' => $firstname, 'lastname' => $lastname));
+ }
+
+ public function getContactByEmail($id, $email)
+ {
+ // FIXME: Maybe it's a good idea to save the e-mail in the database as well!
+ return $this->getContactByDetails($id, 'email', array('email' => $email));
}
public function getContactByFormattedName($id, $name)
{
+ return $this->getContactByDetails($id, 'formattedname', array('formattedname' => $name));
+ }
+
+ public function getContactByUri($id, $uri)
+ {
if(strpos($id, 'webdav://') === 0)
{
$wdc =& plugin_load('helper', 'webdavclient');
if(is_null($wdc))
return $this->getLang('no_wdc');
$connectionId = str_replace('webdav://', '', $id);
$settings = $wdc->getConnection($connectionId);
if($settings === false)
- return $this->getLang('settings_not_found');
+ return array('formattedname' => $this->getLang('settings_not_found'));
if($settings['type'] !== 'contacts')
- return $this->getLang('wrong_type');
+ return array('formattedname' => $this->getLang('wrong_type'));
- $entries = $wdc->getAddressbookEntries($connectionId);
- foreach($entries as $entry)
- {
- if(trim($entry['formattedname']) == $name)
- {
- $info = $this->parseVcard($entry['contactdata']);
- return $info;
- }
- }
+ $row = $wdc->getgetAddressbookEntryByUri($connectionId, $uri);
+ if($row === false)
+ return array('formattedname' => $this->getLang('contact_not_found'));
+ $info = $this->parseVcard($row['contactdata'], $row['uri']);
+ return $info;
}
- return false;
- }
-
- public function getContactByUri($id)
- {
-
+ return array('formattedname' => $this->getLang('contact_not_found'));
}
public function setAddressbookNameForPage($name, $description, $id = null, $userid = null)
{
if(is_null($id))
{
global $ID;
$id = $ID;
}
if(is_null($userid))
{
if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
{
$userid = $_SERVER['REMOTE_USER'];
}
else
{
$userid = uniqid('davcard-');
}
}
$bookid = $this->getAddressbookIdForPage($id);
if($bookid === false)
return $this->createAddressbookForPage($name, $description, $id, $userid);
$query = "UPDATE addressbooks SET displayname = ?, description = ? WHERE id = ?";
$res = $this->sqlite->query($query, $name, $description, $bookid);
if($res !== false)
return true;
return false;
}
public function getAddressbookIdForPage($id = null)
{
if(is_null($id))
{
global $ID;
$id = $ID;
}
$query = "SELECT addressbookid FROM pagetoaddressbookmapping WHERE page = ?";
$res = $this->sqlite->query($query, $id);
$row = $this->sqlite->res2row($res);
if(isset($row['addressbookid']))
{
$calid = $row['addressbookid'];
return $calid;
}
return false;
}
public function createAddressbookForPage($name, $description, $id = null, $userid = null)
{
if(is_null($id))
{
global $ID;
$id = $ID;
}
if(is_null($userid))
{
if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
{
$userid = $_SERVER['REMOTE_USER'];
}
else
{
$userid = uniqid('davcard-');
}
}
$values = array('principals/'.$userid,
$name,
str_replace(array('/', ' ', ':'), '_', $id),
$description,
1);
$query = "INSERT INTO addressbooks (principaluri, displayname, uri, description, synctoken) ".
"VALUES (?, ?, ?, ?, ?)";
$res = $this->sqlite->query($query, $values);
if($res === false)
return false;
// Get the new addressbook ID
$query = "SELECT id FROM addressbooks WHERE principaluri = ? AND displayname = ? AND ".
"uri = ? AND description = ? AND synctoken = ?";
$res = $this->sqlite->query($query, $values);
$row = $this->sqlite->res2row($res);
// Update the pagetocalendarmapping table with the new calendar ID
if(isset($row['id']))
{
$query = "INSERT INTO pagetoaddressbookmapping (page, addressbookid) VALUES (?, ?)";
$res = $this->sqlite->query($query, $id, $row['id']);
return ($res !== false);
}
return false;
}
public function addContactEntryToAddressbookForPage($id, $user, $params)
{
require_once(DOKU_PLUGIN.'davcard/vendor/autoload.php');
$vcard = new \Sabre\VObject\Component\VCard();
$formattedname = $params['firstname'].' '.$params['lastname']; // FIXME: Make this configurable?
$structuredname = array($params['lastname'], $params['firstname'], '', '', '');
$vcard->FN = $formattedname;
if($params['cellphone'] != '')
{
$vcard->add('TEL', $params['cellphone'], array('type' => 'cell'));
}
if($params['phone'] != '')
{
$vcard->add('TEL', $params['phone'], array('type' => 'home'));
}
$vcard->N = $structuredname;
$vcard->add('ADR', array('', '', $params['street'], $params['city'], '', $params['zipcode'], $params['country']), array('type' => 'home'));
$contactdata = $vcard->serialize();
if(strpos($id, 'webdav://') === 0)
{
$wdc =& plugin_load('helper', 'webdavclient');
if(is_null($wdc))
return false;
$connectionId = str_replace('webdav://', '', $id);
return $wdc->addAddressbookEntry($connectionId, $contactdata);
}
else
{
$addressbookid = $this->getAddressbookIdForPage($id);
$uri = uniqid('dokuwiki-').'.vcf';
$now = new \DateTime();
$query = "INSERT INTO addressbookobjects (contactdata, uri, addressbookid, lastmodified, etag, size, formattedname, structuredname) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$res = $this->sqlite->query($query,
$contactdata,
$uri,
$addressbookid,
$now->getTimestamp(),
md5($contactdata),
strlen($contactdata),
$formattedname,
implode(';', $structuredname)
);
// If successfully, update the sync token database
if($res !== false)
{
$this->updateSyncTokenLog($addressbookid, $uri, 'added');
return true;
}
}
}
- private function parseVcard($card)
+ private function parseVcard($card, $uri)
{
require_once(DOKU_PLUGIN.'davcard/vendor/autoload.php');
$vObject = \Sabre\VObject\Reader::read($card);
$formattedname = '';
$structuredname = '';
$tel = array();
$addr = array();
$mail = array();
if(isset($vObject->FN))
$formattedname = (string)$vObject->FN;
if(isset($vObject->N))
$structuredname = join(';', $vObject->N->getParts());
if(isset($vObject->TEL))
{
foreach($vObject->TEL as $number)
{
if(isset($number['TYPE']))
$tel[(string)$number['TYPE']] = (string)$number;
else
$tel[] = (string)$number;
}
}
if(isset($vObject->ADR))
{
foreach($vObject->ADR as $adr)
{
if(isset($adr['TYPE']))
$addr[(string)$adr['TYPE']] = $adr->getParts();
else
$addr[] = $adr->getParts();
}
}
if(isset($vObject->EMAIL))
{
foreach($vObject->EMAIL as $email)
{
if(isset($email['TYPE']))
$mail[(string)$email['TYPE']] = (string)$email;
else
$mail[] = (string)$email;
}
}
return array(
'formattedname' => $formattedname,
'structuredname' => $structuredname,
'tel' => $tel,
'mail' => $mail,
- 'addr' => $addr
+ 'addr' => $addr,
+ 'uri' => $uri
);
}
public function getAddressbookSettings($addressbookid)
{
$query = "SELECT id, principaluri, displayname, uri, description, synctoken FROM addressbooks WHERE id= ? ";
$res = $this->sqlite->query($query, $addressbookid);
$row = $this->sqlite->res2row($res);
return $row;
}
public function getSyncTokenForAddressbook($addressbookid)
{
$row = $this->getAddressbookSettings($addressbookid);
if(isset($row['synctoken']))
return $row['synctoken'];
return false;
}
/**
* Helper function to convert the operation name to
* an operation code as stored in the database
*
* @param string $operationName The operation name
*
* @return mixed The operation code or false
*/
public function operationNameToOperation($operationName)
{
switch($operationName)
{
case 'added':
return 1;
break;
case 'modified':
return 2;
break;
case 'deleted':
return 3;
break;
}
return false;
}
private function updateSyncTokenLog($addressbookid, $uri, $operation)
{
$currentToken = $this->getSyncTokenForAddressbook($addressbookid);
$operationCode = $this->operationNameToOperation($operation);
if(($operationCode === false) || ($currentToken === false))
return false;
$values = array($uri,
$currentToken,
$addressbookid,
$operationCode
);
$query = "INSERT INTO addressbookchanges (uri, synctoken, addressbookid, operation) VALUES(?, ?, ?, ?)";
$res = $this->sqlite->query($query, $uri, $currentToken, $addressbookid, $operationCode);
if($res === false)
return false;
$currentToken++;
$query = "UPDATE addressbooks SET synctoken = ? WHERE id = ?";
$res = $this->sqlite->query($query, $currentToken, $addressbookid);
return ($res !== false);
}
}
diff --git a/lang/en/settings.php b/lang/en/settings.php
--- a/lang/en/settings.php
+++ b/lang/en/settings.php
@@ -1,6 +1,9 @@
<?php
/**
* English language file for DAVCard
*
* @author Andreas Böhler <dev@aboehler.at>
*/
+
+$lang['default_client_id'] = 'Default ID when specified as davcardclient, e.g. webdav://8';
+
diff --git a/syntax/card.php b/syntax/card.php
--- a/syntax/card.php
+++ b/syntax/card.php
@@ -1,173 +1,204 @@
<?php
/**
* DokuWiki Plugin DAVCard (Contact Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Böhler <dev@aboehler.at>
*/
// 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_card extends DokuWiki_Syntax_Plugin {
protected $hlp = null;
// Load the helper plugin
public function syntax_plugin_davcard_card() {
$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('\{\{davcard>[^}]*\}\}',$mode,'plugin_davcard_card');
+ $this->Lexer->addSpecialPattern('\{\{davcardclient>[^}]*\}\}',$mode,'plugin_davcard_card');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, Doku_Handler $handler){
global $ID;
- $options = trim(substr($match,10,-2));
- $options = explode(',', $options);
$data = array('name' => '',
'id' => '',
'firstname' => '',
'lastname' => '',
+ 'email' => '',
+ 'uri' => '',
);
+ if(strpos($match, '{{davcardclient') === 0)
+ {
+ $options = trim(substr($match,16,-2));
+ $defaultId = $this->getConf('default_client_id');
+ if(isset($defaultId) && ($defaultId != ''))
+ {
+ $data['id'] = '#3a87ad';
+ $lastid = $defaultId;
+ }
+ }
+ else
+ {
+ $options = trim(substr($match,10,-2));
+ }
+
+ $options = explode(',', $options);
+
foreach($options as $option)
{
list($key, $val) = explode('=', $option);
$key = strtolower(trim($key));
$val = trim($val);
switch($key)
{
default:
$data[$key] = $val;
}
}
+ // FIXME: This is nonsense
if($data['id'] === '')
{
if(($data['name'] === '') || (($data['firstname'] === '') && ($data['lastname'] === '')))
{
msg($this->getLang('id_name_not_set'), -1);
}
}
return $data;
}
/**
* Create output
*/
function render($format, Doku_Renderer $R, $data) {
if($format == 'metadata')
{
if(strpos($data['id'], 'webdav://') === 0)
{
$connectionId = str_replace('webdav://', '', $data['id']);
$R->meta['plugin_davcard']['webdavclient'][] = $connectionId;
return true;
}
}
if($format != 'xhtml')
return false;
$contactdata = array();
if(strpos($data['id'], 'webdav://') === 0)
{
if($data['name'] !== '')
{
$contactdata = $this->hlp->getContactByFormattedName($data['id'], $data['name']);
}
elseif(($data['firstname'] !== '') || ($data['lastname'] !== ''))
{
$contactdata = $this->hlp->getContactByStructuredName($data['id'], $data['firstname'], $data['lastname']);
}
+ elseif(($data['email'] !== ''))
+ {
+ $contactdata = $this->hlp->getContactByEmail($data['id'], $data['email']);
+ }
+ elseif(($data['uri'] !== ''))
+ {
+ $contactdata = $this->hlp->getContactByUri($data['id'], $data['uri']);
+ }
+ if($contactdata === false)
+ {
+ $contactdata['formattedname'] = $this->getLang('contact_not_found');
+ }
}
$R->doc .= '<a class="url fn plugin_davcard_url" href="#">'.$contactdata['formattedname'];
$R->doc .= '<span class="plugin_davcard_popup vcard">';
if(count($contactdata['addr']) > 0)
{
$R->doc .= '<span class="adr">';
foreach($contactdata['addr'] as $type => $addr)
{
$R->doc .= '<span class="type">'.$this->getLang('adr'.strtolower($type)).'</span>';
if($addr[2] != '')
{
$R->doc .= '<span class="street-address">'.$addr[2].'</span><br>';
}
if($addr[5] != '')
{
$R->doc .= '<span class="postal-code">'.$addr[5].' </span>';
}
if($addr[3] != '')
{
$R->doc .= '<span class="locality">'.$addr[3].'</span><br>';
}
if($addr[6] != '')
{
$R->doc .= '<span class="country-name">'.$addr[6].'</span>';
}
}
$R->doc .= '</span>';
}
if(count($contactdata['tel']) > 0)
{
$R->doc .= '<span class="tel">';
foreach($contactdata['tel'] as $type => $number)
{
$R->doc .= '<span class="type">'.$this->getLang('tel'.strtolower($type)).' </span>';
$R->doc .= $number.'<br>';
}
$R->doc .= '</span>';
}
if(count($contactdata['mail']) > 0)
{
$R->doc .= '<span class="email_outer"><span class="email_type">EMail</span>';
foreach($contactdata['mail'] as $type => $mail)
{
$R->doc .= '<span class="email">'.$mail.'</span><br>';
}
$R->doc .= '</span>';
}
$R->doc .= '</span>';
$R->doc .= '</a>';
}
}
// vim:ts=4:sw=4:et:enc=utf-8:

File Metadata

Mime Type
text/x-diff
Expires
Thu, Dec 5, 5:50 AM (9 h, 15 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
533924
Default Alt Text
(23 KB)

Event Timeline