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