PageRenderTime 21ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/functions-http.php

http://yourls.googlecode.com/
PHP | 190 lines | 93 code | 33 blank | 64 comment | 25 complexity | 7aa1d00e6a759eef6614ec50d755fa45 MD5 | raw file
  1. <?php
  2. // TODO: improve this.
  3. // yourls_get_http_transport: use static vars
  4. // yourls_get_remote_content: return array( content, status, code )
  5. /**
  6. * Determine best transport for GET request. Return 'curl', 'fopen', 'fsockopen' or false if nothing works
  7. *
  8. * Order of preference: curl, fopen, fsockopen.
  9. *
  10. */
  11. function yourls_get_http_transport( $url ) {
  12. $transports = array();
  13. $scheme = parse_url( $url, PHP_URL_SCHEME );
  14. $is_ssl = ( $scheme == 'https' || $scheme == 'ssl' );
  15. // Test transports by order of preference, best first
  16. // curl
  17. if( function_exists( 'curl_init' ) && function_exists( 'curl_exec' ) )
  18. $transports[]= 'curl';
  19. // fopen. Doesn't work with https?
  20. if( !$is_ssl && function_exists( 'fopen' ) && ini_get( 'allow_url_fopen' ) )
  21. $transports[]= 'fopen';
  22. // fsock
  23. if( function_exists( 'fsockopen' ) )
  24. $transports[]= 'fsockopen';
  25. $best = ( $transports ? array_shift( $transports ) : false );
  26. return yourls_apply_filter( 'get_http_transport', $best, $transports );
  27. }
  28. /**
  29. * Get remote content via a GET request using best transport available
  30. *
  31. * Returns $content (might be an error message) or false if no transport available
  32. *
  33. */
  34. function yourls_get_remote_content( $url, $maxlen = 4096, $timeout = 5 ) {
  35. $url = yourls_sanitize_url( $url );
  36. $transport = yourls_get_http_transport( $url );
  37. if( $transport ) {
  38. $content = call_user_func( 'yourls_get_remote_content_'.$transport, $url, $maxlen, $timeout );
  39. } else {
  40. $content = false;
  41. }
  42. return yourls_apply_filter( 'get_remote_content', $content, $url, $maxlen, $timeout );
  43. }
  44. /**
  45. * Get remote content using curl. Needs sanitized $url. Returns $content or false
  46. *
  47. */
  48. function yourls_get_remote_content_curl( $url, $maxlen = 4096, $timeout = 5 ) {
  49. $ch = curl_init();
  50. curl_setopt( $ch, CURLOPT_URL, $url );
  51. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
  52. curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
  53. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  54. curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 ); // follow redirects...
  55. curl_setopt( $ch, CURLOPT_MAXREDIRS, 3 ); // ... but not more than 3
  56. curl_setopt( $ch, CURLOPT_USERAGENT, yourls_http_user_agent() );
  57. curl_setopt( $ch, CURLOPT_RANGE, "0-{$maxlen}" ); // Get no more than $maxlen
  58. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); // dont check SSL certificates
  59. curl_setopt( $ch, CURLOPT_HEADER, 0 );
  60. $response = curl_exec( $ch );
  61. if( !$response || curl_error( $ch ) ) {
  62. //$response = 'Error: '.curl_error( $ch );
  63. return false;
  64. }
  65. curl_close( $ch );
  66. return substr( $response, 0, $maxlen ); // substr in case CURLOPT_RANGE not supported
  67. }
  68. /**
  69. * Get remote content using fopen. Needs sanitized $url. Returns $content or false
  70. *
  71. */
  72. function yourls_get_remote_content_fopen( $url, $maxlen = 4096, $timeout = 5 ) {
  73. $content = false;
  74. $initial_timeout = @ini_set( 'default_socket_timeout', $timeout );
  75. $initial_user_agent = @ini_set( 'user_agent', yourls_http_user_agent() );
  76. // Basic error reporting shortcut
  77. set_error_handler( create_function('$code, $string', 'global $ydb; $ydb->fopen_error = $string;') );
  78. $fp = fopen( $url, 'r');
  79. if( $fp !== false ) {
  80. $buffer = min( $maxlen, 4096 );
  81. while ( !feof( $fp ) && !( strlen( $content ) >= $maxlen ) ) {
  82. $content .= fread( $fp, $buffer );
  83. }
  84. fclose( $fp );
  85. }
  86. if( $initial_timeout !== false )
  87. @ini_set( 'default_socket_timeout', $initial_timeout );
  88. if( $initial_user_agent !== false )
  89. @ini_set( 'user_agent', $initial_user_agent );
  90. restore_error_handler();
  91. if( !$content ) {
  92. //global $ydb;
  93. //$content = 'Error: '.strip_tags( $ydb->fopen_error );
  94. return false;
  95. }
  96. return $content;
  97. }
  98. /**
  99. * Get remote content using fsockopen. Needs sanitized $url. Returns $content or false
  100. *
  101. */
  102. function yourls_get_remote_content_fsockopen( $url, $maxlen = 4096, $timeout = 5 ) {
  103. // get the host name and url path
  104. $parsed_url = parse_url( $url );
  105. $host = $parsed_url['host'];
  106. if ( isset( $parsed_url['path'] ) ) {
  107. $path = $parsed_url['path'];
  108. } else {
  109. $path = '/'; // the url is pointing to the host like http://www.mysite.com
  110. }
  111. if ( isset( $parsed_url['query'] ) ) {
  112. $path .= '?' . $parsed_url['query'];
  113. }
  114. if ( isset( $parsed_url['port'] ) ) {
  115. $port = $parsed_url['port'];
  116. } else {
  117. $port = '80';
  118. }
  119. $response = false;
  120. // connect to the remote server
  121. $fp = @fsockopen( $host, $port, $errno, $errstr, $timeout );
  122. var_dump( $errno, $errstr );
  123. if( $fp !== false ) {
  124. // send some fake headers to mimick a standard browser
  125. fputs($fp, "GET $path HTTP/1.0\r\n" .
  126. "Host: $host\r\n" .
  127. "User-Agent: " . yourls_http_user_agent() . "\r\n" .
  128. "Accept: */*\r\n" .
  129. "Accept-Language: en-us,en;q=0.5\r\n" .
  130. "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" .
  131. "Keep-Alive: 300\r\n" .
  132. "Connection: keep-alive\r\n" .
  133. "Referer: http://$host\r\n\r\n");
  134. // retrieve the response from the remote server
  135. $buffer = min( $maxlen, 4096 );
  136. while ( !feof( $fp ) && !( strlen( $response ) >= $maxlen ) ) { // get more or less $maxlen bytes (between $maxlen and ($maxlen + ($maxlen-1)) actually)
  137. $response .= fread( $fp, $buffer );
  138. }
  139. fclose( $fp );
  140. } else {
  141. //$response = trim( "Error: #$errno. $errstr" );
  142. return false;
  143. }
  144. // return the file content
  145. return $response;
  146. }
  147. /**
  148. * Return funky user agent string
  149. *
  150. */
  151. function yourls_http_user_agent() {
  152. return yourls_apply_filter( 'http_user_agent', 'YOURLS v'.YOURLS_VERSION.' +http://yourls.org/ (running on '.YOURLS_SITE.')' );
  153. }