Merge pull request #4860 from totten/master-ext-cache
[civicrm-core.git] / CRM / Core / Page / AJAX.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This is base class for all ajax calls
38 */
39 class CRM_Core_Page_AJAX {
40
41 /**
42 * Call generic ajax forms
43 *
44 * @static
45 */
46 public static function run() {
47 $className = CRM_Utils_Type::escape($_REQUEST['class_name'], 'String');
48 $type = '';
49 if (!empty($_REQUEST['type'])) {
50 $type = CRM_Utils_Type::escape($_REQUEST['type'], 'String');
51 }
52
53 if (!$className) {
54 CRM_Core_Error::fatal(ts('Invalid className: %1', array(1 => $className)));
55 }
56
57 $fnName = NULL;
58 if (isset($_REQUEST['fn_name'])) {
59 $fnName = CRM_Utils_Type::escape($_REQUEST['fn_name'], 'String');
60 }
61
62 if (!self::checkAuthz($type, $className, $fnName)) {
63 CRM_Utils_System::civiExit();
64 }
65
66 switch ($type) {
67 case 'method':
68 call_user_func(array($className, $fnName));
69 break;
70
71 case 'page':
72 case 'class':
73 case '':
74 // FIXME: This is done to maintain current wire protocol, but it might be
75 // simpler to just require different 'types' for pages and forms
76 if (preg_match('/^CRM_[a-zA-Z0-9]+_Page_Inline_/', $className)) {
77 $page = new $className;
78 $page->run();
79 }
80 else {
81 $wrapper = new CRM_Utils_Wrapper();
82 $wrapper->run($className);
83 }
84 break;
85
86 default:
87 CRM_Core_Error::debug_log_message('Unsupported inline request type: ' . var_export($type, TRUE));
88 }
89 CRM_Utils_System::civiExit();
90 }
91
92 /**
93 * Change is_quick_config priceSet to complex
94 *
95 * @static
96 */
97 public static function setIsQuickConfig() {
98 $id = $context = NULL;
99 if (!empty($_REQUEST['id'])) {
100 $id = CRM_Utils_Type::escape($_REQUEST['id'], 'Integer');
101 }
102
103 if (!empty($_REQUEST['context'])) {
104 $context = CRM_Utils_Type::escape($_REQUEST['context'], 'String');
105 }
106 // return false if $id is null and
107 // $context is not civicrm_event or civicrm_contribution_page
108 if (!$id || !in_array($context, array('civicrm_event', 'civicrm_contribution_page'))) {
109 return FALSE;
110 }
111 $priceSetId = CRM_Price_BAO_PriceSet::getFor($context, $id, NULL);
112 if ($priceSetId) {
113 $result = CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetId, 0);
114 if ($context == 'civicrm_event') {
115 $sql = "UPDATE
116 civicrm_price_set cps
117 INNER JOIN civicrm_discount cd ON cd.price_set_id = cps.id
118 SET cps.is_quick_config = 0
119 WHERE cd.entity_id = (%1) AND cd.entity_table = 'civicrm_event' ";
120 $params = array(1 => array($id, 'Integer'));
121 CRM_Core_DAO::executeQuery($sql, $params);
122 CRM_Core_BAO_Discount::del($id, $context);
123 }
124 }
125 if (!$result) {
126 $priceSetId = NULL;
127 }
128 CRM_Utils_JSON::output($priceSetId);
129 }
130
131 /**
132 * Determine whether the request is for a valid class/method name.
133 *
134 * @param string $type
135 * 'method'|'class'|''.
136 * @param string $className
137 * 'Class_Name'.
138 * @param string $fnName
139 * Method name.
140 *
141 * @return bool
142 */
143 public static function checkAuthz($type, $className, $fnName = NULL) {
144 switch ($type) {
145 case 'method':
146 if (!preg_match('/^CRM_[a-zA-Z0-9]+_Page_AJAX$/', $className)) {
147 return FALSE;
148 }
149 if (!preg_match('/^[a-zA-Z0-9]+$/', $fnName)) {
150 return FALSE;
151 }
152
153 // ensure that function exists
154 return method_exists($className, $fnName);
155
156 case 'page':
157 case 'class':
158 case '':
159 if (!preg_match('/^CRM_[a-zA-Z0-9]+_(Page|Form)_Inline_[a-zA-Z0-9]+$/', $className)) {
160 return FALSE;
161 }
162 return class_exists($className);
163
164 default:
165 return FALSE;
166 }
167 }
168
169 /**
170 * Outputs the CiviCRM standard json-formatted page/form response
171 * @param array|string $response
172 */
173 public static function returnJsonResponse($response) {
174 // Allow lazy callers to not wrap content in an array
175 if (is_string($response)) {
176 $response = array('content' => $response);
177 }
178 // Add session variables to response
179 $session = CRM_Core_Session::singleton();
180 $response += array(
181 'status' => 'success',
182 'userContext' => htmlspecialchars_decode($session->readUserContext()),
183 'title' => CRM_Utils_System::$title,
184 );
185 // crmMessages will be automatically handled by our ajax preprocessor
186 // @see js/Common.js
187 if ($session->getStatus(FALSE)) {
188 $response['crmMessages'] = $session->getStatus(TRUE);
189 }
190 $output = json_encode($response);
191
192 // CRM-11831 @see http://www.malsup.com/jquery/form/#file-upload
193 if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
194 header('Content-Type: application/json');
195 }
196 else {
197 $output = "<textarea>$output</textarea>";
198 }
199 echo $output;
200 CRM_Utils_System::civiExit();
201 }
202
203 /**
204 * Set headers appropriate for a js file
205 */
206 public static function setJsHeaders() {
207 // Encourage browsers to cache for a long time - 1 year
208 $year = 60 * 60 * 24 * 364;
209 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + $year));
210 header('Content-Type: application/javascript');
211 header("Cache-Control: max-age=$year, public");
212 }
213
214 /**
215 * Send autocomplete results to the client. Input can be a simple or nested array.
216 * @param array $results
217 * If nested array, also provide:.
218 * @param string $val
219 * Array key to use as the value.
220 * @param string $key
221 * Array key to use as the key.
222 * @deprecated
223 */
224 public static function autocompleteResults($results, $val = 'label', $key = 'id') {
225 $output = array();
226 if (is_array($results)) {
227 foreach ($results as $k => $v) {
228 if (is_array($v)) {
229 echo $v[$val] . '|' . $v[$key] . "\n";
230 }
231 else {
232 echo "$v|$k\n";
233 }
234 }
235 }
236 CRM_Utils_System::civiExit();
237 }
238 }