summaryrefslogtreecommitdiffstats
path: root/apps/contacts/photo.php
blob: f5c8e6fe4bdac34f26df579582829e56ace68e48 (plain)
1
2
3
4
5
6
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
<?php
/**
 * Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
 * Copyright (c) 2011, 2012 Bart Visscher <bartv@thisnet.nl>
 * Copyright (c) 2011 Jakob Sack mail@jakobsack.de
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

// Init owncloud

OCP\User::checkLoggedIn();
OCP\App::checkAppEnabled('contacts');

function getStandardImage() {
	//OCP\Response::setExpiresHeader('P10D');
	OCP\Response::enableCaching();
	OCP\Response::redirect(OCP\Util::imagePath('contacts', 'person_large.png'));
}

$id = isset($_GET['id']) ? $_GET['id'] : null;
$etag = null;
$caching = null;

if(is_null($id)) {
	getStandardImage();
}

if(!extension_loaded('gd') || !function_exists('gd_info')) {
	OCP\Util::writeLog('contacts',
		'photo.php. GD module not installed', OCP\Util::DEBUG);
	getStandardImage();
}

$contact = OC_Contacts_App::getContactVCard($id);
$image = new OC_Image();
if (!$image) {
	getStandardImage();
}
// invalid vcard
if (is_null($contact)) {
	OCP\Util::writeLog('contacts',
		'photo.php. The VCard for ID ' . $id . ' is not RFC compatible',
		OCP\Util::ERROR);
} else {
	// Photo :-)
	if ($image->loadFromBase64($contact->getAsString('PHOTO'))) {
		// OK
		$etag = md5($contact->getAsString('PHOTO'));
	}
	else
	// Logo :-/
	if ($image->loadFromBase64($contact->getAsString('LOGO'))) {
		// OK
		$etag = md5($contact->getAsString('LOGO'));
	}
	if ($image->valid()) {
		$modified = OC_Contacts_App::lastModified($contact);
		// Force refresh if modified within the last minute.
		if(!is_null($modified)) {
			$caching = (time() - $modified->format('U') > 60) ? null : 0;
		}
		OCP\Response::enableCaching($caching);
		if(!is_null($modified)) {
			OCP\Response::setLastModifiedHeader($modified);
		}
		if($etag) {
			OCP\Response::setETagHeader($etag);
		}
		$max_size = 200;
		if ($image->width() > $max_size || $image->height() > $max_size) {
			$image->resize($max_size);
		}
	}
}
if (!$image->valid()) {
	// Not found :-(
	getStandardImage();
}
header('Content-Type: '.$image->mimeType());
$image->show();