Cdn_RackSpaceCdn_Popup.php 15.6 KB
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 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
<?php
namespace W3TC;



class Cdn_RackSpaceCdn_Popup {
	static public function w3tc_ajax() {
		$o = new Cdn_RackSpaceCdn_Popup();

		add_action( 'w3tc_ajax_cdn_rackspace_intro',
			array( $o, 'w3tc_ajax_cdn_rackspace_intro' ) );
		add_action( 'w3tc_ajax_cdn_rackspace_intro_done',
			array( $o, 'w3tc_ajax_cdn_rackspace_intro_done' ) );
		add_action( 'w3tc_ajax_cdn_rackspace_regions_done',
			array( $o, 'w3tc_ajax_cdn_rackspace_regions_done' ) );
		add_action( 'w3tc_ajax_cdn_rackspace_services_done',
			array( $o, 'w3tc_ajax_cdn_rackspace_services_done' ) );
		add_action( 'w3tc_ajax_cdn_rackspace_service_create_done',
			array( $o, 'w3tc_ajax_cdn_rackspace_service_create_done' ) );
		add_action( 'w3tc_ajax_cdn_rackspace_service_get_state',
			array( $o, 'w3tc_ajax_cdn_rackspace_service_get_state' ) );
		add_action( 'w3tc_ajax_cdn_rackspace_service_created_done',
			array( $o, 'w3tc_ajax_cdn_rackspace_service_created_done' ) );
		add_action( 'w3tc_ajax_cdn_rackspace_service_actualize_done',
			array( $o, 'w3tc_ajax_cdn_rackspace_service_actualize_done' ) );

		add_action( 'w3tc_ajax_cdn_rackspace_configure_domains',
			array( $o, 'w3tc_ajax_cdn_rackspace_configure_domains' ) );
		add_action( 'w3tc_ajax_cdn_rackspace_configure_domains_done',
			array( $o, 'w3tc_ajax_cdn_rackspace_configure_domains_done' ) );

	}



	public function w3tc_ajax_cdn_rackspace_intro() {
		$c = Dispatcher::config();

		$details = array(
			'user_name' => $c->get_string( 'cdn.rackspace_cdn.user_name' ),
			'api_key' => $c->get_string( 'cdn.rackspace_cdn.api_key' )
		);

		include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Intro.php';
		exit();
	}



	public function w3tc_ajax_cdn_rackspace_intro_done() {
		$this->_render_cdn_rackspace_regions( array(
				'user_name' => $_REQUEST['user_name'],
				'api_key' => $_REQUEST['api_key'] ) );
	}



	private function _render_cdn_rackspace_regions( $details ) {
		$user_name = $details['user_name'];
		$api_key = $details['api_key'];

		try {
			$r = Cdn_RackSpace_Api_Tokens::authenticate( $user_name,
				$api_key );
		} catch ( \Exception $ex ) {
			$details = array(
				'user_name' => $user_name,
				'api_key' => $api_key,
				'error_message' => 'Can\'t authenticate: ' . $ex->getMessage()
			);
			include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Intro.php';
			exit();
		}

		$r['regions'] = Cdn_RackSpace_Api_Tokens::cdn_services_by_region(
			$r['services'] );

		$details['access_token'] = $r['access_token'];
		$details['region_descriptors'] = $r['regions'];
		// avoid fights with quotes, magic_quotes may break randomly
		$details['region_descriptors_serialized'] =
			strtr( json_encode( $r['regions'] ), '"\\', '!^' );

		include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Regions.php';
		exit();
	}



	public function w3tc_ajax_cdn_rackspace_regions_done() {
		$user_name = $_REQUEST['user_name'];
		$api_key = $_REQUEST['api_key'];
		$access_token = $_REQUEST['access_token'];
		$region = Util_Request::get( 'region' );
		$region_descriptors = json_decode(
			strtr( $_REQUEST['region_descriptors'], '!^', '"\\' ), true );

		if ( !isset( $region_descriptors[$region] ) ) {
			return $this->_render_cdn_rackspace_regions( array(
					'user_name' => $user_name,
					'api_key' => $api_key,
					'error_message' => 'Please select region ' . $region
				) );
		}

		$api = new Cdn_RackSpace_Api_Cdn( array(
				'access_token' => $access_token,
				'access_region_descriptor' => $region_descriptors[$region],
				'new_access_required' => ''
			) );

		try {
			$services = $api->services();
		} catch ( \Exception $ex ) {
			$details = array(
				'user_name' => $user_name,
				'api_key' => $api_key,
				'error_message' => $ex->getMessage()
			);
			include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Intro.php';
			exit();
		}

		$details = array(
			'user_name' => $user_name,
			'api_key' => $api_key,
			'access_token' => $access_token,
			'access_region_descriptor_serialized' =>
			strtr( json_encode( $region_descriptors[$region] ), '"\\', '!^' ),
			'region' => $region,
			// avoid fights with quotes, magic_quotes may break randomly
			'services' => $services
		);

		include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Services.php';
		exit();
	}



	public function w3tc_ajax_cdn_rackspace_services_done() {
		$user_name = $_REQUEST['user_name'];
		$api_key = $_REQUEST['api_key'];
		$access_token = $_REQUEST['access_token'];
		$access_region_descriptor = json_decode(
			strtr( $_REQUEST['access_region_descriptor'], '!^', '"\\' ), true );
		$region = $_REQUEST['region'];
		$service = Util_Request::get( 'service' );

		if ( !empty( $service ) ) {
			$this->_render_service_actualize( array(
					'user_name' => $user_name,
					'api_key' => $api_key,
					'access_token' => $access_token,
					'access_region_descriptor_serialized' =>
					strtr( json_encode( $access_region_descriptor ), '"\\', '!^' ),
					'region' => $region,
					'service_id' => $service
				) );
			exit();
		}

		$home_url = get_home_url();
		$parsed = parse_url( $home_url );

		$is_https = ( $parsed['scheme'] == 'https' );

		$details = array(
			'user_name' => $user_name,
			'api_key' => $api_key,
			'access_token' => $access_token,
			'access_region_descriptor_serialized' =>
			strtr( json_encode( $access_region_descriptor ), '"\\', '!^' ),
			'region' => $region,
			'name' => '',
			'protocol' => ( $is_https ? 'https' : 'http' ),
			'cname_http' => '',
			'cname_http_style' => ( $is_https ? 'display: none' : '' ),
			'cname_https_prefix' => '',
			'cname_https_style' => ( $is_https ? '' : 'display: none' ),
			'origin' => Util_Environment::home_url_host()
		);

		include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Service_Create.php';
		exit();
	}



	public function w3tc_ajax_cdn_rackspace_service_create_done() {
		$user_name = $_REQUEST['user_name'];
		$api_key = $_REQUEST['api_key'];
		$access_token = $_REQUEST['access_token'];
		$access_region_descriptor = json_decode(
			strtr( $_REQUEST['access_region_descriptor'], '!^', '"\\' ), true );
		$region = $_REQUEST['region'];

		$name = $_REQUEST['name'];
		$protocol = $_REQUEST['protocol'];
		$cname_http = $_REQUEST['cname_http'];
		$cname_https_prefix = $_REQUEST['cname_https_prefix'];

		$is_https = ( $protocol == 'https' );
		$cname = ( $is_https ? $cname_https_prefix : $cname_http );

		$api = new Cdn_RackSpace_Api_Cdn( array(
				'access_token' => $access_token,
				'access_region_descriptor' => $access_region_descriptor,
				'new_access_required' => ''
			) );

		$service_id = null;
		$access_url = null;

		try {
			$domain = array(
				'domain' => $cname,
				'protocol' => ( $is_https ? 'https' : 'http' )
			);
			if ( $is_https )
				$domain['certificate'] = 'shared';

			$service_id = $api->service_create( array(
					'name' => $name,
					'domains' => array( $domain ),
					'origins' => array(
						array(
							'origin' => Util_Environment::home_url_host(),
							'port' => ( $is_https ? 443 : 80 ),
							'ssl' => $is_https,
							'hostheadertype' => 'origin',
							'rules' => array()
						)
					),
					'caching' => array(
						array(
							'name' => 'default',
							'ttl' => 86400
						)
					)
				) );
		} catch ( \Exception $ex ) {
			$details = array(
				'user_name' => $user_name,
				'api_key' => $api_key,
				'access_token' => $access_token,
				'access_region_descriptor_serialized' =>
				strtr( json_encode( $access_region_descriptor ), '"\\', '!^' ),
				'region' => $region,
				'name' => $name,
				'protocol' => ( $is_https ? 'https' : 'http' ),
				'cname_http' => $cname_http,
				'cname_http_style' => ( $is_https ? 'display: none' : '' ),
				'cname_https_prefix' => $cname_https_prefix,
				'cname_https_style' => ( $is_https ? '' : 'display: none' ),
				'origin' => Util_Environment::home_url_host(),
				'error_message' => $ex->getMessage()
			);

			include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Service_Create.php';
			exit();
		}

		$details = array(
			'user_name' => $user_name,
			'api_key' => $api_key,
			'access_token' => $access_token,
			'access_region_descriptor_serialized' =>
			strtr( json_encode( $access_region_descriptor ), '"\\', '!^' ),
			'region' => $region,
			'name' => $name,
			'is_https' => $is_https,
			'cname' => $cname,
			'service_id' => $service_id
		);

		include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Service_Created.php';
	}



	// ajax returning json for js-script about service state
	public function w3tc_ajax_cdn_rackspace_service_get_state() {
		$access_token = $_REQUEST['access_token'];
		$access_region_descriptor = json_decode(
			strtr( $_REQUEST['access_region_descriptor'], '!^', '"\\' ), true );
		$service_id = $_REQUEST['service_id'];

		$api = new Cdn_RackSpace_Api_Cdn( array(
				'access_token' => $access_token,
				'access_region_descriptor' => $access_region_descriptor,
				'new_access_required' => ''
			) );

		$service = $api->service_get( $service_id );

		$response = array( 'status' => 'Unknown' );

		if ( isset( $service['status'] ) )
			$response['status'] = $service['status'];
		if ( isset( $service['links_by_rel']['access_url'] ) )
			$response['access_url'] = $service['links_by_rel']['access_url']['href'];
		if ( isset( $service['domains'] ) )
			$response['cname'] = $service['domains'][0]['domain'];

		// decode to friendly name
		if ( $response['status'] == 'create_in_progress' )
			$response['status'] = 'Creation in progress...';

		echo json_encode( $response );
	}



	public function w3tc_ajax_cdn_rackspace_service_created_done() {
		$this->_save_config();
	}



	private function _render_service_actualize( $details ) {
		$access_region_descriptor = json_decode(
			strtr( $details['access_region_descriptor_serialized'],
				'!^', '"\\' ), true );

		$api = new Cdn_RackSpace_Api_Cdn( array(
				'access_token' => $details['access_token'],
				'access_region_descriptor' => $access_region_descriptor,
				'new_access_required' => ''
			) );

		$service = null;
		try {
			$service = $api->service_get( $details['service_id'] );
		} catch ( \Exception $ex ) {
			$details['error_message'] = $ex->getMessage();
			include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Intro.php';
			exit();
		}

		$origin = '';
		$protocol = 'http';
		if ( isset( $service['origins'] ) && $service['origins'][0]['origin'] ) {
			$protocol = $service['origins'][0]['ssl'] ? 'https' : 'http';
			$origin = $service['origins'][0]['origin'];
		}

		$details['name'] = $service['name'];
		$details['protocol'] = $protocol;
		$details['origin'] = array(
			'current' => $origin,
			'new' => Util_Environment::home_url_host()
		);

		include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Service_Actualize.php';
		exit();
	}



	public function w3tc_ajax_cdn_rackspace_service_actualize_done() {
		$user_name = $_REQUEST['user_name'];
		$api_key = $_REQUEST['api_key'];
		$access_token = $_REQUEST['access_token'];
		$access_region_descriptor = json_decode(
			strtr( $_REQUEST['access_region_descriptor'], '!^', '"\\' ), true );
		$region = $_REQUEST['region'];
		$service_id = $_REQUEST['service_id'];

		$api = new Cdn_RackSpace_Api_Cdn( array(
				'access_token' => $access_token,
				'access_region_descriptor' => $access_region_descriptor,
				'new_access_required' => ''
			) );

		try {
			$service = $api->service_get( $service_id );

			$is_https = false;
			$origin = '';
			if ( isset( $service['origins'] ) && $service['origins'][0]['ssl'] ) {
				$is_https = $service['origins'][0]['ssl'];
				$origin = $service['origins'][0]['origin'];
			}

			$new_origin = Util_Environment::home_url_host();
			if ( $origin != $new_origin ) {
				$api->service_set( $service_id,
					array( array(
							'op' => 'replace',
							'path' => '/origins',
							'value' => array( array(
									'origin' => $new_origin,
									'port' => ( $is_https ? 443 : 80 ),
									'ssl' => $is_https,
									'hostheadertype' => 'origin',
									'rules' => array()
								) ) ) ) );
			}
		} catch ( \Exception $ex ) {
			$details = array(
				'user_name' => $user_name,
				'api_key' => $api_key,
				'error_message' => $ex->getMessage()
			);
			include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Intro.php';
			exit();
		}

		$this->_save_config();
	}



	private function _save_config() {
		$user_name = $_REQUEST['user_name'];
		$api_key = $_REQUEST['api_key'];
		$access_token = $_REQUEST['access_token'];
		$access_region_descriptor = json_decode(
			strtr( $_REQUEST['access_region_descriptor'], '!^', '"\\' ), true );
		$region = $_REQUEST['region'];
		$service_id = $_REQUEST['service_id'];

		$api = new Cdn_RackSpace_Api_Cdn( array(
				'access_token' => $access_token,
				'access_region_descriptor' => $access_region_descriptor,
				'new_access_required' => ''
			) );

		$service = $api->service_get( $service_id );

		$access_url = $service['links_by_rel']['access_url']['href'];
		$protocol = 'http';
		$domain = '';

		if ( isset( $service['domains'] ) && $service['domains'][0]['protocol'] ) {
			$protocol = $service['domains'][0]['protocol'];
			$domain = $service['domains'][0]['domain'];
		}

		$c = Dispatcher::config();

		$c->set( 'cdn.rackspace_cdn.user_name', $user_name );
		$c->set( 'cdn.rackspace_cdn.api_key', $api_key );
		$c->set( 'cdn.rackspace_cdn.region', $region );
		$c->set( 'cdn.rackspace_cdn.service.name', $service['name'] );
		$c->set( 'cdn.rackspace_cdn.service.id', $service_id );
		$c->set( 'cdn.rackspace_cdn.service.access_url', $access_url );
		$c->set( 'cdn.rackspace_cdn.service.protocol', $protocol );

		if ( $protocol != 'https' )
			$c->set( 'cdn.rackspace_cdn.domains', array( $domain ) );

		$c->save();

		// reset calculated state
		$state = Dispatcher::config_state();
		$state->set( 'cdn.rackspace_cdn.access_state', '' );
		$state->save();

		$postfix = Util_Admin::custom_message_id( array(),
			array(
				'cdn_configuration_saved' =>
				'CDN credentials are saved successfully' ) );
		echo 'Location admin.php?page=w3tc_cdn&' . $postfix;
		exit();
	}



	/**
	 * CNAMEs popup
	 */
	public function w3tc_ajax_cdn_rackspace_configure_domains() {
		$this->render_configure_domains_form();
		exit();
	}



	public function w3tc_ajax_cdn_rackspace_configure_domains_done() {
		$details = array(
			'cnames' => Util_Request::get_array( 'cdn_cnames' )
		);

		$core = Dispatcher::component( 'Cdn_Core' );
		$cdn = $core->get_cdn();

		try {
			// try to obtain CNAMEs
			$cdn->service_domains_set( $details['cnames'] );

			$c = Dispatcher::config();
			$c->set( 'cdn.rackspace_cdn.domains', $details['cnames'] );
			$c->save();

			$postfix = Util_Admin::custom_message_id( array(),
				array( 'cdn_cnames_saved' => 'CNAMEs are saved successfully' ) );
			echo 'Location admin.php?page=w3tc_cdn&' . $postfix;
			exit();
		} catch ( \Exception $ex ) {
			$details['error_message'] = $ex->getMessage();
		}

		$this->render_configure_domains_form( $details );
		exit();
	}



	private function render_configure_domains_form( $details = array() ) {
		if ( isset( $details['cnames'] ) )
			$cnames = $details['cnames'];
		else {
			$core = Dispatcher::component( 'Cdn_Core' );
			$cdn = $core->get_cdn();

			try {
				// try to obtain CNAMEs
				$cnames = $cdn->service_domains_get();
			} catch ( \Exception $ex ) {
				$details['error_message'] = $ex->getMessage();
				$cnames = array();
			}
		}

		include  W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_ConfigureDomains.php';
	}



	private function render_service_value_change( $details, $field ) {
		Util_Ui::hidden( '', $field, $details[$field]['new'] );

		if ( !isset( $details[$field]['current'] ) ||
			$details[$field]['current'] == $details[$field]['new'] )
			echo htmlspecialchars( $details[$field]['new'] );
		else {
			echo 'currently set to <strong>' .
				htmlspecialchars( empty( $details[$field]['current'] ) ?
				'<empty>' : $details[$field]['current'] ) .
				'</strong><br />';
			echo 'will be changed to <strong>' .
				htmlspecialchars( $details[$field]['new'] ) . '</strong><br />';
		}
	}
}