CRM-14734 - fix notices for anonymous contact viewing a mailing.
[civicrm-core.git] / CRM / Mailing / Page / View.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * a page for mailing preview
38 */
39class CRM_Mailing_Page_View extends CRM_Core_Page {
40 protected $_mailingID;
41 protected $_mailing;
42 protected $_contactID;
43
44 /**
45 * Lets do permission checking here
46 * First check for valid mailing, if false return fatal
47 * Second check for visibility
48 * Call a hook to see if hook wants to override visibility setting
49 */
50 function checkPermission() {
51 if (!$this->_mailing) {
52 return FALSE;
53 }
54
55 // check for visibility, if visibility is Public Pages and they have the permission
56 // return true
57 if ($this->_mailing->visibility == 'Public Pages' &&
58 CRM_Core_Permission::check('view public CiviMail content')
59 ) {
60 return TRUE;
61 }
62
63 // if user is an admin, return true
64 if (CRM_Core_Permission::check('administer CiviCRM') ||
65 CRM_Core_Permission::check('access CiviMail')
66 ) {
67 return TRUE;
68 }
69
70 return FALSE;
71 }
72
73 /**
74 * run this page (figure out the action needed and perform it).
75 *
77b97be7
EM
76 * @param null $id
77 * @param null $contactID
78 * @param bool $print
79 * @param bool $allowID
80 *
6a488035
TO
81 * @return void
82 */
c57f36a1 83 function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FALSE) {
6a488035
TO
84 if (is_numeric($id)) {
85 $this->_mailingID = $id;
86 }
87 else {
88 $print = TRUE;
c57f36a1 89 $this->_mailingID = CRM_Utils_Request::retrieve('id', 'String', CRM_Core_DAO::$_nullObject, TRUE);
6a488035
TO
90 }
91
92 // # CRM-7651
93 // override contactID from the function level if passed in
94 if (isset($contactID) &&
95 is_numeric($contactID)
96 ) {
97 $this->_contactID = $contactID;
98 }
99 else {
100 $session = CRM_Core_Session::singleton();
101 $this->_contactID = $session->get('userID');
102 }
103
c57f36a1
PJ
104 // mailing key check
105 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'hash_mailing_url')) {
106 $this->_mailing = new CRM_Mailing_BAO_Mailing();
107
108 if (!is_numeric($this->_mailingID)) {
109 $this->_mailing->hash = $this->_mailingID;
110 }
111 elseif (is_numeric($this->_mailingID)) {
112 $this->_mailing->id = $this->_mailingID;
113 // if mailing is present and associated hash is present
114 // while 'hash' is not been used for mailing view : throw 'permissionDenied'
115 if ($this->_mailing->find() &&
303aa3b3 116 CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $this->_mailingID, 'hash', 'id') &&
c57f36a1
PJ
117 !$allowID
118 ) {
119 CRM_Utils_System::permissionDenied();
120 return;
121 }
122 }
123 }
124 else {
125 $this->_mailing = new CRM_Mailing_BAO_Mailing();
126 $this->_mailing->id = $this->_mailingID;
127 }
6a488035
TO
128
129 if (!$this->_mailing->find(TRUE) ||
130 !$this->checkPermission()
131 ) {
132 CRM_Utils_System::permissionDenied();
133 return;
134 }
135
136 CRM_Mailing_BAO_Mailing::tokenReplace($this->_mailing);
137
138 // get and format attachments
139 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing',
140 $this->_mailing->id
141 );
142
143 // get contact detail and compose if contact id exists
54d1bc76 144 $returnProperties = $this->_mailing->getReturnProperties();
6a488035
TO
145 if (isset($this->_contactID)) {
146 //get details of contact with token value including Custom Field Token Values.CRM-3734
72decf50
BS
147 $params = array('contact_id' => $this->_contactID);
148 $details = CRM_Utils_Token::getTokenDetails($params,
6a488035 149 $returnProperties,
72decf50 150 FALSE, TRUE, NULL,
6a488035
TO
151 $this->_mailing->getFlattenedTokens(),
152 get_class($this)
153 );
154 $details = $details[0][$this->_contactID];
72decf50 155 $contactId = $this->_contactID;
6a488035
TO
156 }
157 else {
d20c4dad
EM
158 //get tokens that are not contact specific resolved
159 $params = array('contact_id' => 0);
160 $details = CRM_Utils_Token::getAnonymousTokenDetails($params,
161 $returnProperties,
162 TRUE, TRUE, NULL,
163 $this->_mailing->getFlattenedTokens(),
164 get_class($this)
165 );
166
54d1bc76 167 $details = $details[0];
72decf50 168 $contactId = 0;
6a488035 169 }
72decf50 170 $mime = &$this->_mailing->compose(NULL, NULL, NULL, $contactId,
6a488035
TO
171 $this->_mailing->from_email,
172 $this->_mailing->from_email,
173 TRUE, $details, $attachments
174 );
175
3f32bb8b 176 $title = NULL;
fc164be7 177 if (isset($this->_mailing->body_html) && empty($_GET['text'])) {
6a488035
TO
178 $header = 'Content-Type: text/html; charset=utf-8';
179 $content = $mime->getHTMLBody();
3f32bb8b
DG
180 if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) {
181 $title = '<head><title>' . $this->_mailing->subject . '</title></head>';
182 }
6a488035
TO
183 }
184 else {
185 $header = 'Content-Type: text/plain; charset=utf-8';
186 $content = $mime->getTXTBody();
187 }
fc164be7 188 CRM_Utils_System::setTitle($this->_mailing->subject);
6a488035 189
fc164be7
CW
190 if (CRM_Utils_Array::value('snippet', $_GET) === 'json') {
191 CRM_Core_Page_AJAX::returnJsonResponse($content);
192 }
6a488035
TO
193 if ($print) {
194 header($header);
3f32bb8b 195 print $title;
6a488035
TO
196 print $content;
197 CRM_Utils_System::civiExit();
198 }
199 else {
200 return $content;
201 }
202 }
203}
204