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