attachmentlistitem.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * widget for displaying a list of notice attachments
  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. * @category UI
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Sarven Capadisli <csarven@status.net>
  26. * @copyright 2008 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * widget for displaying a single notice
  33. *
  34. * This widget has the core smarts for showing a single notice: what to display,
  35. * where, and under which circumstances. Its key method is show(); this is a recipe
  36. * that calls all the other show*() methods to build up a single notice. The
  37. * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
  38. * author info (since that's implicit by the data in the page).
  39. *
  40. * @category UI
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://status.net/
  45. * @see NoticeList
  46. * @see ProfileNoticeListItem
  47. */
  48. class AttachmentListItem extends Widget
  49. {
  50. /** The attachment this item will show. */
  51. var $attachment = null;
  52. /**
  53. * @param File $attachment the attachment we will display
  54. */
  55. function __construct(File $attachment, $out=null)
  56. {
  57. parent::__construct($out);
  58. $this->attachment = $attachment;
  59. }
  60. function title() {
  61. return $this->attachment->getTitle();
  62. }
  63. function linkTitle() {
  64. return $this->title();
  65. }
  66. /**
  67. * recipe function for displaying a single notice.
  68. *
  69. * This uses all the other methods to correctly display a notice. Override
  70. * it or one of the others to fine-tune the output.
  71. *
  72. * @return void
  73. */
  74. function show()
  75. {
  76. $this->showStart();
  77. $this->showNoticeAttachment();
  78. $this->showEnd();
  79. }
  80. function linkAttr() {
  81. return array('class' => 'attachment',
  82. 'href' => $this->attachment->getUrl(),
  83. 'id' => 'attachment-' . $this->attachment->id,
  84. 'title' => $this->linkTitle());
  85. }
  86. function showLink() {
  87. $this->out->elementStart('a', $this->linkAttr());
  88. $this->out->element('span', null, $this->linkTitle());
  89. $this->showRepresentation();
  90. $this->out->elementEnd('a');
  91. }
  92. function showNoticeAttachment()
  93. {
  94. $this->showLink();
  95. }
  96. function showRepresentation() {
  97. if (Event::handle('StartShowAttachmentRepresentation', array($this->out, $this->attachment))) {
  98. if (!empty($this->attachment->mimetype)) {
  99. $mediatype = common_get_mime_media($this->attachment->mimetype);
  100. // FIXME: Get proper mime recognition of Ogg files! If system has 'mediainfo', this should do it:
  101. // $ mediainfo --inform='General;%InternetMediaType%'
  102. if ($this->attachment->mimetype === 'application/ogg') {
  103. $mediatype = 'video'; // because this element can handle Ogg/Vorbis etc. on its own
  104. }
  105. switch ($mediatype) {
  106. // Anything we understand as an image, if we need special treatment, do it in StartShowAttachmentRepresentation
  107. case 'image':
  108. try {
  109. // Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still")
  110. $thumb = $this->attachment->getThumbnail(null, null, false, false);
  111. $this->out->element('img', array('class'=>'u-photo', 'src' => $thumb->getUrl(), 'alt' => ''));
  112. } catch (UseFileAsThumbnailException $e) {
  113. $this->out->element('img', array('class'=>'u-photo', 'src' => $e->file->getUrl(), 'alt' => $e->file->title));
  114. } catch (UnsupportedMediaException $e) {
  115. // FIXME: Show a good representation of unsupported/unshowable images
  116. }
  117. break;
  118. // HTML5 media elements
  119. case 'audio':
  120. case 'video':
  121. try {
  122. $thumb = $this->attachment->getThumbnail();
  123. $poster = $thumb->getUrl();
  124. unset ($thumb);
  125. } catch (Exception $e) {
  126. $poster = null;
  127. }
  128. $this->out->elementStart($mediatype,
  129. array('class'=>"attachment_player u-{$mediatype}",
  130. 'poster'=>$poster,
  131. 'controls'=>'controls'));
  132. $this->out->element('source',
  133. array('src'=>$this->attachment->getUrl(),
  134. 'type'=>$this->attachment->mimetype));
  135. $this->out->elementEnd($mediatype);
  136. break;
  137. default:
  138. switch ($this->attachment->mimetype) {
  139. case 'text/html':
  140. if (!empty($this->attachment->filename)
  141. && (GNUsocial::isAjax() || common_config('attachments', 'show_html'))) {
  142. // Locally-uploaded HTML. Scrub and display inline.
  143. $this->showHtmlFile($this->attachment);
  144. break;
  145. }
  146. // Fall through to default if it wasn't a _local_ text/html File object
  147. default:
  148. Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
  149. }
  150. }
  151. } else {
  152. Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
  153. }
  154. }
  155. Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
  156. }
  157. protected function showHtmlFile(File $attachment)
  158. {
  159. $body = $this->scrubHtmlFile($attachment);
  160. if ($body) {
  161. $this->out->raw($body);
  162. }
  163. }
  164. /**
  165. * @return mixed false on failure, HTML fragment string on success
  166. */
  167. protected function scrubHtmlFile(File $attachment)
  168. {
  169. $path = File::path($attachment->filename);
  170. if (!file_exists($path) || !is_readable($path)) {
  171. common_log(LOG_ERR, "Missing local HTML attachment $path");
  172. return false;
  173. }
  174. $raw = file_get_contents($path);
  175. // Normalize...
  176. $dom = new DOMDocument();
  177. if(!$dom->loadHTML($raw)) {
  178. common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
  179. return false;
  180. }
  181. // Remove <script>s or htmlawed will dump their contents into output!
  182. // Note: removing child nodes while iterating seems to mess things up,
  183. // hence the double loop.
  184. $scripts = array();
  185. foreach ($dom->getElementsByTagName('script') as $script) {
  186. $scripts[] = $script;
  187. }
  188. foreach ($scripts as $script) {
  189. common_log(LOG_DEBUG, $script->textContent);
  190. $script->parentNode->removeChild($script);
  191. }
  192. // Trim out everything outside the body...
  193. $body = $dom->saveHTML();
  194. $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
  195. $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
  196. require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
  197. $config = array('safe' => 1,
  198. 'deny_attribute' => 'id,style,on*',
  199. 'comment' => 1); // remove comments
  200. $scrubbed = htmLawed($body, $config);
  201. return $scrubbed;
  202. }
  203. /**
  204. * start a single notice.
  205. *
  206. * @return void
  207. */
  208. function showStart()
  209. {
  210. // XXX: RDFa
  211. // TODO: add notice_type class e.g., notice_video, notice_image
  212. $this->out->elementStart('li');
  213. }
  214. /**
  215. * finish the notice
  216. *
  217. * Close the last elements in the notice list item
  218. *
  219. * @return void
  220. */
  221. function showEnd()
  222. {
  223. $this->out->elementEnd('li');
  224. }
  225. }