OEmbedAction.php 9.9 KB

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