oembed.php 9.4 KB

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