newblogentry.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Save a new blog entry
  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 Blog
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 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. * Save a new blog entry
  37. *
  38. * @category Action
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 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 NewblogentryAction extends Action
  46. {
  47. protected $user;
  48. protected $title;
  49. protected $content;
  50. /**
  51. * For initializing members of the class.
  52. *
  53. * @param array $argarray misc. arguments
  54. *
  55. * @return boolean true
  56. */
  57. function prepare($argarray)
  58. {
  59. parent::prepare($argarray);
  60. if (!$this->isPost()) {
  61. throw new ClientException(_('Must be a POST.'), 405);
  62. }
  63. $this->user = common_current_user();
  64. if (empty($this->user)) {
  65. // TRANS: Client exception thrown when trying to post a blog entry while not logged in.
  66. throw new ClientException(_m('Must be logged in to post a blog entry.'),
  67. 403);
  68. }
  69. $this->checkSessionToken();
  70. $this->title = $this->trimmed('title');
  71. if (empty($this->title)) {
  72. // TRANS: Client exception thrown when trying to post a blog entry without providing a title.
  73. throw new ClientException(_m('Title required.'));
  74. }
  75. $this->content = $this->trimmed('content');
  76. if (empty($this->content)) {
  77. // TRANS: Client exception thrown when trying to post a blog entry without providing content.
  78. throw new ClientException(_m('Content required.'));
  79. }
  80. return true;
  81. }
  82. /**
  83. * Handler method
  84. *
  85. * @param array $argarray is ignored since it's now passed in in prepare()
  86. *
  87. * @return void
  88. */
  89. function handle($argarray=null)
  90. {
  91. $options = array();
  92. // Does the heavy-lifting for getting "To:" information
  93. ToSelector::fillOptions($this, $options);
  94. $options['source'] = 'web';
  95. $profile = $this->user->getProfile();
  96. $saved = Blog_entry::saveNew($profile,
  97. $this->title,
  98. $this->content,
  99. $options);
  100. if ($this->boolean('ajax')) {
  101. $this->startHTML('text/xml;charset=utf-8');
  102. $this->elementStart('head');
  103. // TRANS: Page title after sending a notice.
  104. $this->element('title', null, _m('Blog entry saved'));
  105. $this->elementEnd('head');
  106. $this->elementStart('body');
  107. $nli = new NoticeListItem($saved, $this);
  108. $nli->show();
  109. $this->elementEnd('body');
  110. $this->endHTML();
  111. } else {
  112. common_redirect($saved->getUrl(), 303);
  113. }
  114. }
  115. }