/wp-content/plugins/wp-all-import-pro/helpers/get_file_curl.php

https://gitlab.com/najomie/fit-hippie · PHP · 171 lines · 127 code · 42 blank · 2 comment · 35 complexity · ea086e9aee7b6f9fdcacb63758dae541 MD5 · raw file

  1. <?php
  2. if ( ! function_exists('get_file_curl') ):
  3. function get_file_curl($url, $fullpath, $to_variable = false, $iteration = false) {
  4. if ( ! preg_match('%^(http|ftp)s?://%i', $url) ) return;
  5. $response = wp_remote_get($url);
  6. if ( ! is_wp_error($response) and ( ! isset($response['response']['code']) or isset($response['response']['code']) and ! in_array($response['response']['code'], array(401, 403, 404))) )
  7. {
  8. $rawdata = wp_remote_retrieve_body( $response );
  9. if (empty($rawdata))
  10. {
  11. $result = pmxi_curl_download($url, $fullpath, $to_variable);
  12. if ( ! $result and $iteration === false)
  13. {
  14. $new_url = wp_all_import_translate_uri($url);
  15. return ($new_url !== $url) ? get_file_curl($new_url, $fullpath, $to_variable, true) : $result;
  16. }
  17. return $result;
  18. }
  19. if ( ! @file_put_contents($fullpath, $rawdata) )
  20. {
  21. $fp = fopen($fullpath,'w');
  22. fwrite($fp, $rawdata);
  23. fclose($fp);
  24. }
  25. if ( preg_match('%\W(svg)$%i', basename($fullpath)) or preg_match('%\W(jpg|jpeg|gif|png)$%i', basename($fullpath)) and ( ! ($image_info = apply_filters('pmxi_getimagesize', @getimagesize($fullpath), $fullpath)) or ! in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) ) )
  26. {
  27. $result = pmxi_curl_download($url, $fullpath, $to_variable);
  28. if ( ! $result and $iteration === false)
  29. {
  30. $new_url = wp_all_import_translate_uri($url);
  31. return ($new_url !== $url) ? get_file_curl($new_url, $fullpath, $to_variable, true) : $result;
  32. }
  33. return $result;
  34. }
  35. return ($to_variable) ? $rawdata : true;
  36. }
  37. else
  38. {
  39. $curl = pmxi_curl_download($url, $fullpath, $to_variable);
  40. if ($curl === false and $iteration === false)
  41. {
  42. $new_url = wp_all_import_translate_uri($url);
  43. return ($new_url !== $url) ? get_file_curl($new_url, $fullpath, $to_variable, true) : ( is_wp_error($response) ? $response : false );
  44. }
  45. return ($curl === false) ? ( is_wp_error($response) ? $response : false ) : $curl;
  46. }
  47. }
  48. endif;
  49. if ( ! function_exists('pmxi_curl_download') ) {
  50. function pmxi_curl_download($url, $fullpath, $to_variable){
  51. if ( ! function_exists('curl_version') ) return false;
  52. $ch = curl_init($url);
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  54. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  55. $rawdata = curl_exec_follow($ch);
  56. $result = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  57. curl_close ($ch);
  58. if ( empty($rawdata) ) return false;
  59. if (!@file_put_contents($fullpath, $rawdata)){
  60. $fp = fopen($fullpath,'w');
  61. fwrite($fp, $rawdata);
  62. fclose($fp);
  63. }
  64. return ($result == 200) ? (($to_variable) ? $rawdata : true) : false;
  65. }
  66. }
  67. if ( ! function_exists('curl_exec_follow') ):
  68. function curl_exec_follow($ch, &$maxredirect = null) {
  69. $mr = $maxredirect === null ? 5 : intval($maxredirect);
  70. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
  71. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
  72. curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
  73. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  74. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  75. } else {
  76. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
  77. if ($mr > 0)
  78. {
  79. $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  80. $newurl = $original_url;
  81. $url_data = parse_url($newurl);
  82. if (!empty($url_data['user']) and !empty($url_data['pass'])){
  83. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY );
  84. curl_setopt($ch, CURLOPT_USERPWD, $url_data['user']. ":" . $url_data['pass']);
  85. $newurl = $url_data['scheme'] . '://' . $url_data['host'] . $url_data['path'];
  86. if (!empty($url_data['query']))
  87. {
  88. $newurl .= '?' . $url_data['query'];
  89. }
  90. }
  91. $rch = curl_copy_handle($ch);
  92. curl_setopt($rch, CURLOPT_HEADER, true);
  93. curl_setopt($rch, CURLOPT_NOBODY, true);
  94. curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
  95. curl_setopt($rch, CURLOPT_CONNECTTIMEOUT, 5);
  96. do
  97. {
  98. curl_setopt($rch, CURLOPT_URL, $newurl);
  99. $header = curl_exec($rch);
  100. if (curl_errno($rch)) {
  101. $code = 0;
  102. } else {
  103. $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
  104. if ($code == 301 || $code == 302) {
  105. preg_match('/Location:(.*?)\n/', $header, $matches);
  106. $newurl = trim(array_pop($matches));
  107. // if no scheme is present then the new url is a
  108. // relative path and thus needs some extra care
  109. if(!preg_match("/^https?:/i", $newurl)){
  110. $newurl = $original_url . $newurl;
  111. }
  112. } else {
  113. $code = 0;
  114. }
  115. }
  116. } while ($code && --$mr);
  117. curl_close($rch);
  118. if (!$mr)
  119. {
  120. if ($maxredirect !== null)
  121. $maxredirect = 0;
  122. return false;
  123. }
  124. curl_setopt($ch, CURLOPT_URL, $newurl);
  125. }
  126. }
  127. return curl_exec($ch);
  128. }
  129. endif;