OEmbed.php 11 KB

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