xrdaction.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * @package WebFingerPlugin
  21. * @author James Walker <james@status.net>
  22. * @author Mikael Nordfeldth <mmn@hethane.se>
  23. */
  24. if (!defined('GNUSOCIAL')) { exit(1); }
  25. abstract class XrdAction extends ManagedAction
  26. {
  27. // json or xml for now, this may still be overriden because of
  28. // our back-compatibility with StatusNet <=1.1.1
  29. protected $defaultformat = null;
  30. protected $xrd = null;
  31. public function isReadOnly($args)
  32. {
  33. return true;
  34. }
  35. /*
  36. * Configures $this->xrd which will later be printed. Must be
  37. * implemented by child classes.
  38. */
  39. abstract protected function setXRD();
  40. protected function prepare(array $args=array())
  41. {
  42. if (!isset($args['format'])) {
  43. $args['format'] = $this->defaultformat;
  44. }
  45. parent::prepare($args);
  46. $this->xrd = new XML_XRD();
  47. return true;
  48. }
  49. protected function handle()
  50. {
  51. $this->setXRD();
  52. if (common_config('discovery', 'cors')) {
  53. header('Access-Control-Allow-Origin: *');
  54. }
  55. parent::handle();
  56. }
  57. public function mimeType()
  58. {
  59. try {
  60. return $this->checkAccept();
  61. } catch (Exception $e) {
  62. $supported = Discovery::supportedMimeTypes();
  63. $docformat = $this->arg('format');
  64. if (!empty($docformat) && isset($supported[$docformat])) {
  65. return $supported[$docformat];
  66. }
  67. }
  68. /*
  69. * "A WebFinger resource MUST return a JRD as the representation
  70. * for the resource if the client requests no other supported
  71. * format explicitly via the HTTP "Accept" header. [...]
  72. * The WebFinger resource MUST silently ignore any requested
  73. * representations that it does not understand and support."
  74. * -- RFC 7033 (WebFinger)
  75. * http://tools.ietf.org/html/rfc7033
  76. */
  77. return Discovery::JRD_MIMETYPE;
  78. }
  79. public function showPage()
  80. {
  81. $mimeType = $this->mimeType();
  82. header("Content-type: {$mimeType}");
  83. switch ($mimeType) {
  84. case Discovery::XRD_MIMETYPE:
  85. print $this->xrd->toXML();
  86. break;
  87. case Discovery::JRD_MIMETYPE:
  88. case Discovery::JRD_MIMETYPE_OLD:
  89. print $this->xrd->to('json');
  90. break;
  91. default:
  92. throw new Exception(_('No supported MIME type in Accept header.'));
  93. }
  94. }
  95. protected function checkAccept()
  96. {
  97. $type = null;
  98. $httpaccept = isset($_SERVER['HTTP_ACCEPT'])
  99. ? $_SERVER['HTTP_ACCEPT'] : null;
  100. $useragent = isset($_SERVER['HTTP_USER_AGENT'])
  101. ? $_SERVER['HTTP_USER_AGENT'] : null;
  102. if ($httpaccept !== null && $httpaccept != '*/*') {
  103. $can_serve = implode(',', Discovery::supportedMimeTypes());
  104. $type = common_negotiate_type(common_accept_to_prefs($httpaccept),
  105. common_accept_to_prefs($can_serve));
  106. } else {
  107. /*
  108. * HACK: for StatusNet to work against us, we must always serve an
  109. * XRD to at least versions <1.1.1 (at time of writing) since they
  110. * don't send Accept headers (in their 'Discovery::fetchXrd' calls)
  111. */
  112. $matches = array();
  113. preg_match('/(StatusNet)\/(\d+\.\d+(\.\d+)?)/', $useragent, $browser);
  114. if (count($browser)>2 && $browser[1] === 'StatusNet'
  115. && version_compare($browser[2], '1.1.1') < 1) {
  116. return Discovery::XRD_MIMETYPE;
  117. }
  118. }
  119. if (empty($type)) {
  120. throw new Exception(_('No specified MIME type in Accept header.'));
  121. }
  122. return $type;
  123. }
  124. }