1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-01 13:24:10 +01:00
OwncloudContactsOfficial/lib/app.php

232 lines
6.5 KiB
PHP
Raw Normal View History

<?php
/**
2013-03-16 15:59:23 +01:00
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
* 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;
/**
* This class manages our app actions
*/
2012-10-25 03:34:12 +02:00
App::$l10n = \OC_L10N::get('contacts');
class App {
const THUMBNAIL_PREFIX = 'contact-thumbnail-';
const THUMBNAIL_SIZE = 28;
2013-03-15 17:01:06 +01:00
/**
* @brief language object for calendar app
*
* @var OC_L10N
*/
public static $l10n;
/**
* An array holding the current users address books.
* @var array
*/
protected static $addressBooks = array();
/**
* If backends are added to this map, they will be automatically mapped
* to their respective classes, if constructed with the 'createBackend' method.
*
* @var array
*/
public static $backendClasses = array(
'database' => 'OCA\Contacts\Backend\Database',
'shared' => 'OCA\Contacts\Backend\Shared',
);
public function __construct(
$addressBooksTableName = '*PREFIX*addressbook',
$backendsTableName = '*PREFIX*addressbooks_backend',
$dbBackend = null
) {
$this->addressBooksTableName = $addressBooksTableName;
$this->backendsTableName = $backendsTableName;
$this->dbBackend = $dbBackend ? $dbBackend : new Backend\Database();
}
/**
* Creates the new backend by name, but in addition will also see if
* there's a class mapped to the property name.
*
* @param string $name
* @return \Backend\AbstractBackend
*/
static public function createBackend($name) {
$name = $name ? $name : 'database';
if (isset(self::$backendClasses[$name])) {
2013-03-16 15:59:23 +01:00
return new self::$backendClasses[$name]();
2013-03-15 17:01:06 +01:00
} else {
throw new \Exception('No backend for: ' . $name);
}
}
/**
* Return all registered address books for current user.
* For now this is hard-coded to using the Database and
* Shared backends, but eventually admins will be able to
* register additional backends, and users will be able to
* subscribe to address books using those backends.
*
* @return AddressBook[]
*/
public function getAllAddressBooksForUser() {
if(!self::$addressBooks) {
foreach(array_keys(self::$backendClasses) as $backendName) {
$backend = self::createBackend($backendName);
$addressBooks = $backend->getAddressBooksForUser();
foreach($addressBooks as $addressBook) {
$addressBook['backend'] = $backendName;
self::$addressBooks[] = new AddressBook($backend, $addressBook);
}
}
}
return self::$addressBooks;
}
/**
* Get an address book from a specific backend.
*
* @param string $backendName
* @param string $addressbookid
2013-03-16 15:59:23 +01:00
* @return AddressBook|null
2013-03-15 17:01:06 +01:00
*/
public function getAddressBook($backendName, $addressbookid) {
foreach(self::$addressBooks as $addressBook) {
if($addressBook->backend->name === $backendName
&& $addressBook->getId() === $addressbookid
) {
return $addressBook;
}
}
// TODO: Check for return values
$backend = self::createBackend($backendName);
$info = $backend->getAddressBook($addressbookid);
// FIXME: Backend name should be set by the backend.
$info['backend'] = $backendName;
$addressBook = new AddressBook($backend, $info);
self::$addressBooks[] = $addressBook;
return $addressBook;
}
/**
* Get a Contact from an address book from a specific backend.
*
* @param string $backendName
* @param string $addressbookid
* @param string $id - Contact id
2013-03-16 15:59:23 +01:00
* @return Contact|null
*
2013-03-15 17:01:06 +01:00
*/
public function getContact($backendName, $addressbookid, $id) {
$addressBook = $this->getAddressBook($backendName, $addressbookid);
// TODO: Check for return value
return $addressBook->getChild($id);
}
/**
* @brief returns the categories for the user
* @return (Array) $categories
*/
public static function getCategories($format = null) {
$categories = self::getVCategories()->categories($format);
return ($categories ? $categories : self::getDefaultCategories());
}
/**
* 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());
if(count($vcaddressbooks) > 0) {
$vcaddressbookids = array();
foreach($vcaddressbooks as $vcaddressbook) {
2012-11-01 15:31:36 +01:00
if($vcaddressbook['userid'] === \OCP\User::getUser()) {
$vcaddressbookids[] = $vcaddressbook['id'];
}
}
$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)) {
$cards = array();
foreach($vccontacts as $vccontact) {
$cards[] = array($vccontact['id'], $vccontact['carddata']);
}
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);
// only reset on first batch.
$categories->rescan($cards,
2012-07-30 05:31:06 +02:00
true,
($start == 0 ? true : false));
$start += $batchsize;
}
}
}
}
/**
* 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);
}
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) {
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);
}
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',
__METHOD__.' The VCard for ID ' . $id . ' is not RFC compatible',
2012-10-25 03:34:12 +02:00
\OCP\Util::ERROR);
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)) {
return false;
}
2012-11-22 00:31:08 +01:00
if(!$image->loadFromBase64((string)$vcard->PHOTO)) {
return false;
}
}
if(!$image->centerCrop()) {
2012-10-25 03:34:12 +02:00
\OCP\Util::writeLog('contacts',
'thumbnail.php. Couldn\'t crop thumbnail for ID ' . $id,
2012-10-25 03:34:12 +02:00
\OCP\Util::ERROR);
return false;
}
if(!$image->resize(self::THUMBNAIL_SIZE)) {
2012-10-25 03:34:12 +02:00
\OCP\Util::writeLog('contacts',
'thumbnail.php. Couldn\'t resize thumbnail for ID ' . $id,
2012-10-25 03:34:12 +02:00
\OCP\Util::ERROR);
return false;
}
// 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);
}
}