(flexmailer#29) civicrm/mailing/view - Generate content via Mailing.preview API
[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
39 /**
40 * @var Signal to Flexmailer that this version of the class is usable.
41 */
42 const USES_MAILING_PREVIEW_API = 1;
43
44 protected $_mailingID;
45 protected $_mailing;
46 protected $_contactID;
47
48 /**
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.
53 */
54 public function checkPermission() {
55 if (!$this->_mailing) {
56 return FALSE;
57 }
58
59 // check for visibility, if visibility is Public Pages and they have the permission
60 // return true
61 if ($this->_mailing->visibility == 'Public Pages' &&
62 CRM_Core_Permission::check('view public CiviMail content')
63 ) {
64 return TRUE;
65 }
66
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')
71 ) {
72 return TRUE;
73 }
74
75 return FALSE;
76 }
77
78 /**
79 * Run this page (figure out the action needed and perform it).
80 *
81 * @param int $id
82 * @param int $contactID
83 * @param bool $print
84 * @param bool $allowID
85 *
86 * @return null|string
87 * Not really sure if anything should be returned - parent doesn't
88 */
89 public function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FALSE) {
90 if (is_numeric($id)) {
91 $this->_mailingID = $id;
92 }
93 else {
94 $print = TRUE;
95 $this->_mailingID = CRM_Utils_Request::retrieve('id', 'String', CRM_Core_DAO::$_nullObject, TRUE);
96 }
97
98 // # CRM-7651
99 // override contactID from the function level if passed in
100 if (isset($contactID) &&
101 is_numeric($contactID)
102 ) {
103 $this->_contactID = $contactID;
104 }
105 else {
106 $this->_contactID = CRM_Core_Session::getLoggedInContactID();
107 }
108
109 // mailing key check
110 if (Civi::settings()->get('hash_mailing_url')) {
111 $this->_mailing = new CRM_Mailing_BAO_Mailing();
112
113 if (!is_numeric($this->_mailingID)) {
114 $this->_mailing->hash = $this->_mailingID;
115 }
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') &&
122 !$allowID
123 ) {
124 CRM_Utils_System::permissionDenied();
125 return NULL;
126 }
127 }
128 }
129 else {
130 $this->_mailing = new CRM_Mailing_BAO_Mailing();
131 $this->_mailing->id = $this->_mailingID;
132 }
133
134 if (!$this->_mailing->find(TRUE) ||
135 !$this->checkPermission()
136 ) {
137 CRM_Utils_System::permissionDenied();
138 return NULL;
139 }
140
141 $contactId = isset($this->_contactID) ? $this->_contactID : 0;
142
143 $result = civicrm_api3('Mailing', 'preview', [
144 'id' => $this->_mailingID,
145 'contact_id' => $contactId,
146 ]);
147 $mailing = \CRM_Utils_Array::value('values', $result);
148
149 $title = NULL;
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>';
155 }
156 }
157 else {
158 $header = 'text/plain; charset=utf-8';
159 $content = $mailing['body_text'];
160 }
161 CRM_Utils_System::setTitle($mailing['subject']);
162
163 if (CRM_Utils_Array::value('snippet', $_GET) === 'json') {
164 CRM_Core_Page_AJAX::returnJsonResponse($content);
165 }
166 if ($print) {
167 CRM_Utils_System::setHttpHeader('Content-Type', $header);
168 print $title;
169 print $content;
170 CRM_Utils_System::civiExit();
171 }
172 else {
173 return $content;
174 }
175 }
176
177 }