oembed.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * OembedPlugin implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Craig Andrews <candrews@integralblue.com>
  21. * @author Mikael Nordfeldth <mmn@hethane.se>
  22. * @author hannes
  23. * @author Diogo Cordeiro <diogo@fc.up.pt>
  24. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  25. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  26. */
  27. defined('GNUSOCIAL') || die();
  28. /**
  29. * Oembed provider implementation
  30. *
  31. * This class handles all /main/oembed(.xml|.json)/ requests.
  32. *
  33. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. */
  36. class OEmbedAction extends Action
  37. {
  38. protected function handle()
  39. {
  40. parent::handle();
  41. $url = $this->trimmed('url');
  42. $tls = parse_url($url, PHP_URL_SCHEME) == 'https';
  43. $root_url = common_root_url($tls);
  44. if (substr(strtolower($url), 0, mb_strlen($root_url)) !== strtolower($root_url)) {
  45. // TRANS: Error message displaying attachments. %s is the site's base URL.
  46. throw new ClientException(sprintf(_('Embed data will only be provided for %s URLs.'), $root_url));
  47. }
  48. $path = substr($url, strlen($root_url));
  49. $r = Router::get();
  50. // $r->map will throw ClientException 404 if it fails to find a mapping
  51. $proxy_args = $r->map($path);
  52. $oembed=array();
  53. $oembed['version']='1.0';
  54. $oembed['provider_name']=common_config('site', 'name');
  55. $oembed['provider_url']=common_root_url();
  56. switch ($proxy_args['action']) {
  57. case 'shownotice':
  58. $oembed['type']='link';
  59. try {
  60. $notice = Notice::getByID($proxy_args['notice']);
  61. } catch (NoResultException $e) {
  62. throw new ClientException($e->getMessage(), 404);
  63. }
  64. $profile = $notice->getProfile();
  65. $authorname = $profile->getFancyName();
  66. // TRANS: oEmbed title. %1$s is the author name, %2$s is the creation date.
  67. $oembed['title'] = sprintf(
  68. _('%1$s\'s status on %2$s'),
  69. $authorname,
  70. common_exact_date($notice->created)
  71. );
  72. $oembed['author_name']=$authorname;
  73. $oembed['author_url']=$profile->profileurl;
  74. $oembed['url']=$notice->getUrl();
  75. $oembed['html']=$notice->getRendered();
  76. // maybe add thumbnail
  77. foreach ($notice->attachments() as $attachment) {
  78. if (!$attachment instanceof File) {
  79. common_debug('ATTACHMENTS array entry from notice id=='._ve($notice->getID()).
  80. ' is something else than a File dataobject: '._ve($attachment));
  81. continue;
  82. }
  83. try {
  84. $thumb = $attachment->getThumbnail();
  85. $thumb_url = File_thumbnail::url($thumb->filename);
  86. $oembed['thumbnail_url'] = $thumb_url;
  87. break; // only first one
  88. } catch (UseFileAsThumbnailException $e) {
  89. $oembed['thumbnail_url'] = $attachment->getUrl();
  90. break; // we're happy with that
  91. } catch (ServerException $e) {
  92. //
  93. } catch (ClientException $e) {
  94. //
  95. }
  96. }
  97. break;
  98. case 'attachment':
  99. $id = $proxy_args['attachment'];
  100. $attachment = File::getKV($id);
  101. if (empty($attachment)) {
  102. // TRANS: Client error displayed in oEmbed action when attachment not found.
  103. // TRANS: %d is an attachment ID.
  104. $this->clientError(sprintf(_('Attachment %s not found.'), $id), 404);
  105. }
  106. if (empty($attachment->filename) && $file_oembed = File_oembed::getKV('file_id', $attachment->id)) {
  107. // Proxy the existing oembed information
  108. $oembed['type']=$file_oembed->type;
  109. $oembed['provider']=$file_oembed->provider;
  110. $oembed['provider_url']=$file_oembed->provider_url;
  111. $oembed['width']=$file_oembed->width;
  112. $oembed['height']=$file_oembed->height;
  113. $oembed['html']=$file_oembed->html;
  114. $oembed['title']=$file_oembed->title;
  115. $oembed['author_name']=$file_oembed->author_name;
  116. $oembed['author_url']=$file_oembed->author_url;
  117. $oembed['url']=$file_oembed->getUrl();
  118. } elseif (substr($attachment->mimetype, 0, strlen('image/'))==='image/') {
  119. $oembed['type']='photo';
  120. if ($attachment->filename) {
  121. $filepath = File::path($attachment->filename);
  122. $gis = @getimagesize($filepath);
  123. if ($gis) {
  124. $oembed['width'] = $gis[0];
  125. $oembed['height'] = $gis[1];
  126. } else {
  127. // TODO Either throw an error or find a fallback?
  128. }
  129. }
  130. $oembed['url']=$attachment->getUrl();
  131. try {
  132. $thumb = $attachment->getThumbnail();
  133. $oembed['thumbnail_url'] = $thumb->getUrl();
  134. $oembed['thumbnail_width'] = $thumb->width;
  135. $oembed['thumbnail_height'] = $thumb->height;
  136. unset($thumb);
  137. } catch (UnsupportedMediaException $e) {
  138. // No thumbnail data available
  139. }
  140. } else {
  141. $oembed['type']='link';
  142. $oembed['url']=common_local_url(
  143. 'attachment',
  144. array('attachment' => $attachment->id)
  145. );
  146. }
  147. if ($attachment->title) {
  148. $oembed['title']=$attachment->title;
  149. }
  150. break;
  151. default:
  152. // TRANS: Server error displayed in oEmbed request when a path is not supported.
  153. // TRANS: %s is a path.
  154. $this->serverError(sprintf(_('"%s" not supported for oembed requests.'), $path), 501);
  155. }
  156. switch ($this->trimmed('format')) {
  157. case 'xml':
  158. $this->init_document('xml');
  159. $this->elementStart('oembed');
  160. foreach (array(
  161. 'version', 'type', 'provider_name',
  162. 'provider_url', 'title', 'author_name',
  163. 'author_url', 'url', 'html', 'width',
  164. 'height', 'cache_age', 'thumbnail_url',
  165. 'thumbnail_width', 'thumbnail_height',
  166. ) as $key) {
  167. if (isset($oembed[$key]) && $oembed[$key]!='') {
  168. $this->element($key, null, $oembed[$key]);
  169. }
  170. }
  171. $this->elementEnd('oembed');
  172. $this->end_document('xml');
  173. break;
  174. case 'json':
  175. case null:
  176. $this->init_document('json');
  177. $this->raw(json_encode($oembed));
  178. $this->end_document('json');
  179. break;
  180. default:
  181. // TRANS: Error message displaying attachments. %s is a raw MIME type (eg 'image/png')
  182. $this->serverError(sprintf(_('Content type %s not supported.'), $apidata['content-type']), 501);
  183. }
  184. }
  185. public function init_document($type)
  186. {
  187. switch ($type) {
  188. case 'xml':
  189. header('Content-Type: application/xml; charset=utf-8');
  190. $this->startXML();
  191. break;
  192. case 'json':
  193. header('Content-Type: application/json; charset=utf-8');
  194. // Check for JSONP callback
  195. $callback = $this->arg('callback');
  196. if ($callback) {
  197. print $callback . '(';
  198. }
  199. break;
  200. default:
  201. // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
  202. $this->serverError(_('Not a supported data format.'), 501);
  203. break;
  204. }
  205. }
  206. public function end_document($type)
  207. {
  208. switch ($type) {
  209. case 'xml':
  210. $this->endXML();
  211. break;
  212. case 'json':
  213. // Check for JSONP callback
  214. $callback = $this->arg('callback');
  215. if ($callback) {
  216. print ')';
  217. }
  218. break;
  219. default:
  220. // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
  221. $this->serverError(_('Not a supported data format.'), 501);
  222. break;
  223. }
  224. return;
  225. }
  226. /**
  227. * Is this action read-only?
  228. *
  229. * @param array $args other arguments
  230. *
  231. * @return boolean is read only action?
  232. */
  233. public function isReadOnly($args)
  234. {
  235. return true;
  236. }
  237. }