Blame view
sources/apps/contacts/lib/addressbookprovider.php
6.76 KB
|
d1bafeea1
|
1 2 3 4 5 |
<?php /** * ownCloud - AddressbookProvider * * @author Thomas Tanghus |
|
6d9380f96
|
6 |
* @copyright 2012-2014 Thomas Tanghus (thomas@tanghus.net) |
|
d1bafeea1
|
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
* * 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; |
|
6d9380f96
|
24 |
use OCA\Contacts\Utils\JSONSerializer; |
|
d1bafeea1
|
25 |
use OCA\Contacts\Utils\Properties; |
|
6d9380f96
|
26 27 |
use OCA\Contacts\Utils\TemporaryPhoto; use OCA\Contacts\VObject\VCard; |
|
d1bafeea1
|
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/**
* This class manages our addressbooks.
* TODO: Port this to use the new backend
*/
class AddressbookProvider implements \OCP\IAddressBook {
const CONTACT_TABLE = '*PREFIX*contacts_cards';
const PROPERTY_TABLE = '*PREFIX*contacts_cards_properties';
/**
* Addressbook id
* @var integer
*/
public $id;
/**
* Addressbook info array
* @var AddressBook
*/
public $addressBook;
/**
* Constructor
|
|
6d9380f96
|
52 |
* @param AddressBook $addressBook |
|
d1bafeea1
|
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
*/
public function __construct($addressBook) {
$this->addressBook = $addressBook;
}
public function getAddressbook() {
return $this->addressBook;
}
/**
* @return string defining the technical unique key
*/
public function getKey() {
$metaData = $this->addressBook->getMetaData();
return $metaData['backend'].':'.$metaData['id'];
}
/**
* In comparison to getKey() this function returns a human readable (maybe translated) name
* @return mixed
*/
public function getDisplayName() {
return $this->addressBook->getDisplayName();
}
/**
* @return mixed
*/
public function getPermissions() {
return $this->addressBook->getPermissions();
}
|
|
d1bafeea1
|
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
/**
* @param $pattern
* @param $searchProperties
* @param $options
* @return array|false
*/
public function search($pattern, $searchProperties, $options) {
$ids = array();
$results = array();
$query = 'SELECT DISTINCT `contactid` FROM `' . self::PROPERTY_TABLE . '` WHERE (';
$params = array();
foreach($searchProperties as $property) {
$params[] = $property;
$params[] = '%' . $pattern . '%';
$query .= '(`name` = ? AND `value` LIKE ?) OR ';
}
$query = substr($query, 0, strlen($query) - 4);
$query .= ')';
$stmt = \OCP\DB::prepare($query);
$result = $stmt->execute($params);
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result),
\OCP\Util::ERROR);
return false;
}
while( $row = $result->fetchRow()) {
$ids[] = $row['contactid'];
}
if(count($ids) > 0) {
|
|
6d9380f96
|
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
foreach($ids as $id){
$contact = $this->addressBook->getChild($id);
$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' => $this->addressBook->getId(),
'contactId' => $contact->getId()
));
$url = \OC_Helper::makeURLAbsolute($url);
$j['data']['PHOTO'] = "VALUE=uri:$url";
}
$results[]= $this->convertToSearchResult($j);
}
|
|
d1bafeea1
|
131 |
} |
|
6d9380f96
|
132 |
|
|
d1bafeea1
|
133 134 135 136 137 138 139 140 141 |
return $results;
}
/**
* @param $properties
* @return mixed
*/
public function createOrUpdate($properties) {
$id = null;
|
|
6d9380f96
|
142 143 144 145 |
/** * @var \OCA\Contacts\VObject\VCard */ |
|
d1bafeea1
|
146 147 148 149 150 |
$vcard = null;
if(array_key_exists('id', $properties)) {
// TODO: test if $id belongs to this addressbook
$id = $properties['id'];
// TODO: Test $vcard
|
|
6d9380f96
|
151 |
$vcard = $this->addressBook->getChild($properties['id']); |
|
d1bafeea1
|
152 153 154 155 156 157 158 159 160 161 |
foreach(array_keys($properties) as $name) {
if(isset($vcard->{$name})) {
unset($vcard->{$name});
}
}
} else {
$vcard = \Sabre\VObject\Component::create('VCARD');
$uid = substr(md5(rand().time()), 0, 10);
$vcard->add('UID', $uid);
try {
|
|
6d9380f96
|
162 163 |
$id = $this->addressBook->addChild($vcard);
} catch(\Exception $e) {
|
|
d1bafeea1
|
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
\OCP\Util::writeLog('contacts', __METHOD__ . ' ' . $e->getMessage(), \OCP\Util::ERROR);
return false;
}
}
foreach($properties as $name => $value) {
switch($name) {
case 'ADR':
case 'N':
if(is_array($value)) {
$property = \Sabre\VObject\Property::create($name);
$property->setParts($value);
$vcard->add($property);
} else {
$vcard->{$name} = $value;
}
break;
case 'BDAY':
// TODO: try/catch
$date = New \DateTime($value);
$vcard->BDAY = $date->format('Y-m-d');
$vcard->BDAY->VALUE = 'DATE';
break;
case 'EMAIL':
case 'TEL':
case 'IMPP': // NOTE: We don't know if it's GTalk, Jabber etc. only the protocol
case 'URL':
if(is_array($value)) {
foreach($value as $val) {
$vcard->add($name, strip_tags($val));
}
} else {
$vcard->add($name, strip_tags($value));
}
default:
$vcard->{$name} = $value;
break;
}
}
try {
VCard::edit($id, $vcard);
|
|
6d9380f96
|
206 |
} catch(\Exception $e) {
|
|
d1bafeea1
|
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
\OCP\Util::writeLog('contacts', __METHOD__ . ' ' . $e->getMessage(), \OCP\Util::ERROR);
return false;
}
$asarray = VCard::structureContact($vcard);
$asarray['id'] = $id;
return $asarray;
}
/**
* @param $id
* @return mixed
*/
public function delete($id) {
try {
|
|
6d9380f96
|
222 |
$query = 'SELECT COUNT(*) as `count` FROM `*PREFIX*contacts_cards` WHERE `id` = ? AND `addressbookid` = ?'; |
|
d1bafeea1
|
223 224 225 226 227 228 229 |
$stmt = \OCP\DB::prepare($query);
$result = $stmt->execute(array($id, $this->id));
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result),
\OCP\Util::ERROR);
return false;
}
|
|
6d9380f96
|
230 |
if((int)$result['count'] === 0) {
|
|
d1bafeea1
|
231 232 233 234 235 236 237 238 239 240 241 242 |
\OCP\Util::writeLog('contacts', __METHOD__
. 'Contact with id ' . $id . 'doesn\'t belong to addressbook with id ' . $this->id,
\OCP\Util::ERROR);
return false;
}
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__ . ', exception: ' . $e->getMessage(),
\OCP\Util::ERROR);
return false;
}
return VCard::delete($id);
}
|
|
6d9380f96
|
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
/**
* @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;
}
|
|
d1bafeea1
|
268 |
} |