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