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