PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/lgorence/quickpress
PHP | 147 lines | 83 code | 19 blank | 45 comment | 8 complexity | 5fc91c5b7cb0fba5031629e52a616161 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /*
  3. Plugin Name: FileServe API
  4. Description: Check links to FileServe files using the FileServe API.
  5. Version: 1.0
  6. Author: Janis Elsts
  7. ModuleID: fileserve-checker
  8. ModuleCategory: checker
  9. ModuleContext: on-demand
  10. ModuleLazyInit: true
  11. ModuleClassName: blcFileServeChecker
  12. ModulePriority: 100
  13. ModuleCheckerUrlPattern: @^http://(?:www\.)?fileserve\.com/file/([\w\d]+?)(?:/|$|[?#])@i
  14. */
  15. /**
  16. * FileServe API link checker.
  17. *
  18. * @package Broken Link Checker
  19. * @author Janis Elsts
  20. * @access public
  21. */
  22. class blcFileServeChecker extends blcChecker {
  23. private $fileserve_api_url = 'http://app.fileserve.com/api/download/free/';
  24. /**
  25. * Determine if the checker can parse a specific URL.
  26. * Always returns true because the ModuleCheckerUrlPattern header constitutes sufficient verification.
  27. *
  28. * @param string $url
  29. * @param array $parsed
  30. * @return bool True.
  31. */
  32. function can_check($url, $parsed){
  33. return true;
  34. }
  35. /**
  36. * Check a FileServe link.
  37. *
  38. * See the FileServe API documentation for details:
  39. * http://app.fileserve.com/api/download/
  40. *
  41. * @param string $url File URL.
  42. * @return array
  43. */
  44. function check($url){
  45. $result = array(
  46. 'final_url' => $url,
  47. 'redirect_count' => 0,
  48. 'timeout' => false,
  49. 'broken' => false,
  50. 'log' => sprintf("<em>(%s)</em>\n\n", __('Using FileServe API', 'broken-link-checker')),
  51. 'result_hash' => '',
  52. 'status_code' => '',
  53. 'status_text' => '',
  54. );
  55. //We know the URL will match because ModuleCheckerUrlPattern matched.
  56. preg_match('@^http://(?:www\.)?fileserve\.com/file/([\w\d]+?)(?:/|$|[?#])@i', $url, $matches);
  57. $file_id = $matches[1];
  58. $conf = blc_get_configuration();
  59. $args = array(
  60. 'timeout' => $conf->options['timeout'],
  61. 'body' => array(
  62. 'shorten' => $file_id
  63. ),
  64. );
  65. $start = microtime_float();
  66. $response = wp_remote_post($this->fileserve_api_url, $args);
  67. $result['request_duration'] = microtime_float() - $start;
  68. $error_code = 0;
  69. if ( is_wp_error($response) ){
  70. $result['log'] .= "Error : " . $response->get_error_message();
  71. $result['broken'] = true;
  72. $result['http_code'] = 0;
  73. } else {
  74. $result['http_code'] = intval($response['response']['code']);
  75. if ( $result['http_code'] == 200 ){
  76. //In this case, the HTTP code returned by is not meaningful in itself,
  77. //so we won't store it or display it to the user.
  78. $result['http_code'] = 0;
  79. $json = json_decode($response['body'], false);
  80. if ( isset($json->error_code) ) {
  81. $error_code = intval($json->error_code);
  82. }
  83. $failure_codes = array(
  84. 310 => 'Invalid request',
  85. 403 => 'Not premium',
  86. 404 => 'Invalid link',
  87. 601 => 'Limited free download',
  88. 602 => 'Number of concurrent download exceeded',
  89. 603 => 'Too many invalid capcha',
  90. 605 => 'Expired premium',
  91. 606 => 'Invalid file ID',
  92. 607 => 'File not available',
  93. 608 => 'File not available',
  94. );
  95. if ( array_key_exists($error_code, $failure_codes) ) {
  96. $result['broken'] = true;
  97. $result['status_code'] = BLC_LINK_STATUS_ERROR;
  98. $result['status_text'] = __('Not Found', 'broken-link-checker');
  99. $result['log'] .= sprintf(
  100. __('FileServe : %d %s', 'broken-link-checker') . "\n",
  101. $error_code,
  102. $failure_codes[$error_code]
  103. );
  104. } else {
  105. $result['status_code'] = BLC_LINK_STATUS_OK;
  106. $result['status_text'] = _x('OK', 'link status', 'broken-link-checker');
  107. }
  108. //$result['log'] .= "API response :\n" . htmlspecialchars(print_r((array)$json, true));
  109. $result['log'] .= "API response :\n<code>" . htmlspecialchars($response['body'])."</code>\n";
  110. } else {
  111. //Unexpected error.
  112. $result['log'] .= $response['body'];
  113. $result['broken'] = true;
  114. }
  115. }
  116. //Generate the result hash (used for detecting false positives)
  117. $result['result_hash'] = implode('|', array(
  118. 'fileserve',
  119. $result['http_code'],
  120. $result['broken']?'broken':'0',
  121. $result['timeout']?'timeout':'0',
  122. $error_code
  123. ));
  124. return $result;
  125. }
  126. }