Embed.php 11 KB

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