PageRenderTime 87ms CodeModel.GetById 52ms RepoModel.GetById 0ms app.codeStats 0ms

/content/plugins/w3-total-cache/Cdn_CloudFrontFsd_Popup.php

https://gitlab.com/karlen/ayo_wp
PHP | 345 lines | 280 code | 64 blank | 1 comment | 33 complexity | e9704659db24e44eefe0b777bed3e603 MD5 | raw file
  1. <?php
  2. namespace W3TC;
  3. class Cdn_CloudFrontFsd_Popup {
  4. static public function w3tc_ajax() {
  5. $o = new Cdn_CloudFrontFsd_Popup();
  6. add_action( 'w3tc_ajax_cdn_cloudfront_fsd_intro',
  7. array( $o, 'w3tc_ajax_cdn_cloudfront_fsd_intro' ) );
  8. add_action( 'w3tc_ajax_cdn_cloudfront_fsd_list_distributions',
  9. array( $o, 'w3tc_ajax_cdn_cloudfront_fsd_list_distributions' ) );
  10. add_action( 'w3tc_ajax_cdn_cloudfront_fsd_view_distribution',
  11. array( $o, 'w3tc_ajax_cdn_cloudfront_fsd_view_distribution' ) );
  12. add_action( 'w3tc_ajax_cdn_cloudfront_fsd_configure_distribution',
  13. array( $o, 'w3tc_ajax_cdn_cloudfront_fsd_configure_distribution' ) );
  14. }
  15. public function w3tc_ajax_cdn_cloudfront_fsd_intro() {
  16. $this->render_intro( array() );
  17. }
  18. private function render_intro( $details ) {
  19. $config = Dispatcher::config();
  20. $url_obtain_key = Util_Ui::url( array(
  21. 'page' => 'w3tc_dashboard',
  22. 'w3tc_cdn_maxcdn_authorize' => 'y'
  23. ) );
  24. include W3TC_DIR . '/Cdn_CloudFrontFsd_Popup_View_Intro.php';
  25. exit();
  26. }
  27. public function w3tc_ajax_cdn_cloudfront_fsd_list_distributions() {
  28. $access_key = $_REQUEST['access_key'];
  29. $secret_key = $_REQUEST['secret_key'];
  30. $api = new Cdn_CloudFrontFsd_Api( $access_key, $secret_key );
  31. if ( empty( $access_key ) || empty( $secret_key ) ) {
  32. $this->render_intro( array(
  33. 'error_message' => 'Can\'t authenticate: Access Key or Secret not valid'
  34. ) );
  35. exit();
  36. }
  37. try {
  38. $distributions = $api->distributions_list();
  39. } catch ( \Exception $ex ) {
  40. $error_message = 'Can\'t authenticate: ' . $ex->getMessage();
  41. $this->render_intro( array(
  42. 'error_message' => $error_message
  43. ) );
  44. exit();
  45. }
  46. $items = array();
  47. if ( isset( $distributions['Items']['DistributionSummary'] ) ) {
  48. foreach ( $distributions['Items']['DistributionSummary'] as $i ) {
  49. if ( empty( $i['Comment'] ) )
  50. $i['Comment'] = $i['DomainName'];
  51. if ( isset( $i['Origins']['Items']['Origin'] ) )
  52. $i['Origin_DomainName'] = $i['Origins']['Items']['Origin'][0]['DomainName'];
  53. $items[] = $i;
  54. }
  55. }
  56. $details = array(
  57. 'access_key' => $access_key,
  58. 'secret_key' => $secret_key,
  59. 'distributions' => $items
  60. );
  61. include W3TC_DIR . '/Cdn_CloudFrontFsd_Popup_View_Distributions.php';
  62. exit();
  63. }
  64. public function w3tc_ajax_cdn_cloudfront_fsd_view_distribution() {
  65. $access_key = $_REQUEST['access_key'];
  66. $secret_key = $_REQUEST['secret_key'];
  67. $distribution_id = Util_Request::get( 'distribution_id', '' );
  68. $details = array(
  69. 'access_key' => $access_key,
  70. 'secret_key' => $secret_key,
  71. 'distribution_id' => $distribution_id,
  72. 'distribution_comment' => '',
  73. 'origin' => array(
  74. 'new' => ''
  75. ),
  76. 'forward_querystring' => array(
  77. 'new' => true
  78. ),
  79. 'forward_cookies' => array(
  80. 'new' => true
  81. ),
  82. 'forward_host' => array(
  83. 'new' => true
  84. ),
  85. 'alias' => array(
  86. 'new' => Util_Environment::home_url_host()
  87. )
  88. );
  89. if ( empty( $distribution_id ) ) {
  90. // create new zone mode
  91. $details['distribution_comment'] = Util_Request::get( 'comment_new' );
  92. } else {
  93. $api = new Cdn_CloudFrontFsd_Api( $access_key, $secret_key );
  94. try {
  95. $distribution = $api->distribution_get( $distribution_id );
  96. } catch ( \Exception $ex ) {
  97. $this->render_intro( array(
  98. 'error_message' => 'Can\'t obtain zone: ' . $ex->getMessage()
  99. ) );
  100. exit();
  101. }
  102. if ( isset( $distribution['DistributionConfig'] ) )
  103. $c = $distribution['DistributionConfig'];
  104. else
  105. $c = array();
  106. if ( !empty( $c['Comment'] ) )
  107. $details['distribution_comment'] = $c['Comment'];
  108. else
  109. $details['distribution_comment'] = $c['DomainName'];
  110. if ( isset( $c['Origins']['Items']['Origin'] ) ) {
  111. $details['origin']['current'] =
  112. $c['Origins']['Items']['Origin'][0]['DomainName'];
  113. $details['origin']['new'] = $details['origin']['current'];
  114. }
  115. if ( isset( $c['DefaultCacheBehavior'] ) &&
  116. isset( $c['DefaultCacheBehavior']['ForwardedValues'] ) )
  117. $b = $c['DefaultCacheBehavior']['ForwardedValues'];
  118. else
  119. $b = array();
  120. $details['forward_querystring']['current'] =
  121. ( isset( $b['QueryString'] ) && $b['QueryString'] == 'true' );
  122. $details['forward_cookies']['current'] =
  123. ( isset( $b['Cookies'] ) && isset( $b['Cookies']['Forward'] ) &&
  124. $b['Cookies']['Forward'] == 'all' );
  125. if ( isset( $c['Aliases']['Items']['CNAME'][0] ) )
  126. $details['alias']['current'] = $c['Aliases']['Items']['CNAME'][0];
  127. $details['forward_host']['current'] = false;
  128. if ( isset( $b['Headers']['Items']['Name'] ) ) {
  129. foreach ( $b['Headers']['Items']['Name'] as $name )
  130. if ( $name == 'Host' )
  131. $details['forward_host']['current'] = true;
  132. }
  133. }
  134. include W3TC_DIR . '/Cdn_CloudFrontFsd_Popup_View_Distribution.php';
  135. exit();
  136. }
  137. private function render_zone_value_change( $details, $field ) {
  138. Util_Ui::hidden( '', $field, $details[$field]['new'] );
  139. if ( !isset( $details[$field]['current'] ) ||
  140. $details[$field]['current'] == $details[$field]['new'] )
  141. echo htmlspecialchars( $details[$field]['new'] );
  142. else {
  143. echo 'currently set to <strong>' .
  144. htmlspecialchars( empty( $details[$field]['current'] ) ?
  145. '<empty>' : $details[$field]['current'] ) .
  146. '</strong><br />';
  147. echo 'will be changed to <strong>' .
  148. htmlspecialchars( $details[$field]['new'] ) . '</strong><br />';
  149. }
  150. }
  151. private function render_zone_boolean_change( $details, $field ) {
  152. Util_Ui::hidden( '', $field, $details[$field]['new'] );
  153. if ( !isset( $details[$field]['current'] ) ) {
  154. echo 'will be set to <strong>';
  155. echo $this->render_zone_boolean( $details[$field]['new'] );
  156. echo '</strong>';
  157. } else if ( $details[$field]['current'] == $details[$field]['new'] ) {
  158. echo '<strong>';
  159. echo $this->render_zone_boolean( $details[$field]['new'] );
  160. echo '</strong>';
  161. } else {
  162. echo 'currently set to <strong>';
  163. $this->render_zone_boolean( $details[$field]['current'] );
  164. echo '</strong><br />';
  165. echo 'will be changed to <strong>';
  166. $this->render_zone_boolean( $details[$field]['new'] );
  167. echo '</strong><br />';
  168. }
  169. }
  170. private function render_zone_boolean( $v ) {
  171. if ( $v == 0 )
  172. echo 'disabled';
  173. else
  174. echo 'enabled';
  175. }
  176. private function render_zone_ip_change( $details, $field ) {
  177. Util_Ui::textbox( '', $field, $details[$field]['new'] );
  178. if ( isset( $details[$field]['current'] ) &&
  179. $details[$field]['current'] != $details[$field]['new'] ) {
  180. echo '<br /><span class="description">currently set to <strong>' .
  181. $details[$field]['current'] . '</strong></span>';
  182. }
  183. }
  184. public function w3tc_ajax_cdn_cloudfront_fsd_configure_distribution() {
  185. $access_key = $_REQUEST['access_key'];
  186. $secret_key = $_REQUEST['secret_key'];
  187. $distribution_id = Util_Request::get( 'distribution_id', '' );
  188. $origin_id = rand();
  189. $distribution = array(
  190. 'Comment' => Util_Request::get( 'distribution_comment' ),
  191. 'Origins' => array(
  192. 'Quantity' => 1,
  193. 'Items' => array(
  194. 'Origin' => array(
  195. 'Id' => $origin_id,
  196. 'DomainName' => Util_Request::get( 'origin' ),
  197. 'OriginPath' => '',
  198. 'CustomOriginConfig' => array(
  199. 'HTTPPort' => 80,
  200. 'HTTPSPort' => 443,
  201. 'OriginProtocolPolicy' => 'match-viewer'
  202. )
  203. )
  204. )
  205. ),
  206. 'Aliases' => array(
  207. 'Quantity' => 1,
  208. 'Items' => array(
  209. 'CNAME' => Util_Request::get( 'alias' )
  210. )
  211. ),
  212. 'DefaultCacheBehavior' => array(
  213. 'TargetOriginId' => $origin_id,
  214. 'ForwardedValues' => array(
  215. 'QueryString' => 'true',
  216. 'Cookies' => array(
  217. 'Forward' => 'all'
  218. ),
  219. 'Headers' => array(
  220. 'Quantity' => 1,
  221. 'Items' => array(
  222. 'Name' => 'Host'
  223. )
  224. )
  225. ),
  226. 'AllowedMethods' => array(
  227. 'Quantity' => 7,
  228. 'Items' => array(
  229. 'Method' => array(
  230. 'GET', 'HEAD', 'OPTIONS', 'PUT', 'POST', 'PATCH',
  231. 'DELETE'
  232. )
  233. ),
  234. 'CachedMethods' => array(
  235. 'Quantity' => 2,
  236. 'Items' => array(
  237. 'Method' => array(
  238. 'GET', 'HEAD'
  239. )
  240. )
  241. )
  242. ),
  243. 'MinTTL' => 0,
  244. )
  245. );
  246. try {
  247. $api = new Cdn_CloudFrontFsd_Api( $access_key, $secret_key );
  248. if ( empty( $distribution_id ) ) {
  249. $distribution['DefaultCacheBehavior']['TrustedSigners'] = array(
  250. 'Enabled' => 'false',
  251. 'Quantity' => 0
  252. );
  253. $distribution['DefaultCacheBehavior']['ViewerProtocolPolicy'] =
  254. 'allow-all';
  255. $response = $api->distribution_create( $distribution );
  256. $distribution_id = $response['Id'];
  257. } else {
  258. $response = $api->distribution_update( $distribution_id, $distribution );
  259. }
  260. } catch ( \Exception $ex ) {
  261. $this->render_intro( array(
  262. 'error_message' => 'Failed to configure distribution: ' . $ex->getMessage()
  263. ) );
  264. exit();
  265. }
  266. $distribution_domain = $response['DomainName'];
  267. $c = Dispatcher::config();
  268. $c->set( 'cdn.cloudfront_fsd.access_key', $access_key );
  269. $c->set( 'cdn.cloudfront_fsd.secret_key', $secret_key );
  270. $c->set( 'cdn.cloudfront_fsd.distribution_id', $distribution_id );
  271. $c->set( 'cdn.cloudfront_fsd.distribution_domain', $distribution_domain );
  272. $c->save();
  273. $details = array(
  274. 'name' => $distribution['Comment'],
  275. 'home_domain' => Util_Environment::home_url_host(),
  276. 'dns_cname_target' => $distribution_domain,
  277. );
  278. include W3TC_DIR . '/Cdn_CloudFrontFsd_Popup_View_Success.php';
  279. exit();
  280. }
  281. }