comment fixes
[civicrm-core.git] / CRM / Contact / Page / ImageFile.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33 class CRM_Contact_Page_ImageFile extends CRM_Core_Page {
34 /**
35 * @var int Time to live (seconds).
36 *
37 * 12 hours: 12 * 60 * 60 = 43200
38 */
39 private $ttl = 43200;
40
41 public function run() {
42 if (!preg_match('/^[^\/]+\.(jpg|jpeg|png|gif)$/i', $_GET['photo'])) {
43 CRM_Core_Error::fatal('Malformed photo name');
44 }
45
46 // FIXME Optimize performance of image_url query
47 $sql = "SELECT id FROM civicrm_contact WHERE image_url like %1;";
48 $params = array(
49 1 => array("%" . $_GET['photo'], 'String'),
50 );
51 $dao = CRM_Core_DAO::executeQuery($sql, $params);
52 while ($dao->fetch()) {
53 $cid = $dao->id;
54 }
55 if ($cid) {
56 $config = CRM_Core_Config::singleton();
57 $this->download(
58 $config->customFileUploadDir . $_GET['photo'],
59 'image/' . pathinfo($_GET['photo'], PATHINFO_EXTENSION),
60 $this->ttl
61 );
62 CRM_Utils_System::civiExit();
63 }
64 else {
65 CRM_Core_Error::fatal('Photo does not exist');
66 }
67 }
68
69 /**
70 * @param string $file
71 * Local file path.
72 * @param string $mimeType
73 * @param int $ttl
74 * Time to live (seconds).
75 */
76 protected function download($file, $mimeType, $ttl) {
77 if (!file_exists($file)) {
78 header("HTTP/1.0 404 Not Found");
79 return;
80 } elseif (!is_readable($file)) {
81 header('HTTP/1.0 403 Forbidden');
82 return;
83 }
84 CRM_Utils_System::setHttpHeader('Expires', gmdate('D, d M Y H:i:s \G\M\T', CRM_Utils_Time::getTimeRaw() + $ttl));
85 CRM_Utils_System::setHttpHeader("Content-Type", $mimeType);
86 CRM_Utils_System::setHttpHeader("Content-Disposition", "inline; filename=\"" . basename($file) . "\"");
87 CRM_Utils_System::setHttpHeader("Cache-Control", "max-age=$ttl, public");
88 CRM_Utils_System::setHttpHeader('Pragma', 'public');
89 readfile($file);
90 }
91
92 }