| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | class CRM_Contact_Page_ImageFile extends CRM_Core_Page { |
| 18 | /** |
| 19 | * Time to live (seconds). |
| 20 | * |
| 21 | * @var int |
| 22 | * |
| 23 | * 12 hours: 12 * 60 * 60 = 43200 |
| 24 | */ |
| 25 | private $ttl = 43200; |
| 26 | |
| 27 | /** |
| 28 | * Run page. |
| 29 | * |
| 30 | * @throws \Exception |
| 31 | */ |
| 32 | public function run() { |
| 33 | if (!preg_match('/^[^\/]+\.(jpg|jpeg|png|gif)$/i', $_GET['photo'])) { |
| 34 | throw new CRM_Core_Exception(ts('Malformed photo name')); |
| 35 | } |
| 36 | |
| 37 | // FIXME Optimize performance of image_url query |
| 38 | $sql = "SELECT id FROM civicrm_contact WHERE image_url like %1;"; |
| 39 | $params = [ |
| 40 | 1 => ["%" . $_GET['photo'], 'String'], |
| 41 | ]; |
| 42 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 43 | $cid = NULL; |
| 44 | while ($dao->fetch()) { |
| 45 | $cid = $dao->id; |
| 46 | } |
| 47 | if ($cid) { |
| 48 | $config = CRM_Core_Config::singleton(); |
| 49 | $fileExtension = strtolower(pathinfo($_GET['photo'], PATHINFO_EXTENSION)); |
| 50 | $this->download( |
| 51 | $config->customFileUploadDir . $_GET['photo'], |
| 52 | 'image/' . ($fileExtension == 'jpg' ? 'jpeg' : $fileExtension), |
| 53 | $this->ttl |
| 54 | ); |
| 55 | } |
| 56 | else { |
| 57 | header("HTTP/1.0 404 Not Found"); |
| 58 | } |
| 59 | CRM_Utils_System::civiExit(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Download image. |
| 64 | * |
| 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; |
| 75 | } |
| 76 | elseif (!is_readable($file)) { |
| 77 | header('HTTP/1.0 403 Forbidden'); |
| 78 | return; |
| 79 | } |
| 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'); |
| 85 | readfile($file); |
| 86 | } |
| 87 | |
| 88 | } |