Blame view

sources/apps/contacts/lib/utils/properties.php 9.26 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
  <?php
  /**
6d9380f96   Cédric Dupont   Update sources OC...
3
   * ownCloud - Utility class for VObject properties
d1bafeea1   Kload   [fix] Upgrade to ...
4
5
   *
   * @author Thomas Tanghus
6d9380f96   Cédric Dupont   Update sources OC...
6
   * @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
d1bafeea1   Kload   [fix] Upgrade to ...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   *
   * 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\Utils;
  
  use OCA\Contacts\App;
  
  Properties::$l10n = \OCP\Util::getL10N('contacts');
  
  Class Properties {
  
  	const THUMBNAIL_PREFIX = 'contact-thumbnail-';
6d9380f96   Cédric Dupont   Update sources OC...
32
  	const THUMBNAIL_SIZE = 32;
d1bafeea1   Kload   [fix] Upgrade to ...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  
  	private static $deleteindexstmt;
  	private static $updateindexstmt;
  	protected static $cardsTableName = '*PREFIX*contacts_cards';
  	protected static $indexTableName = '*PREFIX*contacts_cards_properties';
  
  	/**
  	 * @brief language object for calendar app
  	 *
  	 * @var OC_L10N
  	 */
  	public static $l10n;
  
  	/**
  	 * Properties there can be more than one of.
  	 *
  	 * @var array
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
51
  	public static $multiProperties = array('EMAIL', 'TEL', 'IMPP', 'ADR', 'URL');
d1bafeea1   Kload   [fix] Upgrade to ...
52
53
54
55
56
57
  
  	/**
  	 * Properties to index.
  	 *
  	 * @var array
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
58
  	public static $indexProperties = array(
d1bafeea1   Kload   [fix] Upgrade to ...
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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  		'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME',
  		'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO');
  
  	/**
  	 * Get options for IMPP properties
  	 * @param string $im
  	 * @return array of vcard prop => label
  	 */
  	public static function getIMOptions($im = null) {
  		$l10n = self::$l10n;
  		$ims = array(
  				'jabber' => array(
  					'displayname' => (string)$l10n->t('Jabber'),
  					'xname' => 'X-JABBER',
  					'protocol' => 'xmpp',
  				),
  				'sip' => array(
  					'displayname' => (string)$l10n->t('Internet call'),
  					'xname' => 'X-SIP',
  					'protocol' => 'sip',
  				),
  				'aim' => array(
  					'displayname' => (string)$l10n->t('AIM'),
  					'xname' => 'X-AIM',
  					'protocol' => 'aim',
  				),
  				'msn' => array(
  					'displayname' => (string)$l10n->t('MSN'),
  					'xname' => 'X-MSN',
  					'protocol' => 'msn',
  				),
  				'twitter' => array(
  					'displayname' => (string)$l10n->t('Twitter'),
  					'xname' => 'X-TWITTER',
  					'protocol' => 'twitter',
  				),
  				'googletalk' => array(
  					'displayname' => (string)$l10n->t('GoogleTalk'),
  					'xname' => null,
  					'protocol' => 'xmpp',
  				),
  				'facebook' => array(
  					'displayname' => (string)$l10n->t('Facebook'),
  					'xname' => null,
  					'protocol' => 'xmpp',
  				),
  				'xmpp' => array(
  					'displayname' => (string)$l10n->t('XMPP'),
  					'xname' => null,
  					'protocol' => 'xmpp',
  				),
  				'icq' => array(
  					'displayname' => (string)$l10n->t('ICQ'),
  					'xname' => 'X-ICQ',
  					'protocol' => 'icq',
  				),
  				'yahoo' => array(
  					'displayname' => (string)$l10n->t('Yahoo'),
  					'xname' => 'X-YAHOO',
  					'protocol' => 'ymsgr',
  				),
  				'skype' => array(
  					'displayname' => (string)$l10n->t('Skype'),
  					'xname' => 'X-SKYPE',
  					'protocol' => 'skype',
  				),
  				'qq' => array(
  					'displayname' => (string)$l10n->t('QQ'),
  					'xname' => 'X-SKYPE',
  					'protocol' => 'x-apple',
  				),
  				'gadugadu' => array(
  					'displayname' => (string)$l10n->t('GaduGadu'),
  					'xname' => 'X-SKYPE',
  					'protocol' => 'x-apple',
  				),
6d9380f96   Cédric Dupont   Update sources OC...
135
136
137
138
139
  				'owncloud-handle' => array(
  				    'displayname' => (string)$l10n->t('ownCloud'),
  				    'xname' => null,
  				    'protocol' => 'x-owncloud-handle'
  				),
d1bafeea1   Kload   [fix] Upgrade to ...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
  		);
  		if(is_null($im)) {
  			return $ims;
  		} else {
  			$ims['ymsgr'] = $ims['yahoo'];
  			$ims['gtalk'] = $ims['googletalk'];
  			return isset($ims[$im]) ? $ims[$im] : null;
  		}
  	}
  
  	/**
  	 * Get standard set of TYPE values for different properties.
  	 *
  	 * @param string $prop
  	 * @return array Type values for property $prop
  	 */
  	public static function getTypesForProperty($prop) {
  		$l = self::$l10n;
  		switch($prop) {
  			case 'LABEL':
  			case 'ADR':
  			case 'IMPP':
  				return array(
  					'WORK' => (string)$l->t('Work'),
  					'HOME' => (string)$l->t('Home'),
  					'OTHER' => (string)$l->t('Other'),
  				);
  			case 'TEL':
  				return array(
  					'HOME'  =>  (string)$l->t('Home'),
  					'CELL'  =>  (string)$l->t('Mobile'),
  					'WORK'  =>  (string)$l->t('Work'),
  					'TEXT'  =>  (string)$l->t('Text'),
  					'VOICE' =>  (string)$l->t('Voice'),
  					'MSG'   =>  (string)$l->t('Message'),
  					'FAX'   =>  (string)$l->t('Fax'),
  					'VIDEO' =>  (string)$l->t('Video'),
  					'PAGER' =>  (string)$l->t('Pager'),
  					'OTHER' =>  (string)$l->t('Other'),
  				);
  			case 'EMAIL':
  				return array(
  					'WORK' => (string)$l->t('Work'),
  					'HOME' => (string)$l->t('Home'),
  					'INTERNET' => (string)$l->t('Internet'),
  					'OTHER' =>  (string)$l->t('Other'),
  				);
  		}
  	}
  
  	/**
  	 * @brief returns the default categories of ownCloud
  	 * @return (array) $categories
  	 */
  	public static function getDefaultCategories() {
  		$l10n = self::$l10n;
  		return array(
  			(string)$l10n->t('Friends'),
  			(string)$l10n->t('Family'),
  			(string)$l10n->t('Work'),
  			(string)$l10n->t('Other'),
  		);
  	}
  
  	public static function generateUID($app = 'contacts') {
6d9380f96   Cédric Dupont   Update sources OC...
205
206
  		$uuid = new UUID();
  		return $uuid->get() . '@' . \OCP\Util::getServerHostName();
d1bafeea1   Kload   [fix] Upgrade to ...
207
208
209
210
211
212
  	}
  
  	/**
  	 * Purge indexed properties.
  	 *
  	 * @param string[] $ids
d1bafeea1   Kload   [fix] Upgrade to ...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  	 */
  	public static function purgeIndexes($ids) {
  		\OCP\Util::writeLog('contacts', __METHOD__.', ids: ' . print_r($ids, true), \OCP\Util::DEBUG);
  		if(!is_array($ids) || count($ids) === 0) {
  			throw new \Exception(__METHOD__ . ' takes only arrays with at least one value.');
  		}
  		\OCP\Util::writeLog('contacts', __METHOD__.', ids: ' . print_r($ids, true), \OCP\Util::DEBUG);
  		if(!isset(self::$deleteindexstmt)) {
  			self::$deleteindexstmt
  				= \OCP\DB::prepare('DELETE FROM `' . self::$indexTableName . '`'
  					. ' WHERE `contactid` IN (' . str_repeat('?,', count($ids)-1) . '?) ');
  		}
  		try {
  			self::$deleteindexstmt->execute($ids);
  		} catch(\Exception $e) {
  			\OCP\Util::writeLog('contacts', __METHOD__.
  				', exception: ' . $e->getMessage(), \OCP\Util::ERROR);
  			\OCP\Util::writeLog('contacts', __METHOD__.', ids: '
  				. $ids, \OCP\Util::DEBUG);
  			return false;
  		}
  	}
  
  	/**
  	 * Update the contact property index.
  	 *
  	 * If vcard is null the properties for that contact will be purged.
  	 * If it is a valid object the old properties will first be purged
  	 * and the current properties indexed.
  	 *
  	 * @param string $contactid
  	 * @param \OCA\VObject\VCard|null $vcard
  	 */
  	public static function updateIndex($contactid, $vcard = null) {
  		self::purgeIndexes(array($contactid));
  
  		if(is_null($vcard)) {
  			return;
  		}
  
  		if(!isset(self::$updateindexstmt)) {
  			self::$updateindexstmt = \OCP\DB::prepare( 'INSERT INTO `' . self::$indexTableName . '` '
  				. '(`userid`, `contactid`,`name`,`value`,`preferred`) VALUES(?,?,?,?,?)' );
  		}
  		foreach($vcard->children as $property) {
6d9380f96   Cédric Dupont   Update sources OC...
258
  			if(!in_array($property->name, self::$indexProperties)) {
d1bafeea1   Kload   [fix] Upgrade to ...
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  				continue;
  			}
  			$preferred = 0;
  			foreach($property->parameters as $parameter) {
  				if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') {
  					$preferred = 1;
  					break;
  				}
  			}
  			try {
  				$result = self::$updateindexstmt->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;
  				}
  			} catch(\Exception $e) {
  				\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
  				return false;
  			}
  		}
  	}
  
  	public static function cacheThumbnail($backendName, $addressBookId, $contactId,
6d9380f96   Cédric Dupont   Update sources OC...
291
292
  		\OCP\Image $image = null, $vcard = null, $options = array()
  	) {
d1bafeea1   Kload   [fix] Upgrade to ...
293
294
295
  		$cache = \OC::$server->getCache();
  		$key = self::THUMBNAIL_PREFIX . $backendName . '::' . $addressBookId . '::' . $contactId;
  		//$cache->remove($key);
6d9380f96   Cédric Dupont   Update sources OC...
296
  		if ($cache->hasKey($key) && $image === null
d1bafeea1   Kload   [fix] Upgrade to ...
297
298
299
300
  			&& (isset($options['remove']) && $options['remove'] === false)
  			&& (isset($options['update']) && $options['update'] === false)) {
  			return $cache->get($key);
  		}
6d9380f96   Cédric Dupont   Update sources OC...
301
302
  
  		if (isset($options['remove']) && $options['remove']) {
d1bafeea1   Kload   [fix] Upgrade to ...
303
304
305
306
307
  			$cache->remove($key);
  			if(!isset($options['update']) || !$options['update']) {
  				return false;
  			}
  		}
6d9380f96   Cédric Dupont   Update sources OC...
308
309
310
  
  		if (is_null($image)) {
  			if (is_null($vcard)) {
d1bafeea1   Kload   [fix] Upgrade to ...
311
312
313
  				$app = new App();
  				$vcard = $app->getContact($backendName, $addressBookId, $contactId);
  			}
6d9380f96   Cédric Dupont   Update sources OC...
314
315
  			$image = $vcard->getPhoto();
  			if (is_null($image)) {
d1bafeea1   Kload   [fix] Upgrade to ...
316
317
  				return false;
  			}
d1bafeea1   Kload   [fix] Upgrade to ...
318
  		}
6d9380f96   Cédric Dupont   Update sources OC...
319
320
  
  		if (!$image->centerCrop()) {
d1bafeea1   Kload   [fix] Upgrade to ...
321
322
323
324
325
  			\OCP\Util::writeLog('contacts',
  				__METHOD__ .'. Couldn\'t crop thumbnail for ID ' . $key,
  				\OCP\Util::ERROR);
  			return false;
  		}
6d9380f96   Cédric Dupont   Update sources OC...
326
327
  
  		if (!$image->resize(self::THUMBNAIL_SIZE)) {
d1bafeea1   Kload   [fix] Upgrade to ...
328
329
330
331
332
  			\OCP\Util::writeLog('contacts',
  				__METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key,
  				\OCP\Util::ERROR);
  			return false;
  		}
6d9380f96   Cédric Dupont   Update sources OC...
333

d1bafeea1   Kload   [fix] Upgrade to ...
334
335
336
337
338
339
340
  		 // Cache as base64 for around a month
  		$cache->set($key, strval($image), 3000000);
  		\OCP\Util::writeLog('contacts', 'Caching ' . $key, \OCP\Util::DEBUG);
  		return $cache->get($key);
  	}
  
  }