bookmarkforurl.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Returns a pre-filled bookmark form for a given URL
  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 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. * Returns a prefilled bookmark form for a given URL
  37. *
  38. * @category Bookmark
  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 BookmarkforurlAction extends Action
  46. {
  47. protected $url = null;
  48. protected $oembed = null;
  49. protected $thumbnail = null;
  50. protected $title = null;
  51. /**
  52. * For initializing members of the class.
  53. *
  54. * @param array $args misc. arguments
  55. *
  56. * @return boolean true
  57. */
  58. function prepare(array $args = array())
  59. {
  60. parent::prepare($args);
  61. if (!$this->isPost()) {
  62. throw new ClientException(_('POST only'), 405);
  63. }
  64. $this->checkSessionToken();
  65. $this->url = $this->trimmed('url');
  66. if (empty($this->url)) {
  67. throw new ClientException(_('URL is required.'), 400);
  68. }
  69. if (!common_valid_http_url($this->url)) {
  70. throw new ClientException(_('Invalid URL.'), 400);
  71. }
  72. try {
  73. // processNew will first try to fetch a locally stored File entry
  74. $f = File::processNew($this->url);
  75. } catch (ServerException $e) {
  76. $f = null;
  77. }
  78. // How about now?
  79. if ($f instanceof File) {
  80. // FIXME: Use some File metadata Event instead
  81. $this->oembed = File_oembed::getKV('file_id', $f->id);
  82. if ($this->oembed instanceof File_oembed) {
  83. $this->title = $this->oembed->title;
  84. }
  85. $this->thumbnail = File_thumbnail::getKV('file_id', $f->id);
  86. }
  87. return true;
  88. }
  89. /**
  90. * Handler method
  91. *
  92. * @param array $args is ignored since it's now passed in in prepare()
  93. *
  94. * @return void
  95. */
  96. function handle($args=null)
  97. {
  98. $this->startHTML('text/xml;charset=utf-8');
  99. $this->elementStart('head');
  100. $this->element('title', null, _('Bookmark form'));
  101. $this->elementEnd('head');
  102. $this->elementStart('body');
  103. $bf = new BookmarkForm($this, $this->title, $this->url, null, null, $this->thumbnail);
  104. $bf->show();
  105. $this->elementEnd('body');
  106. $this->endHTML();
  107. }
  108. /**
  109. * Return true if read only.
  110. *
  111. * MAY override
  112. *
  113. * @param array $args other arguments
  114. *
  115. * @return boolean is read only action?
  116. */
  117. function isReadOnly($args)
  118. {
  119. return false;
  120. }
  121. }