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