1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-04 15:24:35 +01:00
OwncloudContactsOfficial/lib/backend/localusers.php

404 lines
12 KiB
PHP
Raw Normal View History

2014-03-29 11:54:08 +01:00
<?php
2014-04-01 21:09:48 +02:00
/**
* ownCloud - ownCloud users backend for Contacts
*
* @author Tobia De Koninck
* @copyright 2014 Tobia De Koninck (tobia@ledfan.be)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
2014-03-29 11:54:08 +01:00
namespace OCA\Contacts\Backend;
use OCA\Contacts\Contact,
OCA\Contacts\VObject\VCard,
OCA\Contacts\Utils\Properties,
2014-03-31 15:29:16 +02:00
Sabre\VObject\Reader,
2014-04-03 18:36:11 +02:00
OCA\Contacts\Addressbook,
OCA\Contacts\LocalUsersAddressbookProvider;
2014-04-01 20:48:00 +02:00
2014-03-31 15:29:16 +02:00
/**
* Contact backend for storing all the ownCloud users in this installation.
* Every user has *1* personal addressbook. The id of this addresbook is the
2014-04-01 20:48:00 +02:00
* userid of the owner.
2014-03-31 15:29:16 +02:00
*/
2014-04-02 20:39:04 +02:00
class LocalUsers extends AbstractBackend {
2014-03-29 11:54:08 +01:00
2014-04-02 20:39:04 +02:00
public $name = 'localusers';
2014-04-01 21:08:29 +02:00
2014-03-31 15:29:16 +02:00
/**
* The table that holds the address books.
* For every user there is *1* addressbook.
* @var string
*/
2014-03-29 11:54:08 +01:00
private $addressBooksTableName = '*PREFIX*contacts_ocu_addressbooks';
2014-04-01 21:08:29 +02:00
2014-03-31 15:29:16 +02:00
/**
* The table that holds the contacts.
* @var string
*/
2014-03-29 11:54:08 +01:00
private $cardsTableName = '*PREFIX*contacts_ocu_cards';
2014-04-06 08:39:58 +02:00
/**
* The table that holds the properties of the contacts.
* This is used to provice a search function.
* @var string
*/
private $indexTableName = '*PREFIX*contacts_ocu_cards_properties';
2014-04-01 21:08:29 +02:00
2014-04-06 08:39:58 +02:00
/**
* All possible properties which can be stored in the $indexTableName.
* @var string
*/
private $indexProperties = array(
'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME',
'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO');
2014-04-06 08:39:58 +02:00
/**
* language object
* @var OC_L10N
*/
public static $l10n;
/**
* Defaults object
* @var OC_Defaults
*/
public static $defaults;
2014-03-29 11:54:08 +01:00
public function __construct($userid){
2014-04-06 08:39:58 +02:00
self::$l10n = \OCP\Util::getL10N('contacts');
self::$defaults = new \OCP\Defaults();
2014-04-01 21:08:29 +02:00
$this->userid = $userid ? $userid : \OCP\User::getUser();
2014-03-29 11:54:08 +01:00
}
2014-04-01 21:08:29 +02:00
2014-04-01 20:48:00 +02:00
/**
* {@inheritdoc}
*/
2014-03-29 11:54:08 +01:00
public function getAddressBooksForUser(array $options = array()) {
2014-04-02 21:49:53 +02:00
return array($this->getAddressBook($this->userid));
2014-03-29 11:54:08 +01:00
}
2014-04-01 21:08:29 +02:00
2014-04-01 20:48:00 +02:00
/**
* {@inheritdoc}
* Only 1 addressbook for every user
*/
2014-03-29 11:54:08 +01:00
public function getAddressBook($addressBookId, array $options = array()) {
2014-04-01 22:04:55 +02:00
$addressbook = array(
"id" => $addressBookId,
2014-04-06 08:39:58 +02:00
"displayname" => self::$l10n->t('On this') . self::$defaults->getName(),
"description" => self::$l10n->t('On this') . self::$defaults->getName(),
2014-04-01 22:04:55 +02:00
"ctag" => time(),
2014-04-02 18:54:09 +02:00
"permissions" => \OCP\PERMISSION_READ,
2014-04-01 22:04:55 +02:00
"backend" => $this->name,
"active" => 1
);
return $addressbook;
2014-03-29 11:54:08 +01:00
}
2014-04-01 21:08:29 +02:00
2014-04-01 20:48:00 +02:00
/**
* {@inheritdoc}
* There are as many contacts in this addressbook as in this ownCloud installation
*/
2014-03-29 11:54:08 +01:00
public function getContacts($addressbookid, array $options = array()){
2014-04-01 21:08:29 +02:00
$contacts = array();
2014-04-01 21:20:19 +02:00
try{
2014-04-02 18:54:09 +02:00
$sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?';
2014-04-01 21:20:19 +02:00
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($this->userid));
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return array();
} else {
while($row = $result->fetchRow()){
2014-04-02 21:16:08 +02:00
$row['permissions'] = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE;
2014-04-01 21:20:19 +02:00
$contacts[] = $row;
}
$contactsId = array();
foreach($contacts as $contact){
$contactsId[] = $contact['id'];
}
$users = \OCP\User::getUsers();
$recall = false;
$add = array_diff($users, $contactsId);
$remove = array_diff($contactsId, $users);
if(count($add) > 0){
$this->addContacts($add, $addressbookid);
$recall = true;
}
if(count($remove) > 0){
$this->removeContacts($remove, $addressbookid);
$recall = true;
}
if($recall === true){
return $this->getContacts($addressbookid);
} else {
return $contacts;
}
}
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.' exception: '
. $e->getMessage(), \OCP\Util::ERROR);
2014-04-01 21:25:41 +02:00
return array();
2014-04-01 21:08:29 +02:00
}
2014-04-01 21:20:19 +02:00
2014-03-29 11:54:08 +01:00
}
2014-04-01 21:08:29 +02:00
2014-04-01 20:48:00 +02:00
/**
* {@inheritdoc}
* If your username is "admin" and you want to retrieve your own contact
* the params would be: $addressbookid = 'admin'; $id = 'admin';
* If your username is 'foo' and you want to retrieve the contact with
* ownCloud username 'bar' the params would be: $addressbookid = 'foo'; $id = 'bar';
*/
2014-03-29 11:54:08 +01:00
public function getContact($addressbookid, $id, array $options = array()){
2014-04-01 21:20:19 +02:00
try{
2014-04-02 21:44:03 +02:00
$sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE addressbookid = ? AND id = ?';
2014-04-01 21:20:19 +02:00
$query = \OCP\DB::prepare($sql);
2014-04-02 21:44:03 +02:00
$result = $query->execute(array($this->userid, $id));
2014-04-01 21:20:19 +02:00
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return array();
} else {
$row = $result->fetchRow();
2014-04-02 21:16:08 +02:00
$row['permissions'] = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE;
2014-04-01 21:20:19 +02:00
return $row;
}
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.' exception: '
. $e->getMessage(), \OCP\Util::ERROR);
2014-04-01 21:25:41 +02:00
return array();
2014-04-01 21:20:19 +02:00
}
2014-03-29 11:54:08 +01:00
}
2014-04-01 21:08:29 +02:00
2014-04-01 20:48:00 +02:00
/**
* Help function to add contacts to an addressbook.
* This only happens when an admin creates new users
* @param array $contacts array with userid of ownCloud users
* @param string $addressBookId
* @return bool
*/
2014-03-31 15:29:16 +02:00
private function addContacts($contacts, $addressbookid){
2014-04-01 21:08:29 +02:00
foreach($contacts as $user){
2014-04-01 21:25:41 +02:00
try{
$sql = 'INSERT INTO ' . $this->cardsTableName . ' ('
2014-04-01 21:08:29 +02:00
. 'id, '
. 'addressbookid, '
. 'fullname, '
. 'carddata, '
. 'lastmodified'
. ') VALUES ('
. '?,'
. '?,'
. '?,'
. '?,'
. '?'
. ')';
2014-04-01 21:25:41 +02:00
$query = \OCP\DB::prepare($sql);
2014-04-02 18:54:09 +02:00
$vcard = \Sabre\VObject\Component::create('VCARD');
$vcard->FN = \OCP\User::getDisplayName($user);
$now = new \DateTime('now');
$vcard->REV = $now->format(\DateTime::W3C);
$appinfo = \OCP\App::getAppInfo('contacts');
$appversion = \OCP\App::getAppVersion('contacts');
$prodid = '-//ownCloud//NONSGML ' . $appinfo['name'] . ' ' . $appversion.'//EN';
$vcard->PRODID = $prodid;
2014-04-06 08:21:24 +02:00
$vcard->add('IMPP', 'x-owncloud-handle:' . $user, array("X-SERVICE-TYPE" => array("owncloud-handle")));
2014-04-02 18:54:09 +02:00
$result = $query->execute(array($user, $this->userid, \OCP\User::getDisplayName($user), $vcard->serialize(), time()));
2014-04-01 21:25:41 +02:00
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return false;
} else {
// All done
// now update the index table with all the properties
$this->updateIndex($user, $vcard);
2014-04-01 21:25:41 +02:00
return true;
}
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.' exception: '
. $e->getMessage(), \OCP\Util::ERROR);
return false;
}
2014-04-01 21:08:29 +02:00
}
2014-03-31 15:29:16 +02:00
}
2014-04-06 08:39:58 +02:00
2014-04-01 20:48:00 +02:00
/**
* Help function to remove contacts from an addressbook.
* This only happens when an admin remove an ownCloud user
* @param array $contacts array with userid of ownCloud users
* @param string $addressBookId
* @return bool
*/
2014-03-31 15:29:16 +02:00
private function removeContacts($contacts, $addressbookid){
2014-04-01 21:08:29 +02:00
foreach($contacts as $user){
2014-04-03 18:59:34 +02:00
try{
2014-04-02 21:49:53 +02:00
$sql = 'DELETE FROM ' . $this->cardsTableName . ' WHERE addressbookid = ? AND id = ?';
2014-04-01 21:25:41 +02:00
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($this->userid, $user));
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return false;
} else {
return true;
}
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.' exception: '
. $e->getMessage(), \OCP\Util::ERROR);
return false;
}
2014-04-01 21:08:29 +02:00
}
2014-03-29 11:54:08 +01:00
}
2014-04-01 21:08:29 +02:00
2014-04-01 20:48:00 +02:00
/**
* @inheritdoc
*/
2014-03-31 15:29:16 +02:00
public function updateContact($addressBookId, $id, $contact, array $options = array()) {
2014-04-01 21:08:29 +02:00
$updateRevision = true;
$isCardDAV = false;
if (!$contact instanceof VCard) {
try {
$contact = Reader::read($contact);
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
return false;
}
}
if ($updateRevision || !isset($contact->REV)) {
$now = new \DateTime;
$contact->REV = $now->format(\DateTime::W3C);
}
2014-04-01 21:25:41 +02:00
try{
$sql = 'UPDATE ' . $this->cardsTableName
. ' SET '
. '`fullname` = ?, '
. '`carddata` = ?, '
. '`lastmodified` = ? '
. ' WHERE '
. '`id` = ? '
2014-04-02 21:44:03 +02:00
. 'AND `addressbookid` = ? ';
2014-04-01 21:25:41 +02:00
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($contact->FN, $contact->serialize(), time(), $id, $this->userid));
2014-04-01 21:25:41 +02:00
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return false;
} else {
// All done
// now update the indexes in the DB
$this->updateIndex($id, $contact);
2014-04-01 21:25:41 +02:00
return true;
}
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.' exception: '
. $e->getMessage(), \OCP\Util::ERROR);
return false;
}
2014-03-31 15:29:16 +02:00
}
2014-04-03 18:36:11 +02:00
2014-04-06 08:39:58 +02:00
/**
* This is a hack so backends can have different search functions.
* @return \OCA\Contacts\LocalUsersAddressbookProvider
*/
2014-04-03 18:36:11 +02:00
public function getSearchProvider(){
return new LocalUsersAddressbookProvider();
}
2014-04-06 08:39:58 +02:00
/**
* Updates the index table. All properties of a contact are stored in it.
* Needed for the search function.
* @param type $contactId
* @param type $vcard
* @return boolean
*/
private function updateIndex($contactId, $vcard){
// Utils\Properties::updateIndex($parameters['id'], $contact);
$this->purgeIndex($contactId);
$updatestmt = \OCP\DB::prepare('INSERT INTO `' . $this->indexTableName . '` '
. '(`addressbookid`, `contactid`,`name`,`value`,`preferred`) VALUES(?,?,?,?,?)');
// Insert all properties in the table
foreach($vcard->children as $property) {
if(!in_array($property->name, $this->indexProperties)) {
2014-04-06 08:39:58 +02:00
continue;
}
$preferred = 0;
foreach($property->parameters as $parameter) {
2014-04-06 08:39:58 +02:00
if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') {
$preferred = 1;
break;
}
}
try {
2014-04-06 08:39:58 +02:00
$result = $updatestmt->execute(
array(
\OCP\User::getUser(),
$contactId,
$property->name,
substr($property->value, 0, 254),
$preferred,
)
);
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return false;
2014-04-06 08:39:58 +02:00
}
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
return false;
}
}
}
2014-04-06 08:39:58 +02:00
/**
* Remove all indexes from the table.
* This is always called before adding new properties.
* @param type $contactId
* @param type $vcard
* @return boolean
*/
private function purgeIndex($id){
// Remove all indexes from the table
try {
$query = \OCP\DB::prepare('DELETE FROM `' . $this->indexTableName . '`'
. ' WHERE `contactid` = ?');
$query->execute(array($id));
} catch(\Exception $e) {
return false;
}
}
2014-03-29 11:54:08 +01:00
}