newbookmark.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Add a new bookmark
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Bookmark
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Add a new bookmark
  37. *
  38. * @category Bookmark
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2010 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class NewbookmarkAction extends Action
  46. {
  47. protected $user = null;
  48. protected $error = null;
  49. protected $complete = null;
  50. protected $title = null;
  51. protected $url = null;
  52. protected $tags = null;
  53. protected $description = null;
  54. /**
  55. * Returns the title of the action
  56. *
  57. * @return string Action title
  58. */
  59. function title()
  60. {
  61. // TRANS: Title for action to create a new bookmark.
  62. return _m('New bookmark');
  63. }
  64. /**
  65. * For initializing members of the class.
  66. *
  67. * @param array $argarray misc. arguments
  68. *
  69. * @return boolean true
  70. */
  71. function prepare($argarray)
  72. {
  73. parent::prepare($argarray);
  74. if ($this->boolean('ajax')) {
  75. StatusNet::setApi(true);
  76. }
  77. $this->user = common_current_user();
  78. if (empty($this->user)) {
  79. // TRANS: Client exception thrown when trying to create a new bookmark while not logged in.
  80. throw new ClientException(_m('Must be logged in to post a bookmark.'),
  81. 403);
  82. }
  83. if ($this->isPost()) {
  84. $this->checkSessionToken();
  85. }
  86. $this->title = $this->trimmed('title');
  87. $this->url = $this->trimmed('url');
  88. $this->tags = $this->trimmed('tags');
  89. $this->description = $this->trimmed('description');
  90. return true;
  91. }
  92. /**
  93. * Handler method
  94. *
  95. * @param array $argarray is ignored since it's now passed in in prepare()
  96. *
  97. * @return void
  98. */
  99. function handle($argarray=null)
  100. {
  101. parent::handle($argarray);
  102. if ($this->isPost()) {
  103. $this->newBookmark();
  104. } else {
  105. $this->showPage();
  106. }
  107. return;
  108. }
  109. /**
  110. * Add a new bookmark
  111. *
  112. * @return void
  113. */
  114. function newBookmark()
  115. {
  116. try {
  117. if (empty($this->title)) {
  118. // TRANS: Client exception thrown when trying to create a new bookmark without a title.
  119. throw new ClientException(_m('Bookmark must have a title.'));
  120. }
  121. if (empty($this->url)) {
  122. // TRANS: Client exception thrown when trying to create a new bookmark without a URL.
  123. throw new ClientException(_m('Bookmark must have an URL.'));
  124. }
  125. $options = array();
  126. ToSelector::fillOptions($this, $options);
  127. $saved = Bookmark::saveNew($this->user->getProfile(),
  128. $this->title,
  129. $this->url,
  130. $this->tags,
  131. $this->description,
  132. $options);
  133. } catch (ClientException $ce) {
  134. if ($this->boolean('ajax')) {
  135. $this->startHTML('text/xml;charset=utf-8');
  136. $this->elementStart('head');
  137. // TRANS: Page title after an AJAX error occurs
  138. $this->element('title', null, _('Ajax Error'));
  139. $this->elementEnd('head');
  140. $this->elementStart('body');
  141. $this->element('p', array('id' => 'error'), $ce->getMessage());
  142. $this->elementEnd('body');
  143. $this->endHTML();
  144. return;
  145. } else {
  146. $this->error = $ce->getMessage();
  147. $this->showPage();
  148. return;
  149. }
  150. }
  151. if ($this->boolean('ajax')) {
  152. $this->startHTML('text/xml;charset=utf-8');
  153. $this->elementStart('head');
  154. // TRANS: Page title after posting a bookmark.
  155. $this->element('title', null, _m('Bookmark posted'));
  156. $this->elementEnd('head');
  157. $this->elementStart('body');
  158. $this->showNotice($saved);
  159. $this->elementEnd('body');
  160. $this->endHTML();
  161. } else {
  162. common_redirect($saved->getUrl(), 303);
  163. }
  164. }
  165. /**
  166. * Output a notice
  167. *
  168. * Used to generate the notice code for Ajax results.
  169. *
  170. * @param Notice $notice Notice that was saved
  171. *
  172. * @return void
  173. */
  174. function showNotice(Notice $notice)
  175. {
  176. class_exists('NoticeList'); // @fixme hack for autoloader
  177. $nli = new NoticeListItem($notice, $this);
  178. $nli->show();
  179. }
  180. /**
  181. * Show the bookmark form
  182. *
  183. * @return void
  184. */
  185. function showContent()
  186. {
  187. if (!empty($this->error)) {
  188. $this->element('p', 'error', $this->error);
  189. }
  190. $form = new BookmarkForm($this,
  191. $this->title,
  192. $this->url,
  193. $this->tags,
  194. $this->description);
  195. $form->show();
  196. return;
  197. }
  198. /**
  199. * Return true if read only.
  200. *
  201. * MAY override
  202. *
  203. * @param array $args other arguments
  204. *
  205. * @return boolean is read only action?
  206. */
  207. function isReadOnly($args)
  208. {
  209. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  210. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  211. return true;
  212. } else {
  213. return false;
  214. }
  215. }
  216. }