attachmentlistitem.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. switch ($this->attachment->mimetype) {
  100. case 'image/gif':
  101. case 'image/png':
  102. case 'image/jpg':
  103. case 'image/jpeg':
  104. try {
  105. // Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still")
  106. $thumb = $this->attachment->getThumbnail(null, null, false, false);
  107. $this->out->element('img', array('class'=>'u-photo', 'src' => $thumb->getUrl(), 'alt' => ''));
  108. } catch (UseFileAsThumbnailException $e) {
  109. $this->out->element('img', array('class'=>'u-photo', 'src' => $e->file->getUrl(), 'alt' => $e->file->title));
  110. } catch (UnsupportedMediaException $e) {
  111. // FIXME: Show a good representation of unsupported/unshowable images
  112. }
  113. break;
  114. case 'application/ogg':
  115. $arr = array('type' => $this->attachment->mimetype,
  116. 'data' => $this->attachment->url,
  117. 'width' => 320,
  118. 'height' => 240
  119. );
  120. $this->out->elementStart('object', $arr);
  121. $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
  122. $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
  123. $this->out->elementEnd('object');
  124. break;
  125. case 'audio/ogg':
  126. case 'audio/x-speex':
  127. case 'video/mpeg':
  128. case 'audio/mpeg':
  129. case 'video/mp4':
  130. case 'video/ogg':
  131. case 'video/quicktime':
  132. case 'video/webm':
  133. $mediatype = common_get_mime_media($this->attachment->mimetype);
  134. try {
  135. $thumb = $this->attachment->getThumbnail();
  136. $poster = $thumb->getUrl();
  137. unset ($thumb);
  138. } catch (Exception $e) {
  139. $poster = null;
  140. }
  141. $this->out->elementStart($mediatype,
  142. array('class'=>"attachment_player u-{$mediatype}",
  143. 'poster'=>$poster,
  144. 'controls'=>'controls'));
  145. $this->out->element('source',
  146. array('src'=>$this->attachment->url,
  147. 'type'=>$this->attachment->mimetype));
  148. $this->out->elementEnd($mediatype);
  149. break;
  150. case 'text/html':
  151. if (!empty($this->attachment->filename)
  152. && (StatusNet::isAjax() || common_config('attachments', 'show_html'))) {
  153. // Locally-uploaded HTML. Scrub and display inline.
  154. $this->showHtmlFile($this->attachment);
  155. break;
  156. }
  157. // Fall through to default.
  158. default:
  159. Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
  160. }
  161. } else {
  162. Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
  163. }
  164. }
  165. Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
  166. }
  167. protected function showHtmlFile(File $attachment)
  168. {
  169. $body = $this->scrubHtmlFile($attachment);
  170. if ($body) {
  171. $this->out->raw($body);
  172. }
  173. }
  174. /**
  175. * @return mixed false on failure, HTML fragment string on success
  176. */
  177. protected function scrubHtmlFile(File $attachment)
  178. {
  179. $path = File::path($attachment->filename);
  180. if (!file_exists($path) || !is_readable($path)) {
  181. common_log(LOG_ERR, "Missing local HTML attachment $path");
  182. return false;
  183. }
  184. $raw = file_get_contents($path);
  185. // Normalize...
  186. $dom = new DOMDocument();
  187. if(!$dom->loadHTML($raw)) {
  188. common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
  189. return false;
  190. }
  191. // Remove <script>s or htmlawed will dump their contents into output!
  192. // Note: removing child nodes while iterating seems to mess things up,
  193. // hence the double loop.
  194. $scripts = array();
  195. foreach ($dom->getElementsByTagName('script') as $script) {
  196. $scripts[] = $script;
  197. }
  198. foreach ($scripts as $script) {
  199. common_log(LOG_DEBUG, $script->textContent);
  200. $script->parentNode->removeChild($script);
  201. }
  202. // Trim out everything outside the body...
  203. $body = $dom->saveHTML();
  204. $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
  205. $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
  206. require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
  207. $config = array('safe' => 1,
  208. 'deny_attribute' => 'id,style,on*',
  209. 'comment' => 1); // remove comments
  210. $scrubbed = htmLawed($body, $config);
  211. return $scrubbed;
  212. }
  213. /**
  214. * start a single notice.
  215. *
  216. * @return void
  217. */
  218. function showStart()
  219. {
  220. // XXX: RDFa
  221. // TODO: add notice_type class e.g., notice_video, notice_image
  222. $this->out->elementStart('li');
  223. }
  224. /**
  225. * finish the notice
  226. *
  227. * Close the last elements in the notice list item
  228. *
  229. * @return void
  230. */
  231. function showEnd()
  232. {
  233. $this->out->elementEnd('li');
  234. }
  235. }