urlsettings.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Miscellaneous settings
  18. *
  19. * @category Settings
  20. * @package GNUsocial
  21. * @author Robin Millette <millette@status.net>
  22. * @author Evan Prodromou <evan@status.net>
  23. * @copyright 2008-2009 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * Miscellaneous settings actions
  29. *
  30. * Currently this just manages URL shortening.
  31. *
  32. * @category Settings
  33. * @package GNUsocial
  34. * @author Robin Millette <millette@status.net>
  35. * @author Zach Copley <zach@status.net>
  36. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  37. */
  38. class UrlsettingsAction extends SettingsAction
  39. {
  40. /**
  41. * Title of the page
  42. *
  43. * @return string Title of the page
  44. */
  45. public function title()
  46. {
  47. // TRANS: Title of URL settings tab in profile settings.
  48. return _('URL settings');
  49. }
  50. /**
  51. * Instructions for use
  52. *
  53. * @return instructions for use
  54. */
  55. public function getInstructions()
  56. {
  57. // TRANS: Instructions for tab "Other" in user profile settings.
  58. return _('Manage various other options.');
  59. }
  60. public function showScripts()
  61. {
  62. parent::showScripts();
  63. $this->autofocus('urlshorteningservice');
  64. }
  65. /**
  66. * Content area of the page
  67. *
  68. * Shows a form for uploading an avatar.
  69. *
  70. * @return void
  71. */
  72. public function showContent()
  73. {
  74. $user = $this->scoped->getUser();
  75. $this->elementStart('form', array('method' => 'post',
  76. 'id' => 'form_settings_other',
  77. 'class' => 'form_settings',
  78. 'action' =>
  79. common_local_url('urlsettings')));
  80. $this->elementStart('fieldset');
  81. $this->hidden('token', common_session_token());
  82. $this->elementStart('ul', 'form_data');
  83. $shorteners = array();
  84. Event::handle('GetUrlShorteners', array(&$shorteners));
  85. $services = array();
  86. foreach ($shorteners as $name => $value) {
  87. $services[$name] = $name;
  88. if ($value['freeService']) {
  89. // TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a
  90. // TRANS: user's profile settings. This message has one space at the beginning. Use your
  91. // TRANS: language's word separator here if it has one (most likely a single space).
  92. $services[$name] .= _(' (free service)');
  93. }
  94. }
  95. // Include default values
  96. // TRANS: Default value for URL shortening settings.
  97. $services['none'] = _('[none]');
  98. // TRANS: Default value for URL shortening settings.
  99. $services['internal'] = _('[internal]');
  100. if ($services) {
  101. asort($services);
  102. $this->elementStart('li');
  103. // TRANS: Label for dropdown with URL shortener services.
  104. $this->dropdown(
  105. 'urlshorteningservice',
  106. _('Shorten URLs with'),
  107. // TRANS: Tooltip for for dropdown with URL shortener services.
  108. $services,
  109. _('Automatic shortening service to use.'),
  110. false,
  111. $user->urlshorteningservice
  112. );
  113. $this->elementEnd('li');
  114. }
  115. $this->elementStart('li');
  116. $this->input(
  117. 'maxurllength',
  118. // TRANS: Field label in URL settings in profile.
  119. _('URL longer than'),
  120. ($this->arg('maxurllength') ?? User_urlshortener_prefs::maxUrlLength($user)),
  121. // TRANS: Field title in URL settings in profile.
  122. _('URLs longer than this will be shortened, -1 means never shorten because a URL is long.')
  123. );
  124. $this->elementEnd('li');
  125. $this->elementStart('li');
  126. $this->input(
  127. 'maxnoticelength',
  128. // TRANS: Field label in URL settings in profile.
  129. _('Text longer than'),
  130. ($this->arg('maxnoticelength') ?? User_urlshortener_prefs::maxNoticeLength($user)),
  131. // TRANS: Field title in URL settings in profile.
  132. _('URLs in notices longer than this will always be shortened, -1 means only shorten if the full post exceeds maximum length.')
  133. );
  134. $this->elementEnd('li');
  135. $this->elementEnd('ul');
  136. // TRANS: Button text for saving "Other settings" in profile.
  137. $this->submit('save', _m('BUTTON', 'Save'));
  138. $this->elementEnd('fieldset');
  139. $this->elementEnd('form');
  140. }
  141. protected function doPost()
  142. {
  143. $urlshorteningservice = $this->trimmed('urlshorteningservice');
  144. if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
  145. // TRANS: Form validation error for form "Other settings" in user profile.
  146. throw new ClientException(_('URL shortening service is too long (maximum 50 characters).'));
  147. }
  148. $maxurllength = $this->trimmed('maxurllength');
  149. if (!Validate::number($maxurllength, array('min' => -1))) {
  150. // TRANS: Client exception thrown when the maximum URL settings value is invalid in profile URL settings.
  151. throw new ClientException(_('Invalid number for maximum URL length.'));
  152. }
  153. $maxnoticelength = $this->trimmed('maxnoticelength');
  154. if (!Validate::number($maxnoticelength, array('min' => -1))) {
  155. // TRANS: Client exception thrown when the maximum notice length settings value is invalid in profile URL settings.
  156. throw new ClientException(_('Invalid number for maximum notice length.'));
  157. }
  158. $user = $this->scoped->getUser();
  159. $user->query('START TRANSACTION');
  160. $original = clone($user);
  161. $user->urlshorteningservice = $urlshorteningservice;
  162. $result = $user->update($original);
  163. if ($result === false) {
  164. common_log_db_error($user, 'UPDATE', __FILE__);
  165. $user->query('ROLLBACK');
  166. // TRANS: Server error displayed when "Other" settings in user profile could not be updated on the server.
  167. throw new ServerException(_('Could not update user.'));
  168. }
  169. $prefs = User_urlshortener_prefs::getPrefs($user);
  170. $orig = null;
  171. if (!$prefs instanceof User_urlshortener_prefs) {
  172. $prefs = new User_urlshortener_prefs();
  173. $prefs->user_id = $user->id;
  174. $prefs->created = common_sql_now();
  175. } else {
  176. $orig = clone($prefs);
  177. }
  178. $prefs->urlshorteningservice = $urlshorteningservice;
  179. $prefs->maxurllength = $maxurllength;
  180. $prefs->maxnoticelength = $maxnoticelength;
  181. if ($orig instanceof User_urlshortener_prefs) {
  182. $result = $prefs->update($orig);
  183. } else {
  184. $result = $prefs->insert();
  185. }
  186. if ($result === null) {
  187. $user->query('ROLLBACK');
  188. // TRANS: Server exception thrown in profile URL settings when preferences could not be saved.
  189. throw new ServerException(_('Error saving user URL shortening preferences.'));
  190. }
  191. $user->query('COMMIT');
  192. // TRANS: Confirmation message after saving preferences.
  193. return _('Preferences saved.');
  194. }
  195. }