PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/lgorence/quickpress
PHP | 269 lines | 154 code | 42 blank | 73 comment | 23 complexity | 5e52942446a381231841bd88c157e796 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Megaupload.com has been seized by the USA government, rendering this module irrelevant.
  4. */
  5. /*
  6. Plugin Name: MegaUpload API
  7. Description: Check links to MegaUpload files.
  8. Version: 1.0
  9. Author: Janis Elsts
  10. ModuleID: megaupload-checker
  11. ModuleCategory: checker
  12. ModuleContext: on-demand
  13. ModuleLazyInit: true
  14. ModuleClassName: blcMegaUploadChecker
  15. ModulePriority: 100
  16. ModuleCheckerUrlPattern: @^http://[\w\.]*?megaupload\.com/.*?(?:\?|&)d=([0-9A-Za-z]+)@
  17. */
  18. /**
  19. * MegaUpload API link checker.
  20. *
  21. * @package Broken Link Checker
  22. * @author Janis Elsts
  23. * @access public
  24. */
  25. class blcMegaUploadChecker extends blcChecker {
  26. /**
  27. * Determine if the checker can parse a specific URL.
  28. * Always returns true because the ModuleCheckerUrlPattern header constitutes sufficient verification.
  29. *
  30. * @param string $url
  31. * @param array $parsed
  32. * @return bool True.
  33. */
  34. function can_check($url, $parsed){
  35. return true;
  36. }
  37. /**
  38. * Check a MegaUpload link.
  39. *
  40. * @param string $url
  41. * @return array
  42. */
  43. function check($url){
  44. $result = array(
  45. 'final_url' => $url,
  46. 'redirect_count' => 0,
  47. 'timeout' => false,
  48. 'broken' => false,
  49. 'log' => "<em>(Using MegaUpload API)</em>\n\n",
  50. 'http_code' => 0,
  51. 'result_hash' => '',
  52. );
  53. //Extract the file ID from the URL (we know it's there because the module's URL pattern verifies that)
  54. $components = parse_url($url);
  55. parse_str($components['query'], $query);
  56. $file_id = $query['d'];
  57. $start = microtime_float();
  58. $info = $this->check_files($file_id);
  59. $result['request_duration'] = microtime_float() - $start;
  60. $file_status = 'unknown';
  61. if ( is_wp_error($info) ){
  62. //An unexpected error. Connection problems, IP blocks - it all goes here.
  63. $result['broken'] = true;
  64. if ( $data = $info->get_error_data() ){
  65. //Check for the "domain seized" message.
  66. $code = isset($data['response']['code']) ? $data['response']['code'] : 0;
  67. $body = isset($data['body']) ? $data['body'] : '';
  68. if ( ($code == 404) && (strpos($body, '<title>NOTICE</title>') !== false) ) {
  69. $result['log'] .= "The domain megaupload.com has been seized.";
  70. $result['status_code'] = BLC_LINK_STATUS_ERROR;
  71. $result['status_text'] = __('Not Found', 'broken-link-checker');
  72. $result['http_code'] = 404;
  73. } else {
  74. $result['log'] .= "Error : " . $info->get_error_message();
  75. $result['log'] .= "\n\nError data : " . print_r($data, true);
  76. }
  77. }
  78. } else {
  79. if ( array_key_exists($file_id, $info) ){
  80. $info = $info[$file_id];
  81. $file_status = $info['status'];
  82. switch($file_status){
  83. case '0': //OK
  84. $result['log'] .= 'File OK';
  85. $result['broken'] = false;
  86. if ( isset($info['name']) ){
  87. $result['log'] .= "\n" . sprintf(
  88. "Name : %s",
  89. $info['name']
  90. );
  91. }
  92. if ( isset($info['size']) ){
  93. $result['log'] .= "\n" . sprintf(
  94. "Size : %.0f KiB",
  95. round( floatval($info['size']) / 1024 )
  96. );
  97. }
  98. $result['status_code'] = BLC_LINK_STATUS_OK;
  99. $result['status_text'] = _x('OK', 'link status', 'broken-link-checker');
  100. break;
  101. case '1': //Invalid/removed
  102. $result['log'] .= 'File Not Found';
  103. $result['broken'] = true;
  104. $result['status_code'] = BLC_LINK_STATUS_ERROR;
  105. $result['status_text'] = __('Not Found', 'broken-link-checker');
  106. break;
  107. case '3': //Temporarily unavailable
  108. $result['log'] .= 'File Temporarily Unavailable';
  109. $result['broken'] = true;
  110. $result['status_code'] = BLC_LINK_STATUS_WARNING;
  111. $result['status_text'] = __('File Temporarily Unavailable', 'broken-link-checker');
  112. break;
  113. default: //Other codes are not documented anywhere.
  114. $result['log'] .= 'Received an unknown response code : ' . $file_status;
  115. $result['status_code'] = BLC_LINK_STATUS_INFO;
  116. $result['status_text'] = __('API Error', 'broken-link-checker');
  117. }
  118. } else {
  119. $result['log'] = "No info about file $file_id returned.";
  120. }
  121. }
  122. //Generate the result hash (used for detecting false positives)
  123. $result['result_hash'] = implode('|', array(
  124. 'megaupload',
  125. $result['http_code'],
  126. $result['broken']?'broken':'0',
  127. $result['timeout']?'timeout':'0',
  128. $file_status
  129. ));
  130. return $result;
  131. }
  132. /**
  133. * Check the status of one or more MegaUpload files.
  134. *
  135. * The MegaUpload API that is used in this function isn't documented anywhere.
  136. * The input and output data formats were reverse-engineered by sniffing the
  137. * HTTP requests made by the "Mega Manager" tool.
  138. *
  139. * @param array|string $file_ids
  140. * @return array|WP_Error
  141. */
  142. function check_files($file_ids){
  143. if ( is_string($file_ids) ){
  144. $file_ids = array($file_ids);
  145. }
  146. //The API expects input in this format : id0=file1id&id1=file2id&...
  147. $request_ids = array();
  148. $counter = 0;
  149. foreach($file_ids as $file_id){
  150. $id = 'id' . $counter;
  151. $request_ids[$id] = $file_id;
  152. $counter++;
  153. }
  154. $conf = blc_get_configuration();
  155. $args = array(
  156. 'timeout' => $conf->options['timeout'],
  157. 'body' => $request_ids,
  158. );
  159. //Submit the request
  160. $rez = wp_remote_post('http://www.megaupload.com/mgr_linkcheck.php', $args);
  161. if ( is_wp_error($rez) ){
  162. return $rez;
  163. }
  164. if ( ($rez['response']['code'] == 200) && (!empty($rez['body'])) ){
  165. $api_results = $this->parse_api_response($rez['body']);
  166. $results = array();
  167. //Resort the results by real file IDs
  168. foreach($api_results as $id => $file_info){
  169. if ( !array_key_exists($id, $request_ids) ){
  170. continue;
  171. }
  172. $results[$request_ids[$id]] = $file_info;
  173. }
  174. return $results;
  175. } else {
  176. return new WP_Error(
  177. 'megaupload_api_error',
  178. "MegaUpload API Error",
  179. $rez
  180. );
  181. }
  182. }
  183. /**
  184. * Parse a response received from the MegaUpload file check API
  185. *
  186. * @param string $response
  187. * @return array
  188. */
  189. function parse_api_response($response){
  190. /*
  191. The API response looks like this :
  192. 0=www.megaupload.com&1=www.megaporn.com&id0=0&s=filesize&d=0&n=filename&id1=0&s=filesize&d=0&n=filename...
  193. Despite appearances, it is not actually a valid query string. Each "idX=..." piece
  194. needs to be parsed separately.
  195. */
  196. $pieces = preg_split('@&(?=id\d+=)@', $response);
  197. $results = array();
  198. foreach($pieces as $piece){
  199. //Skip the superfluous response fragments that don't begin with an ID
  200. if ( substr($piece, 0, 2) != 'id' ){
  201. continue;
  202. }
  203. //Extract the "idX" key that identifies files in the request
  204. $id = substr($piece, 0, strpos($piece, '='));
  205. //The per-file data can be parsed as a query string
  206. parse_str($piece, $raw_data);
  207. //Reformat
  208. $file_data = array();
  209. $file_data['status'] = $raw_data[$id];
  210. if ( isset($raw_data['s']) ){
  211. $file_data['size'] = $raw_data['s'];
  212. }
  213. if ( isset($raw_data['n']) ){
  214. $file_data['name'] = $raw_data['n'];
  215. }
  216. if ( isset($raw_data['d']) ){ //No idea what this key is for.
  217. $file_data['d'] = $raw_data['d'];
  218. }
  219. $results[$id] = $file_data;
  220. }
  221. return $results;
  222. }
  223. }
  224. ?>