oembed.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 = $thumb->getUrl();
  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 (
  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. array('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 (array(
  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. public function init_document($type)
  192. {
  193. switch ($type) {
  194. case 'xml':
  195. header('Content-Type: application/xml; charset=utf-8');
  196. $this->startXML();
  197. break;
  198. case 'json':
  199. header('Content-Type: application/json; charset=utf-8');
  200. // Check for JSONP callback
  201. $callback = $this->arg('callback');
  202. if ($callback) {
  203. print $callback . '(';
  204. }
  205. break;
  206. default:
  207. // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
  208. $this->serverError(_('Not a supported data format.'), 501);
  209. break;
  210. }
  211. }
  212. public function end_document($type)
  213. {
  214. switch ($type) {
  215. case 'xml':
  216. $this->endXML();
  217. break;
  218. case 'json':
  219. // Check for JSONP callback
  220. $callback = $this->arg('callback');
  221. if ($callback) {
  222. print ')';
  223. }
  224. break;
  225. default:
  226. // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
  227. $this->serverError(_('Not a supported data format.'), 501);
  228. break;
  229. }
  230. return;
  231. }
  232. /**
  233. * Is this action read-only?
  234. *
  235. * @param array $args other arguments
  236. *
  237. * @return boolean is read only action?
  238. */
  239. public function isReadOnly($args)
  240. {
  241. return true;
  242. }
  243. }