oembed.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. if (substr(strtolower($url),0,strlen(common_root_url())) !== strtolower(common_root_url())) {
  44. // TRANS: Error message displaying attachments. %s is the site's base URL.
  45. $this->clientError(sprintf(_('oEmbed data will only be provided for %s URLs.'), common_root_url()), 400);
  46. }
  47. $path = substr($url,strlen(common_root_url()));
  48. $r = Router::get();
  49. $proxy_args = $r->map($path);
  50. if (!$proxy_args) {
  51. // TRANS: Client error displayed in oEmbed action when path not found.
  52. // TRANS: %s is a path.
  53. $this->clientError(sprintf(_('"%s" not found.'),$path), 404);
  54. }
  55. $oembed=array();
  56. $oembed['version']='1.0';
  57. $oembed['provider_name']=common_config('site', 'name');
  58. $oembed['provider_url']=common_root_url();
  59. switch ($proxy_args['action']) {
  60. case 'shownotice':
  61. $oembed['type']='link';
  62. $id = $proxy_args['notice'];
  63. $notice = Notice::getKV($id);
  64. if(empty($notice)){
  65. // TRANS: Client error displayed in oEmbed action when notice not found.
  66. // TRANS: %s is a notice.
  67. $this->clientError(sprintf(_("Notice %s not found."),$id), 404);
  68. }
  69. $profile = $notice->getProfile();
  70. if (empty($profile)) {
  71. // TRANS: Server error displayed in oEmbed action when notice has not profile.
  72. $this->serverError(_('Notice has no profile.'), 500);
  73. }
  74. $authorname = $profile->getFancyName();
  75. // TRANS: oEmbed title. %1$s is the author name, %2$s is the creation date.
  76. $oembed['title'] = sprintf(_('%1$s\'s status on %2$s'),
  77. $authorname,
  78. common_exact_date($notice->created));
  79. $oembed['author_name']=$authorname;
  80. $oembed['author_url']=$profile->profileurl;
  81. $oembed['url']=$notice->getUrl();
  82. $oembed['html']=$notice->getRendered();
  83. // maybe add thumbnail
  84. $attachments = $notice->attachments();
  85. if (!empty($attachments)) {
  86. foreach ($attachments as $attachment) {
  87. if(is_object($attachment)) {
  88. try {
  89. $thumb = $attachment->getThumbnail();
  90. } catch (ServerException $e) {
  91. //
  92. }
  93. try {
  94. $thumb_url = File_thumbnail::url($thumb->filename);
  95. $oembed['thumbnail_url'] = $thumb_url;
  96. break; // only first one
  97. } catch (ClientException $e) {
  98. //
  99. }
  100. }
  101. }
  102. }
  103. break;
  104. case 'attachment':
  105. $id = $proxy_args['attachment'];
  106. $attachment = File::getKV($id);
  107. if(empty($attachment)){
  108. // TRANS: Client error displayed in oEmbed action when attachment not found.
  109. // TRANS: %d is an attachment ID.
  110. $this->clientError(sprintf(_('Attachment %s not found.'),$id), 404);
  111. }
  112. if (empty($attachment->filename) && $file_oembed = File_oembed::getKV('file_id', $attachment->id)) {
  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('attachment',
  149. array('attachment' => $attachment->id));
  150. }
  151. if ($attachment->title) {
  152. $oembed['title']=$attachment->title;
  153. }
  154. break;
  155. default:
  156. // TRANS: Server error displayed in oEmbed request when a path is not supported.
  157. // TRANS: %s is a path.
  158. $this->serverError(sprintf(_('"%s" not supported for oembed requests.'),$path), 501);
  159. }
  160. switch ($this->trimmed('format')) {
  161. case 'xml':
  162. $this->init_document('xml');
  163. $this->elementStart('oembed');
  164. foreach(array(
  165. 'version', 'type', 'provider_name',
  166. 'provider_url', 'title', 'author_name',
  167. 'author_url', 'url', 'html', 'width',
  168. 'height', 'cache_age', 'thumbnail_url',
  169. 'thumbnail_width', 'thumbnail_height',
  170. ) as $key) {
  171. if (isset($oembed[$key]) && $oembed[$key]!='') {
  172. $this->element($key, null, $oembed[$key]);
  173. }
  174. }
  175. $this->elementEnd('oembed');
  176. $this->end_document('xml');
  177. break;
  178. case 'json':
  179. case null:
  180. $this->init_document('json');
  181. $this->raw(json_encode($oembed));
  182. $this->end_document('json');
  183. break;
  184. default:
  185. // TRANS: Error message displaying attachments. %s is a raw MIME type (eg 'image/png')
  186. $this->serverError(sprintf(_('Content type %s not supported.'), $apidata['content-type']), 501);
  187. }
  188. }
  189. public function init_document($type)
  190. {
  191. switch ($type) {
  192. case 'xml':
  193. header('Content-Type: application/xml; charset=utf-8');
  194. $this->startXML();
  195. break;
  196. case 'json':
  197. header('Content-Type: application/json; charset=utf-8');
  198. // Check for JSONP callback
  199. $callback = $this->arg('callback');
  200. if ($callback) {
  201. print $callback . '(';
  202. }
  203. break;
  204. default:
  205. // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
  206. $this->serverError(_('Not a supported data format.'), 501);
  207. break;
  208. }
  209. }
  210. public function end_document($type)
  211. {
  212. switch ($type) {
  213. case 'xml':
  214. $this->endXML();
  215. break;
  216. case 'json':
  217. // Check for JSONP callback
  218. $callback = $this->arg('callback');
  219. if ($callback) {
  220. print ')';
  221. }
  222. break;
  223. default:
  224. // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
  225. $this->serverError(_('Not a supported data format.'), 501);
  226. break;
  227. }
  228. return;
  229. }
  230. /**
  231. * Is this action read-only?
  232. *
  233. * @param array $args other arguments
  234. *
  235. * @return boolean is read only action?
  236. */
  237. function isReadOnly($args)
  238. {
  239. return true;
  240. }
  241. }