PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/lgorence/quickpress
PHP | 215 lines | 112 code | 28 blank | 75 comment | 9 complexity | 10618b34017c48f8cb102493c93aae7c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /*
  3. Plugin Name: RapidShare API
  4. Description: Check links to RapidShare files using the RapidShare API.
  5. Version: 1.0
  6. Author: Janis Elsts
  7. ModuleID: rapidshare-checker
  8. ModuleCategory: checker
  9. ModuleContext: on-demand
  10. ModuleLazyInit: true
  11. ModuleClassName: blcRapidShareChecker
  12. ModulePriority: 100
  13. ModuleCheckerUrlPattern: @^https?://(?:[\w\d]+\.)*rapidshare\.\w+/files/(\d+)/([^&?#/]+?)(?:$|[&?#/])@i
  14. */
  15. /**
  16. * RapidShare API link checker.
  17. *
  18. * @package Broken Link Checker
  19. * @author Janis Elsts
  20. * @access public
  21. */
  22. class blcRapidShareChecker 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 RapidShare file.
  36. *
  37. * @param string $url File 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' => sprintf("<em>(%s)</em>\n\n", __('Using RapidShare API', 'broken-link-checker')),
  47. 'result_hash' => '',
  48. 'status_code' => '',
  49. 'status_text' => '',
  50. );
  51. //We know the URL will match because ModuleCheckerUrlPattern matched.
  52. preg_match('@^https?://(?:[\w\d]+\.)*rapidshare\.\w+/files/(\d+)/([^&?#/]+?)(?:$|[&?#/])@i', $url, $matches);
  53. $file_id = $matches[1];
  54. $file_name = $matches[2];
  55. /*
  56. We use the checkfiles function to check file status. The RapidShare API docs can be found here :
  57. http://images.rapidshare.com/apidoc.txt
  58. The relevant function is documented thusly :
  59. sub=checkfiles
  60. Description:
  61. Gets status details about a list of given files. (files parameter limited to 3000 bytes.
  62. filenames parameter limited to 30000 bytes.)
  63. Parameters:
  64. files=comma separated list of file ids
  65. filenames=comma separated list of the respective filename. Example: files=50444381,50444382 filenames=test1.rar,test2.rar
  66. Reply fields:
  67. 1:File ID
  68. 2:Filename
  69. 3:Size (in bytes. If size is 0, this file does not exist.)
  70. 4:Server ID
  71. 5:Status integer, which can have the following numeric values:
  72. 0=File not found
  73. 1=File OK
  74. 3=Server down
  75. 4=File marked as illegal
  76. 6:Short host (Use the short host to get the best download mirror: http://rs$serverid$shorthost.rapidshare.com/files/$fileid/$filename)
  77. 7:MD5 (hexadecimal)
  78. Reply format: integer,string,integer,integer,integer,string,string
  79. */
  80. $api_url = sprintf(
  81. 'http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=checkfiles&files=%d&filenames=%s',
  82. $file_id,
  83. $file_name
  84. );
  85. $conf = blc_get_configuration();
  86. $args = array( 'timeout' => $conf->options['timeout'], );
  87. $start = microtime_float();
  88. $response = wp_remote_get($api_url, $args);
  89. $result['request_duration'] = microtime_float() - $start;
  90. $file_status = 0;
  91. $file_status_text = '';
  92. //Is the response valid?
  93. if ( is_wp_error($response) ){
  94. $result['log'] .= "Error : " . $response->get_error_message();
  95. $result['broken'] = true;
  96. $result['http_code'] = 0;
  97. } else {
  98. $result['http_code'] = intval($response['response']['code']);
  99. if ( $result['http_code'] == 200 ){
  100. //Parse the API response
  101. $data = explode(',', $response['body']);
  102. //Check file status
  103. if ( isset($data[4]) ){
  104. $file_status = intval($data[4]);
  105. $file_status_text = '';
  106. if ( $file_status >= 0 && $file_status <= 6 ){
  107. //Lets not confuse the user by showing the HTTP code we got from the API.
  108. //It's always "200" - whether the file exists or not.
  109. $result['http_code'] = 0;
  110. }
  111. switch( $file_status ){
  112. case 0:
  113. $file_status_text = 'File not found';
  114. $result['broken'] = true;
  115. $result['status_code'] = BLC_LINK_STATUS_ERROR;
  116. $result['status_text'] = __('Not Found', 'broken-link-checker');
  117. break;
  118. case 1:
  119. $file_status_text = 'File OK (Anonymous downloading)';
  120. $result['status_code'] = BLC_LINK_STATUS_OK;
  121. $result['status_text'] = _x('OK', 'link status', 'broken-link-checker');
  122. break;
  123. case 2:
  124. $file_status_text = 'File OK (TrafficShare direct download without any logging)';
  125. $result['status_code'] = BLC_LINK_STATUS_OK;
  126. $result['status_text'] = _x('OK', 'link status', 'broken-link-checker');
  127. break;
  128. case 3:
  129. $file_status_text = 'Server down';
  130. $result['broken'] = true;
  131. $result['status_code'] = BLC_LINK_STATUS_WARNING;
  132. $result['status_text'] = __('RS Server Down', 'broken-link-checker');
  133. break;
  134. case 4:
  135. $file_status_text = 'File marked as illegal';
  136. $result['broken'] = true;
  137. $result['status_code'] = BLC_LINK_STATUS_ERROR;
  138. $result['status_text'] = __('File Blocked', 'broken-link-checker');
  139. break;
  140. case 5:
  141. $file_status_text = 'Anonymous file locked because it has more than 10 downloads';
  142. $result['broken'] = true;
  143. $result['status_code'] = BLC_LINK_STATUS_WARNING;
  144. $result['status_text'] = __('File Locked', 'broken-link-checker');
  145. break;
  146. case 6:
  147. $file_status_text = 'File OK (TrafficShare direct download with enabled logging)';
  148. $result['status_code'] = BLC_LINK_STATUS_OK;
  149. $result['status_text'] = _x('OK', 'link status', 'broken-link-checker');
  150. break;
  151. }
  152. $result['log'] .= sprintf(
  153. __('RapidShare : %s', 'broken-link-checker'),
  154. $file_status_text
  155. );
  156. } else {
  157. $result['log'] .= sprintf(
  158. __('RapidShare API error: %s', 'broken-link-checker'),
  159. $response['body']
  160. );
  161. }
  162. } else {
  163. //Unexpected error.
  164. $result['log'] .= $response['body'];
  165. $result['broken'] = true;
  166. }
  167. }
  168. //Generate the result hash (used for detecting false positives)
  169. $result['result_hash'] = implode('|', array(
  170. 'rapidshare',
  171. $result['http_code'],
  172. $result['broken']?'broken':'0',
  173. $result['timeout']?'timeout':'0',
  174. $file_status
  175. ));
  176. return $result;
  177. }
  178. }
  179. ?>