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.
|
|
|
|
*/
|
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
namespace OCA\Contacts;
|
|
|
|
|
2012-11-22 00:14:03 +01:00
|
|
|
use Sabre\VObject;
|
|
|
|
|
2011-12-05 21:51:25 +01:00
|
|
|
/**
|
|
|
|
* This class manages our app actions
|
|
|
|
*/
|
2012-10-25 03:34:12 +02:00
|
|
|
App::$l10n = \OC_L10N::get('contacts');
|
|
|
|
|
|
|
|
class App {
|
2012-10-24 21:35:51 +02:00
|
|
|
const THUMBNAIL_PREFIX = 'contact-thumbnail-';
|
|
|
|
const THUMBNAIL_SIZE = 28;
|
|
|
|
|
2012-06-27 02:12:14 +02:00
|
|
|
/**
|
2012-05-13 23:25:51 +02:00
|
|
|
* @brief returns the categories for the user
|
|
|
|
* @return (Array) $categories
|
|
|
|
*/
|
2012-09-17 16:15:31 +02:00
|
|
|
public static function getCategories($format = null) {
|
|
|
|
$categories = self::getVCategories()->categories($format);
|
2012-06-27 02:12:14 +02:00
|
|
|
return ($categories ? $categories : self::getDefaultCategories());
|
2012-04-12 22:31:28 +02: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-11-01 15:31:36 +01:00
|
|
|
$vcaddressbooks = Addressbook::all(\OCP\USER::getUser());
|
2012-04-12 22:31:28 +02:00
|
|
|
if(count($vcaddressbooks) > 0) {
|
|
|
|
$vcaddressbookids = array();
|
|
|
|
foreach($vcaddressbooks as $vcaddressbook) {
|
2012-11-01 15:31:36 +01:00
|
|
|
if($vcaddressbook['userid'] === \OCP\User::getUser()) {
|
2012-10-03 22:03:29 +02:00
|
|
|
$vcaddressbookids[] = $vcaddressbook['id'];
|
|
|
|
}
|
2012-04-12 22:31:28 +02:00
|
|
|
}
|
2012-07-09 00:16:14 +02:00
|
|
|
$start = 0;
|
|
|
|
$batchsize = 10;
|
2012-10-25 03:34:12 +02:00
|
|
|
$categories = new \OC_VCategories('contact');
|
2012-07-30 05:31:06 +02:00
|
|
|
while($vccontacts =
|
2012-10-30 07:08:41 +01:00
|
|
|
VCard::all($vcaddressbookids, $start, $batchsize)) {
|
2012-07-09 00:16:14 +02:00
|
|
|
$cards = array();
|
|
|
|
foreach($vccontacts as $vccontact) {
|
2012-09-11 14:19:28 +02:00
|
|
|
$cards[] = array($vccontact['id'], $vccontact['carddata']);
|
2012-07-09 00:16:14 +02:00
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts',
|
2012-07-30 05:31:06 +02:00
|
|
|
__CLASS__.'::'.__METHOD__
|
|
|
|
.', scanning: '.$batchsize.' starting from '.$start,
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::DEBUG);
|
2012-07-09 00:16:14 +02:00
|
|
|
// only reset on first batch.
|
2012-09-11 14:19:28 +02:00
|
|
|
$categories->rescan($cards,
|
2012-07-30 05:31:06 +02:00
|
|
|
true,
|
|
|
|
($start == 0 ? true : false));
|
2012-07-09 00:16:14 +02:00
|
|
|
$start += $batchsize;
|
|
|
|
}
|
2012-04-12 22:31:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check VCard for new categories.
|
|
|
|
* @see OC_VCategories::loadFromVObject
|
|
|
|
*/
|
2012-11-22 00:14:03 +01:00
|
|
|
public static function loadCategoriesFromVCard($id, $contact) {
|
|
|
|
if(!$contact instanceof \OC_VObject) {
|
|
|
|
$contact = new \OC_VObject($contact);
|
|
|
|
}
|
2012-09-11 14:19:28 +02:00
|
|
|
self::getVCategories()->loadFromVObject($id, $contact, true);
|
2012-03-07 21:50:55 +01:00
|
|
|
}
|
|
|
|
|
2012-11-22 00:14:03 +01:00
|
|
|
public static function cacheThumbnail($id, \OC_Image $image = null) {
|
2013-03-04 15:15:47 +01:00
|
|
|
if(\OC_Cache::hasKey(self::THUMBNAIL_PREFIX . $id) && $image === null) {
|
2012-10-25 03:34:12 +02:00
|
|
|
return \OC_Cache::get(self::THUMBNAIL_PREFIX . $id);
|
2012-10-24 21:35:51 +02:00
|
|
|
}
|
|
|
|
if(is_null($image)) {
|
|
|
|
$vcard = self::getContactVCard($id);
|
|
|
|
|
|
|
|
// invalid vcard
|
|
|
|
if(is_null($vcard)) {
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts',
|
2012-10-24 21:35:51 +02:00
|
|
|
__METHOD__.' The VCard for ID ' . $id . ' is not RFC compatible',
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::ERROR);
|
2012-10-24 21:35:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
$image = new \OC_Image();
|
2012-11-22 00:14:03 +01:00
|
|
|
if(!isset($vcard->PHOTO)) {
|
2012-10-24 21:35:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-22 00:31:08 +01:00
|
|
|
if(!$image->loadFromBase64((string)$vcard->PHOTO)) {
|
2012-10-24 21:35:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!$image->centerCrop()) {
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts',
|
2012-10-24 21:35:51 +02:00
|
|
|
'thumbnail.php. Couldn\'t crop thumbnail for ID ' . $id,
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::ERROR);
|
2012-10-24 21:35:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!$image->resize(self::THUMBNAIL_SIZE)) {
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts',
|
2012-10-24 21:35:51 +02:00
|
|
|
'thumbnail.php. Couldn\'t resize thumbnail for ID ' . $id,
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::ERROR);
|
2012-10-24 21:35:51 +02:00
|
|
|
return false;
|
2012-02-12 17:20:30 +01:00
|
|
|
}
|
2012-10-24 21:35:51 +02:00
|
|
|
// Cache for around a month
|
2012-10-25 03:34:12 +02:00
|
|
|
\OC_Cache::set(self::THUMBNAIL_PREFIX . $id, $image->data(), 3000000);
|
2012-11-01 02:00:29 +01:00
|
|
|
\OCP\Util::writeLog('contacts', 'Caching ' . $id, \OCP\Util::DEBUG);
|
2012-10-25 03:34:12 +02:00
|
|
|
return \OC_Cache::get(self::THUMBNAIL_PREFIX . $id);
|
2012-02-12 17:20:30 +01:00
|
|
|
}
|
2012-11-12 02:11:19 +01:00
|
|
|
|
2011-12-05 21:51:25 +01:00
|
|
|
}
|