Commit | Line | Data |
---|---|---|
5da97e99 M |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
5da97e99 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
5da97e99 M |
32 | */ |
33 | class CRM_Contact_Page_ImageFile extends CRM_Core_Page { | |
9942d1dc TO |
34 | /** |
35 | * @var int Time to live (seconds). | |
36 | * | |
37 | * 12 hours: 12 * 60 * 60 = 43200 | |
38 | */ | |
39 | private $ttl = 43200; | |
40 | ||
1a7d2e92 | 41 | /** |
42 | * Run page. | |
43 | * | |
44 | * @throws \Exception | |
45 | */ | |
00be9182 | 46 | public function run() { |
f2b3f596 TO |
47 | if (!preg_match('/^[^\/]+\.(jpg|jpeg|png|gif)$/i', $_GET['photo'])) { |
48 | CRM_Core_Error::fatal('Malformed photo name'); | |
49 | } | |
50 | ||
51 | // FIXME Optimize performance of image_url query | |
52 | $sql = "SELECT id FROM civicrm_contact WHERE image_url like %1;"; | |
53 | $params = array( | |
21dfd5f5 | 54 | 1 => array("%" . $_GET['photo'], 'String'), |
f2b3f596 | 55 | ); |
5da97e99 | 56 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
1a7d2e92 | 57 | $cid = NULL; |
f2b3f596 TO |
58 | while ($dao->fetch()) { |
59 | $cid = $dao->id; | |
5da97e99 | 60 | } |
f2b3f596 TO |
61 | if ($cid) { |
62 | $config = CRM_Core_Config::singleton(); | |
b09172d2 | 63 | $fileExtension = strtolower(pathinfo($_GET['photo'], PATHINFO_EXTENSION)); |
9942d1dc TO |
64 | $this->download( |
65 | $config->customFileUploadDir . $_GET['photo'], | |
2d3de629 | 66 | 'image/' . ($fileExtension == 'jpg' ? 'jpeg' : $fileExtension), |
9942d1dc | 67 | $this->ttl |
5da97e99 | 68 | ); |
f2b3f596 | 69 | CRM_Utils_System::civiExit(); |
5da97e99 | 70 | } |
f2b3f596 TO |
71 | else { |
72 | CRM_Core_Error::fatal('Photo does not exist'); | |
5da97e99 | 73 | } |
5da97e99 | 74 | } |
96025800 | 75 | |
9942d1dc | 76 | /** |
1a7d2e92 | 77 | * Download image. |
78 | * | |
9942d1dc TO |
79 | * @param string $file |
80 | * Local file path. | |
81 | * @param string $mimeType | |
82 | * @param int $ttl | |
83 | * Time to live (seconds). | |
84 | */ | |
85 | protected function download($file, $mimeType, $ttl) { | |
86 | if (!file_exists($file)) { | |
87 | header("HTTP/1.0 404 Not Found"); | |
88 | return; | |
1a7d2e92 | 89 | } |
90 | elseif (!is_readable($file)) { | |
9942d1dc TO |
91 | header('HTTP/1.0 403 Forbidden'); |
92 | return; | |
93 | } | |
d42a224c CW |
94 | CRM_Utils_System::setHttpHeader('Expires', gmdate('D, d M Y H:i:s \G\M\T', CRM_Utils_Time::getTimeRaw() + $ttl)); |
95 | CRM_Utils_System::setHttpHeader("Content-Type", $mimeType); | |
96 | CRM_Utils_System::setHttpHeader("Content-Disposition", "inline; filename=\"" . basename($file) . "\""); | |
97 | CRM_Utils_System::setHttpHeader("Cache-Control", "max-age=$ttl, public"); | |
98 | CRM_Utils_System::setHttpHeader('Pragma', 'public'); | |
9942d1dc TO |
99 | readfile($file); |
100 | } | |
5da97e99 | 101 | |
5da97e99 | 102 | } |