urlsettings.php 8.3 KB

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