shownotice.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show a single notice
  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 Personal
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2008-2011 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL')) { exit(1); }
  30. require_once INSTALLDIR.'/lib/noticelist.php';
  31. /**
  32. * Show a single notice
  33. *
  34. * @category Personal
  35. * @package StatusNet
  36. * @author Evan Prodromou <evan@status.net>
  37. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  38. * @link http://status.net/
  39. */
  40. class ShownoticeAction extends ManagedAction
  41. {
  42. /**
  43. * Notice object to show
  44. */
  45. var $notice = null;
  46. /**
  47. * Profile of the notice object
  48. */
  49. var $profile = null;
  50. /**
  51. * Avatar of the profile of the notice object
  52. */
  53. var $avatar = null;
  54. /**
  55. * Load attributes based on database arguments
  56. *
  57. * Loads all the DB stuff
  58. *
  59. * @param array $args $_REQUEST array
  60. *
  61. * @return success flag
  62. */
  63. protected function prepare(array $args=array())
  64. {
  65. parent::prepare($args);
  66. if ($this->boolean('ajax')) {
  67. GNUsocial::setApi(true);
  68. }
  69. $this->notice = $this->getNotice();
  70. if (!$this->notice->inScope($this->scoped)) {
  71. // TRANS: Client exception thrown when trying a view a notice the user has no access to.
  72. throw new ClientException(_('Access restricted.'), 403);
  73. }
  74. $this->profile = $this->notice->getProfile();
  75. if (!$this->profile instanceof Profile) {
  76. // TRANS: Server error displayed trying to show a notice without a connected profile.
  77. $this->serverError(_('Notice has no profile.'), 500);
  78. }
  79. try {
  80. $this->user = $this->profile->getUser();
  81. } catch (NoSuchUserException $e) {
  82. // FIXME: deprecate $this->user stuff in extended classes
  83. $this->user = null;
  84. }
  85. try {
  86. $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
  87. } catch (Exception $e) {
  88. $this->avatar = null;
  89. }
  90. return true;
  91. }
  92. /**
  93. * Fetch the notice to show. This may be overridden by child classes to
  94. * customize what we fetch without duplicating all of the prepare() method.
  95. *
  96. * @return Notice
  97. */
  98. protected function getNotice()
  99. {
  100. $id = $this->arg('notice');
  101. $notice = Notice::getKV('id', $id);
  102. if ($notice instanceof Notice) {
  103. // Alright, got it!
  104. return $notice;
  105. }
  106. // Did we use to have it, and it got deleted?
  107. $deleted = Deleted_notice::getKV('id', $id);
  108. if ($deleted instanceof Deleted_notice) {
  109. // TRANS: Client error displayed trying to show a deleted notice.
  110. $this->clientError(_('Notice deleted.'), 410);
  111. }
  112. // TRANS: Client error displayed trying to show a non-existing notice.
  113. $this->clientError(_('No such notice.'), 404);
  114. }
  115. /**
  116. * Is this action read-only?
  117. *
  118. * @return boolean true
  119. */
  120. function isReadOnly($args)
  121. {
  122. return true;
  123. }
  124. /**
  125. * Last-modified date for page
  126. *
  127. * When was the content of this page last modified? Based on notice,
  128. * profile, avatar.
  129. *
  130. * @return int last-modified date as unix timestamp
  131. */
  132. function lastModified()
  133. {
  134. return max(strtotime($this->notice->modified),
  135. strtotime($this->profile->modified),
  136. ($this->avatar) ? strtotime($this->avatar->modified) : 0);
  137. }
  138. /**
  139. * An entity tag for this page
  140. *
  141. * Shows the ETag for the page, based on the notice ID and timestamps
  142. * for the notice, profile, and avatar. It's weak, since we change
  143. * the date text "one hour ago", etc.
  144. *
  145. * @return string etag
  146. */
  147. function etag()
  148. {
  149. $avtime = ($this->avatar) ?
  150. strtotime($this->avatar->modified) : 0;
  151. return 'W/"' . implode(':', array($this->arg('action'),
  152. common_user_cache_hash(),
  153. common_language(),
  154. $this->notice->id,
  155. strtotime($this->notice->created),
  156. strtotime($this->profile->modified),
  157. $avtime)) . '"';
  158. }
  159. /**
  160. * Title of the page
  161. *
  162. * @return string title of the page
  163. */
  164. function title()
  165. {
  166. return $this->notice->getTitle();
  167. }
  168. /**
  169. * Fill the content area of the page
  170. *
  171. * Shows a single notice list item.
  172. *
  173. * @return void
  174. */
  175. function showContent()
  176. {
  177. $this->elementStart('ol', array('class' => 'notices xoxo'));
  178. $nli = new NoticeListItem($this->notice, $this);
  179. $nli->show();
  180. $this->elementEnd('ol');
  181. }
  182. /**
  183. * Don't show page notice
  184. *
  185. * @return void
  186. */
  187. function showPageNoticeBlock()
  188. {
  189. }
  190. /**
  191. * Don't show aside
  192. *
  193. * @return void
  194. */
  195. function showAside() {
  196. }
  197. /**
  198. * Extra <head> content
  199. *
  200. * We show the microid(s) for the author, if any.
  201. *
  202. * @return void
  203. */
  204. function extraHead()
  205. {
  206. $user = User::getKV($this->profile->id);
  207. if (!$user instanceof User) {
  208. return;
  209. }
  210. if ($user->emailmicroid && $user->email && $this->notice->uri) {
  211. $id = new Microid('mailto:'. $user->email,
  212. $this->notice->uri);
  213. $this->element('meta', array('name' => 'microid',
  214. 'content' => $id->toString()));
  215. }
  216. // Extras to aid in sharing notices to Facebook
  217. $avatarUrl = $this->profile->avatarUrl(AVATAR_PROFILE_SIZE);
  218. $this->element('meta', array('property' => 'og:image',
  219. 'content' => $avatarUrl));
  220. $this->element('meta', array('property' => 'og:description',
  221. 'content' => $this->notice->content));
  222. }
  223. }