3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * A page for mailing preview.
37 class CRM_Mailing_Page_View
extends CRM_Core_Page
{
40 * Signal to Flexmailer that this version of the class is usable.
44 const USES_MAILING_PREVIEW_API
= 1;
46 protected $_mailingID;
48 protected $_contactID;
51 * Lets do permission checking here.
52 * First check for valid mailing, if false return fatal.
53 * Second check for visibility.
54 * Call a hook to see if hook wants to override visibility setting.
56 public function checkPermission() {
57 if (!$this->_mailing
) {
61 // check for visibility, if visibility is Public Pages and they have the permission
63 if ($this->_mailing
->visibility
== 'Public Pages' &&
64 CRM_Core_Permission
::check('view public CiviMail content')
69 // if user is an admin, return true
70 if (CRM_Core_Permission
::check('administer CiviCRM') ||
71 CRM_Core_Permission
::check('approve mailings') ||
72 CRM_Core_Permission
::check('access CiviMail')
81 * Run this page (figure out the action needed and perform it).
84 * @param int $contactID
86 * @param bool $allowID
89 * Not really sure if anything should be returned - parent doesn't
91 public function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FALSE) {
92 if (is_numeric($id)) {
93 $this->_mailingID
= $id;
97 $this->_mailingID
= CRM_Utils_Request
::retrieve('id', 'String', CRM_Core_DAO
::$_nullObject, TRUE);
101 // override contactID from the function level if passed in
102 if (isset($contactID) &&
103 is_numeric($contactID)
105 $this->_contactID
= $contactID;
108 $this->_contactID
= CRM_Core_Session
::getLoggedInContactID();
112 if (Civi
::settings()->get('hash_mailing_url')) {
113 $this->_mailing
= new CRM_Mailing_BAO_Mailing();
115 if (!is_numeric($this->_mailingID
)) {
117 //lets get the id from the hash
118 $result_id = civicrm_api3('Mailing', 'get', [
120 'hash' => $this->_mailingID
,
122 $this->_mailing
->hash
= $this->_mailingID
;
123 $this->_mailingID
= $result_id['id'];
125 elseif (is_numeric($this->_mailingID
)) {
126 $this->_mailing
->id
= $this->_mailingID
;
127 // if mailing is present and associated hash is present
128 // while 'hash' is not been used for mailing view : throw 'permissionDenied'
129 if ($this->_mailing
->find() &&
130 CRM_Core_DAO
::getFieldValue('CRM_Mailing_BAO_Mailing', $this->_mailingID
, 'hash', 'id') &&
133 CRM_Utils_System
::permissionDenied();
139 $this->_mailing
= new CRM_Mailing_BAO_Mailing();
140 $this->_mailing
->id
= $this->_mailingID
;
143 if (!$this->_mailing
->find(TRUE) ||
144 !$this->checkPermission()
146 CRM_Utils_System
::permissionDenied();
150 $contactId = isset($this->_contactID
) ?
$this->_contactID
: 0;
152 $result = civicrm_api3('Mailing', 'preview', [
153 'id' => $this->_mailingID
,
154 'contact_id' => $contactId,
156 $mailing = \CRM_Utils_Array
::value('values', $result);
159 if (isset($mailing['body_html']) && empty($_GET['text'])) {
160 $header = 'text/html; charset=utf-8';
161 $content = $mailing['body_html'];
162 if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) {
163 $title = '<head><title>' . $mailing['subject'] . '</title></head>';
167 $header = 'text/plain; charset=utf-8';
168 $content = $mailing['body_text'];
170 CRM_Utils_System
::setTitle($mailing['subject']);
172 if (CRM_Utils_Array
::value('snippet', $_GET) === 'json') {
173 CRM_Core_Page_AJAX
::returnJsonResponse($content);
176 CRM_Utils_System
::setHttpHeader('Content-Type', $header);
179 CRM_Utils_System
::civiExit();