Skip to content
Snippets Groups Projects
Rest.php 10.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
    <?php
    /**
     * Rest controller class.
     *
     * @since 0.1
     */
    
    namespace CiviCRM_WP_REST\Controller;
    
    class Rest extends Base {
    
    	/**
    	 * The base route.
    	 *
    	 * @since 0.1
    	 * @var string
    	 */
    	protected $rest_base = 'rest';
    
    	/**
    	 * Registers routes.
    	 *
    	 * @since 0.1
    	 */
    	public function register_routes() {
    
    		register_rest_route( $this->get_namespace(), $this->get_rest_base(), [
    			[
    				'methods' => \WP_REST_Server::ALLMETHODS,
    				'callback' => [ $this, 'get_items' ],
    				'permission_callback' => [ $this, 'permissions_check' ],
    				'args' => $this->get_item_args()
    			],
    			'schema' => [ $this, 'get_item_schema' ]
    		] );
    
    	}
    
    	/**
    	 * Check get permission.
    	 *
    	 * @since 0.1
    	 * @param WP_REST_Request $request
    	 * @return bool
    	 */
    	public function permissions_check( $request ) {
    
    		if ( ! $this->is_valid_api_key( $request ) )
    			return $this->civi_rest_error( __( 'Param api_key is not valid.' ) );
    
    		if ( ! $this->is_valid_site_key() )
    			return $this->civi_rest_error( __( 'Param key is not valid.' ) );
    
    		return true;
    
    	}
    
    	/**
    	 * Get items.
    	 *
    	 * @since 0.1
    	 * @param WP_REST_Request $request
    	 */
    	public function get_items( $request ) {
    
    		/**
    		 * Filter formatted api params.
    		 *
    		 * @since 0.1
    		 * @param array $params
    		 * @param WP_REST_Request $request
    		 */
    		$params = apply_filters( 'civi_wp_rest/controller/rest/api_params', $this->get_formatted_api_params( $request ), $request );
    
    		try {
    
    			$items = civicrm_api3( ...$params );
    
    		} catch ( \CiviCRM_API3_Exception $e ) {
    
    			return $this->civi_rest_error( $e->getMessage() );
    
    		}
    
    		if ( ! isset( $items ) || empty( $items ) )
    			return rest_ensure_response( [] );
    
    		/**
    		 * Filter civi api result.
    		 *
    		 * @since 0.1
    		 * @param array $items
    		 * @param WP_REST_Request $request
    		 */
    		$data = apply_filters( 'civi_wp_rest/controller/rest/api_result', $items, $params, $request );
    
    		// only collections of items, ie any action but 'getsingle'
    		if ( isset( $data['values'] ) ) {
    
    			$data['values'] = array_reduce( $items['values'] ?? $items, function( $items, $item ) use ( $request ) {
    
    				$response = $this->prepare_item_for_response( $item, $request );
    
    				$items[] = $this->prepare_response_for_collection( $response );
    
    				return $items;
    
    			}, [] );
    
    		}
    
    		$response = rest_ensure_response( $data );
    
    		// check wheather we need to serve xml or json
    		if ( ! in_array( 'json', array_keys( $request->get_params() ) ) ) {
    
    			/**
    			 * Adds our response holding Civi data before dispatching.
    			 *
    			 * @since 0.1
    			 * @param WP_HTTP_Response $result Result to send to client
    			 * @param WP_REST_Server $server The REST server
    			 * @param WP_REST_Request $request The request
    			 * @return WP_HTTP_Response $result Result to send to client
    			 */
    			add_filter( 'rest_post_dispatch', function( $result, $server, $request ) use ( $response ) {
    
    				return $response;
    
    			}, 10, 3 );
    
    			// serve xml
    			add_filter( 'rest_pre_serve_request', [ $this, 'serve_xml_response' ], 10, 4 );
    
    		} else {
    
    			// return json
    			return $response;
    
    		}
    
    	}
    
    	/**
    	 * Get formatted api params.
    	 *
    	 * @since 0.1
    	 * @param WP_REST_Resquest $request
    	 * @return array $params
    	 */
    	public function get_formatted_api_params( $request ) {
    
    		$args = $request->get_params();
    
    		// destructure entity and action
    		[ 'entity' => $entity, 'action' => $action ] = $args;
    
    		// unset unnecessary args
    		unset( $args['entity'], $args['action'], $args['key'], $args['api_key'] );
    
    		if ( ! isset( $args['json'] ) || is_numeric( $args['json'] ) ) {
    
    			$params = $args;
    
    		} else {
    
    			$params = is_string( $args['json'] ) ? json_decode( $args['json'], true ) : [];
    
    		}
    
    		// ensure check permissions is enabled
    		$params['check_permissions'] = true;
    
    		return [ $entity, $action, $params ];
    
    	}
    
    	/**
    	 * Matches the item data to the schema.
    	 *
    	 * @since 0.1
    	 * @param object $item
    	 * @param WP_REST_Request $request
    	 */
    	public function prepare_item_for_response( $item, $request ) {
    
    		return rest_ensure_response( $item );
    
    	}
    
    	/**
    	 * Serves XML response.
    	 *
    	 * @since 0.1
    	 * @param bool $served Whether the request has already been served
    	 * @param WP_REST_Response $result
    	 * @param WP_REST_Request $request
    	 * @param WP_REST_Server $server
    	 */
    	public function serve_xml_response( $served, $result, $request, $server ) {
    
    		// get xml from response
    		$xml = $this->get_xml_formatted_data( $result->get_data() );
    
    		// set content type header
    		$server->send_header( 'Content-Type', 'text/xml' );
    
    		echo $xml;
    
    		return true;
    
    	}
    
    	/**
    	 * Formats CiviCRM API result to XML.
    	 *
    	 * @since 0.1
    	 * @param array $data The CiviCRM api result
    	 * @return string $xml The formatted xml
    	 */
    	protected function get_xml_formatted_data( array $data ) {
    
    		// xml document
    		$xml = new \DOMDocument();
    
    		// result set element <ResultSet>
    		$result_set = $xml->createElement( 'ResultSet' );
    
    		// xmlns:xsi attribute
    		$result_set->setAttribute( 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance' );
    
    		// count attribute
    		if ( isset( $data['count'] ) ) $result_set->setAttribute( 'count', $data['count'] );
    
    		// build result from result => values
    		if ( isset( $data['values'] ) ) {
    
    			array_map( function( $item ) use ( $result_set, $xml ) {
    
    				// result element <Result>
    				$result = $xml->createElement( 'Result' );
    
    				// format item
    				$result = $this->get_xml_formatted_item( $item, $result, $xml );
    
    				// append result to result set
    				$result_set->appendChild( $result );
    
    			}, $data['values'] );
    
    		} else {
    
    			// result element <Result>
    			$result = $xml->createElement( 'Result' );
    
    			// format item
    			$result = $this->get_xml_formatted_item( $data, $result, $xml );
    
    			// append result to result set
    			$result_set->appendChild( $result );
    
    		}
    
    		// append result set
    		$xml->appendChild( $result_set );
    
    		return $xml->saveXML();
    
    	}
    
    	/**
    	 * Formats a single api result to xml.
    	 *
    	 * @since 0.1
    	 * @param array $item The single api result
    	 * @param DOMElement $parent The parent element to append to
    	 * @param DOMDocument $doc The document
    	 * @return DOMElement $parent The parent element
    	 */
    	public function get_xml_formatted_item( array $item, \DOMElement $parent, \DOMDocument $doc ) {
    
    		// build field => values
    		array_map( function( $field, $value ) use ( $parent, $doc ) {
    
    			// entity field element
    			$element = $doc->createElement( $field );
    
    			// handle array values
    			if ( is_array( $value ) ) {
    
    				array_map( function( $key, $val ) use ( $element, $doc ) {
    
    					// child element, append underscore '_' otherwise createElement
    					// will throw an Invalid character exception as elements cannot start with a number
    					$child = $doc->createElement( '_' . $key, $val );
    
    					// append child
    					$element->appendChild( $child );
    
    				}, array_keys( $value ), $value );
    
    			} else {
    
    				// assign value
    				$element->nodeValue = $value;
    
    			}
    
    			// append element
    			$parent->appendChild( $element );
    
    		}, array_keys( $item ), $item );
    
    		return $parent;
    
    	}
    
    	/**
    	 * Item schema.
    	 *
    	 * @since 0.1
    	 * @return array $schema
    	 */
    	public function get_item_schema() {
    
    		return [
    			'$schema' => 'http://json-schema.org/draft-04/schema#',
    			'title' => 'civicrm/v3/rest',
    			'description' => 'CiviCRM API3 WP rest endpoint wrapper',
    			'type' => 'object',
    			'required' => [ 'entity', 'action', 'params' ],
    			'properties' => [
    				'is_error' => [
    					'type' => 'integer'
    				],
    				'version' => [
    					'type' => 'integer'
    				],
    				'count' => [
    					'type' => 'integer'
    				],
    				'values' => [
    					'type' => 'array'
    				]
    			]
    		];
    
    	}
    
    	/**
    	 * Item arguments.
    	 *
    	 * @since 0.1
    	 * @return array $arguments
    	 */
    	public function get_item_args() {
    
    		return [
    			'key' => [
    				'type' => 'string',
    				'required' => true,
    				'validate_callback' => function( $value, $request, $key ) {
    
    					return $this->is_valid_site_key();
    
    				}
    			],
    			'api_key' => [
    				'type' => 'string',
    				'required' => true,
    				'validate_callback' => function( $value, $request, $key ) {
    
    					return $this->is_valid_api_key( $request );
    
    				}
    			],
    			'entity' => [
    				'type' => 'string',
    				'required' => true,
    				'validate_callback' => function( $value, $request, $key ) {
    
    					return is_string( $value );
    
    				}
    			],
    			'action' => [
    				'type' => 'string',
    				'required' => true,
    				'validate_callback' => function( $value, $request, $key ) {
    
    					return is_string( $value );
    
    				}
    			],
    			'json' => [
    				'type' => ['integer', 'string', 'array'],
    				'required' => false,
    				'validate_callback' => function( $value, $request, $key ) {
    
    					return is_numeric( $value ) || is_array( $value ) || $this->is_valid_json( $value );
    
    				}
    			]
    		];
    
    	}
    
    	/**
    	 * Checks if string is a valid json.
    	 *
    	 * @since 0.1
    	 * @param string $param
    	 * @return bool
    	 */
    	protected function is_valid_json( $param ) {
    
    		$param = json_decode( $param, true );
    
    		if ( ! is_array( $param ) ) return false;
    
     		return ( json_last_error() == JSON_ERROR_NONE );
    
    	}
    
    	/**
    	 * Validates the site key.
    	 *
    	 * @since 0.1
    	 * @return bool $is_valid_site_key
    	 */
    	private function is_valid_site_key() {
    
    		return \CRM_Utils_System::authenticateKey( false );
    
    	}
    
    	/**
    	 * Validates the api key.
    	 *
    	 * @since 0.1
    	 * @param WP_REST_Resquest $request
    	 * @return bool $is_valid_api_key
    	 */
    	private function is_valid_api_key( $request ) {
    
    		$api_key = $request->get_param( 'api_key' );
    
    		if ( ! $api_key ) return false;
    
    		$contact_id = \CRM_Core_DAO::getFieldValue( 'CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key' );
    
    		// validate contact and login
    		if ( $contact_id ) {
    
    			$wp_user = $this->get_wp_user( $contact_id );
    
    			$this->do_user_login( $wp_user );
    
    			return true;
    
    		}
    
    		return false;
    
    	}
    
    	/**
    	 * Get WordPress user data.
    	 *
    	 * @since 0.1
    	 * @param int $contact_id The contact id
    	 * @return bool|WP_User $user The WordPress user data
    	 */
    	protected function get_wp_user( int $contact_id ) {
    
    		try {
    
    			// Get CiviCRM domain group ID from constant, if set.
    			$domain_id = defined( 'CIVICRM_DOMAIN_ID' ) ? CIVICRM_DOMAIN_ID : 0;
    
    			// If this fails, get it from config.
    			if ( $domain_id === 0 ) {
    				$domain_id = CRM_Core_Config::domainID();
    			}
    
    			// Call API.
    			$uf_match = civicrm_api3( 'UFMatch', 'getsingle', [
    				'contact_id' => $contact_id,
    				'domain_id' => $domain_id,
    			] );
    
    		} catch ( \CiviCRM_API3_Exception $e ) {
    
    			return $this->civi_rest_error( $e->getMessage() );
    
    		}
    
    		$wp_user = get_userdata( $uf_match['uf_id'] );
    
    		return $wp_user;
    
    	}
    
    	/**
    	 * Logs in the WordPress user, needed to respect CiviCRM ACL and permissions.
    	 *
    	 * @since 0.1
    	 * @param  WP_User $user
    	 */
    	protected function do_user_login( \WP_User $user ) {
    
    		if ( is_user_logged_in() ) return;
    
    		wp_set_current_user( $user->ID, $user->user_login );
    
    		wp_set_auth_cookie( $user->ID );
    
    		do_action( 'wp_login', $user->user_login, $user );
    
    	}
    
    }