2011-09-17 00:26:57 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - Addressbook
|
|
|
|
*
|
|
|
|
* @author Jakob Sack
|
|
|
|
* @copyright 2011 Jakob Sack mail@jakobsack.de
|
2012-03-07 16:39:56 +01:00
|
|
|
* @copyright 2012 Thomas Tanghus <thomas@tanghus.net>
|
2011-09-17 00:26:57 +02: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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* The following SQL statement is just a help for developers and will not be
|
|
|
|
* executed!
|
|
|
|
*
|
|
|
|
* CREATE TABLE contacts_cards (
|
|
|
|
* id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
|
|
|
* addressbookid INT(11) UNSIGNED NOT NULL,
|
|
|
|
* fullname VARCHAR(255),
|
|
|
|
* carddata TEXT,
|
|
|
|
* uri VARCHAR(100),
|
|
|
|
* lastmodified INT(11) UNSIGNED
|
|
|
|
* );
|
|
|
|
*/
|
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
namespace OCA\Contacts;
|
|
|
|
|
2012-11-22 00:14:03 +01:00
|
|
|
use Sabre\VObject;
|
|
|
|
|
2011-09-17 00:26:57 +02:00
|
|
|
/**
|
|
|
|
* This class manages our vCards
|
|
|
|
*/
|
2012-10-25 03:34:12 +02:00
|
|
|
class VCard {
|
2011-09-17 00:26:57 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns all cards of an address book
|
|
|
|
* @param integer $id
|
2012-12-10 05:52:57 +01:00
|
|
|
* @param integer $offset
|
|
|
|
* @param integer $limit
|
|
|
|
* @param array $fields An array of the fields to return. Defaults to all.
|
2012-06-26 17:54:00 +02:00
|
|
|
* @return array|false
|
2011-09-17 00:26:57 +02:00
|
|
|
*
|
|
|
|
* The cards are associative arrays. You'll find the original vCard in
|
|
|
|
* ['carddata']
|
|
|
|
*/
|
2012-12-10 05:52:57 +01:00
|
|
|
public static function all($id, $offset=null, $limit=null, $fields = array()) {
|
2012-02-14 02:15:21 +01:00
|
|
|
$result = null;
|
2012-12-10 05:52:57 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.'count fields:' . count($fields), \OCP\Util::DEBUG);
|
|
|
|
$qfields = count($fields) > 0
|
|
|
|
? '`' . implode('`,`', $fields) . '`'
|
|
|
|
: '*';
|
2012-06-26 17:54:00 +02:00
|
|
|
if(is_array($id) && count($id)) {
|
2011-12-30 21:07:16 +01:00
|
|
|
$id_sql = join(',', array_fill(0, count($id), '?'));
|
2012-12-10 05:52:57 +01:00
|
|
|
$sql = 'SELECT ' . $qfields . ' FROM `*PREFIX*contacts_cards` WHERE `addressbookid` IN ('.$id_sql.') ORDER BY `fullname`';
|
2011-12-30 21:07:16 +01:00
|
|
|
try {
|
2012-12-10 05:52:57 +01:00
|
|
|
$stmt = \OCP\DB::prepare($sql, $limit, $offset);
|
2011-12-30 21:07:16 +01:00
|
|
|
$result = $stmt->execute($id);
|
2012-10-31 21:37:49 +01:00
|
|
|
if (\OC_DB::isError($result)) {
|
2012-11-15 00:27:09 +01:00
|
|
|
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
|
2012-10-31 21:37:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: ' . $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', ids: ' . join(',', $id), \OCP\Util::DEBUG);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.'SQL:' . $sql, \OCP\Util::DEBUG);
|
2012-06-26 17:54:00 +02:00
|
|
|
return false;
|
2012-07-08 15:41:38 +02:00
|
|
|
}
|
2012-06-26 17:54:00 +02:00
|
|
|
} elseif(is_int($id) || is_string($id)) {
|
2012-02-14 02:15:21 +01:00
|
|
|
try {
|
2012-12-10 05:52:57 +01:00
|
|
|
$sql = 'SELECT ' . $qfields . ' FROM `*PREFIX*contacts_cards` WHERE `addressbookid` = ? ORDER BY `fullname`';
|
2013-02-08 20:14:54 +01:00
|
|
|
$stmt = \OCP\DB::prepare($sql, $limit, $offset);
|
2012-02-14 02:15:21 +01:00
|
|
|
$result = $stmt->execute(array($id));
|
2012-10-31 21:37:49 +01:00
|
|
|
if (\OC_DB::isError($result)) {
|
2012-11-15 00:27:09 +01:00
|
|
|
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
|
2012-10-31 21:37:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', ids: '. $id, \OCP\Util::DEBUG);
|
2012-06-26 17:54:00 +02:00
|
|
|
return false;
|
2012-02-14 02:15:21 +01:00
|
|
|
}
|
2012-07-08 15:41:38 +02:00
|
|
|
} else {
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__
|
|
|
|
. '. Addressbook id(s) argument is empty: '
|
|
|
|
. print_r($id, true), \OCP\Util::DEBUG);
|
2012-06-26 17:54:00 +02:00
|
|
|
return false;
|
2011-12-30 21:07:16 +01:00
|
|
|
}
|
2011-12-09 15:10:51 +01:00
|
|
|
$cards = array();
|
2012-02-14 02:15:21 +01:00
|
|
|
if(!is_null($result)) {
|
2012-08-30 14:42:58 +02:00
|
|
|
while( $row = $result->fetchRow()) {
|
2012-02-14 02:15:21 +01:00
|
|
|
$cards[] = $row;
|
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
}
|
|
|
|
|
2011-12-09 15:10:51 +01:00
|
|
|
return $cards;
|
2011-09-17 00:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns a card
|
|
|
|
* @param integer $id
|
2012-12-10 05:52:57 +01:00
|
|
|
* @param array $fields An array of the fields to return. Defaults to all.
|
2012-06-26 17:54:00 +02:00
|
|
|
* @return associative array or false.
|
2011-09-17 00:26:57 +02:00
|
|
|
*/
|
2012-12-10 05:52:57 +01:00
|
|
|
public static function find($id, $fields = array() ) {
|
2012-06-26 17:54:00 +02:00
|
|
|
try {
|
2012-12-10 05:52:57 +01:00
|
|
|
$qfields = count($fields) > 0
|
|
|
|
? '`' . implode('`,`', $fields) . '`'
|
|
|
|
: '*';
|
|
|
|
$stmt = \OCP\DB::prepare( 'SELECT ' . $qfields . ' FROM `*PREFIX*contacts_cards` WHERE `id` = ?' );
|
2012-06-26 17:54:00 +02:00
|
|
|
$result = $stmt->execute(array($id));
|
2012-10-31 21:37:49 +01:00
|
|
|
if (\OC_DB::isError($result)) {
|
2012-11-15 00:27:09 +01:00
|
|
|
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
|
2012-10-31 21:37:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', id: '. $id, \OCP\Util::DEBUG);
|
2012-06-26 17:54:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
|
|
|
|
return $result->fetchRow();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief finds a card by its DAV Data
|
|
|
|
* @param integer $aid Addressbook id
|
|
|
|
* @param string $uri the uri ('filename')
|
2012-06-26 17:54:00 +02:00
|
|
|
* @return associative array or false.
|
2011-09-17 00:26:57 +02:00
|
|
|
*/
|
2012-11-26 20:35:37 +01:00
|
|
|
public static function findWhereDAVDataIs($aid, $uri) {
|
2012-06-26 17:54:00 +02:00
|
|
|
try {
|
2012-10-25 03:34:12 +02:00
|
|
|
$stmt = \OCP\DB::prepare( 'SELECT * FROM `*PREFIX*contacts_cards` WHERE `addressbookid` = ? AND `uri` = ?' );
|
2012-06-26 17:54:00 +02:00
|
|
|
$result = $stmt->execute(array($aid,$uri));
|
2012-10-31 21:37:49 +01:00
|
|
|
if (\OC_DB::isError($result)) {
|
2012-11-15 00:27:09 +01:00
|
|
|
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
|
2012-10-31 21:37:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', aid: '.$aid.' uri'.$uri, \OCP\Util::DEBUG);
|
2012-06-26 17:54:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
|
|
|
|
return $result->fetchRow();
|
|
|
|
}
|
|
|
|
|
2012-07-30 12:28:10 +02:00
|
|
|
/**
|
2012-02-21 23:19:13 +01:00
|
|
|
* @brief Format property TYPE parameters for upgrading from v. 2.1
|
|
|
|
* @param $property Reference to a Sabre_VObject_Property.
|
|
|
|
* In version 2.1 e.g. a phone can be formatted like: TEL;HOME;CELL:123456789
|
|
|
|
* This has to be changed to either TEL;TYPE=HOME,CELL:123456789 or TEL;TYPE=HOME;TYPE=CELL:123456789 - both are valid.
|
|
|
|
*/
|
|
|
|
public static function formatPropertyTypes(&$property) {
|
2012-09-07 15:21:03 +02:00
|
|
|
foreach($property->parameters as $key=>&$parameter) {
|
2012-10-25 03:34:12 +02:00
|
|
|
$types = App::getTypesOfProperty($property->name);
|
2012-02-21 23:19:13 +01:00
|
|
|
if(is_array($types) && in_array(strtoupper($parameter->name), array_keys($types)) || strtoupper($parameter->name) == 'PREF') {
|
2012-10-30 07:19:56 +01:00
|
|
|
$property->parameters[] = new \Sabre\VObject\Parameter('TYPE', $parameter->name);
|
2012-02-21 23:19:13 +01:00
|
|
|
}
|
|
|
|
unset($property->parameters[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 12:28:10 +02:00
|
|
|
/**
|
2012-02-21 23:19:13 +01:00
|
|
|
* @brief Decode properties for upgrading from v. 2.1
|
|
|
|
* @param $property Reference to a Sabre_VObject_Property.
|
|
|
|
* The only encoding allowed in version 3.0 is 'b' for binary. All encoded strings
|
|
|
|
* must therefor be decoded and the parameters removed.
|
|
|
|
*/
|
|
|
|
public static function decodeProperty(&$property) {
|
|
|
|
// Check out for encoded string and decode them :-[
|
2012-09-07 15:21:03 +02:00
|
|
|
foreach($property->parameters as $key=>&$parameter) {
|
2012-02-21 23:19:13 +01:00
|
|
|
if(strtoupper($parameter->name) == 'ENCODING') {
|
|
|
|
if(strtoupper($parameter->value) == 'QUOTED-PRINTABLE') { // what kind of other encodings could be used?
|
2012-09-03 17:11:03 +02:00
|
|
|
// Decode quoted-printable and strip any control chars
|
|
|
|
// except \n and \r
|
|
|
|
$property->value = preg_replace(
|
2012-09-07 14:32:45 +02:00
|
|
|
'/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/',
|
|
|
|
'',
|
2012-09-03 17:11:03 +02:00
|
|
|
quoted_printable_decode($property->value)
|
|
|
|
);
|
2012-02-21 23:19:13 +01:00
|
|
|
unset($property->parameters[$key]);
|
|
|
|
}
|
|
|
|
} elseif(strtoupper($parameter->name) == 'CHARSET') {
|
|
|
|
unset($property->parameters[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-24 11:35:56 +02:00
|
|
|
* @brief Checks if a contact with the same UID already exist in the address book.
|
|
|
|
* @param $aid Address book ID.
|
|
|
|
* @param $uid UID (passed by reference).
|
|
|
|
* @returns true if the UID has been changed.
|
|
|
|
*/
|
|
|
|
protected static function trueUID($aid, &$uid) {
|
2012-10-25 03:34:12 +02:00
|
|
|
$stmt = \OCP\DB::prepare( 'SELECT * FROM `*PREFIX*contacts_cards` WHERE `addressbookid` = ? AND `uri` = ?' );
|
2012-04-24 11:35:56 +02:00
|
|
|
$uri = $uid.'.vcf';
|
2012-06-26 17:54:00 +02:00
|
|
|
try {
|
|
|
|
$result = $stmt->execute(array($aid,$uri));
|
2012-10-31 21:37:49 +01:00
|
|
|
if (\OC_DB::isError($result)) {
|
2012-11-15 00:27:09 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
|
2012-10-31 21:37:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-12 22:14:05 +01:00
|
|
|
} catch(\Exception $e) {
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', aid: '.$aid.' uid'.$uid, \OCP\Util::DEBUG);
|
2012-06-26 17:54:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-20 00:17:59 +02:00
|
|
|
if($result->numRows() > 0) {
|
2012-04-24 11:35:56 +02:00
|
|
|
while(true) {
|
2012-07-20 00:17:59 +02:00
|
|
|
$tmpuid = substr(md5(rand().time()), 0, 10);
|
2012-04-24 11:35:56 +02:00
|
|
|
$uri = $tmpuid.'.vcf';
|
2012-07-20 00:17:59 +02:00
|
|
|
$result = $stmt->execute(array($aid, $uri));
|
|
|
|
if($result->numRows() > 0) {
|
2012-04-24 11:35:56 +02:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
$uid = $tmpuid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Tries to update imported VCards to adhere to rfc2426 (VERSION: 3.0) and add mandatory fields if missing.
|
|
|
|
* @param aid Address book id.
|
2012-11-22 00:14:03 +01:00
|
|
|
* @param vcard A Sabre\VObject\Component of type VCARD (passed by reference).
|
2012-02-21 23:19:13 +01:00
|
|
|
*/
|
2012-04-24 11:35:56 +02:00
|
|
|
protected static function updateValuesFromAdd($aid, &$vcard) { // any suggestions for a better method name? ;-)
|
2012-02-21 23:19:13 +01:00
|
|
|
$stringprops = array('N', 'FN', 'ORG', 'NICK', 'ADR', 'NOTE');
|
|
|
|
$typeprops = array('ADR', 'TEL', 'EMAIL');
|
|
|
|
$upgrade = false;
|
2012-04-12 22:38:34 +02:00
|
|
|
$fn = $n = $uid = $email = $org = null;
|
2012-11-22 00:14:03 +01:00
|
|
|
$version = isset($vcard->VERSION) ? $vcard->VERSION : null;
|
2012-02-21 23:19:13 +01:00
|
|
|
// Add version if needed
|
|
|
|
if($version && $version < '3.0') {
|
|
|
|
$upgrade = true;
|
2012-10-25 03:34:12 +02:00
|
|
|
//OCP\Util::writeLog('contacts', 'OCA\Contacts\VCard::updateValuesFromAdd. Updating from version: '.$version, OCP\Util::DEBUG);
|
2012-02-21 23:19:13 +01:00
|
|
|
}
|
2012-09-07 15:21:03 +02:00
|
|
|
foreach($vcard->children as &$property) {
|
2012-02-21 23:19:13 +01:00
|
|
|
// Decode string properties and remove obsolete properties.
|
|
|
|
if($upgrade && in_array($property->name, $stringprops)) {
|
|
|
|
self::decodeProperty($property);
|
|
|
|
}
|
2012-10-25 18:04:57 +02:00
|
|
|
if(function_exists('iconv')) {
|
|
|
|
$property->value = str_replace("\r\n", "\n", iconv(mb_detect_encoding($property->value, 'UTF-8, ISO-8859-1'), 'utf-8', $property->value));
|
2012-10-25 23:09:39 +02:00
|
|
|
} else {
|
|
|
|
$property->value = str_replace("\r\n", "\n", mb_convert_encoding($property->value, 'UTF-8', mb_detect_encoding($property->value, 'UTF-8, ISO-8859-1'), $property->value));
|
2012-10-25 18:04:57 +02:00
|
|
|
}
|
2012-05-21 21:49:35 +02:00
|
|
|
if(in_array($property->name, $stringprops)) {
|
|
|
|
$property->value = strip_tags($property->value);
|
|
|
|
}
|
2012-02-21 23:19:13 +01:00
|
|
|
// Fix format of type parameters.
|
|
|
|
if($upgrade && in_array($property->name, $typeprops)) {
|
2012-10-25 03:34:12 +02:00
|
|
|
//OCP\Util::writeLog('contacts', 'OCA\Contacts\VCard::updateValuesFromAdd. before: '.$property->serialize(), OCP\Util::DEBUG);
|
2012-02-21 23:19:13 +01:00
|
|
|
self::formatPropertyTypes($property);
|
2012-10-25 03:34:12 +02:00
|
|
|
//OCP\Util::writeLog('contacts', 'OCA\Contacts\VCard::updateValuesFromAdd. after: '.$property->serialize(), OCP\Util::DEBUG);
|
2012-02-21 23:19:13 +01:00
|
|
|
}
|
2012-07-20 00:17:59 +02:00
|
|
|
if($property->name == 'FN') {
|
2012-02-21 23:19:13 +01:00
|
|
|
$fn = $property->value;
|
|
|
|
}
|
2012-11-06 01:36:27 +01:00
|
|
|
else if($property->name == 'N') {
|
2012-02-21 23:19:13 +01:00
|
|
|
$n = $property->value;
|
|
|
|
}
|
2012-11-06 01:36:27 +01:00
|
|
|
else if($property->name == 'UID') {
|
2012-02-21 23:19:13 +01:00
|
|
|
$uid = $property->value;
|
|
|
|
}
|
2012-11-06 01:36:27 +01:00
|
|
|
else if($property->name == 'ORG') {
|
2012-03-07 21:29:23 +01:00
|
|
|
$org = $property->value;
|
|
|
|
}
|
2012-11-06 01:36:27 +01:00
|
|
|
else if($property->name == 'EMAIL' && is_null($email)) { // only use the first email as substitute for missing N or FN.
|
2012-02-21 23:19:13 +01:00
|
|
|
$email = $property->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check for missing 'N', 'FN' and 'UID' properties
|
|
|
|
if(!$fn) {
|
2012-07-20 00:17:59 +02:00
|
|
|
if($n && $n != ';;;;') {
|
2012-02-21 23:19:13 +01:00
|
|
|
$fn = join(' ', array_reverse(array_slice(explode(';', $n), 0, 2)));
|
|
|
|
} elseif($email) {
|
|
|
|
$fn = $email;
|
2012-03-07 21:29:23 +01:00
|
|
|
} elseif($org) {
|
|
|
|
$fn = $org;
|
2012-02-21 23:19:13 +01:00
|
|
|
} else {
|
|
|
|
$fn = 'Unknown Name';
|
|
|
|
}
|
2012-11-22 00:14:03 +01:00
|
|
|
$vcard->FN = $fn;
|
2012-10-25 03:34:12 +02:00
|
|
|
//OCP\Util::writeLog('contacts', 'OCA\Contacts\VCard::updateValuesFromAdd. Added missing \'FN\' field: '.$fn, OCP\Util::DEBUG);
|
2012-02-21 23:19:13 +01:00
|
|
|
}
|
2012-07-20 00:17:59 +02:00
|
|
|
if(!$n || $n == ';;;;') { // Fix missing 'N' field. Ugly hack ahead ;-)
|
2012-02-21 23:19:13 +01:00
|
|
|
$slice = array_reverse(array_slice(explode(' ', $fn), 0, 2)); // Take 2 first name parts of 'FN' and reverse.
|
|
|
|
if(count($slice) < 2) { // If not enought, add one more...
|
|
|
|
$slice[] = "";
|
|
|
|
}
|
|
|
|
$n = implode(';', $slice).';;;';
|
2012-11-22 00:14:03 +01:00
|
|
|
$vcard->N = $n;
|
2012-10-25 03:34:12 +02:00
|
|
|
//OCP\Util::writeLog('contacts', 'OCA\Contacts\VCard::updateValuesFromAdd. Added missing \'N\' field: '.$n, OCP\Util::DEBUG);
|
2012-02-21 23:19:13 +01:00
|
|
|
}
|
|
|
|
if(!$uid) {
|
2012-11-22 00:14:03 +01:00
|
|
|
$uid = substr(md5(rand().time()), 0, 10);
|
|
|
|
$vcard->add('UID', $uid);
|
2012-10-25 03:34:12 +02:00
|
|
|
//OCP\Util::writeLog('contacts', 'OCA\Contacts\VCard::updateValuesFromAdd. Added missing \'UID\' field: '.$uid, OCP\Util::DEBUG);
|
2012-02-21 23:19:13 +01:00
|
|
|
}
|
2012-04-24 11:35:56 +02:00
|
|
|
if(self::trueUID($aid, $uid)) {
|
2012-11-22 00:14:03 +01:00
|
|
|
$vcard->{'UID'} = $uid;
|
2012-04-24 11:35:56 +02:00
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
$now = new \DateTime;
|
2012-11-22 00:14:03 +01:00
|
|
|
$vcard->{'REV'} = $now->format(\DateTime::W3C);
|
2012-02-21 23:19:13 +01:00
|
|
|
}
|
|
|
|
|
2011-09-17 00:26:57 +02:00
|
|
|
/**
|
|
|
|
* @brief Adds a card
|
2012-08-09 17:03:46 +02:00
|
|
|
* @param $aid integer Addressbook id
|
2012-11-22 00:14:03 +01:00
|
|
|
* @param $card Sabre\VObject\Component vCard file
|
2012-08-09 17:03:46 +02:00
|
|
|
* @param $uri string the uri of the card, default based on the UID
|
|
|
|
* @param $isChecked boolean If the vCard should be checked for validity and version.
|
2012-06-26 17:54:00 +02:00
|
|
|
* @return insertid on success or false.
|
2011-09-17 00:26:57 +02:00
|
|
|
*/
|
2012-11-22 00:14:03 +01:00
|
|
|
public static function add($aid, VObject\Component $card, $uri=null, $isChecked=false) {
|
2012-07-20 00:17:59 +02:00
|
|
|
if(is_null($card)) {
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__ . ', No vCard supplied', \OCP\Util::ERROR);
|
2012-03-07 21:27:03 +01:00
|
|
|
return null;
|
2011-09-17 00:26:57 +02:00
|
|
|
};
|
2012-10-25 03:34:12 +02:00
|
|
|
$addressbook = Addressbook::find($aid);
|
|
|
|
if ($addressbook['userid'] != \OCP\User::getUser()) {
|
|
|
|
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $aid);
|
2012-11-12 00:46:15 +01:00
|
|
|
if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & \OCP\PERMISSION_CREATE)) {
|
2012-10-25 03:34:12 +02:00
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t(
|
2012-09-01 22:55:19 +02:00
|
|
|
'You do not have the permissions to add contacts to this addressbook.'
|
|
|
|
)
|
|
|
|
);
|
2012-08-09 03:43:02 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-09 17:03:46 +02:00
|
|
|
if(!$isChecked) {
|
2012-05-10 16:53:25 +02:00
|
|
|
self::updateValuesFromAdd($aid, $card);
|
|
|
|
}
|
2012-11-22 00:14:03 +01:00
|
|
|
$card->{'VERSION'} = '3.0';
|
2012-05-11 01:13:19 +02:00
|
|
|
// Add product ID is missing.
|
2012-11-22 00:14:03 +01:00
|
|
|
//$prodid = trim($card->getAsString('PRODID'));
|
|
|
|
//if(!$prodid) {
|
|
|
|
if(!isset($card->PRODID)) {
|
2012-10-25 03:34:12 +02:00
|
|
|
$appinfo = \OCP\App::getAppInfo('contacts');
|
|
|
|
$appversion = \OCP\App::getAppVersion('contacts');
|
2012-05-11 01:13:19 +02:00
|
|
|
$prodid = '-//ownCloud//NONSGML '.$appinfo['name'].' '.$appversion.'//EN';
|
2012-11-22 00:14:03 +01:00
|
|
|
$card->add('PRODID', $prodid);
|
2012-05-11 01:13:19 +02:00
|
|
|
}
|
|
|
|
|
2012-11-22 00:14:03 +01:00
|
|
|
$fn = isset($card->FN) ? $card->FN : '';
|
2012-03-07 21:27:03 +01:00
|
|
|
|
2012-11-22 00:14:03 +01:00
|
|
|
$uri = isset($uri) ? $uri : $card->UID . '.vcf';
|
2012-03-07 21:27:03 +01:00
|
|
|
|
|
|
|
$data = $card->serialize();
|
2012-10-25 03:34:12 +02:00
|
|
|
$stmt = \OCP\DB::prepare( 'INSERT INTO `*PREFIX*contacts_cards` (`addressbookid`,`fullname`,`carddata`,`uri`,`lastmodified`) VALUES(?,?,?,?,?)' );
|
2012-06-26 17:54:00 +02:00
|
|
|
try {
|
2012-10-31 21:37:49 +01:00
|
|
|
$result = $stmt->execute(array($aid, $fn, $data, $uri, time()));
|
|
|
|
if (\OC_DB::isError($result)) {
|
2012-11-15 00:27:09 +01:00
|
|
|
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
|
2012-10-31 21:37:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', aid: '.$aid.' uri'.$uri, \OCP\Util::DEBUG);
|
2012-06-26 17:54:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
$newid = \OCP\DB::insertid('*PREFIX*contacts_cards');
|
|
|
|
App::loadCategoriesFromVCard($newid, $card);
|
2012-11-13 22:42:55 +01:00
|
|
|
App::updateDBProperties($newid, $card);
|
2012-10-25 03:34:12 +02:00
|
|
|
App::cacheThumbnail($newid);
|
2011-09-17 00:26:57 +02:00
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
Addressbook::touch($aid);
|
|
|
|
\OC_Hook::emit('\OCA\Contacts\VCard', 'post_createVCard', $newid);
|
2012-01-09 23:14:11 +01:00
|
|
|
return $newid;
|
2011-09-17 00:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Adds a card with the data provided by sabredav
|
|
|
|
* @param integer $id Addressbook id
|
|
|
|
* @param string $uri the uri the card will have
|
|
|
|
* @param string $data vCard file
|
2012-11-22 00:14:03 +01:00
|
|
|
* @returns integer|false insertid or false on error
|
2011-09-17 00:26:57 +02:00
|
|
|
*/
|
2012-10-25 03:34:12 +02:00
|
|
|
public static function addFromDAVData($id, $uri, $data) {
|
2012-11-22 00:14:03 +01:00
|
|
|
try {
|
2012-11-26 23:09:19 +01:00
|
|
|
$vcard = \Sabre\VObject\Reader::read($data);
|
2012-11-22 00:14:03 +01:00
|
|
|
return self::add($id, $vcard, $uri);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
}
|
|
|
|
|
2012-03-07 16:39:56 +01:00
|
|
|
/**
|
|
|
|
* @brief Mass updates an array of cards
|
|
|
|
* @param array $objects An array of [id, carddata].
|
|
|
|
*/
|
2012-09-07 15:21:03 +02:00
|
|
|
public static function updateDataByID($objects) {
|
2012-10-25 03:34:12 +02:00
|
|
|
$stmt = \OCP\DB::prepare( 'UPDATE `*PREFIX*contacts_cards` SET `carddata` = ?, `lastmodified` = ? WHERE `id` = ?' );
|
|
|
|
$now = new \DateTime;
|
2012-03-07 16:39:56 +01:00
|
|
|
foreach($objects as $object) {
|
2012-11-22 00:14:03 +01:00
|
|
|
$vcard = null;
|
|
|
|
try {
|
|
|
|
$vcard = Sabre\VObject\Reader::read($contact['carddata']);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OC_Log::write('contacts', __METHOD__. $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
}
|
2012-07-20 00:17:59 +02:00
|
|
|
if(!is_null($vcard)) {
|
2012-08-09 19:54:59 +02:00
|
|
|
$oldcard = self::find($object[0]);
|
|
|
|
if (!$oldcard) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-12 00:46:15 +01:00
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
$addressbook = Addressbook::find($oldcard['addressbookid']);
|
|
|
|
if ($addressbook['userid'] != \OCP\User::getUser()) {
|
|
|
|
$sharedContact = \OCP\Share::getItemSharedWithBySource('contact', $object[0], \OCP\Share::FORMAT_NONE, null, true);
|
2012-11-12 00:46:15 +01:00
|
|
|
if (!$sharedContact || !($sharedContact['permissions'] & \OCP\PERMISSION_UPDATE)) {
|
2012-08-09 19:54:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-22 00:14:03 +01:00
|
|
|
$vcard->{'REV'} = $now->format(\DateTime::W3C);
|
2012-03-07 16:39:56 +01:00
|
|
|
$data = $vcard->serialize();
|
|
|
|
try {
|
|
|
|
$result = $stmt->execute(array($data,time(),$object[0]));
|
2012-10-31 21:37:49 +01:00
|
|
|
if (\OC_DB::isError($result)) {
|
2012-11-15 00:27:09 +01:00
|
|
|
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
|
2012-10-31 21:37:49 +01:00
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
//OCP\Util::writeLog('contacts','OCA\Contacts\VCard::updateDataByID, id: '.$object[0].': '.$object[1],OCP\Util::DEBUG);
|
2013-03-12 22:14:05 +01:00
|
|
|
} catch(\Exception $e) {
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', id: '.$object[0], \OCP\Util::DEBUG);
|
2012-03-07 16:39:56 +01:00
|
|
|
}
|
2012-11-12 02:11:19 +01:00
|
|
|
App::updateDBProperties($object[0], $vcard);
|
2012-03-07 16:39:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-17 00:26:57 +02:00
|
|
|
/**
|
|
|
|
* @brief edits a card
|
|
|
|
* @param integer $id id of card
|
2012-11-22 00:14:03 +01:00
|
|
|
* @param Sabre\VObject\Component $card vCard file
|
2012-08-30 18:42:31 +02:00
|
|
|
* @return boolean true on success, otherwise an exception will be thrown
|
2011-09-17 00:26:57 +02:00
|
|
|
*/
|
2012-11-22 00:14:03 +01:00
|
|
|
public static function edit($id, VObject\Component $card) {
|
2011-09-17 00:26:57 +02:00
|
|
|
$oldcard = self::find($id);
|
2012-08-09 19:54:59 +02:00
|
|
|
if (!$oldcard) {
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', id: '
|
|
|
|
. $id . ' not found.', \OCP\Util::DEBUG);
|
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t(
|
2012-08-30 18:42:31 +02:00
|
|
|
'Could not find the vCard with ID.' . $id
|
|
|
|
)
|
|
|
|
);
|
2012-08-09 19:54:59 +02:00
|
|
|
}
|
2012-03-07 21:27:03 +01:00
|
|
|
if(is_null($card)) {
|
2012-01-11 06:20:06 +01:00
|
|
|
return false;
|
2011-09-17 00:26:57 +02:00
|
|
|
}
|
2012-08-30 18:42:31 +02:00
|
|
|
// NOTE: Owner checks are being made in the ajax files, which should be done
|
|
|
|
// inside the lib files to prevent any redundancies with sharing checks
|
2012-10-25 03:34:12 +02:00
|
|
|
$addressbook = Addressbook::find($oldcard['addressbookid']);
|
|
|
|
if ($addressbook['userid'] != \OCP\User::getUser()) {
|
|
|
|
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource(
|
|
|
|
'addressbook',
|
|
|
|
$oldcard['addressbookid'],
|
|
|
|
\OCP\Share::FORMAT_NONE, null, true);
|
|
|
|
$sharedContact = \OCP\Share::getItemSharedWithBySource('contact', $id, \OCP\Share::FORMAT_NONE, null, true);
|
2012-09-04 13:39:25 +02:00
|
|
|
$addressbook_permissions = 0;
|
|
|
|
$contact_permissions = 0;
|
|
|
|
if ($sharedAddressbook) {
|
|
|
|
$addressbook_permissions = $sharedAddressbook['permissions'];
|
|
|
|
}
|
|
|
|
if ($sharedContact) {
|
|
|
|
$contact_permissions = $sharedEvent['permissions'];
|
|
|
|
}
|
|
|
|
$permissions = max($addressbook_permissions, $contact_permissions);
|
2012-11-12 00:46:15 +01:00
|
|
|
if (!($permissions & \OCP\PERMISSION_UPDATE)) {
|
2012-10-25 03:34:12 +02:00
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t(
|
2012-08-30 18:42:31 +02:00
|
|
|
'You do not have the permissions to edit this contact.'
|
|
|
|
)
|
|
|
|
);
|
2012-08-09 19:54:59 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
App::loadCategoriesFromVCard($id, $card);
|
2012-03-07 21:49:46 +01:00
|
|
|
|
2012-11-22 00:14:03 +01:00
|
|
|
$fn = isset($card->FN) ? $card->FN : '';
|
2012-03-07 21:27:03 +01:00
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
$now = new \DateTime;
|
2012-12-07 14:53:11 +01:00
|
|
|
$card->REV = $now->format(\DateTime::W3C);
|
2011-09-17 00:26:57 +02:00
|
|
|
|
2012-03-07 21:27:03 +01:00
|
|
|
$data = $card->serialize();
|
2012-10-25 03:34:12 +02:00
|
|
|
$stmt = \OCP\DB::prepare( 'UPDATE `*PREFIX*contacts_cards` SET `fullname` = ?,`carddata` = ?, `lastmodified` = ? WHERE `id` = ?' );
|
2012-06-26 17:54:00 +02:00
|
|
|
try {
|
2012-10-31 21:37:49 +01:00
|
|
|
$result = $stmt->execute(array($fn, $data, time(), $id));
|
|
|
|
if (\OC_DB::isError($result)) {
|
2012-11-15 00:27:09 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
|
2012-10-31 21:37:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '
|
|
|
|
. $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', id'.$id, \OCP\Util::DEBUG);
|
2012-06-26 17:54:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
App::cacheThumbnail($oldcard['id']);
|
2012-11-12 02:11:19 +01:00
|
|
|
App::updateDBProperties($id, $card);
|
2012-10-25 03:34:12 +02:00
|
|
|
Addressbook::touch($oldcard['addressbookid']);
|
|
|
|
\OC_Hook::emit('\OCA\Contacts\VCard', 'post_updateVCard', $id);
|
2011-09-17 00:26:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief edits a card with the data provided by sabredav
|
|
|
|
* @param integer $id Addressbook id
|
|
|
|
* @param string $uri the uri of the card
|
|
|
|
* @param string $data vCard file
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-07 15:21:03 +02:00
|
|
|
public static function editFromDAVData($aid, $uri, $data) {
|
2012-07-20 00:17:59 +02:00
|
|
|
$oldcard = self::findWhereDAVDataIs($aid, $uri);
|
2012-11-22 00:14:03 +01:00
|
|
|
try {
|
2012-11-26 23:09:19 +01:00
|
|
|
$vcard = \Sabre\VObject\Reader::read($data);
|
2012-11-22 00:14:03 +01:00
|
|
|
} catch(\Exception $e) {
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.
|
2012-11-22 00:14:03 +01:00
|
|
|
', Unable to parse VCARD, : ' . $e->getMessage(), \OCP\Util::ERROR);
|
2012-08-23 22:02:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
2012-11-22 00:14:03 +01:00
|
|
|
self::edit($oldcard['id'], $vcard);
|
2012-08-23 22:02:38 +02:00
|
|
|
return true;
|
2012-10-25 03:34:12 +02:00
|
|
|
} catch(\Exception $e) {
|
2012-11-15 00:27:09 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '
|
2012-08-23 22:02:38 +02:00
|
|
|
. $e->getMessage() . ', '
|
2012-10-25 03:34:12 +02:00
|
|
|
. \OCP\USER::getUser(), \OCP\Util::ERROR);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', uri'
|
|
|
|
. $uri, \OCP\Util::DEBUG);
|
2012-06-10 20:54:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief deletes a card
|
|
|
|
* @param integer $id id of card
|
2012-08-30 18:42:31 +02:00
|
|
|
* @return boolean true on success, otherwise an exception will be thrown
|
2011-09-17 00:26:57 +02:00
|
|
|
*/
|
2012-09-07 15:21:03 +02:00
|
|
|
public static function delete($id) {
|
2012-11-26 20:36:31 +01:00
|
|
|
$contact = self::find($id);
|
|
|
|
if (!$contact) {
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', id: '
|
|
|
|
. $id . ' not found.', \OCP\Util::DEBUG);
|
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t(
|
2012-11-26 20:36:31 +01:00
|
|
|
'Could not find the vCard with ID: ' . $id, 404
|
2012-08-30 18:42:31 +02:00
|
|
|
)
|
|
|
|
);
|
2012-08-09 19:54:59 +02:00
|
|
|
}
|
2012-11-26 20:36:31 +01:00
|
|
|
$addressbook = Addressbook::find($contact['addressbookid']);
|
2012-08-30 18:42:31 +02:00
|
|
|
if(!$addressbook) {
|
2012-10-25 03:34:12 +02:00
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t(
|
2012-09-07 14:32:45 +02:00
|
|
|
'Could not find the Addressbook with ID: '
|
2012-11-26 20:36:31 +01:00
|
|
|
. $contact['addressbookid'], 404
|
2012-08-30 18:42:31 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
if ($addressbook['userid'] != \OCP\User::getUser() && !\OC_Group::inGroup(\OCP\User::getUser(), 'admin')) {
|
2012-11-15 00:27:09 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', '
|
2012-10-25 03:34:12 +02:00
|
|
|
. $addressbook['userid'] . ' != ' . \OCP\User::getUser(), \OCP\Util::DEBUG);
|
|
|
|
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource(
|
|
|
|
'addressbook',
|
2012-11-26 20:36:31 +01:00
|
|
|
$contact['addressbookid'],
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Share::FORMAT_NONE, null, true);
|
|
|
|
$sharedContact = \OCP\Share::getItemSharedWithBySource(
|
|
|
|
'contact',
|
|
|
|
$id,
|
|
|
|
\OCP\Share::FORMAT_NONE, null, true);
|
2012-09-04 13:39:25 +02:00
|
|
|
$addressbook_permissions = 0;
|
|
|
|
$contact_permissions = 0;
|
|
|
|
if ($sharedAddressbook) {
|
|
|
|
$addressbook_permissions = $sharedAddressbook['permissions'];
|
|
|
|
}
|
|
|
|
if ($sharedContact) {
|
|
|
|
$contact_permissions = $sharedEvent['permissions'];
|
|
|
|
}
|
|
|
|
$permissions = max($addressbook_permissions, $contact_permissions);
|
2012-11-12 00:46:15 +01:00
|
|
|
|
|
|
|
if (!($permissions & \OCP\PERMISSION_DELETE)) {
|
2012-10-25 03:34:12 +02:00
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t(
|
2012-11-26 20:36:31 +01:00
|
|
|
'You do not have the permissions to delete this contact.', 403
|
2012-08-23 22:02:38 +02:00
|
|
|
)
|
|
|
|
);
|
2012-08-09 19:54:59 +02:00
|
|
|
}
|
|
|
|
}
|
2012-11-26 20:36:31 +01:00
|
|
|
$aid = $contact['addressbookid'];
|
2012-11-15 00:27:09 +01:00
|
|
|
\OC_Hook::emit('\OCA\Contacts\VCard', 'pre_deleteVCard',
|
2012-08-23 22:02:38 +02:00
|
|
|
array('aid' => null, 'id' => $id, 'uri' => null)
|
|
|
|
);
|
2012-11-15 00:27:09 +01:00
|
|
|
$stmt = \OCP\DB::prepare('DELETE FROM `*PREFIX*contacts_cards` WHERE `id` = ?');
|
2012-06-26 17:54:00 +02:00
|
|
|
try {
|
|
|
|
$stmt->execute(array($id));
|
2012-10-25 03:34:12 +02:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.
|
|
|
|
', exception: ' . $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', id: '
|
|
|
|
. $id, \OCP\Util::DEBUG);
|
|
|
|
throw new \Exception(
|
|
|
|
App::$l10n->t(
|
2012-08-30 18:42:31 +02:00
|
|
|
'There was an error deleting this contact.'
|
|
|
|
)
|
|
|
|
);
|
2012-06-26 17:54:00 +02:00
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
|
2012-11-12 02:11:19 +01:00
|
|
|
App::updateDBProperties($id);
|
2012-10-25 03:34:12 +02:00
|
|
|
App::getVCategories()->purgeObject($id);
|
2012-11-26 20:37:47 +01:00
|
|
|
Addressbook::touch($addressbook['id']);
|
2012-10-22 15:37:50 +02:00
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Share::unshareAll('contact', $id);
|
2011-09-17 00:26:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief deletes a card with the data provided by sabredav
|
|
|
|
* @param integer $aid Addressbook id
|
|
|
|
* @param string $uri the uri of the card
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-11-12 02:11:19 +01:00
|
|
|
public static function deleteFromDAVData($aid, $uri) {
|
2012-11-26 20:38:59 +01:00
|
|
|
$contact = self::findWhereDAVDataIs($aid, $uri);
|
|
|
|
if(!$contact) {
|
2012-11-26 21:15:44 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', contact not found: '
|
2012-11-26 20:38:59 +01:00
|
|
|
. $uri, \OCP\Util::DEBUG);
|
|
|
|
throw new \Sabre_DAV_Exception_NotFound(
|
|
|
|
App::$l10n->t(
|
|
|
|
'Contact not found.'
|
|
|
|
)
|
|
|
|
);
|
2012-08-09 19:54:59 +02:00
|
|
|
}
|
2012-11-26 20:38:59 +01:00
|
|
|
$id = $contact['id'];
|
2012-06-26 17:54:00 +02:00
|
|
|
try {
|
2012-11-26 20:38:59 +01:00
|
|
|
return self::delete($id);
|
2013-03-12 22:14:05 +01:00
|
|
|
} catch (\Exception $e) {
|
2012-11-26 20:38:59 +01:00
|
|
|
switch($e->getCode()) {
|
|
|
|
case 403:
|
2012-11-26 21:15:44 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', forbidden: '
|
|
|
|
. $uri, \OCP\Util::DEBUG);
|
2012-11-26 20:38:59 +01:00
|
|
|
throw new \Sabre_DAV_Exception_Forbidden(
|
|
|
|
App::$l10n->t(
|
|
|
|
$e->getMessage()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 404:
|
2012-11-26 21:15:44 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', contact not found: '
|
|
|
|
. $uri, \OCP\Util::DEBUG);
|
2012-11-26 20:38:59 +01:00
|
|
|
throw new \Sabre_DAV_Exception_NotFound(
|
|
|
|
App::$l10n->t(
|
|
|
|
$e->getMessage()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw $e;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-12 02:11:19 +01:00
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Data structure of vCard
|
2012-11-22 00:14:03 +01:00
|
|
|
* @param Sabre\VObject\Component $property
|
2011-09-17 00:26:57 +02:00
|
|
|
* @return associative array
|
|
|
|
*
|
|
|
|
* look at code ...
|
|
|
|
*/
|
2012-11-22 00:14:03 +01:00
|
|
|
public static function structureContact($vcard) {
|
2011-09-17 00:26:57 +02:00
|
|
|
$details = array();
|
2012-08-22 18:50:50 +02:00
|
|
|
|
2012-11-22 00:14:03 +01:00
|
|
|
foreach($vcard->children as $property) {
|
2012-08-22 18:50:50 +02:00
|
|
|
$pname = $property->name;
|
2011-09-17 00:26:57 +02:00
|
|
|
$temp = self::structureProperty($property);
|
2012-05-05 13:44:54 +02:00
|
|
|
if(!is_null($temp)) {
|
2012-08-22 18:50:50 +02:00
|
|
|
// Get Apple X-ABLabels
|
2012-11-22 00:14:03 +01:00
|
|
|
if(isset($vcard->{$property->group . '.X-ABLABEL'})) {
|
|
|
|
$temp['label'] = $vcard->{$property->group . '.X-ABLABEL'}->value;
|
2012-08-21 03:56:12 +02:00
|
|
|
if($temp['label'] == '_$!<Other>!$_') {
|
2012-10-25 03:34:12 +02:00
|
|
|
$temp['label'] = App::$l10n->t('Other');
|
2012-08-21 03:56:12 +02:00
|
|
|
}
|
2012-09-17 16:15:31 +02:00
|
|
|
if($temp['label'] == '_$!<HomePage>!$_') {
|
2012-10-25 03:34:12 +02:00
|
|
|
$temp['label'] = App::$l10n->t('HomePage');
|
2012-09-17 16:15:31 +02:00
|
|
|
}
|
2012-08-21 03:56:12 +02:00
|
|
|
}
|
2012-08-22 18:50:50 +02:00
|
|
|
if(array_key_exists($pname, $details)) {
|
|
|
|
$details[$pname][] = $temp;
|
2012-05-05 13:44:54 +02:00
|
|
|
}
|
|
|
|
else{
|
2012-08-22 18:50:50 +02:00
|
|
|
$details[$pname] = array($temp);
|
2012-05-05 13:44:54 +02:00
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $details;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Data structure of properties
|
|
|
|
* @param object $property
|
|
|
|
* @return associative array
|
|
|
|
*
|
|
|
|
* returns an associative array with
|
|
|
|
* ['name'] name of property
|
|
|
|
* ['value'] htmlspecialchars escaped value of property
|
|
|
|
* ['parameters'] associative array name=>value
|
|
|
|
* ['checksum'] checksum of whole property
|
2012-01-25 01:10:56 +01:00
|
|
|
* NOTE: $value is not escaped anymore. It shouldn't make any difference
|
|
|
|
* but we should look out for any problems.
|
2011-09-17 00:26:57 +02:00
|
|
|
*/
|
2012-07-20 00:17:59 +02:00
|
|
|
public static function structureProperty($property) {
|
2012-11-22 00:14:03 +01:00
|
|
|
if(!in_array($property->name, App::$index_properties)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
$value = $property->value;
|
2012-11-22 00:14:03 +01:00
|
|
|
if($property->name == 'ADR' || $property->name == 'N' || $property->name == 'ORG' || $property->name == 'CATEGORIES') {
|
|
|
|
$value = $property->getParts();
|
2013-01-08 12:50:13 +01:00
|
|
|
$value = array_map('trim', $value);
|
2012-09-17 16:15:31 +02:00
|
|
|
}
|
|
|
|
elseif($property->name == 'BDAY') {
|
2012-05-04 01:27:59 +02:00
|
|
|
if(strpos($value, '-') === false) {
|
|
|
|
if(strlen($value) >= 8) {
|
|
|
|
$value = substr($value, 0, 4).'-'.substr($value, 4, 2).'-'.substr($value, 6, 2);
|
|
|
|
} else {
|
|
|
|
return null; // Badly malformed :-(
|
|
|
|
}
|
|
|
|
}
|
2012-10-05 02:08:42 +02:00
|
|
|
} elseif($property->name == 'PHOTO') {
|
2012-10-15 21:34:22 +02:00
|
|
|
$value = true;
|
2012-05-04 01:27:59 +02:00
|
|
|
}
|
2012-09-17 16:15:31 +02:00
|
|
|
elseif($property->name == 'IMPP') {
|
|
|
|
if(strpos($value, ':') !== false) {
|
|
|
|
$value = explode(':', $value);
|
|
|
|
$protocol = array_shift($value);
|
2012-10-05 05:05:49 +02:00
|
|
|
if(!isset($property['X-SERVICE-TYPE'])) {
|
2013-02-08 23:23:29 +01:00
|
|
|
$property['X-SERVICE-TYPE'] = strtoupper(\OCP\Util::sanitizeHTML($protocol));
|
2012-09-17 16:15:31 +02:00
|
|
|
}
|
|
|
|
$value = implode('', $value);
|
|
|
|
}
|
|
|
|
}
|
2012-08-19 16:32:32 +02:00
|
|
|
if(is_string($value)) {
|
|
|
|
$value = strtr($value, array('\,' => ',', '\;' => ';'));
|
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
$temp = array(
|
2012-11-24 01:46:00 +01:00
|
|
|
//'name' => $property->name,
|
2013-04-09 00:43:02 +02:00
|
|
|
'value' => $value,
|
2012-10-25 23:10:36 +02:00
|
|
|
'parameters' => array()
|
|
|
|
);
|
|
|
|
|
|
|
|
// This cuts around a 3rd off of the json response size.
|
|
|
|
if(in_array($property->name, App::$multi_properties)) {
|
2012-10-26 00:02:44 +02:00
|
|
|
$temp['checksum'] = substr(md5($property->serialize()), 0, 8);
|
2012-10-25 23:10:36 +02:00
|
|
|
}
|
2012-09-07 15:21:03 +02:00
|
|
|
foreach($property->parameters as $parameter) {
|
2011-09-17 00:26:57 +02:00
|
|
|
// Faulty entries by kaddressbook
|
2012-02-21 23:19:13 +01:00
|
|
|
// Actually TYPE=PREF is correct according to RFC 2426
|
|
|
|
// but this way is more handy in the UI. Tanghus.
|
2012-08-21 03:56:12 +02:00
|
|
|
if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') {
|
2011-09-17 00:26:57 +02:00
|
|
|
$parameter->name = 'PREF';
|
|
|
|
$parameter->value = '1';
|
|
|
|
}
|
2012-02-21 23:19:13 +01:00
|
|
|
// NOTE: Apparently Sabre_VObject_Reader can't always deal with value list parameters
|
|
|
|
// like TYPE=HOME,CELL,VOICE. Tanghus.
|
2012-09-17 16:15:31 +02:00
|
|
|
// TODO: Check if parameter is has commas and split + merge if so.
|
|
|
|
if ($parameter->name == 'TYPE') {
|
|
|
|
$pvalue = $parameter->value;
|
|
|
|
if(is_string($pvalue) && strpos($pvalue, ',') !== false) {
|
|
|
|
$pvalue = array_map('trim', explode(',', $pvalue));
|
|
|
|
}
|
|
|
|
$pvalue = is_array($pvalue) ? $pvalue : array($pvalue);
|
2012-07-20 00:17:59 +02:00
|
|
|
if (isset($temp['parameters'][$parameter->name])) {
|
2013-02-08 23:23:29 +01:00
|
|
|
$temp['parameters'][$parameter->name][] = \OCP\Util::sanitizeHTML($pvalue);
|
2011-11-23 21:06:51 +01:00
|
|
|
}
|
2012-07-20 00:17:59 +02:00
|
|
|
else {
|
2013-02-08 23:23:29 +01:00
|
|
|
$temp['parameters'][$parameter->name] = \OCP\Util::sanitizeHTML($pvalue);
|
2011-11-23 21:06:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2013-02-08 23:23:29 +01:00
|
|
|
$temp['parameters'][$parameter->name] = \OCP\Util::sanitizeHTML($parameter->value);
|
2011-11-23 21:06:51 +01:00
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
}
|
|
|
|
return $temp;
|
|
|
|
}
|
2012-02-14 00:56:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Move card(s) to an address book
|
|
|
|
* @param integer $aid Address book id
|
|
|
|
* @param $id Array or integer of cards to be moved.
|
|
|
|
* @return boolean
|
|
|
|
*
|
|
|
|
*/
|
2012-08-16 00:24:38 +02:00
|
|
|
public static function moveToAddressBook($aid, $id, $isAddressbook = false) {
|
2012-10-25 03:34:12 +02:00
|
|
|
Addressbook::find($aid);
|
|
|
|
$addressbook = Addressbook::find($aid);
|
|
|
|
if ($addressbook['userid'] != \OCP\User::getUser()) {
|
|
|
|
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $aid);
|
2012-11-12 00:46:15 +01:00
|
|
|
if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & \OCP\PERMISSION_CREATE)) {
|
2012-08-09 19:54:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-02-14 00:56:50 +01:00
|
|
|
if(is_array($id)) {
|
2012-08-09 19:54:59 +02:00
|
|
|
foreach ($id as $index => $cardId) {
|
|
|
|
$card = self::find($cardId);
|
|
|
|
if (!$card) {
|
|
|
|
unset($id[$index]);
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
$oldAddressbook = Addressbook::find($card['addressbookid']);
|
|
|
|
if ($oldAddressbook['userid'] != \OCP\User::getUser()) {
|
|
|
|
$sharedContact = \OCP\Share::getItemSharedWithBySource('contact', $cardId, \OCP\Share::FORMAT_NONE, null, true);
|
2012-11-12 00:46:15 +01:00
|
|
|
if (!$sharedContact || !($sharedContact['permissions'] & \OCP\PERMISSION_DELETE)) {
|
2012-08-09 19:54:59 +02:00
|
|
|
unset($id[$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-14 00:56:50 +01:00
|
|
|
$id_sql = join(',', array_fill(0, count($id), '?'));
|
2012-07-30 20:46:14 +02:00
|
|
|
$prep = 'UPDATE `*PREFIX*contacts_cards` SET `addressbookid` = ? WHERE `id` IN ('.$id_sql.')';
|
2012-02-14 00:56:50 +01:00
|
|
|
try {
|
2012-10-25 03:34:12 +02:00
|
|
|
$stmt = \OCP\DB::prepare( $prep );
|
2012-02-14 00:56:50 +01:00
|
|
|
//$aid = array($aid);
|
|
|
|
$vals = array_merge((array)$aid, $id);
|
|
|
|
$result = $stmt->execute($vals);
|
2012-10-31 21:37:49 +01:00
|
|
|
if (\OC_DB::isError($result)) {
|
|
|
|
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', ids: '.join(',', $vals), \OCP\Util::DEBUG);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', SQL:'.$prep, \OCP\Util::DEBUG);
|
2012-02-14 00:56:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-16 00:24:38 +02:00
|
|
|
$stmt = null;
|
|
|
|
if($isAddressbook) {
|
2012-10-25 03:34:12 +02:00
|
|
|
$stmt = \OCP\DB::prepare( 'UPDATE `*PREFIX*contacts_cards` SET `addressbookid` = ? WHERE `addressbookid` = ?' );
|
2012-08-16 00:24:38 +02:00
|
|
|
} else {
|
2012-08-20 04:29:01 +02:00
|
|
|
$card = self::find($id);
|
|
|
|
if (!$card) {
|
2012-08-09 19:54:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
$oldAddressbook = Addressbook::find($card['addressbookid']);
|
|
|
|
if ($oldAddressbook['userid'] != \OCP\User::getUser()) {
|
|
|
|
$sharedContact = \OCP\Share::getItemSharedWithBySource('contact', $id, \OCP\Share::FORMAT_NONE, null, true);
|
2012-11-12 00:46:15 +01:00
|
|
|
if (!$sharedContact || !($sharedContact['permissions'] & \OCP\PERMISSION_DELETE)) {
|
2012-08-20 04:29:01 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
$stmt = \OCP\DB::prepare( 'UPDATE `*PREFIX*contacts_cards` SET `addressbookid` = ? WHERE `id` = ?' );
|
2012-08-16 00:24:38 +02:00
|
|
|
}
|
|
|
|
try {
|
2012-02-14 00:56:50 +01:00
|
|
|
$result = $stmt->execute(array($aid, $id));
|
2012-10-31 21:37:49 +01:00
|
|
|
if (\OC_DB::isError($result)) {
|
|
|
|
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::DEBUG);
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.' id: '.$id, \OCP\Util::DEBUG);
|
2012-02-14 00:56:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
\OC_Hook::emit('\OCA\Contacts\VCard', 'post_moveToAddressbook', array('aid' => $aid, 'id' => $id));
|
|
|
|
Addressbook::touch($aid);
|
2012-02-14 00:56:50 +01:00
|
|
|
return true;
|
|
|
|
}
|
2011-09-17 00:26:57 +02:00
|
|
|
}
|