Merge pull request #14389 from christianwach/lab-1005
[civicrm-core.git] / CRM / Contact / Page / ImageFile.php
CommitLineData
5da97e99
M
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
5da97e99 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
5da97e99
M
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
5da97e99
M
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
5da97e99
M
32 */
33class CRM_Contact_Page_ImageFile extends CRM_Core_Page {
9942d1dc 34 /**
9f266042 35 * Time to live (seconds).
36 *
37 * @var int
9942d1dc
TO
38 *
39 * 12 hours: 12 * 60 * 60 = 43200
40 */
41 private $ttl = 43200;
42
1a7d2e92 43 /**
44 * Run page.
45 *
46 * @throws \Exception
47 */
00be9182 48 public function run() {
f2b3f596 49 if (!preg_match('/^[^\/]+\.(jpg|jpeg|png|gif)$/i', $_GET['photo'])) {
ed1cdf0e 50 throw new CRM_Core_Exception(ts('Malformed photo name'));
f2b3f596
TO
51 }
52
53 // FIXME Optimize performance of image_url query
54 $sql = "SELECT id FROM civicrm_contact WHERE image_url like %1;";
be2fb01f
CW
55 $params = [
56 1 => ["%" . $_GET['photo'], 'String'],
57 ];
5da97e99 58 $dao = CRM_Core_DAO::executeQuery($sql, $params);
1a7d2e92 59 $cid = NULL;
f2b3f596
TO
60 while ($dao->fetch()) {
61 $cid = $dao->id;
5da97e99 62 }
f2b3f596
TO
63 if ($cid) {
64 $config = CRM_Core_Config::singleton();
b09172d2 65 $fileExtension = strtolower(pathinfo($_GET['photo'], PATHINFO_EXTENSION));
9942d1dc
TO
66 $this->download(
67 $config->customFileUploadDir . $_GET['photo'],
2d3de629 68 'image/' . ($fileExtension == 'jpg' ? 'jpeg' : $fileExtension),
9942d1dc 69 $this->ttl
5da97e99 70 );
f2b3f596 71 CRM_Utils_System::civiExit();
5da97e99 72 }
f2b3f596 73 else {
ed1cdf0e 74 throw new CRM_Core_Exception(ts('Photo does not exist'));
5da97e99 75 }
5da97e99 76 }
96025800 77
9942d1dc 78 /**
1a7d2e92 79 * Download image.
80 *
9942d1dc
TO
81 * @param string $file
82 * Local file path.
83 * @param string $mimeType
84 * @param int $ttl
85 * Time to live (seconds).
86 */
87 protected function download($file, $mimeType, $ttl) {
88 if (!file_exists($file)) {
89 header("HTTP/1.0 404 Not Found");
90 return;
1a7d2e92 91 }
92 elseif (!is_readable($file)) {
9942d1dc
TO
93 header('HTTP/1.0 403 Forbidden');
94 return;
95 }
d42a224c
CW
96 CRM_Utils_System::setHttpHeader('Expires', gmdate('D, d M Y H:i:s \G\M\T', CRM_Utils_Time::getTimeRaw() + $ttl));
97 CRM_Utils_System::setHttpHeader("Content-Type", $mimeType);
98 CRM_Utils_System::setHttpHeader("Content-Disposition", "inline; filename=\"" . basename($file) . "\"");
99 CRM_Utils_System::setHttpHeader("Cache-Control", "max-age=$ttl, public");
100 CRM_Utils_System::setHttpHeader('Pragma', 'public');
9942d1dc
TO
101 readfile($file);
102 }
5da97e99 103
5da97e99 104}