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