123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- abstract class XrdAction extends ManagedAction
- {
-
-
- protected $defaultformat = null;
- protected $xrd = null;
- public function isReadOnly($args)
- {
- return true;
- }
-
- abstract protected function setXRD();
- protected function prepare(array $args=array())
- {
- if (!isset($args['format'])) {
- $args['format'] = $this->defaultformat;
- }
- parent::prepare($args);
-
- $this->xrd = new XML_XRD();
- return true;
- }
- protected function handle()
- {
- $this->setXRD();
- if (common_config('discovery', 'cors')) {
- header('Access-Control-Allow-Origin: *');
- }
- parent::handle();
- }
- public function mimeType()
- {
- try {
- return $this->checkAccept();
- } catch (Exception $e) {
- $supported = Discovery::supportedMimeTypes();
- $docformat = $this->arg('format');
- if (!empty($docformat) && isset($supported[$docformat])) {
- return $supported[$docformat];
- }
- }
-
- return Discovery::JRD_MIMETYPE;
- }
- public function showPage()
- {
- $mimeType = $this->mimeType();
- header("Content-type: {$mimeType}");
- switch ($mimeType) {
- case Discovery::XRD_MIMETYPE:
- print $this->xrd->toXML();
- break;
- case Discovery::JRD_MIMETYPE:
- case Discovery::JRD_MIMETYPE_OLD:
- print $this->xrd->to('json');
- break;
- default:
- throw new Exception(_('No supported MIME type in Accept header.'));
- }
- }
- protected function checkAccept()
- {
- $type = null;
- $httpaccept = isset($_SERVER['HTTP_ACCEPT'])
- ? $_SERVER['HTTP_ACCEPT'] : null;
- $useragent = isset($_SERVER['HTTP_USER_AGENT'])
- ? $_SERVER['HTTP_USER_AGENT'] : null;
- if ($httpaccept !== null && $httpaccept != '*/*') {
- $can_serve = implode(',', Discovery::supportedMimeTypes());
- $type = common_negotiate_type(common_accept_to_prefs($httpaccept),
- common_accept_to_prefs($can_serve));
- } else {
-
- $matches = array();
- preg_match('/(StatusNet)\/(\d+\.\d+(\.\d+)?)/', $useragent, $browser);
- if (count($browser)>2 && $browser[1] === 'StatusNet'
- && version_compare($browser[2], '1.1.1') < 1) {
- return Discovery::XRD_MIMETYPE;
- }
- }
- if (empty($type)) {
- throw new Exception(_('No specified MIME type in Accept header.'));
- }
- return $type;
- }
- }
|