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