Merge pull request #16714 from christianwach/lab-1638
[civicrm-core.git] / CRM / Contact / Page / ImageFile.php
CommitLineData
5da97e99
M
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
5da97e99 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
5da97e99 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
5da97e99
M
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
5da97e99
M
16 */
17class CRM_Contact_Page_ImageFile extends CRM_Core_Page {
9942d1dc 18 /**
9f266042 19 * Time to live (seconds).
20 *
21 * @var int
9942d1dc
TO
22 *
23 * 12 hours: 12 * 60 * 60 = 43200
24 */
25 private $ttl = 43200;
26
1a7d2e92 27 /**
28 * Run page.
29 *
30 * @throws \Exception
31 */
00be9182 32 public function run() {
f2b3f596 33 if (!preg_match('/^[^\/]+\.(jpg|jpeg|png|gif)$/i', $_GET['photo'])) {
ed1cdf0e 34 throw new CRM_Core_Exception(ts('Malformed photo name'));
f2b3f596
TO
35 }
36
37 // FIXME Optimize performance of image_url query
38 $sql = "SELECT id FROM civicrm_contact WHERE image_url like %1;";
be2fb01f
CW
39 $params = [
40 1 => ["%" . $_GET['photo'], 'String'],
41 ];
5da97e99 42 $dao = CRM_Core_DAO::executeQuery($sql, $params);
1a7d2e92 43 $cid = NULL;
f2b3f596
TO
44 while ($dao->fetch()) {
45 $cid = $dao->id;
5da97e99 46 }
f2b3f596
TO
47 if ($cid) {
48 $config = CRM_Core_Config::singleton();
b09172d2 49 $fileExtension = strtolower(pathinfo($_GET['photo'], PATHINFO_EXTENSION));
9942d1dc
TO
50 $this->download(
51 $config->customFileUploadDir . $_GET['photo'],
2d3de629 52 'image/' . ($fileExtension == 'jpg' ? 'jpeg' : $fileExtension),
9942d1dc 53 $this->ttl
5da97e99 54 );
f2b3f596 55 CRM_Utils_System::civiExit();
5da97e99 56 }
f2b3f596 57 else {
ed1cdf0e 58 throw new CRM_Core_Exception(ts('Photo does not exist'));
5da97e99 59 }
5da97e99 60 }
96025800 61
9942d1dc 62 /**
1a7d2e92 63 * Download image.
64 *
9942d1dc
TO
65 * @param string $file
66 * Local file path.
67 * @param string $mimeType
68 * @param int $ttl
69 * Time to live (seconds).
70 */
71 protected function download($file, $mimeType, $ttl) {
72 if (!file_exists($file)) {
73 header("HTTP/1.0 404 Not Found");
74 return;
1a7d2e92 75 }
76 elseif (!is_readable($file)) {
9942d1dc
TO
77 header('HTTP/1.0 403 Forbidden');
78 return;
79 }
d42a224c
CW
80 CRM_Utils_System::setHttpHeader('Expires', gmdate('D, d M Y H:i:s \G\M\T', CRM_Utils_Time::getTimeRaw() + $ttl));
81 CRM_Utils_System::setHttpHeader("Content-Type", $mimeType);
82 CRM_Utils_System::setHttpHeader("Content-Disposition", "inline; filename=\"" . basename($file) . "\"");
83 CRM_Utils_System::setHttpHeader("Cache-Control", "max-age=$ttl, public");
84 CRM_Utils_System::setHttpHeader('Pragma', 'public');
9942d1dc
TO
85 readfile($file);
86 }
5da97e99 87
5da97e99 88}