Make is_archived work as a url-search parameter
[civicrm-core.git] / CRM / Mailing / Page / View.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * A page for mailing preview.
20 */
21 class CRM_Mailing_Page_View extends CRM_Core_Page {
22
23 /**
24 * Signal to Flexmailer that this version of the class is usable.
25 *
26 * @var bool
27 */
28 const USES_MAILING_PREVIEW_API = 1;
29
30 protected $_mailingID;
31 protected $_mailing;
32 protected $_contactID;
33
34 /**
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.
39 */
40 public function checkPermission() {
41 if (!$this->_mailing) {
42 return FALSE;
43 }
44
45 // check for visibility, if visibility is Public Pages and they have the permission
46 // return true
47 if ($this->_mailing->visibility == 'Public Pages' &&
48 CRM_Core_Permission::check('view public CiviMail content')
49 ) {
50 return TRUE;
51 }
52
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')
57 ) {
58 return TRUE;
59 }
60
61 return FALSE;
62 }
63
64 /**
65 * Run this page (figure out the action needed and perform it).
66 *
67 * @param int $id
68 * @param int $contactID
69 * @param bool $print
70 * @param bool $allowID
71 *
72 * @return null|string
73 * Not really sure if anything should be returned - parent doesn't
74 */
75 public function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FALSE) {
76 if (is_numeric($id)) {
77 $this->_mailingID = $id;
78 }
79 else {
80 $print = TRUE;
81 $this->_mailingID = CRM_Utils_Request::retrieve('id', 'String', CRM_Core_DAO::$_nullObject, TRUE);
82 }
83
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');
87
88 // # CRM-7651
89 // override contactID from the function level if passed in
90 if (isset($contactID) &&
91 is_numeric($contactID)
92 ) {
93 $this->_contactID = $contactID;
94 }
95
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;
99 }
100
101 else {
102 $this->_contactID = CRM_Core_Session::getLoggedInContactID();
103 }
104
105 // mailing key check
106 if (Civi::settings()->get('hash_mailing_url')) {
107 $this->_mailing = new CRM_Mailing_BAO_Mailing();
108
109 if (!is_numeric($this->_mailingID)) {
110
111 //lets get the id from the hash
112 $result_id = civicrm_api3('Mailing', 'get', [
113 'return' => ['id'],
114 'hash' => $this->_mailingID,
115 ]);
116 $this->_mailing->hash = $this->_mailingID;
117 $this->_mailingID = $result_id['id'];
118 }
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') &&
125 !$allowID
126 ) {
127 CRM_Utils_System::permissionDenied();
128 return NULL;
129 }
130 }
131 }
132 else {
133 $this->_mailing = new CRM_Mailing_BAO_Mailing();
134 $this->_mailing->id = $this->_mailingID;
135 }
136
137 if (!$this->_mailing->find(TRUE) ||
138 !$this->checkPermission()
139 ) {
140 CRM_Utils_System::permissionDenied();
141 return NULL;
142 }
143
144 $contactId = $this->_contactID ?? 0;
145
146 $result = civicrm_api3('Mailing', 'preview', [
147 'id' => $this->_mailingID,
148 'contact_id' => $contactId,
149 ]);
150 $mailing = $result['values'] ?? NULL;
151
152 $title = 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>';
158 }
159 }
160 else {
161 $header = 'text/plain; charset=utf-8';
162 $content = $mailing['body_text'];
163 }
164 CRM_Utils_System::setTitle($mailing['subject']);
165
166 if (CRM_Utils_Array::value('snippet', $_GET) === 'json') {
167 CRM_Core_Page_AJAX::returnJsonResponse($content);
168 }
169 if ($print) {
170 CRM_Utils_System::setHttpHeader('Content-Type', $header);
171 print $title;
172 print $content;
173 CRM_Utils_System::civiExit();
174 }
175 else {
176 return $content;
177 }
178 }
179
180 }