PageRenderTime 24ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/broken-link-checker/modules/extras/mediafire.php

https://bitbucket.org/lgorence/quickpress
PHP | 167 lines | 86 code | 16 blank | 65 comment | 19 complexity | 24e50ebebf46dc5e5e4b97c107858ef8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /*
  3. Plugin Name: MediaFire API
  4. Description: Check links to files hosted on MediaFire.
  5. Version: 1.0
  6. Author: Janis Elsts
  7. ModuleID: mediafire-checker
  8. ModuleCategory: checker
  9. ModuleContext: on-demand
  10. ModuleLazyInit: true
  11. ModuleClassName: blcMediaFireChecker
  12. ModulePriority: 100
  13. ModuleCheckerUrlPattern: @^http://(?:www\.)?mediafire\.com/(?:download\.php)?\?([0-9a-zA-Z]{11})(?:$|[^0-9a-zA-Z])@
  14. */
  15. /**
  16. * MediaFire link checker.
  17. *
  18. * @package Broken Link Checker
  19. * @author Janis Elsts
  20. * @access public
  21. */
  22. class blcMediaFireChecker extends blcChecker {
  23. /**
  24. * Determine if the checker can parse a specific URL.
  25. * Always returns true because the ModuleCheckerUrlPattern header constitutes sufficient verification.
  26. *
  27. * @param string $url
  28. * @param array $parsed
  29. * @return bool True.
  30. */
  31. function can_check($url, $parsed){
  32. return true;
  33. }
  34. /**
  35. * Check a MediaFire link.
  36. *
  37. * @param string $url
  38. * @return array
  39. */
  40. function check($url){
  41. $result = array(
  42. 'final_url' => $url,
  43. 'redirect_count' => 0,
  44. 'timeout' => false,
  45. 'broken' => false,
  46. 'log' => "<em>(Using MediaFire checker module)</em>\n\n",
  47. 'http_code' => 0,
  48. 'result_hash' => '',
  49. );
  50. //URLs like http://www.mediafire.com/download.php?03mj0mwmnnm are technically valid,
  51. //but they introduce unnecessary redirects.
  52. $url = str_replace('download.php','', $url);
  53. //Since MediaFire doesn't have an API, we just send a HEAD request
  54. //and try do divine the file state from the response headers.
  55. $start = microtime_float();
  56. $rez = $this->head($url);
  57. $result['request_duration'] = microtime_float() - $start;
  58. if ( is_wp_error($rez) ){
  59. //An unexpected error.
  60. $result['broken'] = true;
  61. $result['log'] .= "Error : " . $rez->get_error_message();
  62. if ( $data = $rez->get_error_data() ){
  63. $result['log'] .= "\n\nError data : " . print_r($data, true);
  64. }
  65. } else {
  66. $result['http_code'] = intval($rez['response']['code']);
  67. if ( $result['http_code'] == 200 ){
  68. //200 - OK
  69. $result['broken'] = false;
  70. $result['log'] .= "File OK";
  71. } elseif ( isset($rez['headers']['location']) ) {
  72. //Redirect = some kind of error. Redirects to an error page with an explanatory message.
  73. //The redirect URL is structured like this : '/error.php?errno=320'. The 'errno' argument
  74. //contains an (undocumented) error code. The only known value is 'errno=320', which
  75. //indicates that the file is invalid or has been deleted.
  76. $result['broken'] = true;
  77. if ( strpos($rez['headers']['location'], 'errno=320') !== false ){
  78. $result['status_code'] = BLC_LINK_STATUS_ERROR;
  79. $result['status_text'] = __('Not Found', 'broken-link-checker');
  80. $result['http_code'] = 0;
  81. $result['log'] .= "The file is invalid or has been removed.";
  82. } else if ( strpos($rez['headers']['location'], 'errno=378') !== false ){
  83. $result['status_code'] = BLC_LINK_STATUS_ERROR;
  84. $result['status_text'] = __('Not Found', 'broken-link-checker');
  85. $result['http_code'] = 0;
  86. $result['log'] .= "The file has been removed due to a violation of MediaFire ToS.";
  87. } else {
  88. $result['status_code'] = BLC_LINK_STATUS_INFO;
  89. $result['status_text'] = __('Unknown Error', 'broken-link-checker');
  90. $result['log'] .= "Unknown error.\n\n";
  91. foreach($rez['headers'] as $name => $value){
  92. $result['log'] .= printf("%s: %s\n", $name, $value);
  93. }
  94. }
  95. } else {
  96. $result['log'] .= "Unknown error.\n\n" . implode("\n",$rez['headers']);
  97. }
  98. }
  99. //Generate the result hash (used for detecting false positives)
  100. $result['result_hash'] = implode('|', array(
  101. 'mediafire',
  102. $result['http_code'],
  103. $result['broken']?'broken':'0',
  104. $result['timeout']?'timeout':'0'
  105. ));
  106. return $result;
  107. }
  108. /**
  109. * Perform a HEAD request to the specified URL.
  110. *
  111. * Note :
  112. *
  113. * Since the MediaFire checker works by parsing the "Location" header, redirect following
  114. * _must_ be disabled. This can become a problem on servers where WP is forced to fall back
  115. * on using WP_Http_Fopen which ignores the 'redirection' flag. WP_Http_Fsockopen would work,
  116. * but it has the lowest priority of all transports.
  117. *
  118. * Alas, there is no way to reliably influence which transport is chosen - the WP_Http::_getTransport
  119. * function caches the available choices, so plugins can disable individual transports only during
  120. * its first run. Therefore, we must pick the best transport manually.
  121. *
  122. * @param string $url
  123. * @return array|WP_Error
  124. */
  125. function head($url){
  126. //Only consider transports that allow redirection to be disabled.
  127. $args = array();
  128. if ( class_exists('WP_Http_ExtHttp') && (true === WP_Http_ExtHttp::test($args)) ) {
  129. $transport = new WP_Http_ExtHttp();
  130. } else if ( class_exists('WP_Http_Curl') && (true === WP_Http_Curl::test($args)) ) {
  131. $transport = new WP_Http_Curl();
  132. } else if ( class_exists('WP_Http_Curl') && (true === WP_Http_Fsockopen::test($args)) ) {
  133. $transport = new WP_Http_Fsockopen();
  134. } else {
  135. return new WP_Error(
  136. 'no_suitable_transport',
  137. "No suitable HTTP transport found. Please upgrade to a more recent version of PHP or install the CURL extension."
  138. );
  139. }
  140. $conf = blc_get_configuration();
  141. $args = array(
  142. 'timeout' => $conf->options['timeout'],
  143. 'redirection' => 0,
  144. 'method' => 'HEAD',
  145. );
  146. return $transport->request($url, $args);
  147. }
  148. }
  149. ?>