urlsettings.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Miscellaneous settings
  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 Settings
  23. * @package StatusNet
  24. * @author Robin Millette <millette@status.net>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2008-2009 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. /**
  34. * Miscellaneous settings actions
  35. *
  36. * Currently this just manages URL shortening.
  37. *
  38. * @category Settings
  39. * @package StatusNet
  40. * @author Robin Millette <millette@status.net>
  41. * @author Zach Copley <zach@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://status.net/
  44. */
  45. class UrlsettingsAction extends SettingsAction
  46. {
  47. /**
  48. * Title of the page
  49. *
  50. * @return string Title of the page
  51. */
  52. function title()
  53. {
  54. // TRANS: Title of URL settings tab in profile settings.
  55. return _('URL settings');
  56. }
  57. /**
  58. * Instructions for use
  59. *
  60. * @return instructions for use
  61. */
  62. function getInstructions()
  63. {
  64. // TRANS: Instructions for tab "Other" in user profile settings.
  65. return _('Manage various other options.');
  66. }
  67. function showScripts()
  68. {
  69. parent::showScripts();
  70. $this->autofocus('urlshorteningservice');
  71. }
  72. /**
  73. * Content area of the page
  74. *
  75. * Shows a form for uploading an avatar.
  76. *
  77. * @return void
  78. */
  79. function showContent()
  80. {
  81. $user = common_current_user();
  82. $this->elementStart('form', array('method' => 'post',
  83. 'id' => 'form_settings_other',
  84. 'class' => 'form_settings',
  85. 'action' =>
  86. common_local_url('urlsettings')));
  87. $this->elementStart('fieldset');
  88. $this->hidden('token', common_session_token());
  89. $this->elementStart('ul', 'form_data');
  90. $shorteners = array();
  91. Event::handle('GetUrlShorteners', array(&$shorteners));
  92. $services = array();
  93. foreach ($shorteners as $name => $value)
  94. {
  95. $services[$name] = $name;
  96. if ($value['freeService']) {
  97. // TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a
  98. // TRANS: user's profile settings. This message has one space at the beginning. Use your
  99. // TRANS: language's word separator here if it has one (most likely a single space).
  100. $services[$name] .= _(' (free service)');
  101. }
  102. }
  103. // Include default values
  104. // TRANS: Default value for URL shortening settings.
  105. $services['none'] = _('[none]');
  106. // TRANS: Default value for URL shortening settings.
  107. $services['internal'] = _('[internal]');
  108. if ($services) {
  109. asort($services);
  110. $this->elementStart('li');
  111. // TRANS: Label for dropdown with URL shortener services.
  112. $this->dropdown('urlshorteningservice', _('Shorten URLs with'),
  113. // TRANS: Tooltip for for dropdown with URL shortener services.
  114. $services, _('Automatic shortening service to use.'),
  115. false, $user->urlshorteningservice);
  116. $this->elementEnd('li');
  117. }
  118. $this->elementStart('li');
  119. $this->input('maxurllength',
  120. // TRANS: Field label in URL settings in profile.
  121. _('URL longer than'),
  122. (!is_null($this->arg('maxurllength'))) ?
  123. $this->arg('maxurllength') : User_urlshortener_prefs::maxUrlLength($user),
  124. // TRANS: Field title in URL settings in profile.
  125. _('URLs longer than this will be shortened, -1 means never shorten because a URL is long.'));
  126. $this->elementEnd('li');
  127. $this->elementStart('li');
  128. $this->input('maxnoticelength',
  129. // TRANS: Field label in URL settings in profile.
  130. _('Text longer than'),
  131. (!is_null($this->arg('maxnoticelength'))) ?
  132. $this->arg('maxnoticelength') : User_urlshortener_prefs::maxNoticeLength($user),
  133. // TRANS: Field title in URL settings in profile.
  134. _('URLs in notices longer than this will always be shortened, -1 means only shorten if the full post exceeds maximum length.'));
  135. $this->elementEnd('li');
  136. $this->elementEnd('ul');
  137. // TRANS: Button text for saving "Other settings" in profile.
  138. $this->submit('save', _m('BUTTON','Save'));
  139. $this->elementEnd('fieldset');
  140. $this->elementEnd('form');
  141. }
  142. /**
  143. * Handle a post
  144. *
  145. * Saves the changes to url-shortening prefs and shows a success or failure
  146. * message.
  147. *
  148. * @return void
  149. */
  150. function handlePost()
  151. {
  152. // CSRF protection
  153. $token = $this->trimmed('token');
  154. if (!$token || $token != common_session_token()) {
  155. // TRANS: Client error displayed when the session token does not match or is not given.
  156. $this->showForm(_('There was a problem with your session token. '.
  157. 'Try again, please.'));
  158. return;
  159. }
  160. $urlshorteningservice = $this->trimmed('urlshorteningservice');
  161. if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
  162. // TRANS: Form validation error for form "Other settings" in user profile.
  163. $this->showForm(_('URL shortening service is too long (maximum 50 characters).'));
  164. return;
  165. }
  166. $maxurllength = $this->trimmed('maxurllength');
  167. if (!Validate::number($maxurllength, array('min' => -1))) {
  168. // TRANS: Client exception thrown when the maximum URL settings value is invalid in profile URL settings.
  169. throw new ClientException(_('Invalid number for maximum URL length.'));
  170. }
  171. $maxnoticelength = $this->trimmed('maxnoticelength');
  172. if (!Validate::number($maxnoticelength, array('min' => -1))) {
  173. // TRANS: Client exception thrown when the maximum notice length settings value is invalid in profile URL settings.
  174. throw new ClientException(_('Invalid number for maximum notice length.'));
  175. }
  176. $user = common_current_user();
  177. assert(!is_null($user)); // should already be checked
  178. $user->query('BEGIN');
  179. $original = clone($user);
  180. $user->urlshorteningservice = $urlshorteningservice;
  181. $result = $user->update($original);
  182. if ($result === false) {
  183. common_log_db_error($user, 'UPDATE', __FILE__);
  184. // TRANS: Server error displayed when "Other" settings in user profile could not be updated on the server.
  185. $this->serverError(_('Could not update user.'));
  186. }
  187. $prefs = User_urlshortener_prefs::getPrefs($user);
  188. $orig = null;
  189. if (empty($prefs)) {
  190. $prefs = new User_urlshortener_prefs();
  191. $prefs->user_id = $user->id;
  192. $prefs->created = common_sql_now();
  193. } else {
  194. $orig = clone($prefs);
  195. }
  196. $prefs->urlshorteningservice = $urlshorteningservice;
  197. $prefs->maxurllength = $maxurllength;
  198. $prefs->maxnoticelength = $maxnoticelength;
  199. if (!empty($orig)) {
  200. $result = $prefs->update($orig);
  201. } else {
  202. $result = $prefs->insert();
  203. }
  204. if (!$result) {
  205. // TRANS: Server exception thrown in profile URL settings when preferences could not be saved.
  206. throw new ServerException(_('Error saving user URL shortening preferences.'));
  207. }
  208. $user->query('COMMIT');
  209. // TRANS: Confirmation message after saving preferences.
  210. $this->showForm(_('Preferences saved.'), true);
  211. }
  212. }