PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/smart-slider-3/nextend/library/nextend.php

https://bitbucket.org/wallindev/wallindev-wp
PHP | 136 lines | 113 code | 22 blank | 1 comment | 22 complexity | 91326edb50c28adfd34af17c7f310524 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. class N2 {
  3. public static $version = '2.0.21';
  4. public static $api = 'https://secure.nextendweb.com/api/api.php';
  5. public static function api($posts, $returnUrl = false) {
  6. if ($returnUrl) {
  7. $posts_default = array(
  8. 'platform' => N2Platform::getPlatform()
  9. );
  10. return self::$api . '?' . http_build_query($posts + $posts_default);
  11. }
  12. if (!isset($data)) {
  13. if (function_exists('curl_init') && function_exists('curl_exec') && N2Settings::get('curl', 1)) {
  14. $ch = curl_init();
  15. curl_setopt($ch, CURLOPT_URL, self::$api);
  16. $posts_default = array(
  17. 'platform' => N2Platform::getPlatform()
  18. );
  19. curl_setopt($ch, CURLOPT_POSTFIELDS, $posts + $posts_default);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  21. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
  22. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  23. if (N2Settings::get('curl-clean-proxy', 0)) {
  24. curl_setopt($ch, CURLOPT_PROXY, '');
  25. }
  26. $data = curl_exec($ch);
  27. $errorNumber = curl_errno($ch);
  28. if ($errorNumber == 60 || $errorNumber == 77) {
  29. curl_setopt($ch, CURLOPT_CAINFO, N2LIBRARY . '/cacert.pem');
  30. $data = curl_exec($ch);
  31. }
  32. $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  33. $error = curl_error($ch);
  34. $curlErrorNumber = curl_errno($ch);
  35. curl_close($ch);
  36. if ($curlErrorNumber) {
  37. N2Message::error($curlErrorNumber . $error);
  38. return array(
  39. 'status' => 'ERROR_HANDLED'
  40. );
  41. }
  42. } else {
  43. $posts_default = array(
  44. 'platform' => N2Platform::getPlatform()
  45. );
  46. $opts = array(
  47. 'http' => array(
  48. 'method' => 'POST',
  49. 'header' => 'Content-type: application/x-www-form-urlencoded',
  50. 'content' => http_build_query($posts + $posts_default)
  51. )
  52. );
  53. $context = stream_context_create($opts);
  54. $data = file_get_contents(self::$api, false, $context);
  55. if ($data === false) {
  56. N2Message::error(n2_('CURL disabled in your php.ini configuration. Please enable it!'));
  57. return array(
  58. 'status' => 'ERROR_HANDLED'
  59. );
  60. }
  61. $headers = self::parseHeaders($http_response_header);
  62. if ($headers['status'] != '200') {
  63. N2Message::error(n2_('Unable to contact with the licensing server, please try again later!'));
  64. return array(
  65. 'status' => 'ERROR_HANDLED'
  66. );
  67. }
  68. if (isset($headers['content-type'])) {
  69. $contentType = $headers['content-type'];
  70. }
  71. }
  72. }
  73. switch ($contentType) {
  74. case 'text/html; charset=UTF-8':
  75. //CloudFlare challenge
  76. preg_match('/"your_ip">.*?:[ ]*(.*?)<\/span>/', $data, $matches);
  77. if (count($matches)) {
  78. $blockedIP = $matches[1];
  79. N2Message::error(sprintf('Your ip address (%s) is blocked by our hosting provider.<br>Please contact us (support@nextendweb.com) with your ip to whitelist it.', $blockedIP));
  80. return array(
  81. 'status' => 'ERROR_HANDLED'
  82. );
  83. }
  84. N2Message::error(sprintf('Unexpected response from the API.<br>Please contact us (support@nextendweb.com) with the following log:') . '<br><textarea style="width: 100%;height:200px;font-size:8px;">' . base64_encode($data) . '</textarea>');
  85. return array(
  86. 'status' => 'ERROR_HANDLED'
  87. );
  88. break;
  89. case 'application/json':
  90. return json_decode($data, true);
  91. }
  92. return $data;
  93. }
  94. private static function parseHeaders(array $headers, $header = null) {
  95. $output = array();
  96. if ('HTTP' === substr($headers[0], 0, 4)) {
  97. list(, $output['status'], $output['status_text']) = explode(' ', $headers[0]);
  98. unset($headers[0]);
  99. }
  100. foreach ($headers as $v) {
  101. $h = preg_split('/:\s*/', $v);
  102. if (count($h) >= 2) {
  103. $output[strtolower($h[0])] = $h[1];
  104. }
  105. }
  106. if (null !== $header) {
  107. if (isset($output[strtolower($header)])) {
  108. return $output[strtolower($header)];
  109. }
  110. return;
  111. }
  112. return $output;
  113. }
  114. }