Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F1880249
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Size
39 KB
Subscribers
None
View Options
diff --git a/action/ajax.php b/action/ajax.php
--- a/action/ajax.php
+++ b/action/ajax.php
@@ -1,245 +1,246 @@
<?php
/**
* DokuWiki DAVCard PlugIn - Ajax component
*/
if(!defined('DOKU_INC')) die();
class action_plugin_davcard_ajax extends DokuWiki_Action_Plugin {
/**
* @var helper_plugin_davcard
*/
private $hlp = null;
function __construct() {
$this->hlp =& plugin_load('helper','davcard');
}
function register(Doku_Event_Handler $controller) {
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
}
function handle_ajax_call_unknown(&$event, $param) {
if($event->data != 'plugin_davcard') return;
$event->preventDefault();
$event->stopPropagation();
global $INPUT;
$action = trim($INPUT->post->str('action'));
$id = trim($INPUT->post->str('id'));
$page = trim($INPUT->post->str('page'));
$params = $INPUT->post->arr('params');
if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
$user = $_SERVER['REMOTE_USER'];
else
$user = null;
if(!checkSecurityToken())
{
echo "CSRF Attack.";
return;
}
$data = array();
$data['result'] = false;
$data['html'] = $this->getLang('unknown_error');
// Parse the requested action
switch($action)
{
// Add a new Contact
case 'newContact':
if($this->hlp->addContactEntryToAddressbookForPage($id, $user, $params) === true)
{
$data['result'] = true;
}
else
{
$data['result'] = false;
$data['html'] = $this->getLang('error_adding');
}
break;
// Retrieve contact details
case 'getContactDetails':
$contactdata = $this->hlp->getContactByUri($id, $params['uri']);
if($contactdata['result'] === true)
{
// When we support pictures for editing contacts,
// we need to use the following line:
// $contactdata['photo'] = base64_encode($contactdata['photo']);
// For now, we just save bandwidth :)
unset($contactdata['photo']);
$data['result'] = true;
$data['contactdata'] = $contactdata;
}
else
{
$data['result'] = false;
$data['html'] = sprintf($this->getLang('contact_not_found'), 'ID='.$id.' URI='.$params['uri']);
}
break;
// Edit a contact
case 'editContact':
if($this->hlp->editContactEntryToAddressbookForPage($id, $user, $params['uri'], $params) === true)
{
$data['result'] = true;
}
else
{
$data['result'] = false;
$data['html'] = $this->getLang('error_editing');
}
break;
// Delete a Contact
case 'deleteContact':
if($this->hlp->deleteContactEntryToAddressbookForPage($id, $user, $params['uri']) === true)
{
$data['result'] = true;
}
else
{
$data['result'] = false;
$data['html'] = $this->getLang('error_deleting');
}
break;
// Get AJAX popup
case 'getContactAjax':
$contactdata = $this->hlp->getContactByUri($id, $params['uri']);
$cardpattern = $this->getConf('popup_content');
if($contactdata['result'] === false)
{
echo hsc($contactdata['formattedname']);
+ return;
}
echo '<div class="plugin_davcard_popup_container">';
foreach($contactdata['photo'] as $data)
{
if(isset($data['type']))
$type = $data['type'];
else
$type = '';
echo '<div class="plugin_davcard_popup_image">';
$imgdata = base64_encode($data['photo']);
$pattern = '/^(?:[;\/?:@&=+$,]|(?:[^\W_]|[-_.!~*\()\[\] ])|(?:%[\da-fA-F]{2}))*$/';
// PNG images
if($type == 'png')
{
$imgdata = 'data:image/png;base64,'.$imgdata;
echo '<img src="'.hsc($imgdata).'" alt="contact image" />';
}
// JPEG images
elseif(($type == 'jpeg') || ($type == 'jpg'))
{
$imgdata = 'data:image/jpeg;base64,'.$imgdata;
echo '<img src="'.hsc($imgdata).'" alt="contact image" />';
}
// GIF images
elseif($type == 'gif')
{
$imgdata = 'data:image/gif;base64,'.$imgdata;
echo '<img src="'.hsc($imgdata).'" alt="contact image" />';
}
// URLs (no type given)
elseif(preg_match( $pattern, $string ) == 1)
{
echo '<img src="'.hsc($data['photo']).'" alt="contact image" />';
}
echo '</div>';
}
echo '<div class="plugin_davcard_popup_content">';
$contactname = explode(';', $contactdata['structuredname']);
if(count($contactname) > 1)
{
$cardpattern = str_replace('<DCLASTNAME>', $contactname[0], $cardpattern);
$cardpattern = str_replace('<DCFIRSTNAME>', $contactname[1], $cardpattern);
}
if(count($contactdata['addr']) > 0)
{
foreach($contactdata['addr'] as $data)
{
if(isset($data['type']) && ($data['type'] == 'work'))
$prefix = 'WORK';
else
$prefix = 'PRIVATE';
$cardpattern = str_replace('<DC'.$prefix.'STREET>', $data['address'][2], $cardpattern);
$cardpattern = str_replace('<DC'.$prefix.'CITY>', $data['address'][3], $cardpattern);
$cardpattern = str_replace('<DC'.$prefix.'ZIP>', $data['address'][5], $cardpattern);
$cardpattern = str_replace('<DC'.$prefix.'COUNTRY>', $data['address'][6], $cardpattern);
}
}
if(count($contactdata['tel']) > 0)
{
$telArr = array();
foreach($contactdata['tel'] as $data)
{
if(isset($data['type']))
$type = $data['type'];
else
$type = 'other';
$type = $data['type'];
$telArr[] = $this->getLang('tel'.$type).': '.$data['number'];
}
$telStr = implode(' \\\\ ', $telArr);
$cardpattern = str_replace('<DCPHONE>', $telStr, $cardpattern);
}
if(count($contactdata['mail']) > 0)
{
$mailArr = array();
foreach($contactdata['mail'] as $data)
{
$mailArr[] = '[['.$data['mail'].']]';
}
$mailStr = implode(' \\\\ ', $mailArr);
$cardpattern = str_replace('<DCEMAIL>', $mailStr, $cardpattern);
}
if($contactdata['birthday'] != '')
{
$date = DateTime::createFromFormat('Ymd', $contactdata['birthday']);
$dateStr = $date->format($this->getConf('date_format'));
$cardpattern = str_replace('<DCBIRTHDAY>', $dateStr, $cardpattern);
}
if($contactdata['note'] != '')
{
$notestr = str_replace('\n', ' \\ ', $contactdata['note']);
$cardpattern = str_replace('<DCNOTE>', $notestr, $cardpattern);
}
if($contactdata['title'] != '')
{
$cardpattern = str_replace('<DCTITLE>', $contactdata['title'], $cardpattern);
}
if($contactdata['url'] != '')
{
$url = $contactdata['url'];
if(strpos($url, '://') === false)
$url = 'http://'.$url;
$url = '[['.$url.']]';
$cardpattern = str_replace('<DCWEBSITE>', $url, $cardpattern);
}
$replace_match = '/^.*<DC.*>.*$(?:\r\n|\n)?/m';
$cardpattern = preg_replace($replace_match, '', $cardpattern);
echo $this->render_text($cardpattern);
echo '</div>';
echo '</div>';
return;
break;
}
// If we are still here, JSON output is requested
//json library of DokuWiki
require_once DOKU_INC . 'inc/JSON.php';
$json = new JSON();
//set content type
header('Content-Type: application/json');
echo $json->encode($data);
}
}
diff --git a/helper.php b/helper.php
--- a/helper.php
+++ b/helper.php
@@ -1,615 +1,614 @@
<?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;
}
}
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 array('formattedname' => $this->getLang('settings_not_found'));
+ return array('formattedname' => $this->getLang('settings_not_found'), 'result' => false);
if($settings['type'] !== 'contacts')
- return array('formattedname' => $this->getLang('wrong_type'));
+ return array('formattedname' => $this->getLang('wrong_type'), 'result' => false);
$entries = $wdc->getAddressbookEntries($connectionId);
}
else
{
$addressbookid = $this->getAddressbookIdForPage($id);
$entries = $this->getAddressbookEntries($addressbookid);
}
foreach($entries as $entry)
{
switch($type)
{
case 'structuredname':
$contactdata = explode(';', strtolower($entry['structuredname']));
if(count($contactdata) < 2) // We need at least first and last name
- return array('formattedname' => sprintf($this->getLang('contact_not_found'), $params['firstname']. ' '.$params['lastname']));
+ return array('formattedname' => sprintf($this->getLang('contact_not_found'), $params['firstname']. ' '.$params['lastname']), 'result' => false);
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(strtolower($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 $data)
{
- if($data['mail'] === strtolower($params['email']))
+ if(trim(strtolower($data['mail'])) === $params['email'])
return $info;
}
break;
}
}
return array('formattedname' => sprintf($this->getLang('contact_not_found'), $this->getLang('invalid_options')), 'result' => false);
}
public function getAddressbookEntries($id)
{
$query = "SELECT contactdata, uri, formattedname, structuredname FROM addressbookobjects WHERE addressbookid = ? ORDER BY formattedname ASC";
$res = $this->sqlite->query($query, $id);
return $this->sqlite->res2arr($res);
}
public function getContactByStructuredName($id, $firstname = '', $lastname = '')
{
return $this->getContactByDetails($id, 'structuredname',
array('firstname' => strtolower($firstname), 'lastname' => strtolower($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' => strtolower($email)));
}
public function getContactByFormattedName($id, $name)
{
return $this->getContactByDetails($id, 'formattedname', array('formattedname' => strtolower($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 array('formattedname' => $this->getLang('settings_not_found'), 'result' => false);
if($settings['type'] !== 'contacts')
return array('formattedname' => $this->getLang('wrong_type'), 'result' => false);
$row = $wdc->getAddressbookEntryByUri($connectionId, $uri);
}
else
{
$addressbookid = $this->getAddressbookIdForPage($id);
$row = $this->getAddressbookEntryByUri($addressbookid, $uri);
}
if($row === false)
return array('formattedname' => sprintf($this->getLang('contact_not_found'), 'ID='.$id.' URI='.$uri), 'result' => false);
$info = $this->parseVcard($row['contactdata'], $row['uri']);
$info['result'] = true;
return $info;
}
private function getAddressbookEntryByUri($id, $uri)
{
$query = "SELECT contactdata, addressbookid, etag, uri, formattedname, structuredname FROM addressbookobjects WHERE addressbookid = ? AND uri = ?";
$res = $this->sqlite->query($query, $id, $uri);
return $this->sqlite->res2row($res);
}
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 deleteContactEntryToAddressbookForPage($id, $user, $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 array('formattedname' => $this->getLang('settings_not_found'), 'result' => false);
if($settings['type'] !== 'contacts')
return array('formattedname' => $this->getLang('wrong_type'), 'result' => false);
return $wdc->deleteAddressbookEntry($connectionId, $uri);
}
$addressbookid = $this->getAddressbookIdForPage($id);
dbglog($addressbookid);
dbglog($uri);
$query = "DELETE FROM addressbookobjects WHERE uri = ? AND addressbookid = ?";
dbglog($query);
$res = $this->sqlite->query($query, $uri, $addressbookid);
if($res !== false)
{
$this->updateSyncTokenLog($addressbookid, $uri, 'deleted');
return true;
}
return false;
}
public function editContactEntryToAddressbookForPage($id, $user, $uri, $params)
{
require_once(DOKU_PLUGIN.'davcard/vendor/autoload.php');
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 array('formattedname' => $this->getLang('settings_not_found'), 'result' => false);
if($settings['type'] !== 'contacts')
return array('formattedname' => $this->getLang('wrong_type'), 'result' => false);
$row = $wdc->getAddressbookEntryByUri($connectionId, $uri);
}
else
{
$addressbookid = $this->getAddressbookIdForPage($id);
$row = $this->getAddressbookEntryByUri($addressbookid, $uri);
}
$vcard = \Sabre\VObject\Reader::read($row['contactdata']);
$vcard->remove('ADR');
$vcard->remove('TEL');
$vcard->remove('EMAIL');
if(isset($params['phones']))
{
foreach($params['phones'] as $data)
{
$vcard->add('TEL', $data['number'], array('type' => $data['type']));
}
}
if(isset($params['email']))
{
foreach($params['email'] as $data)
{
$vcard->add('EMAIL', $data['mail'], array('type' => $data['type']));
}
}
if(isset($params['addresses']))
{
foreach($params['addresses'] as $data)
{
$vcard->add('ADR', array('', '', $data['street'], $data['city'], '', $data['zipcode'], $data['country']), array('type' => $data['type']));
}
}
$structuredname = explode(';', (string)$vcard->N);
$structuredname[0] = $params['lastname'];
$structuredname[1] = $params['firstname'];
$formattedname = $params['firstname'].' '.$params['lastname']; // FIXME: Make this configurable?
$vcard->N = $structuredname;
$vcard->FN = $formattedname;
$contactdata = $vcard->serialize();
if(strpos($id, 'webdav://') === 0)
{
return $wdc->editAddressbookEntry($connectionId, $uri, $contactdata);
}
else
{
$now = new \DateTime();
$query = "UPDATE addressbookobjects SET contactdata = ?, lastmodified = ?, etag = ?, size = ?, formattedname = ?, structuredname = ? WHERE addressbookid = ? AND uri = ?";
$res = $this->sqlite->query($query,
$contactdata,
$now->getTimestamp(),
md5($contactdata),
strlen($contactdata),
$formattedname,
implode(';', $structuredname),
$addressbookid,
$uri
);
if($res !== false)
{
$this->updateSyncTokenLog($addressbookid, $uri, 'modified');
return true;
}
}
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;
$vcard->N = $structuredname;
if(isset($params['phones']))
{
foreach($params['phones'] as $data)
{
$vcard->add('TEL', $data['number'], array('type' => $data['type']));
}
}
if(isset($params['email']))
{
foreach($params['email'] as $data)
{
$vcard->add('EMAIL', $data['mail'], array('type' => $data['type']));
}
}
if(isset($params['addresses']))
{
foreach($params['addresses'] as $data)
{
$vcard->add('ADR', array('', '', $data['street'], $data['city'], '', $data['zipcode'], $data['country']), array('type' => $data['type']));
}
}
$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;
}
}
return false;
}
public 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();
$photo = array();
$birthday = '';
$note = '';
$title = '';
$url = '';
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[] = array('type' => strtolower((string)$number['TYPE']), 'number' => (string)$number);
else
$tel[] = array('number' => (string)$number);
}
}
if(isset($vObject->ADR))
{
foreach($vObject->ADR as $adr)
{
if(isset($adr['TYPE']))
$addr[] = array('type' => strtolower((string)$adr['TYPE']), 'address' => $adr->getParts());
else
$addr[] = array('address' => $adr->getParts());
}
}
if(isset($vObject->EMAIL))
{
foreach($vObject->EMAIL as $email)
{
if(isset($email['TYPE']))
$mail[] = array('type' => strtolower((string)$email['TYPE']), 'mail' => (string)$email);
else
$mail[] = array('mail' => (string)$email);
}
}
if(isset($vObject->PHOTO))
{
if(isset($vObject->PHOTO['TYPE']))
{
$photo[] = array('type' => strtolower((string)$vObject->PHOTO['TYPE']), 'photo' => (string)$vObject->PHOTO);
}
else
$photo[] = array('photo' => (string)$vObject->PHOTO);
}
if(isset($vObject->BDAY))
{
$birthday = (string)$vObject->BDAY;
$birthday = str_replace('-', '', $birthday);
}
if(isset($vObject->NOTE))
{
$note = (string)$vObject->NOTE;
}
if(isset($vObject->TITLE))
{
$title = (string)$vObject->TITLE;
}
if(isset($vObject->URL))
{
$url = (string)$vObject->URL;
}
return array(
'formattedname' => $formattedname,
'structuredname' => $structuredname,
'tel' => $tel,
'mail' => $mail,
'addr' => $addr,
'uri' => $uri,
'photo' => $photo,
'birthday' => $birthday,
'note' => $note,
'title' => $title,
'url' => $url,
'result' => true
);
}
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/syntax/book.php b/syntax/book.php
--- a/syntax/book.php
+++ b/syntax/book.php
@@ -1,219 +1,231 @@
<?php
/**
* DokuWiki Plugin DAVCard (Book 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_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(),
);
foreach($options as $option)
{
list($key, $val) = explode('=', $option);
$key = strtolower(trim($key));
$val = trim($val);
switch($key)
{
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;
// FIXME: Check if the user has write permissions on the page!
$R->doc .= '<div class="davcardAddressbookAddNew"><a href="#" class="davcardAddressbookAddNew">'.$this->getLang('add_new').'</a></div>';
$R->doc .= '<div id="davcardAddressbookList" data-addressbookpage="'.$ID.'">';
$R->doc .= '<table class="davcardAddressbookTable">';
$R->doc .= '<tr><th>'.$this->getLang('name').'</th><th>'.$this->getLang('address').'</th><th>'.$this->getLang('phone').'</th><th>'.$this->getLang('email').'</th></tr>';
foreach($data['id'] as $id)
{
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);
}
else
{
$addressbookid = $this->hlp->getAddressbookIdForPage($id);
$entries = $this->hlp->getAddressbookEntries($addressbookid);
}
foreach($entries as $entry)
{
$contactdata = $this->hlp->parseVcard($entry['contactdata'], $entry['uri']);
$R->doc .= '<tr><td><a href="#" class="plugin_davcard_edit_vcard" data-davcardid="'.$id.'" data-davcarduri="'.$entry['uri'].'">'.$entry['formattedname'].'</a></td><td>';
if(count($contactdata['addr']) > 0)
{
$R->doc .= '<span class="adr">';
foreach($contactdata['addr'] as $dat)
{
if(isset($dat['type']))
$type = $dat['type'];
else
$type = 'other';
$R->doc .= '<span class="type">'.$this->getLang('adr'.strtolower($type)).'</span>';
if($dat['address'][2] != '')
{
$R->doc .= '<span class="street-address">'.$dat['address'][2].'</span><br>';
}
if($dat['address'][5] != '')
{
$R->doc .= '<span class="postal-code">'.$dat['address'][5].' </span>';
}
if($dat['address'][3] != '')
{
$R->doc .= '<span class="locality">'.$dat['address'][3].'</span><br>';
}
if($dat['address'][6] != '')
{
$R->doc .= '<span class="country-name">'.$dat['address'][6].'</span>';
}
}
$R->doc .= '</span>';
}
$R->doc .= '</td><td>';
if(count($contactdata['tel']) > 0)
{
$R->doc .= '<span class="tel">';
foreach($contactdata['tel'] as $dat)
{
if(isset($dat['type']))
$type = $dat['type'];
else
$type = 'other';
$R->doc .= '<span class="type">'.$this->getLang('tel'.strtolower($type)).' </span>';
$R->doc .= $dat['number'].'<br>';
}
$R->doc .= '</span>';
}
$R->doc .= '</td><td>';
if(count($contactdata['mail']) > 0)
{
foreach($contactdata['mail'] as $dat)
{
$R->doc .= '<span class="email">'.$dat['mail'].'</span><br>';
}
}
$R->doc .= '</td></tr>';
}
}
$R->doc .= '</table>';
$R->doc .= '</div>';
}
}
// vim:ts=4:sw=4:et:enc=utf-8:
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Jan 24, 3:23 AM (1 d, 10 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
532494
Default Alt Text
(39 KB)
Attached To
rDAVCARD DokuWiki davcard PlugIn
Event Timeline
Log In to Comment