3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * A page for mailing preview.
21 class CRM_Mailing_Page_View
extends CRM_Core_Page
{
24 * Signal to Flexmailer that this version of the class is usable.
28 const USES_MAILING_PREVIEW_API
= 1;
30 protected $_mailingID;
32 protected $_contactID;
35 * Lets do permission checking here.
36 * First check for valid mailing, if false return fatal.
37 * Second check for visibility.
38 * Call a hook to see if hook wants to override visibility setting.
40 public function checkPermission() {
41 if (!$this->_mailing
) {
45 // check for visibility, if visibility is Public Pages and they have the permission
47 if ($this->_mailing
->visibility
== 'Public Pages' &&
48 CRM_Core_Permission
::check('view public CiviMail content')
53 // if user is an admin, return true
54 if (CRM_Core_Permission
::check('administer CiviCRM') ||
55 CRM_Core_Permission
::check('approve mailings') ||
56 CRM_Core_Permission
::check('access CiviMail')
65 * Run this page (figure out the action needed and perform it).
68 * @param int $contactID
70 * @param bool $allowID
73 * Not really sure if anything should be returned - parent doesn't
75 public function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FALSE) {
76 if (is_numeric($id)) {
77 $this->_mailingID
= $id;
81 $this->_mailingID
= CRM_Utils_Request
::retrieve('id', 'String', CRM_Core_DAO
::$_nullObject, TRUE);
84 // Retrieve contact ID and checksum from the URL
85 $cs = CRM_Utils_Request
::retrieve('cs', 'String');
86 $cid = CRM_Utils_Request
::retrieve('cid', 'Int');
89 // override contactID from the function level if passed in
90 if (isset($contactID) &&
91 is_numeric($contactID)
93 $this->_contactID
= $contactID;
96 // Support checksummed view of the mailing to replace tokens
97 elseif (!empty($cs) && !empty($cid) && CRM_Contact_BAO_Contact_Utils
::validChecksum($cid, $cs)) {
98 $this->_contactID
= $cid;
102 $this->_contactID
= CRM_Core_Session
::getLoggedInContactID();
106 if (Civi
::settings()->get('hash_mailing_url')) {
107 $this->_mailing
= new CRM_Mailing_BAO_Mailing();
109 if (!is_numeric($this->_mailingID
)) {
111 //lets get the id from the hash
112 $result_id = civicrm_api3('Mailing', 'get', [
114 'hash' => $this->_mailingID
,
116 $this->_mailing
->hash
= $this->_mailingID
;
117 $this->_mailingID
= $result_id['id'];
119 elseif (is_numeric($this->_mailingID
)) {
120 $this->_mailing
->id
= $this->_mailingID
;
121 // if mailing is present and associated hash is present
122 // while 'hash' is not been used for mailing view : throw 'permissionDenied'
123 if ($this->_mailing
->find() &&
124 CRM_Core_DAO
::getFieldValue('CRM_Mailing_BAO_Mailing', $this->_mailingID
, 'hash', 'id') &&
127 CRM_Utils_System
::permissionDenied();
133 $this->_mailing
= new CRM_Mailing_BAO_Mailing();
134 $this->_mailing
->id
= $this->_mailingID
;
137 if (!$this->_mailing
->find(TRUE) ||
138 !$this->checkPermission()
140 CRM_Utils_System
::permissionDenied();
144 $contactId = $this->_contactID ??
0;
146 $result = civicrm_api3('Mailing', 'preview', [
147 'id' => $this->_mailingID
,
148 'contact_id' => $contactId,
150 $mailing = $result['values'] ??
NULL;
153 if (isset($mailing['body_html']) && empty($_GET['text'])) {
154 $header = 'text/html; charset=utf-8';
155 $content = $mailing['body_html'];
156 if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) {
157 $title = '<head><title>' . $mailing['subject'] . '</title></head>';
161 $header = 'text/plain; charset=utf-8';
162 $content = $mailing['body_text'];
164 CRM_Utils_System
::setTitle($mailing['subject']);
166 if (CRM_Utils_Array
::value('snippet', $_GET) === 'json') {
167 CRM_Core_Page_AJAX
::returnJsonResponse($content);
170 CRM_Utils_System
::setHttpHeader('Content-Type', $header);
173 CRM_Utils_System
::civiExit();