2012-12-12 00:07:36 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - AddressbookProvider
|
|
|
|
*
|
|
|
|
* @author Thomas Tanghus
|
2014-01-26 00:40:22 +01:00
|
|
|
* @copyright 2012-2014 Thomas Tanghus (thomas@tanghus.net)
|
2012-12-12 00:07:36 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Contacts;
|
2014-07-22 12:42:59 +02:00
|
|
|
use OCA\Contacts\Utils\JSONSerializer;
|
2013-04-30 02:05:15 +02:00
|
|
|
use OCA\Contacts\Utils\Properties;
|
2014-07-22 12:42:59 +02:00
|
|
|
use OCA\Contacts\Utils\TemporaryPhoto;
|
|
|
|
use OCA\Contacts\VObject\VCard;
|
2012-12-12 00:07:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class manages our addressbooks.
|
2013-03-20 11:27:40 +01:00
|
|
|
* TODO: Port this to use the new backend
|
2012-12-12 00:07:36 +01:00
|
|
|
*/
|
|
|
|
class AddressbookProvider implements \OCP\IAddressBook {
|
|
|
|
|
|
|
|
const CONTACT_TABLE = '*PREFIX*contacts_cards';
|
|
|
|
const PROPERTY_TABLE = '*PREFIX*contacts_cards_properties';
|
2014-11-21 13:03:15 +01:00
|
|
|
const ADDRESSBOOK_TABLE = '*PREFIX*contacts_addressbooks';
|
2012-12-12 00:07:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Addressbook id
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
public $id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Addressbook info array
|
2013-04-30 02:05:15 +02:00
|
|
|
* @var AddressBook
|
2012-12-12 00:07:36 +01:00
|
|
|
*/
|
2013-04-30 02:05:15 +02:00
|
|
|
public $addressBook;
|
2012-12-12 00:07:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2014-04-17 04:59:14 +02:00
|
|
|
* @param AddressBook $addressBook
|
2012-12-12 00:07:36 +01:00
|
|
|
*/
|
2013-04-30 02:05:15 +02:00
|
|
|
public function __construct($addressBook) {
|
|
|
|
$this->addressBook = $addressBook;
|
2014-11-21 11:05:06 +01:00
|
|
|
$this->app = new App();
|
2012-12-12 00:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAddressbook() {
|
2013-04-30 02:05:15 +02:00
|
|
|
return $this->addressBook;
|
2012-12-12 00:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string defining the technical unique key
|
|
|
|
*/
|
|
|
|
public function getKey() {
|
2013-04-30 02:05:15 +02:00
|
|
|
$metaData = $this->addressBook->getMetaData();
|
|
|
|
return $metaData['backend'].':'.$metaData['id'];
|
2012-12-12 00:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In comparison to getKey() this function returns a human readable (maybe translated) name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getDisplayName() {
|
2013-04-30 02:05:15 +02:00
|
|
|
return $this->addressBook->getDisplayName();
|
2012-12-12 00:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getPermissions() {
|
2013-04-30 02:05:15 +02:00
|
|
|
return $this->addressBook->getPermissions();
|
2012-12-12 00:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $pattern
|
|
|
|
* @param $searchProperties
|
|
|
|
* @param $options
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function search($pattern, $searchProperties, $options) {
|
2014-11-21 11:36:28 +01:00
|
|
|
$propTable = self::PROPERTY_TABLE;
|
|
|
|
$contTable = self::CONTACT_TABLE;
|
2014-11-21 13:03:15 +01:00
|
|
|
$addrTable = self::ADDRESSBOOK_TABLE;
|
2012-12-12 00:07:36 +01:00
|
|
|
$results = array();
|
2014-11-21 13:03:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This query will fetch all contacts which match the $searchProperties
|
|
|
|
* It will look up the addressbookid of the contact and the user id of the owner of the contact app
|
|
|
|
*/
|
2014-11-21 11:36:28 +01:00
|
|
|
$query = <<<SQL
|
|
|
|
SELECT
|
|
|
|
DISTINCT
|
|
|
|
`$propTable`.`contactid`,
|
2014-11-21 13:03:15 +01:00
|
|
|
`$contTable`.`addressbookid`,
|
|
|
|
`$addrTable`.`userid`
|
|
|
|
|
2014-11-21 11:36:28 +01:00
|
|
|
FROM
|
|
|
|
`$propTable`
|
|
|
|
INNER JOIN
|
|
|
|
`$contTable`
|
|
|
|
ON `$contTable`.`id` = `$propTable`.`contactid`
|
2014-11-21 13:03:15 +01:00
|
|
|
INNER JOIN `$addrTable`
|
|
|
|
ON `$addrTable`.id = `$contTable`.addressbookid
|
2014-11-21 11:36:28 +01:00
|
|
|
WHERE
|
2015-03-22 04:14:55 +01:00
|
|
|
(`$contTable`.addressbookid = ?) AND
|
|
|
|
(
|
2014-11-21 11:36:28 +01:00
|
|
|
SQL;
|
|
|
|
|
2014-11-21 13:03:15 +01:00
|
|
|
$params = array();
|
2015-03-22 04:14:55 +01:00
|
|
|
$params[] = $this->addressBook->getMetaData()['id'];
|
2014-11-21 11:36:28 +01:00
|
|
|
foreach ($searchProperties as $property) {
|
2013-03-25 21:34:18 +01:00
|
|
|
$params[] = $property;
|
|
|
|
$params[] = '%' . $pattern . '%';
|
|
|
|
$query .= '(`name` = ? AND `value` LIKE ?) OR ';
|
2012-12-12 00:07:36 +01:00
|
|
|
}
|
|
|
|
$query = substr($query, 0, strlen($query) - 4);
|
|
|
|
$query .= ')';
|
|
|
|
|
|
|
|
$stmt = \OCP\DB::prepare($query);
|
2013-03-25 21:34:18 +01:00
|
|
|
$result = $stmt->execute($params);
|
2013-09-16 02:24:08 +02:00
|
|
|
if (\OCP\DB::isError($result)) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result),
|
2012-12-12 00:07:36 +01:00
|
|
|
\OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-21 11:36:28 +01:00
|
|
|
while ($row = $result->fetchRow()) {
|
2014-11-21 11:06:54 +01:00
|
|
|
$id = $row['contactid'];
|
2014-11-21 11:36:28 +01:00
|
|
|
$addressbookKey = $row['addressbookid'];
|
2014-11-21 13:03:15 +01:00
|
|
|
// Check if we are the owner of the contact
|
2015-02-16 18:42:41 +01:00
|
|
|
if ($row['userid'] !== \OC::$server->getUserSession()->getUser()->getUId()) {
|
2014-11-21 13:03:15 +01:00
|
|
|
// we aren't the owner of the contact
|
|
|
|
try {
|
|
|
|
// it is possible that the contact is shared with us
|
|
|
|
// if so, $contact will be an object
|
|
|
|
// if not getContact will throw an Exception
|
2014-11-21 11:06:54 +01:00
|
|
|
$contact = $this->app->getContact('shared', $addressbookKey, $id);
|
2014-11-21 13:03:15 +01:00
|
|
|
} catch (\Exception $e){
|
|
|
|
// the contact isn't shared with us
|
|
|
|
$contact = null;
|
2014-07-22 12:42:59 +02:00
|
|
|
}
|
2014-11-21 13:03:15 +01:00
|
|
|
} else {
|
|
|
|
// We are the owner of the contact
|
|
|
|
// thus we can easily fetch it
|
|
|
|
$contact = $this->app->getContact('local', $addressbookKey, $id);
|
2014-07-22 12:42:59 +02:00
|
|
|
}
|
2014-11-21 13:03:15 +01:00
|
|
|
if ($contact !== null) {
|
|
|
|
$j = JSONSerializer::serializeContact($contact);
|
|
|
|
$j['data']['id'] = $id;
|
|
|
|
if (isset($contact->PHOTO)) {
|
|
|
|
$url = \OCP\Util::linkToRoute('contacts_contact_photo',
|
|
|
|
array(
|
|
|
|
'backend' => $contact->getBackend()->name,
|
|
|
|
'addressBookId' => $addressbookKey,
|
|
|
|
'contactId' => $contact->getId()
|
|
|
|
));
|
|
|
|
$url = \OC_Helper::makeURLAbsolute($url);
|
|
|
|
$j['data']['PHOTO'] = "VALUE=uri:$url";
|
|
|
|
}
|
|
|
|
$results[] = $this->convertToSearchResult($j);
|
2014-11-21 11:06:54 +01:00
|
|
|
}
|
2012-12-12 00:07:36 +01:00
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $properties
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function createOrUpdate($properties) {
|
2014-11-19 11:26:02 +01:00
|
|
|
$addressBook = $this->getAddressbook();
|
2012-12-12 00:07:36 +01:00
|
|
|
|
2014-12-07 14:01:39 +01:00
|
|
|
if(array_key_exists('id', $properties)) {
|
|
|
|
// we must "update" the contact by replacing the entire data set
|
|
|
|
$id = $properties['id'];
|
|
|
|
$contact = $this->addressBook->getChild($properties['id']);
|
|
|
|
foreach(array_keys($properties) as $name) {
|
|
|
|
if(isset($contact->{$name})) {
|
|
|
|
unset($contact->{$name});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// the contact doesn't exist
|
|
|
|
// we must create a new one
|
|
|
|
try {
|
|
|
|
$id = $addressBook->addChild();
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
}
|
|
|
|
$contact = $addressBook->getChild($id);
|
2014-11-19 11:26:02 +01:00
|
|
|
}
|
2014-11-17 15:26:14 +01:00
|
|
|
|
2014-11-19 11:26:02 +01:00
|
|
|
foreach($properties as $name => $value) {
|
2014-12-07 09:22:13 +01:00
|
|
|
$contact->setPropertyByName($name, $value);
|
2012-12-12 00:07:36 +01:00
|
|
|
}
|
2014-11-19 11:26:02 +01:00
|
|
|
$contact->save();
|
2014-11-19 11:29:22 +01:00
|
|
|
return $contact;
|
2012-12-12 00:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function delete($id) {
|
|
|
|
try {
|
2013-12-30 14:10:22 +01:00
|
|
|
$query = 'SELECT COUNT(*) as `count` FROM `*PREFIX*contacts_cards` WHERE `id` = ? AND `addressbookid` = ?';
|
2012-12-12 00:07:36 +01:00
|
|
|
$stmt = \OCP\DB::prepare($query);
|
|
|
|
$result = $stmt->execute(array($id, $this->id));
|
2013-09-16 02:24:08 +02:00
|
|
|
if (\OCP\DB::isError($result)) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result),
|
2012-12-12 00:07:36 +01:00
|
|
|
\OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-30 14:10:22 +01:00
|
|
|
if((int)$result['count'] === 0) {
|
2013-09-16 02:24:08 +02:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__
|
2014-11-19 11:26:02 +01:00
|
|
|
. 'Contact with id ' . $id . 'doesn\'t belong to addressbook with id ' . $this->id,
|
2012-12-12 00:07:36 +01:00
|
|
|
\OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch(\Exception $e) {
|
2014-11-19 11:26:02 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__ . ', exception: ' . $e->getMessage(),
|
2012-12-12 00:07:36 +01:00
|
|
|
\OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return VCard::delete($id);
|
|
|
|
}
|
2014-07-30 17:05:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $j
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function convertToSearchResult($j) {
|
|
|
|
$data = $j['data'];
|
|
|
|
$result = array();
|
|
|
|
foreach( $data as $key => $d) {
|
|
|
|
$d = $data[$key];
|
|
|
|
if (in_array($key, Properties::$multiProperties)) {
|
|
|
|
$result[$key] = array_map(function($v){
|
|
|
|
return $v['value'];
|
|
|
|
}, $d);
|
|
|
|
} else {
|
|
|
|
if (is_array($d)) {
|
|
|
|
$result[$key] = $d[0]['value'];
|
|
|
|
} else {
|
|
|
|
$result[$key] = $d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2014-11-21 11:05:06 +01:00
|
|
|
|
2012-12-12 21:39:58 +01:00
|
|
|
}
|