apiqvitteroembednotice.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3. · ·
  4. · Qvitter's Oembed response for notices ·
  5. · ·
  6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7. · ·
  8. · ·
  9. · Q V I T T E R ·
  10. · ·
  11. · https://git.gnu.io/h2p/Qvitter ·
  12. · ·
  13. · ·
  14. · <o) ·
  15. · /_//// ·
  16. · (____/ ·
  17. · (o< ·
  18. · o> \\\\_\ ·
  19. · \\) \____) ·
  20. · ·
  21. · ·
  22. · ·
  23. · Qvitter is free software: you can redistribute it and / or modify it ·
  24. · under the terms of the GNU Affero General Public License as published by ·
  25. · the Free Software Foundation, either version three of the License or (at ·
  26. · your option) any later version. ·
  27. · ·
  28. · Qvitter is distributed in hope that it will be useful but WITHOUT ANY ·
  29. · WARRANTY; without even the implied warranty of MERCHANTABILTY or FITNESS ·
  30. · FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for ·
  31. · more details. ·
  32. · ·
  33. · You should have received a copy of the GNU Affero General Public License ·
  34. · along with Qvitter. If not, see <http://www.gnu.org/licenses/>. ·
  35. · ·
  36. · Contact h@nnesmannerhe.im if you have any questions. ·
  37. · ·
  38. · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
  39. if (!defined('GNUSOCIAL')) { exit(1); }
  40. class ApiQvitterOembedNoticeAction extends ApiAction
  41. {
  42. var $format = null;
  43. var $url = null;
  44. /**
  45. * Take arguments for running
  46. *
  47. * @param array $args $_REQUEST args
  48. *
  49. * @return boolean success flag
  50. */
  51. protected function prepare(array $args=array())
  52. {
  53. parent::prepare($args);
  54. $this->format = $this->arg('format');
  55. $this->url = $this->arg('url');
  56. return true;
  57. }
  58. /**
  59. * Handle the request
  60. *
  61. * @param array $args $_REQUEST data (unused)
  62. *
  63. * @return void
  64. */
  65. protected function handle()
  66. {
  67. parent::handle();
  68. $noticeurl = common_path('notice/', StatusNet::isHTTPS());
  69. $instanceurl = common_path('', StatusNet::isHTTPS());
  70. // remove protocol for the comparison below
  71. $noticeurl_wo_protocol = preg_replace('(^https?://)', '', $noticeurl);
  72. $instanceurl_wo_protocol = preg_replace('(^https?://)', '', $instanceurl);
  73. $url_wo_protocol = preg_replace('(^https?://)', '', $this->url);
  74. // find local notice
  75. if(strpos($url_wo_protocol, $noticeurl_wo_protocol) === 0) {
  76. $possible_notice_id = str_replace($noticeurl_wo_protocol,'',$url_wo_protocol);
  77. if(ctype_digit($possible_notice_id)) {
  78. $notice = Notice::getKV('id',$possible_notice_id);;
  79. } else {
  80. $this->clientError("Notice not found.", 404);
  81. }
  82. }
  83. if(!$notice instanceof Notice){
  84. // TRANS: Client error displayed in oEmbed action when notice not found.
  85. // TRANS: %s is a notice.
  86. $this->clientError(sprintf(_("Notice %s not found."),$this->id), 404);
  87. }
  88. $profile = $notice->getProfile();
  89. if (!$profile instanceof Profile) {
  90. // TRANS: Server error displayed in oEmbed action when notice has not profile.
  91. $this->serverError(_('Notice has no profile.'), 500);
  92. }
  93. $authorname = $profile->getFancyName();
  94. $oembed=array();
  95. $oembed['version']='1.0';
  96. $oembed['provider_name']=common_config('site', 'name');
  97. $oembed['provider_url']=common_root_url();
  98. $oembed['type']='link';
  99. // TRANS: oEmbed title. %1$s is the author name, %2$s is the creation date.
  100. $oembed['title'] = ApiAction::dateTwitter($notice->created).' (Qvitter)';
  101. $oembed['author_name']=$authorname;
  102. $oembed['author_url']=$profile->profileurl;
  103. $oembed['url']=$notice->getUrl();
  104. $oembed['html']=$notice->getRendered();
  105. // maybe add thumbnail
  106. $attachments = $notice->attachments();
  107. if (!empty($attachments)) {
  108. foreach ($attachments as $attachment) {
  109. if(is_object($attachment)) {
  110. try {
  111. $thumb = $attachment->getThumbnail();
  112. } catch (ServerException $e) {
  113. //
  114. }
  115. if(!empty($thumb) && method_exists('File_thumbnail','url')) {
  116. try {
  117. $thumb_url = File_thumbnail::url($thumb->filename);
  118. $oembed['thumbnail_url'] = $thumb_url;
  119. break; // only first one
  120. } catch (ClientException $e) {
  121. //
  122. }
  123. }
  124. }
  125. }
  126. }
  127. if($this->format == 'json') {
  128. $this->initDocument('json');
  129. print json_encode($oembed);
  130. $this->endDocument('json');
  131. } elseif ($this->format == 'xml') {
  132. $this->initDocument('xml');
  133. $this->elementStart('oembed');
  134. foreach(array(
  135. 'version', 'type', 'provider_name',
  136. 'provider_url', 'title', 'author_name',
  137. 'author_url', 'url', 'html'
  138. ) as $key) {
  139. if (isset($oembed[$key]) && $oembed[$key]!='') {
  140. $this->element($key, null, $oembed[$key]);
  141. }
  142. }
  143. $this->elementEnd('oembed');
  144. $this->endDocument('xml');
  145. } else {
  146. $this->serverError(sprintf(_('Format %s not supported.'), $this->format), 501);
  147. }
  148. }
  149. }