123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- defined('GNUSOCIAL') || die();
- class UrlsettingsAction extends SettingsAction
- {
-
- public function title()
- {
-
- return _('URL settings');
- }
-
- public function getInstructions()
- {
-
- return _('Manage various other options.');
- }
- public function showScripts()
- {
- parent::showScripts();
- $this->autofocus('urlshorteningservice');
- }
-
- public function showContent()
- {
- $user = $this->scoped->getUser();
- $this->elementStart('form', array('method' => 'post',
- 'id' => 'form_settings_other',
- 'class' => 'form_settings',
- 'action' =>
- common_local_url('urlsettings')));
- $this->elementStart('fieldset');
- $this->hidden('token', common_session_token());
- $this->elementStart('ul', 'form_data');
- $shorteners = array();
- Event::handle('GetUrlShorteners', array(&$shorteners));
- $services = array();
- foreach ($shorteners as $name => $value) {
- $services[$name] = $name;
- if ($value['freeService']) {
-
-
-
- $services[$name] .= _(' (free service)');
- }
- }
-
-
- $services['none'] = _('[none]');
-
- $services['internal'] = _('[internal]');
- if ($services) {
- asort($services);
- $this->elementStart('li');
-
- $this->dropdown(
- 'urlshorteningservice',
- _('Shorten URLs with'),
-
- $services,
- _('Automatic shortening service to use.'),
- false,
- $user->urlshorteningservice
- );
- $this->elementEnd('li');
- }
- $this->elementStart('li');
- $this->input(
- 'maxurllength',
-
- _('URL longer than'),
- ($this->arg('maxurllength') ?? User_urlshortener_prefs::maxUrlLength($user)),
-
- _('URLs longer than this will be shortened, -1 means never shorten because a URL is long.')
- );
- $this->elementEnd('li');
- $this->elementStart('li');
- $this->input(
- 'maxnoticelength',
-
- _('Text longer than'),
- ($this->arg('maxnoticelength') ?? User_urlshortener_prefs::maxNoticeLength($user)),
-
- _('URLs in notices longer than this will always be shortened, -1 means only shorten if the full post exceeds maximum length.')
- );
- $this->elementEnd('li');
- $this->elementEnd('ul');
-
- $this->submit('save', _m('BUTTON', 'Save'));
- $this->elementEnd('fieldset');
- $this->elementEnd('form');
- }
- protected function doPost()
- {
- $urlshorteningservice = $this->trimmed('urlshorteningservice');
- if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
-
- throw new ClientException(_('URL shortening service is too long (maximum 50 characters).'));
- }
- $maxurllength = $this->trimmed('maxurllength');
- if (!Validate::number($maxurllength, array('min' => -1))) {
-
- throw new ClientException(_('Invalid number for maximum URL length.'));
- }
- $maxnoticelength = $this->trimmed('maxnoticelength');
- if (!Validate::number($maxnoticelength, array('min' => -1))) {
-
- throw new ClientException(_('Invalid number for maximum notice length.'));
- }
- $user = $this->scoped->getUser();
- $user->query('START TRANSACTION');
- $original = clone($user);
- $user->urlshorteningservice = $urlshorteningservice;
- $result = $user->update($original);
- if ($result === false) {
- common_log_db_error($user, 'UPDATE', __FILE__);
- $user->query('ROLLBACK');
-
- throw new ServerException(_('Could not update user.'));
- }
- $prefs = User_urlshortener_prefs::getPrefs($user);
- $orig = null;
- if (!$prefs instanceof User_urlshortener_prefs) {
- $prefs = new User_urlshortener_prefs();
- $prefs->user_id = $user->id;
- $prefs->created = common_sql_now();
- } else {
- $orig = clone($prefs);
- }
- $prefs->urlshorteningservice = $urlshorteningservice;
- $prefs->maxurllength = $maxurllength;
- $prefs->maxnoticelength = $maxnoticelength;
- if ($orig instanceof User_urlshortener_prefs) {
- $result = $prefs->update($orig);
- } else {
- $result = $prefs->insert();
- }
- if ($result === null) {
- $user->query('ROLLBACK');
-
- throw new ServerException(_('Error saving user URL shortening preferences.'));
- }
- $user->query('COMMIT');
-
- return _('Preferences saved.');
- }
- }
|