oembed.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. $path = substr($url,strlen(common_root_url()));
  45. $r = Router::get();
  46. $proxy_args = $r->map($path);
  47. if (!$proxy_args) {
  48. // TRANS: Client error displayed in oEmbed action when path not found.
  49. // TRANS: %s is a path.
  50. $this->clientError(sprintf(_('"%s" not found.'),$path), 404);
  51. }
  52. $oembed=array();
  53. $oembed['version']='1.0';
  54. $oembed['provider_name']=common_config('site', 'name');
  55. $oembed['provider_url']=common_root_url();
  56. switch ($proxy_args['action']) {
  57. case 'shownotice':
  58. $oembed['type']='link';
  59. $id = $proxy_args['notice'];
  60. $notice = Notice::getKV($id);
  61. if(empty($notice)){
  62. // TRANS: Client error displayed in oEmbed action when notice not found.
  63. // TRANS: %s is a notice.
  64. $this->clientError(sprintf(_("Notice %s not found."),$id), 404);
  65. }
  66. $profile = $notice->getProfile();
  67. if (empty($profile)) {
  68. // TRANS: Server error displayed in oEmbed action when notice has not profile.
  69. $this->serverError(_('Notice has no profile.'), 500);
  70. }
  71. $authorname = $profile->getFancyName();
  72. // TRANS: oEmbed title. %1$s is the author name, %2$s is the creation date.
  73. $oembed['title'] = sprintf(_('%1$s\'s status on %2$s'),
  74. $authorname,
  75. common_exact_date($notice->created));
  76. $oembed['author_name']=$authorname;
  77. $oembed['author_url']=$profile->profileurl;
  78. $oembed['url']=$notice->getUrl();
  79. $oembed['html']=$notice->rendered;
  80. break;
  81. case 'attachment':
  82. $id = $proxy_args['attachment'];
  83. $attachment = File::getKV($id);
  84. if(empty($attachment)){
  85. // TRANS: Client error displayed in oEmbed action when attachment not found.
  86. // TRANS: %d is an attachment ID.
  87. $this->clientError(sprintf(_('Attachment %s not found.'),$id), 404);
  88. }
  89. if (empty($attachment->filename) && $file_oembed = File_oembed::getKV('file_id', $attachment->id)) {
  90. // Proxy the existing oembed information
  91. $oembed['type']=$file_oembed->type;
  92. $oembed['provider']=$file_oembed->provider;
  93. $oembed['provider_url']=$file_oembed->provider_url;
  94. $oembed['width']=$file_oembed->width;
  95. $oembed['height']=$file_oembed->height;
  96. $oembed['html']=$file_oembed->html;
  97. $oembed['title']=$file_oembed->title;
  98. $oembed['author_name']=$file_oembed->author_name;
  99. $oembed['author_url']=$file_oembed->author_url;
  100. $oembed['url']=$file_oembed->getUrl();
  101. } elseif (substr($attachment->mimetype,0,strlen('image/'))==='image/') {
  102. $oembed['type']='photo';
  103. if ($attachment->filename) {
  104. $filepath = File::path($attachment->filename);
  105. $gis = @getimagesize($filepath);
  106. if ($gis) {
  107. $oembed['width'] = $gis[0];
  108. $oembed['height'] = $gis[1];
  109. } else {
  110. // TODO Either throw an error or find a fallback?
  111. }
  112. }
  113. $oembed['url']=$attachment->getUrl();
  114. try {
  115. $thumb = $attachment->getThumbnail();
  116. $oembed['thumbnail_url'] = $thumb->getUrl();
  117. $oembed['thumbnail_width'] = $thumb->width;
  118. $oembed['thumbnail_height'] = $thumb->height;
  119. unset($thumb);
  120. } catch (UnsupportedMediaException $e) {
  121. // No thumbnail data available
  122. }
  123. } else {
  124. $oembed['type']='link';
  125. $oembed['url']=common_local_url('attachment',
  126. array('attachment' => $attachment->id));
  127. }
  128. if ($attachment->title) {
  129. $oembed['title']=$attachment->title;
  130. }
  131. break;
  132. default:
  133. // TRANS: Server error displayed in oEmbed request when a path is not supported.
  134. // TRANS: %s is a path.
  135. $this->serverError(sprintf(_('"%s" not supported for oembed requests.'),$path), 501);
  136. }
  137. switch ($this->trimmed('format')) {
  138. case 'xml':
  139. $this->init_document('xml');
  140. $this->elementStart('oembed');
  141. foreach(array(
  142. 'version', 'type', 'provider_name',
  143. 'provider_url', 'title', 'author_name',
  144. 'author_url', 'url', 'html', 'width',
  145. 'height', 'cache_age', 'thumbnail_url',
  146. 'thumbnail_width', 'thumbnail_height',
  147. ) as $key) {
  148. if (isset($oembed[$key]) && $oembed[$key]!='') {
  149. $this->element($key, null, $oembed[$key]);
  150. }
  151. }
  152. $this->elementEnd('oembed');
  153. $this->end_document('xml');
  154. break;
  155. case 'json':
  156. case null:
  157. $this->init_document('json');
  158. $this->raw(json_encode($oembed));
  159. $this->end_document('json');
  160. break;
  161. default:
  162. // TRANS: Error message displaying attachments. %s is a raw MIME type (eg 'image/png')
  163. $this->serverError(sprintf(_('Content type %s not supported.'), $apidata['content-type']), 501);
  164. }
  165. } else {
  166. // TRANS: Error message displaying attachments. %s is the site's base URL.
  167. // FIXME: 404 not found?! (this will automatically become a 500 because it's not a serverError!)
  168. $this->serverError(sprintf(_('Only %s URLs over plain HTTP please.'), common_root_url()), 404);
  169. }
  170. }
  171. public function init_document($type)
  172. {
  173. switch ($type) {
  174. case 'xml':
  175. header('Content-Type: application/xml; charset=utf-8');
  176. $this->startXML();
  177. break;
  178. case 'json':
  179. header('Content-Type: application/json; charset=utf-8');
  180. // Check for JSONP callback
  181. $callback = $this->arg('callback');
  182. if ($callback) {
  183. print $callback . '(';
  184. }
  185. break;
  186. default:
  187. // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
  188. $this->serverError(_('Not a supported data format.'), 501);
  189. break;
  190. }
  191. }
  192. public function end_document($type)
  193. {
  194. switch ($type) {
  195. case 'xml':
  196. $this->endXML();
  197. break;
  198. case 'json':
  199. // Check for JSONP callback
  200. $callback = $this->arg('callback');
  201. if ($callback) {
  202. print ')';
  203. }
  204. break;
  205. default:
  206. // TRANS: Server error displayed in oEmbed action when request specifies an unsupported data format.
  207. $this->serverError(_('Not a supported data format.'), 501);
  208. break;
  209. }
  210. return;
  211. }
  212. /**
  213. * Is this action read-only?
  214. *
  215. * @param array $args other arguments
  216. *
  217. * @return boolean is read only action?
  218. */
  219. function isReadOnly($args)
  220. {
  221. return true;
  222. }
  223. }