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 * @var Signal to Flexmailer that this version of the class is usable.
42 const USES_MAILING_PREVIEW_API
= 1;
44 protected $_mailingID;
46 protected $_contactID;
49 * Lets do permission checking here.
50 * First check for valid mailing, if false return fatal.
51 * Second check for visibility.
52 * Call a hook to see if hook wants to override visibility setting.
54 public function checkPermission() {
55 if (!$this->_mailing
) {
59 // check for visibility, if visibility is Public Pages and they have the permission
61 if ($this->_mailing
->visibility
== 'Public Pages' &&
62 CRM_Core_Permission
::check('view public CiviMail content')
67 // if user is an admin, return true
68 if (CRM_Core_Permission
::check('administer CiviCRM') ||
69 CRM_Core_Permission
::check('approve mailings') ||
70 CRM_Core_Permission
::check('access CiviMail')
79 * Run this page (figure out the action needed and perform it).
82 * @param int $contactID
84 * @param bool $allowID
87 * Not really sure if anything should be returned - parent doesn't
89 public function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FALSE) {
90 if (is_numeric($id)) {
91 $this->_mailingID
= $id;
95 $this->_mailingID
= CRM_Utils_Request
::retrieve('id', 'String', CRM_Core_DAO
::$_nullObject, TRUE);
99 // override contactID from the function level if passed in
100 if (isset($contactID) &&
101 is_numeric($contactID)
103 $this->_contactID
= $contactID;
106 $this->_contactID
= CRM_Core_Session
::getLoggedInContactID();
110 if (Civi
::settings()->get('hash_mailing_url')) {
111 $this->_mailing
= new CRM_Mailing_BAO_Mailing();
113 if (!is_numeric($this->_mailingID
)) {
114 $this->_mailing
->hash
= $this->_mailingID
;
116 elseif (is_numeric($this->_mailingID
)) {
117 $this->_mailing
->id
= $this->_mailingID
;
118 // if mailing is present and associated hash is present
119 // while 'hash' is not been used for mailing view : throw 'permissionDenied'
120 if ($this->_mailing
->find() &&
121 CRM_Core_DAO
::getFieldValue('CRM_Mailing_BAO_Mailing', $this->_mailingID
, 'hash', 'id') &&
124 CRM_Utils_System
::permissionDenied();
130 $this->_mailing
= new CRM_Mailing_BAO_Mailing();
131 $this->_mailing
->id
= $this->_mailingID
;
134 if (!$this->_mailing
->find(TRUE) ||
135 !$this->checkPermission()
137 CRM_Utils_System
::permissionDenied();
141 $contactId = isset($this->_contactID
) ?
$this->_contactID
: 0;
143 $result = civicrm_api3('Mailing', 'preview', [
144 'id' => $this->_mailingID
,
145 'contact_id' => $contactId,
147 $mailing = \CRM_Utils_Array
::value('values', $result);
150 if (isset($mailing['body_html']) && empty($_GET['text'])) {
151 $header = 'text/html; charset=utf-8';
152 $content = $mailing['body_html'];
153 if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) {
154 $title = '<head><title>' . $mailing['subject'] . '</title></head>';
158 $header = 'text/plain; charset=utf-8';
159 $content = $mailing['body_text'];
161 CRM_Utils_System
::setTitle($mailing['subject']);
163 if (CRM_Utils_Array
::value('snippet', $_GET) === 'json') {
164 CRM_Core_Page_AJAX
::returnJsonResponse($content);
167 CRM_Utils_System
::setHttpHeader('Content-Type', $header);
170 CRM_Utils_System
::civiExit();