2011-12-06 00:51:25 +04: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
|
|
|
|
*/
|
2012-04-14 18:44:15 +04:00
|
|
|
OC_Contacts_App::$l10n = OC_L10N::get('contacts');
|
2012-02-12 00:57:38 +04:00
|
|
|
class OC_Contacts_App {
|
2012-05-14 01:25:51 +04:00
|
|
|
/*
|
|
|
|
* @brief language object for calendar app
|
|
|
|
*/
|
|
|
|
|
2011-12-06 00:51:25 +04:00
|
|
|
public static $l10n;
|
2012-05-14 01:25:51 +04:00
|
|
|
/*
|
|
|
|
* @brief categories of the user
|
|
|
|
*/
|
|
|
|
public static $categories = null;
|
2011-12-06 00:51:25 +04:00
|
|
|
|
2012-02-12 00:57:38 +04:00
|
|
|
public static function getAddressbook($id) {
|
2011-12-07 01:31:04 +04:00
|
|
|
$addressbook = OC_Contacts_Addressbook::find( $id );
|
2012-05-01 20:50:31 +04:00
|
|
|
if( $addressbook === false || $addressbook['userid'] != OCP\USER::getUser()) {
|
2012-02-12 00:52:39 +04:00
|
|
|
if ($addressbook === false) {
|
2012-05-01 19:38:27 +04:00
|
|
|
OCP\Util::writeLog('contacts', 'Addressbook not found: '. $id, OCP\Util::ERROR);
|
2012-05-03 14:23:29 +04:00
|
|
|
OCP\JSON::error(array('data' => array( 'message' => self::$l10n->t('Addressbook not found.'))));
|
2012-02-12 00:52:39 +04:00
|
|
|
}
|
|
|
|
else {
|
2012-05-01 20:50:31 +04:00
|
|
|
OCP\Util::writeLog('contacts', 'Addressbook('.$id.') is not from '.OCP\USER::getUser(), OCP\Util::ERROR);
|
2012-05-03 14:23:29 +04:00
|
|
|
OCP\JSON::error(array('data' => array( 'message' => self::$l10n->t('This is not your addressbook.'))));
|
2012-02-12 00:52:39 +04:00
|
|
|
}
|
2011-12-07 01:31:04 +04:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
return $addressbook;
|
|
|
|
}
|
|
|
|
|
2012-02-12 00:57:38 +04:00
|
|
|
public static function getContactObject($id) {
|
2011-12-07 01:31:04 +04:00
|
|
|
$card = OC_Contacts_VCard::find( $id );
|
2012-02-12 00:57:38 +04:00
|
|
|
if( $card === false ) {
|
2012-05-01 19:38:27 +04:00
|
|
|
OCP\Util::writeLog('contacts', 'Contact could not be found: '.$id, OCP\Util::ERROR);
|
2012-07-15 06:15:57 +04:00
|
|
|
OCP\JSON::error(array('data' => array( 'message' => self::$l10n->t('Contact could not be found.').' '.print_r($id, true))));
|
2011-12-07 01:31:04 +04:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2012-02-12 00:52:39 +04:00
|
|
|
self::getAddressbook( $card['addressbookid'] );//access check
|
2011-12-07 01:31:04 +04:00
|
|
|
return $card;
|
|
|
|
}
|
|
|
|
|
2012-01-12 21:04:23 +04:00
|
|
|
/**
|
2012-02-09 22:04:07 +04:00
|
|
|
* @brief Gets the VCard as an OC_VObject
|
2012-01-12 21:04:23 +04:00
|
|
|
* @returns The card or null if the card could not be parsed.
|
|
|
|
*/
|
2012-02-12 00:57:38 +04:00
|
|
|
public static function getContactVCard($id) {
|
2011-12-07 01:31:04 +04:00
|
|
|
$card = self::getContactObject( $id );
|
|
|
|
|
|
|
|
$vcard = OC_VObject::parse($card['carddata']);
|
2012-02-09 22:04:07 +04:00
|
|
|
// Try to fix cards with missing 'N' field from pre ownCloud 4. Hot damn, this is ugly...
|
2012-02-12 00:57:38 +04:00
|
|
|
if(!is_null($vcard) && !$vcard->__isset('N')) {
|
2012-05-11 03:10:50 +04:00
|
|
|
$version = OCP\App::getAppVersion('contacts');
|
|
|
|
if($version >= 5) {
|
2012-07-20 02:17:59 +04:00
|
|
|
OCP\Util::writeLog('contacts', 'OC_Contacts_App::getContactVCard. Deprecated check for missing N field', OCP\Util::DEBUG);
|
2012-02-09 22:04:07 +04:00
|
|
|
}
|
2012-07-20 02:17:59 +04:00
|
|
|
OCP\Util::writeLog('contacts', 'getContactVCard, Missing N field', OCP\Util::DEBUG);
|
2012-02-09 22:04:07 +04:00
|
|
|
if($vcard->__isset('FN')) {
|
2012-07-20 02:17:59 +04:00
|
|
|
OCP\Util::writeLog('contacts', 'getContactVCard, found FN field: '.$vcard->__get('FN'), OCP\Util::DEBUG);
|
2012-02-09 22:04:07 +04:00
|
|
|
$n = implode(';', array_reverse(array_slice(explode(' ', $vcard->__get('FN')), 0, 2))).';;;';
|
2012-02-12 18:12:08 +04:00
|
|
|
$vcard->setString('N', $n);
|
2012-03-08 00:27:03 +04:00
|
|
|
OC_Contacts_VCard::edit( $id, $vcard);
|
2012-02-09 22:04:07 +04:00
|
|
|
} else { // Else just add an empty 'N' field :-P
|
|
|
|
$vcard->setString('N', 'Unknown;Name;;;');
|
|
|
|
}
|
|
|
|
}
|
2012-02-12 18:13:30 +04:00
|
|
|
if (!is_null($vcard) && !isset($vcard->REV)) {
|
|
|
|
$rev = new DateTime('@'.$card['lastmodified']);
|
|
|
|
$vcard->setString('REV', $rev->format(DateTime::W3C));
|
|
|
|
}
|
2011-12-07 01:31:04 +04:00
|
|
|
return $vcard;
|
|
|
|
}
|
|
|
|
|
2012-02-12 00:57:38 +04:00
|
|
|
public static function getPropertyLineByChecksum($vcard, $checksum) {
|
2011-12-07 01:31:04 +04:00
|
|
|
$line = null;
|
2012-02-12 00:57:38 +04:00
|
|
|
for($i=0;$i<count($vcard->children);$i++) {
|
|
|
|
if(md5($vcard->children[$i]->serialize()) == $checksum ) {
|
2011-12-07 01:31:04 +04:00
|
|
|
$line = $i;
|
2012-01-11 06:56:53 +04:00
|
|
|
break;
|
2011-12-07 01:31:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $line;
|
|
|
|
}
|
|
|
|
|
2011-12-06 00:51:25 +04:00
|
|
|
/**
|
|
|
|
* @return array of vcard prop => label
|
|
|
|
*/
|
2012-02-12 00:57:38 +04:00
|
|
|
public static function getAddPropertyOptions() {
|
2011-12-07 01:31:04 +04:00
|
|
|
$l10n = self::$l10n;
|
2011-12-06 00:51:25 +04: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-12 00:57:38 +04:00
|
|
|
public static function getTypesOfProperty($prop) {
|
2011-12-07 01:31:04 +04:00
|
|
|
$l = self::$l10n;
|
2012-02-12 00:57:38 +04:00
|
|
|
switch($prop) {
|
2011-12-06 00:51:25 +04: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'),
|
2012-04-23 23:08:38 +04:00
|
|
|
'MSG' => $l->t('Message'),
|
2011-12-06 00:51:25 +04:00
|
|
|
'FAX' => $l->t('Fax'),
|
|
|
|
'VIDEO' => $l->t('Video'),
|
|
|
|
'PAGER' => $l->t('Pager'),
|
|
|
|
);
|
2012-04-30 00:10:16 +04:00
|
|
|
case 'EMAIL':
|
|
|
|
return array(
|
|
|
|
'WORK' => $l->t('Work'),
|
|
|
|
'HOME' => $l->t('Home'),
|
|
|
|
'INTERNET' => $l->t('Internet'),
|
|
|
|
);
|
2011-12-06 00:51:25 +04:00
|
|
|
}
|
|
|
|
}
|
2012-02-12 20:20:30 +04:00
|
|
|
|
2012-06-27 04:12:14 +04:00
|
|
|
/**
|
2012-05-14 01:25:51 +04:00
|
|
|
* @brief returns the vcategories object of the user
|
|
|
|
* @return (object) $vcategories
|
|
|
|
*/
|
|
|
|
protected static function getVCategories() {
|
|
|
|
if (is_null(self::$categories)) {
|
2012-06-27 04:12:14 +04:00
|
|
|
self::$categories = new OC_VCategories('contacts', null, self::getDefaultCategories());
|
2012-05-14 01:25:51 +04:00
|
|
|
}
|
|
|
|
return self::$categories;
|
|
|
|
}
|
|
|
|
|
2012-06-27 04:12:14 +04:00
|
|
|
/**
|
2012-05-14 01:25:51 +04:00
|
|
|
* @brief returns the categories for the user
|
|
|
|
* @return (Array) $categories
|
|
|
|
*/
|
2012-03-08 00:50:55 +04:00
|
|
|
public static function getCategories() {
|
2012-06-27 04:12:14 +04:00
|
|
|
$categories = self::getVCategories()->categories();
|
2012-04-13 00:31:28 +04:00
|
|
|
if(count($categories) == 0) {
|
|
|
|
self::scanCategories();
|
|
|
|
$categories = self::$categories->categories();
|
|
|
|
}
|
2012-06-27 04:12:14 +04:00
|
|
|
return ($categories ? $categories : self::getDefaultCategories());
|
2012-04-13 00:31:28 +04:00
|
|
|
}
|
|
|
|
|
2012-06-27 04:12:14 +04:00
|
|
|
/**
|
|
|
|
* @brief returns the default categories of ownCloud
|
|
|
|
* @return (array) $categories
|
|
|
|
*/
|
2012-07-06 05:22:02 +04:00
|
|
|
public static function getDefaultCategories(){
|
2012-06-27 04:12:14 +04:00
|
|
|
return array(
|
|
|
|
(string)self::$l10n->t('Birthday'),
|
|
|
|
(string)self::$l10n->t('Business'),
|
|
|
|
(string)self::$l10n->t('Call'),
|
|
|
|
(string)self::$l10n->t('Clients'),
|
|
|
|
(string)self::$l10n->t('Deliverer'),
|
|
|
|
(string)self::$l10n->t('Holidays'),
|
|
|
|
(string)self::$l10n->t('Ideas'),
|
|
|
|
(string)self::$l10n->t('Journey'),
|
|
|
|
(string)self::$l10n->t('Jubilee'),
|
|
|
|
(string)self::$l10n->t('Meeting'),
|
|
|
|
(string)self::$l10n->t('Other'),
|
|
|
|
(string)self::$l10n->t('Personal'),
|
|
|
|
(string)self::$l10n->t('Projects'),
|
|
|
|
(string)self::$l10n->t('Questions'),
|
|
|
|
(string)self::$l10n->t('Work'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-04-13 00:31:28 +04:00
|
|
|
/**
|
|
|
|
* scan vcards for categories.
|
|
|
|
* @param $vccontacts VCards to scan. null to check all vcards for the current user.
|
|
|
|
*/
|
|
|
|
public static function scanCategories($vccontacts = null) {
|
|
|
|
if (is_null($vccontacts)) {
|
2012-05-01 20:50:31 +04:00
|
|
|
$vcaddressbooks = OC_Contacts_Addressbook::all(OCP\USER::getUser());
|
2012-04-13 00:31:28 +04:00
|
|
|
if(count($vcaddressbooks) > 0) {
|
|
|
|
$vcaddressbookids = array();
|
|
|
|
foreach($vcaddressbooks as $vcaddressbook) {
|
|
|
|
$vcaddressbookids[] = $vcaddressbook['id'];
|
|
|
|
}
|
2012-07-09 02:16:14 +04:00
|
|
|
$start = 0;
|
|
|
|
$batchsize = 10;
|
|
|
|
while($vccontacts = OC_Contacts_VCard::all($vcaddressbookids, $start, $batchsize)){
|
|
|
|
$cards = array();
|
|
|
|
foreach($vccontacts as $vccontact) {
|
|
|
|
$cards[] = $vccontact['carddata'];
|
|
|
|
}
|
2012-07-20 02:17:59 +04:00
|
|
|
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', scanning: '.$batchsize.' starting from '.$start, OCP\Util::DEBUG);
|
2012-07-09 02:16:14 +04:00
|
|
|
// only reset on first batch.
|
2012-07-20 02:17:59 +04:00
|
|
|
self::getVCategories()->rescan($cards, true, ($start == 0 ? true : false));
|
2012-07-09 02:16:14 +04:00
|
|
|
$start += $batchsize;
|
|
|
|
}
|
2012-04-13 00:31:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check VCard for new categories.
|
|
|
|
* @see OC_VCategories::loadFromVObject
|
|
|
|
*/
|
|
|
|
public static function loadCategoriesFromVCard(OC_VObject $contact) {
|
2012-05-14 01:25:51 +04:00
|
|
|
self::getVCategories()->loadFromVObject($contact, true);
|
2012-03-08 00:50:55 +04:00
|
|
|
}
|
|
|
|
|
2012-02-12 23:26:54 +04:00
|
|
|
public static function setLastModifiedHeader($contact) {
|
2012-02-12 20:20:30 +04:00
|
|
|
$rev = $contact->getAsString('REV');
|
|
|
|
if ($rev) {
|
|
|
|
$rev = DateTime::createFromFormat(DateTime::W3C, $rev);
|
2012-05-03 12:46:27 +04:00
|
|
|
OCP\Response::setLastModifiedHeader($rev);
|
2012-02-12 20:20:30 +04:00
|
|
|
}
|
|
|
|
}
|
2011-12-06 00:51:25 +04:00
|
|
|
}
|