Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | * This class handles all REST client requests. | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * |
34 | */ | |
35 | class CRM_Utils_REST { | |
36 | ||
37 | /** | |
38 | * Number of seconds we should let a REST process idle | |
39 | * @static | |
40 | */ | |
41 | static $rest_timeout = 0; | |
42 | ||
43 | /** | |
44 | * Cache the actual UF Class | |
45 | */ | |
46 | public $ufClass; | |
47 | ||
48 | /** | |
49 | * Class constructor. This caches the real user framework class locally, | |
50 | * so we can use it for authentication and validation. | |
51 | * | |
f4aaa82a | 52 | * @internal param string $uf The userframework class |
6a488035 TO |
53 | */ |
54 | public function __construct() { | |
55 | // any external program which call Rest Server is responsible for | |
56 | // creating and attaching the session | |
57 | $args = func_get_args(); | |
58 | $this->ufClass = array_shift($args); | |
59 | } | |
60 | ||
61 | /** | |
62 | * Simple ping function to test for liveness. | |
63 | * | |
77855840 TO |
64 | * @param string $var |
65 | * The string to be echoed. | |
6a488035 | 66 | * |
a6c01b45 | 67 | * @return string |
6a488035 | 68 | */ |
7f2d6a61 | 69 | public static function ping($var = NULL) { |
6a488035 TO |
70 | $session = CRM_Core_Session::singleton(); |
71 | $key = $session->get('key'); | |
72 | //$session->set( 'key', $var ); | |
73 | return self::simple(array('message' => "PONG: $key")); | |
74 | } | |
75 | ||
6a488035 | 76 | // Generates values needed for error messages |
5bc392e6 EM |
77 | /** |
78 | * @param string $message | |
79 | * | |
80 | * @return array | |
81 | */ | |
00be9182 | 82 | public static function error($message = 'Unknown Error') { |
6a488035 TO |
83 | $values = array( |
84 | 'error_message' => $message, | |
85 | 'is_error' => 1, | |
86 | ); | |
87 | return $values; | |
88 | } | |
89 | ||
90 | // Generates values needed for non-error responses. | |
5bc392e6 | 91 | /** |
c490a46a | 92 | * @param array $params |
5bc392e6 EM |
93 | * |
94 | * @return array | |
95 | */ | |
00be9182 | 96 | public static function simple($params) { |
6a488035 TO |
97 | $values = array('is_error' => 0); |
98 | $values += $params; | |
99 | return $values; | |
100 | } | |
101 | ||
5bc392e6 EM |
102 | /** |
103 | * @return string | |
104 | */ | |
00be9182 | 105 | public function run() { |
6a488035 TO |
106 | $result = self::handle(); |
107 | return self::output($result); | |
108 | } | |
109 | ||
5bc392e6 EM |
110 | /** |
111 | * @return string | |
112 | */ | |
00be9182 | 113 | public function bootAndRun() { |
7f2d6a61 TO |
114 | $response = $this->loadCMSBootstrap(); |
115 | if (is_array($response)) { | |
116 | return self::output($response); | |
117 | } | |
118 | return $this->run(); | |
119 | } | |
120 | ||
5bc392e6 EM |
121 | /** |
122 | * @param $result | |
123 | * | |
124 | * @return string | |
125 | */ | |
00be9182 | 126 | public static function output(&$result) { |
ba56a28f TO |
127 | $requestParams = CRM_Utils_Request::exportValues(); |
128 | ||
6a488035 TO |
129 | $hier = FALSE; |
130 | if (is_scalar($result)) { | |
131 | if (!$result) { | |
132 | $result = 0; | |
133 | } | |
134 | $result = self::simple(array('result' => $result)); | |
135 | } | |
136 | elseif (is_array($result)) { | |
137 | if (CRM_Utils_Array::isHierarchical($result)) { | |
138 | $hier = TRUE; | |
139 | } | |
140 | elseif (!array_key_exists('is_error', $result)) { | |
141 | $result['is_error'] = 0; | |
142 | } | |
143 | } | |
144 | else { | |
145 | $result = self::error('Could not interpret return values from function.'); | |
146 | } | |
147 | ||
3eec106c CW |
148 | if (!empty($requestParams['json'])) { |
149 | header('Content-Type: application/json'); | |
6a488035 | 150 | $json = json_encode(array_merge($result)); |
3eec106c CW |
151 | if (!empty($requestParams['prettyprint'])) { |
152 | // Used by the api explorer | |
6a488035 TO |
153 | return self::jsonFormated($json); |
154 | } | |
155 | return $json; | |
156 | } | |
157 | ||
6a488035 | 158 | if (isset($result['count'])) { |
6a488035 | 159 | $count = ' count="' . $result['count'] . '" '; |
6a488035 | 160 | } |
a3e55d9c TO |
161 | else { |
162 | $count = ""; | |
e7292422 | 163 | } |
6a488035 TO |
164 | $xml = "<?xml version=\"1.0\"?> |
165 | <ResultSet xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" $count> | |
166 | "; | |
167 | // check if this is a single element result (contact_get etc) | |
168 | // or multi element | |
169 | if ($hier) { | |
170 | foreach ($result['values'] as $n => $v) { | |
171 | $xml .= "<Result>\n" . CRM_Utils_Array::xml($v) . "</Result>\n"; | |
172 | } | |
173 | } | |
174 | else { | |
175 | $xml .= "<Result>\n" . CRM_Utils_Array::xml($result) . "</Result>\n"; | |
176 | } | |
177 | ||
178 | $xml .= "</ResultSet>\n"; | |
179 | return $xml; | |
180 | } | |
181 | ||
5bc392e6 EM |
182 | /** |
183 | * @param $json | |
184 | * | |
185 | * @return string | |
186 | */ | |
00be9182 | 187 | public static function jsonFormated($json) { |
353ffa53 TO |
188 | $tabcount = 0; |
189 | $result = ''; | |
190 | $inquote = FALSE; | |
191 | $inarray = FALSE; | |
6a488035 TO |
192 | $ignorenext = FALSE; |
193 | ||
194 | $tab = "\t"; | |
195 | $newline = "\n"; | |
196 | ||
197 | for ($i = 0; $i < strlen($json); $i++) { | |
198 | $char = $json[$i]; | |
199 | ||
200 | if ($ignorenext) { | |
201 | $result .= $char; | |
202 | $ignorenext = FALSE; | |
203 | } | |
204 | else { | |
205 | switch ($char) { | |
206 | case '{': | |
207 | if ($inquote) { | |
208 | $result .= $char; | |
209 | } | |
210 | else { | |
211 | $inarray = FALSE; | |
212 | $tabcount++; | |
213 | $result .= $char . $newline . str_repeat($tab, $tabcount); | |
214 | } | |
215 | break; | |
216 | ||
217 | case '}': | |
218 | if ($inquote) { | |
219 | $result .= $char; | |
220 | } | |
221 | else { | |
222 | $tabcount--; | |
223 | $result = trim($result) . $newline . str_repeat($tab, $tabcount) . $char; | |
224 | } | |
225 | break; | |
226 | ||
227 | case ',': | |
228 | if ($inquote || $inarray) { | |
229 | $result .= $char; | |
230 | } | |
a3e55d9c TO |
231 | else { |
232 | $result .= $char . $newline . str_repeat($tab, $tabcount); | |
e7292422 | 233 | } |
6a488035 TO |
234 | break; |
235 | ||
236 | case '"': | |
237 | $inquote = !$inquote; | |
238 | $result .= $char; | |
239 | break; | |
240 | ||
241 | case '\\': | |
242 | if ($inquote) { | |
243 | $ignorenext = TRUE; | |
244 | } | |
245 | $result .= $char; | |
246 | break; | |
247 | ||
248 | case '[': | |
249 | $inarray = TRUE; | |
250 | $result .= $char; | |
251 | break; | |
252 | ||
253 | case ']': | |
254 | $inarray = FALSE; | |
255 | $result .= $char; | |
256 | break; | |
257 | ||
258 | default: | |
259 | $result .= $char; | |
260 | } | |
261 | } | |
262 | } | |
263 | ||
264 | return $result; | |
265 | } | |
266 | ||
5bc392e6 EM |
267 | /** |
268 | * @return array|int | |
269 | */ | |
00be9182 | 270 | public static function handle() { |
ba56a28f TO |
271 | $requestParams = CRM_Utils_Request::exportValues(); |
272 | ||
6a488035 | 273 | // Get the function name being called from the q parameter in the query string |
ba56a28f | 274 | $q = CRM_Utils_array::value('q', $requestParams); |
6a488035 | 275 | // or for the rest interface, from fnName |
ba56a28f | 276 | $r = CRM_Utils_array::value('fnName', $requestParams); |
6a488035 TO |
277 | if (!empty($r)) { |
278 | $q = $r; | |
279 | } | |
775d38eb | 280 | $entity = CRM_Utils_array::value('entity', $requestParams); |
481a74f4 | 281 | if (empty($entity) && !empty($q)) { |
6a488035 TO |
282 | $args = explode('/', $q); |
283 | // If the function isn't in the civicrm namespace, reject the request. | |
284 | if ($args[0] != 'civicrm') { | |
285 | return self::error('Unknown function invocation.'); | |
286 | } | |
287 | ||
288 | // If the query string is malformed, reject the request. | |
7f2d6a61 TO |
289 | // Does this mean it will reject it |
290 | if ((count($args) != 3) && ($args[1] != 'ping')) { | |
6a488035 TO |
291 | return self::error('Unknown function invocation.'); |
292 | } | |
293 | $store = NULL; | |
f813f78e | 294 | |
7f2d6a61 | 295 | if ($args[1] == 'ping') { |
6a488035 TO |
296 | return self::ping(); |
297 | } | |
0db6c3e1 TO |
298 | } |
299 | else { | |
6a488035 | 300 | // or the new format (entity+action) |
7f2d6a61 TO |
301 | $args = array(); |
302 | $args[0] = 'civicrm'; | |
ba56a28f TO |
303 | $args[1] = CRM_Utils_array::value('entity', $requestParams); |
304 | $args[2] = CRM_Utils_array::value('action', $requestParams); | |
6a488035 | 305 | } |
f813f78e | 306 | |
6a488035 TO |
307 | // Everyone should be required to provide the server key, so the whole |
308 | // interface can be disabled in more change to the configuration file. | |
6a488035 TO |
309 | // first check for civicrm site key |
310 | if (!CRM_Utils_System::authenticateKey(FALSE)) { | |
311 | $docLink = CRM_Utils_System::docURL2("Managing Scheduled Jobs", TRUE, NULL, NULL, NULL, "wiki"); | |
ba56a28f | 312 | $key = CRM_Utils_array::value('key', $requestParams); |
6a488035 TO |
313 | if (empty($key)) { |
314 | return self::error("FATAL: mandatory param 'key' missing. More info at: " . $docLink); | |
315 | } | |
316 | return self::error("FATAL: 'key' is incorrect. More info at: " . $docLink); | |
317 | } | |
318 | ||
7f2d6a61 TO |
319 | // At this point we know we are not calling ping which does not require authentication. |
320 | // Therefore, at this point we need to make sure we're working with a trusted user. | |
321 | // Valid users are those who provide a valid server key and API key | |
6a488035 TO |
322 | |
323 | $valid_user = FALSE; | |
324 | ||
7f2d6a61 TO |
325 | // Check and see if a valid secret API key is provided. |
326 | $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST'); | |
327 | if (!$api_key || strtolower($api_key) == 'null') { | |
328 | return self::error("FATAL: mandatory param 'api_key' (user key) missing"); | |
6a488035 | 329 | } |
7f2d6a61 | 330 | $valid_user = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key'); |
6a488035 | 331 | |
7f2d6a61 | 332 | // If we didn't find a valid user, die |
6a488035 | 333 | if (empty($valid_user)) { |
7f2d6a61 | 334 | return self::error("User API key invalid"); |
6a488035 TO |
335 | } |
336 | ||
a323a20f | 337 | return self::process($args, self::buildParamList()); |
6a488035 TO |
338 | } |
339 | ||
5bc392e6 EM |
340 | /** |
341 | * @param $args | |
c490a46a | 342 | * @param array $params |
5bc392e6 EM |
343 | * |
344 | * @return array|int | |
345 | */ | |
00be9182 | 346 | public static function process(&$args, $params) { |
6a488035 TO |
347 | $params['check_permissions'] = TRUE; |
348 | $fnName = $apiFile = NULL; | |
349 | // clean up all function / class names. they should be alphanumeric and _ only | |
350 | for ($i = 1; $i <= 3; $i++) { | |
351 | if (!empty($args[$i])) { | |
352 | $args[$i] = CRM_Utils_String::munge($args[$i]); | |
353 | } | |
354 | } | |
355 | ||
356 | // incase of ajax functions className is passed in url | |
357 | if (isset($params['className'])) { | |
358 | $params['className'] = CRM_Utils_String::munge($params['className']); | |
359 | ||
360 | // functions that are defined only in AJAX.php can be called via | |
361 | // rest interface | |
362 | if (!CRM_Core_Page_AJAX::checkAuthz('method', $params['className'], $params['fnName'])) { | |
363 | return self::error('Unknown function invocation.'); | |
364 | } | |
365 | ||
366 | return call_user_func(array($params['className'], $params['fnName']), $params); | |
367 | } | |
368 | ||
369 | if (!array_key_exists('version', $params)) { | |
370 | $params['version'] = 3; | |
371 | } | |
372 | ||
373 | if ($params['version'] == 2) { | |
374 | $result['is_error'] = 1; | |
375 | $result['error_message'] = "FATAL: API v2 not accessible from ajax/REST"; | |
376 | $result['deprecated'] = "Please upgrade to API v3"; | |
377 | return $result; | |
378 | } | |
379 | ||
481a74f4 | 380 | if ($_SERVER['REQUEST_METHOD'] == 'GET' && strtolower(substr($args[2], 0, 3)) != 'get') { |
f9e16e9a | 381 | // get only valid for non destructive methods |
6a488035 TO |
382 | require_once 'api/v3/utils.php'; |
383 | return civicrm_api3_create_error("SECURITY: All requests that modify the database must be http POST, not GET.", | |
384 | array( | |
385 | 'IP' => $_SERVER['REMOTE_ADDR'], | |
386 | 'level' => 'security', | |
387 | 'referer' => $_SERVER['HTTP_REFERER'], | |
388 | 'reason' => 'Destructive HTTP GET', | |
389 | ) | |
390 | ); | |
391 | } | |
392 | ||
393 | // trap all fatal errors | |
ca32aecc | 394 | $errorScope = CRM_Core_TemporaryErrorScope::create(array('CRM_Utils_REST', 'fatal')); |
6a488035 | 395 | $result = civicrm_api($args[1], $args[2], $params); |
ca32aecc | 396 | unset($errorScope); |
6a488035 TO |
397 | |
398 | if ($result === FALSE) { | |
399 | return self::error('Unknown error.'); | |
400 | } | |
401 | return $result; | |
402 | } | |
403 | ||
5bc392e6 EM |
404 | /** |
405 | * @return array|mixed|null | |
406 | */ | |
00be9182 | 407 | public static function &buildParamList() { |
ba56a28f | 408 | $requestParams = CRM_Utils_Request::exportValues(); |
6a488035 TO |
409 | $params = array(); |
410 | ||
411 | $skipVars = array( | |
412 | 'q' => 1, | |
413 | 'json' => 1, | |
414 | 'key' => 1, | |
415 | 'api_key' => 1, | |
416 | 'entity' => 1, | |
417 | 'action' => 1, | |
418 | ); | |
419 | ||
353ffa53 | 420 | if (array_key_exists('json', $requestParams) && $requestParams['json'][0] == "{") { |
ba56a28f | 421 | $params = json_decode($requestParams['json'], TRUE); |
22e263ad | 422 | if ($params === NULL) { |
ecdef330 | 423 | CRM_Utils_JSON::output(array('is_error' => 1, 'error_message', 'Unable to decode supplied JSON.')); |
6a488035 TO |
424 | } |
425 | } | |
ba56a28f | 426 | foreach ($requestParams as $n => $v) { |
6a488035 TO |
427 | if (!array_key_exists($n, $skipVars)) { |
428 | $params[$n] = $v; | |
429 | } | |
430 | } | |
ba56a28f | 431 | if (array_key_exists('return', $requestParams) && is_array($requestParams['return'])) { |
a3e55d9c TO |
432 | foreach ($requestParams['return'] as $key => $v) { |
433 | $params['return.' . $key] = 1; | |
e7292422 | 434 | } |
6a488035 TO |
435 | } |
436 | return $params; | |
437 | } | |
438 | ||
5bc392e6 EM |
439 | /** |
440 | * @param $pearError | |
441 | */ | |
00be9182 | 442 | public static function fatal($pearError) { |
6a488035 TO |
443 | header('Content-Type: text/xml'); |
444 | $error = array(); | |
445 | $error['code'] = $pearError->getCode(); | |
446 | $error['error_message'] = $pearError->getMessage(); | |
447 | $error['mode'] = $pearError->getMode(); | |
448 | $error['debug_info'] = $pearError->getDebugInfo(); | |
449 | $error['type'] = $pearError->getType(); | |
450 | $error['user_info'] = $pearError->getUserInfo(); | |
451 | $error['to_string'] = $pearError->toString(); | |
452 | $error['is_error'] = 1; | |
453 | ||
454 | echo self::output($error); | |
455 | ||
456 | CRM_Utils_System::civiExit(); | |
457 | } | |
458 | ||
00be9182 | 459 | public static function APIDoc() { |
6a488035 TO |
460 | |
461 | CRM_Utils_System::setTitle("API Parameters"); | |
462 | $template = CRM_Core_Smarty::singleton(); | |
463 | return CRM_Utils_System::theme( | |
464 | $template->fetch('CRM/Core/APIDoc.tpl') | |
465 | ); | |
466 | } | |
467 | ||
00be9182 | 468 | public static function ajaxDoc() { |
6024aa1b | 469 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/api/explorer')); |
6a488035 TO |
470 | } |
471 | ||
472 | /** used to load a template "inline", eg. for ajax, without having to build a menu for each template */ | |
353ffa53 | 473 | static function loadTemplate() { |
481a74f4 | 474 | $request = CRM_Utils_Request::retrieve('q', 'String'); |
e7292422 | 475 | if (FALSE !== strpos($request, '..')) { |
6a488035 TO |
476 | die ("SECURITY FATAL: the url can't contain '..'. Please report the issue on the forum at civicrm.org"); |
477 | } | |
478 | ||
d3e86119 | 479 | $request = explode('/', $request); |
6a488035 | 480 | $entity = _civicrm_api_get_camel_name($request[2]); |
e7292422 | 481 | $tplfile = _civicrm_api_get_camel_name($request[3]); |
6a488035 | 482 | |
92fcb95f | 483 | $tpl = 'CRM/' . $entity . '/Page/Inline/' . $tplfile . '.tpl'; |
481a74f4 TO |
484 | $smarty = CRM_Core_Smarty::singleton(); |
485 | CRM_Utils_System::setTitle("$entity::$tplfile inline $tpl"); | |
486 | if (!$smarty->template_exists($tpl)) { | |
6a488035 TO |
487 | header("Status: 404 Not Found"); |
488 | die ("Can't find the requested template file templates/$tpl"); | |
489 | } | |
e7292422 TO |
490 | if (array_key_exists('id', $_GET)) {// special treatmenent, because it's often used |
491 | $smarty->assign('id', (int) $_GET['id']);// an id is always positive | |
6a488035 | 492 | } |
e7292422 | 493 | $pos = strpos(implode(array_keys($_GET)), '<'); |
6a488035 | 494 | |
e7292422 | 495 | if ($pos !== FALSE) { |
6a488035 TO |
496 | die ("SECURITY FATAL: one of the param names contains <"); |
497 | } | |
481a74f4 | 498 | $param = array_map('htmlentities', $_GET); |
6a488035 TO |
499 | unset($param['q']); |
500 | $smarty->assign_by_ref("request", $param); | |
501 | ||
353ffa53 TO |
502 | if (!array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) || |
503 | $_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest" | |
504 | ) { | |
6a488035 | 505 | |
481a74f4 | 506 | $smarty->assign('tplFile', $tpl); |
e7292422 | 507 | $config = CRM_Core_Config::singleton(); |
353ffa53 | 508 | $content = $smarty->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl'); |
6a488035 | 509 | |
e7292422 TO |
510 | if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) { |
511 | CRM_Utils_System::addHTMLHead($region->render('')); | |
512 | } | |
481a74f4 | 513 | CRM_Utils_System::appendTPLFile($tpl, $content); |
6a488035 | 514 | |
e7292422 | 515 | return CRM_Utils_System::theme($content); |
6a488035 | 516 | |
0db6c3e1 TO |
517 | } |
518 | else { | |
e7292422 | 519 | $content = "<!-- .tpl file embeded: $tpl -->\n"; |
481a74f4 | 520 | CRM_Utils_System::appendTPLFile($tpl, $content); |
e7292422 | 521 | echo $content . $smarty->fetch($tpl); |
481a74f4 | 522 | CRM_Utils_System::civiExit(); |
6a488035 TO |
523 | } |
524 | } | |
525 | ||
526 | /** This is a wrapper so you can call an api via json (it returns json too) | |
527 | * http://example.org/civicrm/api/json?entity=Contact&action=Get"&json={"contact_type":"Individual","email.get.email":{}} to take all the emails from individuals | |
528 | * works for POST & GET (POST recommended) | |
529 | **/ | |
00be9182 | 530 | public static function ajaxJson() { |
ba56a28f TO |
531 | $requestParams = CRM_Utils_Request::exportValues(); |
532 | ||
6a488035 | 533 | require_once 'api/v3/utils.php'; |
ba56a28f | 534 | // Why is $config undefined -- $config = CRM_Core_Config::singleton(); |
7f2d6a61 | 535 | if (!$config->debug && (!array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) || |
6a488035 | 536 | $_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest" |
353ffa53 TO |
537 | ) |
538 | ) { | |
e4176358 | 539 | $error = civicrm_api3_create_error("SECURITY ALERT: Ajax requests can only be issued by javascript clients, eg. CRM.api3().", |
6a488035 TO |
540 | array( |
541 | 'IP' => $_SERVER['REMOTE_ADDR'], | |
542 | 'level' => 'security', | |
543 | 'referer' => $_SERVER['HTTP_REFERER'], | |
544 | 'reason' => 'CSRF suspected', | |
545 | ) | |
546 | ); | |
ecdef330 | 547 | CRM_Utils_JSON::output($error); |
6a488035 | 548 | } |
ba56a28f | 549 | if (empty($requestParams['entity'])) { |
ecdef330 | 550 | CRM_Utils_JSON::output(civicrm_api3_create_error('missing entity param')); |
6a488035 | 551 | } |
ba56a28f | 552 | if (empty($requestParams['entity'])) { |
ecdef330 | 553 | CRM_Utils_JSON::output(civicrm_api3_create_error('missing entity entity')); |
6a488035 | 554 | } |
ba56a28f TO |
555 | if (!empty($requestParams['json'])) { |
556 | $params = json_decode($requestParams['json'], TRUE); | |
6a488035 | 557 | } |
ba56a28f TO |
558 | $entity = CRM_Utils_String::munge(CRM_Utils_Array::value('entity', $requestParams)); |
559 | $action = CRM_Utils_String::munge(CRM_Utils_Array::value('action', $requestParams)); | |
6a488035 | 560 | if (!is_array($params)) { |
353ffa53 TO |
561 | CRM_Utils_JSON::output(array( |
562 | 'is_error' => 1, | |
563 | 'error_message', | |
564 | 'invalid json format: ?{"param_with_double_quote":"value"}' | |
565 | )); | |
6a488035 TO |
566 | } |
567 | ||
568 | $params['check_permissions'] = TRUE; | |
569 | $params['version'] = 3; | |
ba56a28f | 570 | $_GET['json'] = $requestParams['json'] = 1; // $requestParams is local-only; this line seems pointless unless there's a side-effect influencing other functions |
6a488035 TO |
571 | if (!$params['sequential']) { |
572 | $params['sequential'] = 1; | |
573 | } | |
ca32aecc | 574 | |
6a488035 | 575 | // trap all fatal errors |
ca32aecc | 576 | $errorScope = CRM_Core_TemporaryErrorScope::create(array('CRM_Utils_REST', 'fatal')); |
6a488035 | 577 | $result = civicrm_api($entity, $action, $params); |
ca32aecc | 578 | unset($errorScope); |
6a488035 TO |
579 | |
580 | echo self::output($result); | |
581 | ||
582 | CRM_Utils_System::civiExit(); | |
583 | } | |
584 | ||
00be9182 | 585 | public static function ajax() { |
ba56a28f TO |
586 | $requestParams = CRM_Utils_Request::exportValues(); |
587 | ||
6a488035 TO |
588 | // this is driven by the menu system, so we can use permissioning to |
589 | // restrict calls to this etc | |
590 | // the request has to be sent by an ajax call. First line of protection against csrf | |
591 | $config = CRM_Core_Config::singleton(); | |
7f2d6a61 | 592 | if (!$config->debug && |
6a488035 TO |
593 | (!array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) || |
594 | $_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest" | |
595 | ) | |
596 | ) { | |
597 | require_once 'api/v3/utils.php'; | |
a323a20f | 598 | $error = civicrm_api3_create_error("SECURITY ALERT: Ajax requests can only be issued by javascript clients, eg. CRM.api3().", |
6a488035 TO |
599 | array( |
600 | 'IP' => $_SERVER['REMOTE_ADDR'], | |
601 | 'level' => 'security', | |
602 | 'referer' => $_SERVER['HTTP_REFERER'], | |
603 | 'reason' => 'CSRF suspected', | |
604 | ) | |
605 | ); | |
ecdef330 | 606 | CRM_Utils_JSON::output($error); |
6a488035 TO |
607 | } |
608 | ||
ba56a28f | 609 | $q = CRM_Utils_Array::value('fnName', $requestParams); |
6a488035 | 610 | if (!$q) { |
ba56a28f TO |
611 | $entity = CRM_Utils_Array::value('entity', $requestParams); |
612 | $action = CRM_Utils_Array::value('action', $requestParams); | |
6a488035 TO |
613 | if (!$entity || !$action) { |
614 | $err = array('error_message' => 'missing mandatory params "entity=" or "action="', 'is_error' => 1); | |
615 | echo self::output($err); | |
616 | CRM_Utils_System::civiExit(); | |
617 | } | |
618 | $args = array('civicrm', $entity, $action); | |
619 | } | |
620 | else { | |
621 | $args = explode('/', $q); | |
622 | } | |
623 | ||
624 | // get the class name, since all ajax functions pass className | |
ba56a28f | 625 | $className = CRM_Utils_Array::value('className', $requestParams); |
6a488035 TO |
626 | |
627 | // If the function isn't in the civicrm namespace, reject the request. | |
628 | if (($args[0] != 'civicrm' && count($args) != 3) && !$className) { | |
629 | return self::error('Unknown function invocation.'); | |
630 | } | |
631 | ||
a323a20f CW |
632 | // Support for multiple api calls |
633 | if (isset($entity) && $entity === 'api3') { | |
634 | $result = self::processMultiple(); | |
635 | } | |
636 | else { | |
637 | $result = self::process($args, self::buildParamList()); | |
638 | } | |
6a488035 TO |
639 | |
640 | echo self::output($result); | |
641 | ||
642 | CRM_Utils_System::civiExit(); | |
643 | } | |
644 | ||
a323a20f CW |
645 | /** |
646 | * Callback for multiple ajax api calls from CRM.api3() | |
647 | * @return array | |
648 | */ | |
00be9182 | 649 | public static function processMultiple() { |
a323a20f CW |
650 | $output = array(); |
651 | foreach (json_decode($_REQUEST['json'], TRUE) as $key => $call) { | |
652 | $args = array( | |
653 | 'civicrm', | |
654 | $call[0], | |
655 | $call[1], | |
656 | ); | |
657 | $output[$key] = self::process($args, CRM_Utils_Array::value(2, $call, array())); | |
658 | } | |
659 | return $output; | |
660 | } | |
661 | ||
7f2d6a61 TO |
662 | /** |
663 | * @return array|NULL NULL if execution should proceed; array if the response is already known | |
664 | */ | |
00be9182 | 665 | public function loadCMSBootstrap() { |
ba56a28f TO |
666 | $requestParams = CRM_Utils_Request::exportValues(); |
667 | $q = CRM_Utils_array::value('q', $requestParams); | |
6a488035 TO |
668 | $args = explode('/', $q); |
669 | ||
7f2d6a61 TO |
670 | // Proceed with bootstrap for "?entity=X&action=Y" |
671 | // Proceed with bootstrap for "?q=civicrm/X/Y" but not "?q=civicrm/ping" | |
672 | if (!empty($q)) { | |
673 | if (count($args) == 2 && $args[1] == 'ping') { | |
674 | return NULL; // this is pretty wonky but maybe there's some reason I can't see | |
675 | } | |
676 | if (count($args) != 3) { | |
677 | return self::error('ERROR: Malformed REST path'); | |
678 | } | |
679 | if ($args[0] != 'civicrm') { | |
680 | return self::error('ERROR: Malformed REST path'); | |
681 | } | |
682 | // Therefore we have reasonably well-formed "?q=civicrm/X/Y" | |
6a488035 TO |
683 | } |
684 | ||
685 | if (!CRM_Utils_System::authenticateKey(FALSE)) { | |
7f2d6a61 TO |
686 | // FIXME: At time of writing, this doesn't actually do anything because |
687 | // authenticateKey abends, but that's a bad behavior which sends a | |
688 | // malformed response. | |
689 | return self::error('Failed to authenticate key'); | |
6a488035 TO |
690 | } |
691 | ||
692 | $uid = NULL; | |
6a488035 | 693 | if (!$uid) { |
353ffa53 TO |
694 | $store = NULL; |
695 | $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST'); | |
7f2d6a61 TO |
696 | if (empty($api_key)) { |
697 | return self::error("FATAL: mandatory param 'api_key' (user key) missing"); | |
698 | } | |
6a488035 TO |
699 | $contact_id = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key'); |
700 | if ($contact_id) { | |
701 | $uid = CRM_Core_BAO_UFMatch::getUFId($contact_id); | |
702 | } | |
703 | } | |
704 | ||
705 | if ($uid) { | |
706 | CRM_Utils_System::loadBootStrap(array('uid' => $uid), TRUE, FALSE); | |
7f2d6a61 TO |
707 | return NULL; |
708 | } | |
709 | else { | |
710 | return self::error('ERROR: No CMS user associated with given api-key'); | |
6a488035 TO |
711 | } |
712 | } | |
713 | } |