commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Mailing / Page / View.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * a page for mailing preview
38 */
39 class CRM_Mailing_Page_View extends CRM_Core_Page {
40 protected $_mailingID;
41 protected $_mailing;
42 protected $_contactID;
43
44 /**
45 * Lets do permission checking here.
46 * First check for valid mailing, if false return fatal
47 * Second check for visibility
48 * Call a hook to see if hook wants to override visibility setting
49 */
50 public function checkPermission() {
51 if (!$this->_mailing) {
52 return FALSE;
53 }
54
55 // check for visibility, if visibility is Public Pages and they have the permission
56 // return true
57 if ($this->_mailing->visibility == 'Public Pages' &&
58 CRM_Core_Permission::check('view public CiviMail content')
59 ) {
60 return TRUE;
61 }
62
63 // if user is an admin, return true
64 if (CRM_Core_Permission::check('administer CiviCRM') ||
65 CRM_Core_Permission::check('approve mailings') ||
66 CRM_Core_Permission::check('access CiviMail')
67 ) {
68 return TRUE;
69 }
70
71 return FALSE;
72 }
73
74 /**
75 * Run this page (figure out the action needed and perform it).
76 *
77 * @param int $id
78 * @param int $contactID
79 * @param bool $print
80 * @param bool $allowID
81 *
82 * @return void
83 */
84 public function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FALSE) {
85 if (is_numeric($id)) {
86 $this->_mailingID = $id;
87 }
88 else {
89 $print = TRUE;
90 $this->_mailingID = CRM_Utils_Request::retrieve('id', 'String', CRM_Core_DAO::$_nullObject, TRUE);
91 }
92
93 // # CRM-7651
94 // override contactID from the function level if passed in
95 if (isset($contactID) &&
96 is_numeric($contactID)
97 ) {
98 $this->_contactID = $contactID;
99 }
100 else {
101 $session = CRM_Core_Session::singleton();
102 $this->_contactID = $session->get('userID');
103 }
104
105 // mailing key check
106 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'hash_mailing_url')) {
107 $this->_mailing = new CRM_Mailing_BAO_Mailing();
108
109 if (!is_numeric($this->_mailingID)) {
110 $this->_mailing->hash = $this->_mailingID;
111 }
112 elseif (is_numeric($this->_mailingID)) {
113 $this->_mailing->id = $this->_mailingID;
114 // if mailing is present and associated hash is present
115 // while 'hash' is not been used for mailing view : throw 'permissionDenied'
116 if ($this->_mailing->find() &&
117 CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $this->_mailingID, 'hash', 'id') &&
118 !$allowID
119 ) {
120 CRM_Utils_System::permissionDenied();
121 return;
122 }
123 }
124 }
125 else {
126 $this->_mailing = new CRM_Mailing_BAO_Mailing();
127 $this->_mailing->id = $this->_mailingID;
128 }
129
130 if (!$this->_mailing->find(TRUE) ||
131 !$this->checkPermission()
132 ) {
133 CRM_Utils_System::permissionDenied();
134 return;
135 }
136
137 CRM_Mailing_BAO_Mailing::tokenReplace($this->_mailing);
138
139 // get and format attachments
140 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing',
141 $this->_mailing->id
142 );
143
144 // get contact detail and compose if contact id exists
145 $returnProperties = $this->_mailing->getReturnProperties();
146 if (isset($this->_contactID)) {
147 //get details of contact with token value including Custom Field Token Values.CRM-3734
148 $params = array('contact_id' => $this->_contactID);
149 $details = CRM_Utils_Token::getTokenDetails($params,
150 $returnProperties,
151 FALSE, TRUE, NULL,
152 $this->_mailing->getFlattenedTokens(),
153 get_class($this)
154 );
155 $details = $details[0][$this->_contactID];
156 $contactId = $this->_contactID;
157 }
158 else {
159 //get tokens that are not contact specific resolved
160 $params = array('contact_id' => 0);
161 $details = CRM_Utils_Token::getAnonymousTokenDetails($params,
162 $returnProperties,
163 TRUE, TRUE, NULL,
164 $this->_mailing->getFlattenedTokens(),
165 get_class($this)
166 );
167
168 $details = CRM_Utils_Array::value(0, $details[0]);
169 $contactId = 0;
170 }
171 $mime = &$this->_mailing->compose(NULL, NULL, NULL, $contactId,
172 $this->_mailing->from_email,
173 $this->_mailing->from_email,
174 TRUE, $details, $attachments
175 );
176
177 $title = NULL;
178 if (isset($this->_mailing->body_html) && empty($_GET['text'])) {
179 $header = 'Content-Type: text/html; charset=utf-8';
180 $content = $mime->getHTMLBody();
181 if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) {
182 $title = '<head><title>' . $this->_mailing->subject . '</title></head>';
183 }
184 }
185 else {
186 $header = 'Content-Type: text/plain; charset=utf-8';
187 $content = $mime->getTXTBody();
188 }
189 CRM_Utils_System::setTitle($this->_mailing->subject);
190
191 if (CRM_Utils_Array::value('snippet', $_GET) === 'json') {
192 CRM_Core_Page_AJAX::returnJsonResponse($content);
193 }
194 if ($print) {
195 header($header);
196 print $title;
197 print $content;
198 CRM_Utils_System::civiExit();
199 }
200 else {
201 return $content;
202 }
203 }
204
205 }