redirecturl.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Redirect to the 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 URL
  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. * Redirect to a given URL
  37. *
  38. * This is our internal low-budget URL-shortener
  39. *
  40. * @category Action
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @copyright 2010 StatusNet, Inc.
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  45. * @link http://status.net/
  46. */
  47. class RedirecturlAction extends Action
  48. {
  49. protected $id = null;
  50. protected $file = null;
  51. /**
  52. * For initializing members of the class.
  53. *
  54. * @param array $argarray misc. arguments
  55. *
  56. * @return boolean true
  57. */
  58. function prepare($argarray)
  59. {
  60. parent::prepare($argarray);
  61. $this->id = $this->trimmed('id');
  62. if (empty($this->id)) {
  63. // TRANS: Client exception thrown when no ID parameter was provided.
  64. throw new ClientException(_('No id parameter.'));
  65. }
  66. $this->file = File::getKV('id', $this->id);
  67. if (empty($this->file)) {
  68. // TRANS: Client exception thrown when an invalid ID parameter was provided for a file.
  69. // TRANS: %d is the provided ID for which the file is not present (number).
  70. throw new ClientException(sprintf(_('No such file "%d".'),
  71. $this->id),
  72. 404);
  73. }
  74. return true;
  75. }
  76. /**
  77. * Handler method
  78. *
  79. * @param array $argarray is ignored since it's now passed in in prepare()
  80. *
  81. * @return void
  82. */
  83. function handle($argarray=null)
  84. {
  85. common_redirect($this->file->url, 307);
  86. }
  87. /**
  88. * Return true if read only.
  89. *
  90. * MAY override
  91. *
  92. * @param array $args other arguments
  93. *
  94. * @return boolean is read only action?
  95. */
  96. function isReadOnly($args)
  97. {
  98. return true;
  99. }
  100. /**
  101. * Return last modified, if applicable.
  102. *
  103. * MAY override
  104. *
  105. * @return string last modified http header
  106. */
  107. function lastModified()
  108. {
  109. // For comparison with If-Last-Modified
  110. // If not applicable, return null
  111. return strtotime($this->file->modified);
  112. }
  113. /**
  114. * Return etag, if applicable.
  115. *
  116. * MAY override
  117. *
  118. * @return string etag http header
  119. */
  120. function etag()
  121. {
  122. return 'W/"' . implode(':', array($this->arg('action'),
  123. common_user_cache_hash(),
  124. common_language(),
  125. $this->file->id)) . '"';
  126. }
  127. }