2011-12-05 21:51:25 +01:00
< ? 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' );
2012-02-11 21:57:38 +01:00
class OC_Contacts_App {
2011-12-05 21:51:25 +01:00
public static $l10n ;
/**
* Render templates / part . details to json output
* @ param int $id of contact
* @ param Sabre_VObject_Component $vcard to render
*/
2012-02-11 21:57:38 +01:00
public static function renderDetails ( $id , $vcard , $new = false ) {
2011-12-06 22:31:04 +01:00
$property_types = self :: getAddPropertyOptions ();
$adr_types = self :: getTypesOfProperty ( 'ADR' );
$phone_types = self :: getTypesOfProperty ( 'TEL' );
2012-01-19 16:04:39 +01:00
$upload_max_filesize = OC_Helper :: computerFileSize ( ini_get ( 'upload_max_filesize' ));
$post_max_size = OC_Helper :: computerFileSize ( ini_get ( 'post_max_size' ));
$maxUploadFilesize = min ( $upload_max_filesize , $post_max_size );
$freeSpace = OC_Filesystem :: free_space ( '/' );
$freeSpace = max ( $freeSpace , 0 );
$maxUploadFilesize = min ( $maxUploadFilesize , $freeSpace );
2011-12-05 21:51:25 +01:00
$details = OC_Contacts_VCard :: structureContact ( $vcard );
$name = $details [ 'FN' ][ 0 ][ 'value' ];
2012-01-19 16:04:39 +01:00
$t = $new ? 'part.contact' : 'part.details' ;
$tmpl = new OC_Template ( 'contacts' , $t );
2011-12-05 21:51:25 +01:00
$tmpl -> assign ( 'details' , $details );
$tmpl -> assign ( 'id' , $id );
2012-01-19 16:04:39 +01:00
$tmpl -> assign ( 'uploadMaxFilesize' , $maxUploadFilesize );
$tmpl -> assign ( 'uploadMaxHumanFilesize' , OC_Helper :: humanFileSize ( $maxUploadFilesize ));
2011-12-05 21:51:25 +01:00
$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 )));
}
2012-02-11 21:57:38 +01:00
public static function getAddressbook ( $id ) {
2011-12-06 22:31:04 +01:00
$addressbook = OC_Contacts_Addressbook :: find ( $id );
2012-02-11 21:57:38 +01:00
if ( $addressbook === false || $addressbook [ 'userid' ] != OC_User :: getUser ()) {
2012-02-11 21:52:39 +01:00
if ( $addressbook === false ) {
OC_Log :: write ( 'contacts' , 'Addressbook not found: ' . $id , OC_Log :: ERROR );
}
else {
OC_Log :: write ( 'contacts' , 'Addressbook(' . $id . ') is not from ' . $OC_User :: getUser (), OC_Log :: ERROR );
}
2011-12-06 22:31:04 +01:00
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 ;
}
2012-02-11 21:57:38 +01:00
public static function getContactObject ( $id ) {
2011-12-06 22:31:04 +01:00
$card = OC_Contacts_VCard :: find ( $id );
2012-02-11 21:57:38 +01:00
if ( $card === false ) {
2012-02-11 21:52:39 +01:00
OC_Log :: write ( 'contacts' , 'Contact could not be found: ' . $id , OC_Log :: ERROR );
2012-02-08 07:56:43 +01:00
OC_JSON :: error ( array ( 'data' => array ( 'message' => self :: $l10n -> t ( 'Contact could not be found.' ) . ' ' . $id )));
2011-12-06 22:31:04 +01:00
exit ();
}
2012-02-11 21:52:39 +01:00
self :: getAddressbook ( $card [ 'addressbookid' ] ); //access check
2011-12-06 22:31:04 +01:00
return $card ;
}
2012-01-12 18:04:23 +01:00
/**
2012-02-09 19:04:07 +01:00
* @ brief Gets the VCard as an OC_VObject
2012-01-12 18:04:23 +01:00
* @ returns The card or null if the card could not be parsed .
*/
2012-02-11 21:57:38 +01:00
public static function getContactVCard ( $id ) {
2011-12-06 22:31:04 +01:00
$card = self :: getContactObject ( $id );
$vcard = OC_VObject :: parse ( $card [ 'carddata' ]);
2012-02-09 19:04:07 +01:00
// Try to fix cards with missing 'N' field from pre ownCloud 4. Hot damn, this is ugly...
2012-02-11 21:57:38 +01:00
if ( ! is_null ( $vcard ) && ! $vcard -> __isset ( 'N' )) {
2012-02-09 19:04:07 +01:00
$appinfo = $info = OC_App :: getAppInfo ( 'contacts' );
if ( $appinfo [ 'version' ] >= 5 ) {
OC_Log :: write ( 'contacts' , 'OC_Contacts_App::getContactVCard. Deprecated check for missing N field' , OC_Log :: DEBUG );
}
OC_Log :: write ( 'contacts' , 'getContactVCard, Missing N field' , OC_Log :: DEBUG );
if ( $vcard -> __isset ( 'FN' )) {
OC_Log :: write ( 'contacts' , 'getContactVCard, found FN field: ' . $vcard -> __get ( 'FN' ), OC_Log :: DEBUG );
$n = implode ( ';' , array_reverse ( array_slice ( explode ( ' ' , $vcard -> __get ( 'FN' )), 0 , 2 ))) . ';;;' ;
2012-02-12 15:12:08 +01:00
$vcard -> setString ( 'N' , $n );
2012-02-09 19:04:07 +01:00
OC_Contacts_VCard :: edit ( $id , $vcard -> serialize ());
} else { // Else just add an empty 'N' field :-P
$vcard -> setString ( 'N' , 'Unknown;Name;;;' );
}
}
2011-12-06 22:31:04 +01:00
return $vcard ;
}
2012-02-11 21:57:38 +01:00
public static function getPropertyLineByChecksum ( $vcard , $checksum ) {
2011-12-06 22:31:04 +01:00
$line = null ;
2012-02-11 21:57:38 +01:00
for ( $i = 0 ; $i < count ( $vcard -> children ); $i ++ ) {
if ( md5 ( $vcard -> children [ $i ] -> serialize ()) == $checksum ) {
2011-12-06 22:31:04 +01:00
$line = $i ;
2012-01-11 03:56:53 +01:00
break ;
2011-12-06 22:31:04 +01:00
}
}
return $line ;
}
2011-12-05 21:51:25 +01:00
/**
* @ return array of vcard prop => label
*/
2012-02-11 21:57:38 +01:00
public static function getAddPropertyOptions () {
2011-12-06 22:31:04 +01:00
$l10n = self :: $l10n ;
2011-12-05 21:51:25 +01:00
return array (
'ADR' => $l10n -> t ( 'Address' ),
'TEL' => $l10n -> t ( 'Telephone' ),
'EMAIL' => $l10n -> t ( 'Email' ),
'ORG' => $l10n -> t ( 'Organization' ),
);
}
/**
* @ return types for property $prop
*/
2012-02-11 21:57:38 +01:00
public static function getTypesOfProperty ( $prop ) {
2011-12-06 22:31:04 +01:00
$l = self :: $l10n ;
2012-02-11 21:57:38 +01:00
switch ( $prop ) {
2011-12-05 21:51:25 +01:00
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' ),
);
}
}
}