aboutsummaryrefslogtreecommitdiffstats
path: root/apps/contacts/lib
diff options
context:
space:
mode:
Diffstat (limited to 'apps/contacts/lib')
-rw-r--r--apps/contacts/lib/app.php122
-rw-r--r--apps/contacts/lib/vcard.php135
2 files changed, 136 insertions, 121 deletions
diff --git a/apps/contacts/lib/app.php b/apps/contacts/lib/app.php
new file mode 100644
index 00000000000..ba086e4aca8
--- /dev/null
+++ b/apps/contacts/lib/app.php
@@ -0,0 +1,122 @@
+<?php
+/**
+ * Copyright (c) 2011 Bart Visscher bartv@thisnet.nl
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+/**
+ * This class manages our app actions
+ */
+OC_Contacts_App::$l10n = new OC_L10N('contacts');
+class OC_Contacts_App{
+ public static $l10n;
+
+ /**
+ * Render templates/part.details to json output
+ * @param int $id of contact
+ * @param Sabre_VObject_Component $vcard to render
+ */
+ public static function renderDetails($id, $vcard){
+ $property_types = self::getAddPropertyOptions();
+ $adr_types = self::getTypesOfProperty('ADR');
+ $phone_types = self::getTypesOfProperty('TEL');
+
+ $details = OC_Contacts_VCard::structureContact($vcard);
+ $name = $details['FN'][0]['value'];
+ $tmpl = new OC_Template('contacts','part.details');
+ $tmpl->assign('details',$details);
+ $tmpl->assign('id',$id);
+ $tmpl->assign('property_types',$property_types);
+ $tmpl->assign('adr_types',$adr_types);
+ $tmpl->assign('phone_types',$phone_types);
+ $page = $tmpl->fetchPage();
+
+ OC_JSON::success(array('data' => array( 'id' => $id, 'name' => $name, 'page' => $page )));
+ }
+
+ public static function getAddressbook($id){
+ $addressbook = OC_Contacts_Addressbook::find( $id );
+ if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
+ OC_JSON::error(array('data' => array( 'message' => self::$l10n->t('This is not your addressbook.')))); // Same here (as with the contact error). Could this error be improved?
+ exit();
+ }
+ return $addressbook;
+ }
+
+ public static function getContactObject($id){
+ $card = OC_Contacts_VCard::find( $id );
+ if( $card === false ){
+ OC_JSON::error(array('data' => array( 'message' => self::$l10n->t('Contact could not be found.'))));
+ exit();
+ }
+
+ self::getAddressbook( $card['addressbookid'] );
+ return $card;
+ }
+
+ public static function getContactVCard($id){
+ $card = self::getContactObject( $id );
+
+ $vcard = OC_VObject::parse($card['carddata']);
+ // Check if the card is valid
+ if(is_null($vcard)){
+ OC_JSON::error(array('data' => array( 'message' => self::$l10n->t('vCard could not be read.'))));
+ exit();
+ }
+ return $vcard;
+ }
+
+ public static function getPropertyLineByChecksum($vcard, $checksum){
+ $line = null;
+ for($i=0;$i<count($vcard->children);$i++){
+ if(md5($vcard->children[$i]->serialize()) == $checksum ){
+ $line = $i;
+ }
+ }
+ if(is_null($line)){
+ OC_JSON::error(array('data' => array( 'message' => self::$l10n->t('Information about vCard is incorrect. Please reload the page.'))));
+ exit();
+ }
+ return $line;
+ }
+
+ /**
+ * @return array of vcard prop => label
+ */
+ public static function getAddPropertyOptions(){
+ $l10n = self::$l10n;
+ return array(
+ 'ADR' => $l10n->t('Address'),
+ 'TEL' => $l10n->t('Telephone'),
+ 'EMAIL' => $l10n->t('Email'),
+ 'ORG' => $l10n->t('Organization'),
+ );
+ }
+
+ /**
+ * @return types for property $prop
+ */
+ public static function getTypesOfProperty($prop){
+ $l = self::$l10n;
+ switch($prop){
+ case 'ADR':
+ return array(
+ 'WORK' => $l->t('Work'),
+ 'HOME' => $l->t('Home'),
+ );
+ case 'TEL':
+ return array(
+ 'HOME' => $l->t('Home'),
+ 'CELL' => $l->t('Mobile'),
+ 'WORK' => $l->t('Work'),
+ 'TEXT' => $l->t('Text'),
+ 'VOICE' => $l->t('Voice'),
+ 'FAX' => $l->t('Fax'),
+ 'VIDEO' => $l->t('Video'),
+ 'PAGER' => $l->t('Pager'),
+ );
+ }
+ }
+}
diff --git a/apps/contacts/lib/vcard.php b/apps/contacts/lib/vcard.php
index b03e6ede998..87f2ff5e666 100644
--- a/apps/contacts/lib/vcard.php
+++ b/apps/contacts/lib/vcard.php
@@ -111,31 +111,21 @@ class OC_Contacts_VCard{
*/
public static function add($id,$data){
$fn = null;
- $uri = null;
- $card = self::parse($data);
+ $card = OC_VObject::parse($data);
if(!is_null($card)){
- // VCARD must have a version
- $hasversion = false;
- foreach($card->children as $property){
- if($property->name == 'FN'){
- $fn = $property->value;
- }
- elseif($property->name == 'VERSION'){
- $hasversion = true;
- }
- elseif(is_null($uri) && $property->name == 'UID' ){
- $uri = $property->value.'.vcf';
- }
- }
- if(is_null($uri)){
- $uid = self::createUID();
- $uri = $uid.'.vcf';
- $card->add(new Sabre_VObject_Property('UID',$uid));
+ $fn = $card->getAsString('FN');
+ $uid = $card->getAsString('UID');
+ if(is_null($uid)){
+ $card->setUID();
+ $uid = $card->getAsString('UID');
$data = $card->serialize();
};
+ $uri = $uid.'.vcf';
+ // VCARD must have a version
+ $version = $card->getAsString('VERSION');
// Add version if needed
- if(!$hasversion){
+ if(is_null($version)){
$card->add(new Sabre_VObject_Property('VERSION','3.0'));
$data = $card->serialize();
}
@@ -163,7 +153,7 @@ class OC_Contacts_VCard{
*/
public static function addFromDAVData($id,$uri,$data){
$fn = null;
- $card = self::parse($data);
+ $card = OC_VObject::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
@@ -190,7 +180,7 @@ class OC_Contacts_VCard{
$oldcard = self::find($id);
$fn = null;
- $card = self::parse($data);
+ $card = OC_VObject::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
@@ -218,7 +208,7 @@ class OC_Contacts_VCard{
$oldcard = self::findWhereDAVDataIs($aid,$uri);
$fn = null;
- $card = self::parse($data);
+ $card = OC_VObject::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
@@ -269,67 +259,6 @@ class OC_Contacts_VCard{
}
/**
- * @brief Escapes semicolons
- * @param string $value
- * @return string
- */
- public static function escapeSemicolons($value){
- foreach($value as &$i ){
- $i = implode("\\\\;", explode(';', $i));
- }
- return implode(';',$value);
- }
-
- /**
- * @brief Creates an array out of a multivalue property
- * @param string $value
- * @return array
- */
- public static function unescapeSemicolons($value){
- $array = explode(';',$value);
- for($i=0;$i<count($array);$i++){
- if(substr($array[$i],-2,2)=="\\\\"){
- if(isset($array[$i+1])){
- $array[$i] = substr($array[$i],0,count($array[$i])-2).';'.$array[$i+1];
- unset($array[$i+1]);
- }
- else{
- $array[$i] = substr($array[$i],0,count($array[$i])-2).';';
- }
- $i = $i - 1;
- }
- }
- return $array;
- }
-
- /**
- * @brief Add property to vcard object
- * @param object $vcard
- * @param object $name of property
- * @param object $value of property
- * @param object $paramerters of property
- */
- public static function addVCardProperty($vcard, $name, $value, $parameters=array()){
- if(is_array($value)){
- $value = OC_Contacts_VCard::escapeSemicolons($value);
- }
- $property = new Sabre_VObject_Property( $name, $value );
- $parameternames = array_keys($parameters);
- foreach($parameternames as $i){
- $values = $parameters[$i];
- if (!is_array($values)){
- $values = array($values);
- }
- foreach($values as $value){
- $property->add($i, $value);
- }
- }
-
- $vcard->add($property);
- return $property;
- }
-
- /**
* @brief Data structure of vCard
* @param object $property
* @return associative array
@@ -365,7 +294,7 @@ class OC_Contacts_VCard{
$value = $property->value;
$value = htmlspecialchars($value);
if($property->name == 'ADR' || $property->name == 'N'){
- $value = self::unescapeSemicolons($value);
+ $value = OC_VObject::unescapeSemicolons($value);
}
$temp = array(
'name' => $property->name,
@@ -392,40 +321,4 @@ class OC_Contacts_VCard{
}
return $temp;
}
-
- /**
- * @brief Parses a vcard file
- * @param string vCard
- * @return Sabre_VObject or null
- *
- * Will retun the vobject if sabre DAV is able to parse the file.
- */
- public static function parse($data){
- try {
- $card = Sabre_VObject_Reader::read($data);
- return $card;
- } catch (Exception $e) {
- return null;
- }
- }
- public static function getTypesOfProperty($l, $prop){
- switch($prop){
- case 'ADR':
- return array(
- 'WORK' => $l->t('Work'),
- 'HOME' => $l->t('Home'),
- );
- case 'TEL':
- return array(
- 'HOME' => $l->t('Home'),
- 'CELL' => $l->t('Mobile'),
- 'WORK' => $l->t('Work'),
- 'TEXT' => $l->t('Text'),
- 'VOICE' => $l->t('Voice'),
- 'FAX' => $l->t('Fax'),
- 'VIDEO' => $l->t('Video'),
- 'PAGER' => $l->t('Pager'),
- );
- }
- }
}