oembed.php 9.7 KB

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