profilesettings.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. * Change profile settings
  18. *
  19. * @category Settings
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Zach Copley <zach@status.net>
  23. * @author Sarven Capadisli <csarven@status.net>
  24. * @copyright 2008-2009 StatusNet, Inc.
  25. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  26. */
  27. defined('GNUSOCIAL') || die();
  28. /**
  29. * Change profile settings
  30. *
  31. * @copyright 2008-2009 StatusNet, Inc.
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class ProfilesettingsAction extends SettingsAction
  35. {
  36. /**
  37. * Title of the page
  38. *
  39. * @return string Title of the page
  40. */
  41. public function title()
  42. {
  43. // TRANS: Page title for profile settings.
  44. return _('Profile settings');
  45. }
  46. /**
  47. * Instructions for use
  48. *
  49. * @return instructions for use
  50. */
  51. public function getInstructions()
  52. {
  53. // TRANS: Usage instructions for profile settings.
  54. return _('You can update your personal profile info here '.
  55. 'so people know more about you.');
  56. }
  57. public function showScripts()
  58. {
  59. parent::showScripts();
  60. $this->autofocus('fullname');
  61. }
  62. /**
  63. * Content area of the page
  64. *
  65. * Shows a form for uploading an avatar.
  66. *
  67. * @return void
  68. */
  69. public function showContent()
  70. {
  71. $user = $this->scoped->getUser();
  72. $this->elementStart('form', array('method' => 'post',
  73. 'id' => 'form_settings_profile',
  74. 'class' => 'form_settings',
  75. 'action' => common_local_url('profilesettings')));
  76. $this->elementStart('fieldset');
  77. // TRANS: Profile settings form legend.
  78. $this->element('legend', null, _('Profile information'));
  79. $this->hidden('token', common_session_token());
  80. // too much common patterns here... abstractable?
  81. $this->elementStart('ul', 'form_data');
  82. if (Event::handle('StartProfileFormData', array($this))) {
  83. $this->elementStart('li');
  84. // TRANS: Field label in form for profile settings.
  85. $this->input(
  86. 'nickname',
  87. _('Nickname'),
  88. $this->trimmed('nickname') ?: $this->scoped->getNickname(),
  89. // TRANS: Tooltip for field label in form for profile settings.
  90. _('1-64 lowercase letters or numbers, no punctuation or spaces.'),
  91. null,
  92. false, // "name" (will be set to id), then "required"
  93. (common_config('profile', 'changenick')
  94. ? ['placeholder' => null]
  95. : ['disabled' => 'disabled', 'placeholder' => null])
  96. );
  97. $this->elementEnd('li');
  98. $this->elementStart('li');
  99. // TRANS: Field label in form for profile settings.
  100. $this->input(
  101. 'fullname',
  102. _('Full name'),
  103. $this->trimmed('fullname') ?: $this->scoped->getFullname(),
  104. // TRANS: Instructions for full name text field on profile settings
  105. _('A full name is required, if empty it will be set to your nickname.'),
  106. null,
  107. true
  108. );
  109. $this->elementEnd('li');
  110. $this->elementStart('li');
  111. // TRANS: Field label in form for profile settings.
  112. $this->input(
  113. 'homepage',
  114. _('Homepage'),
  115. $this->trimmed('homepage') ?: $this->scoped->getHomepage(),
  116. // TRANS: Tooltip for field label in form for profile settings.
  117. _('URL of your homepage, blog, or profile on another site.')
  118. );
  119. $this->elementEnd('li');
  120. $this->elementStart('li');
  121. $maxBio = Profile::maxBio();
  122. if ($maxBio > 0) {
  123. // TRANS: Tooltip for field label in form for profile settings. Plural
  124. // TRANS: is decided by the number of characters available for the
  125. // TRANS: biography (%d).
  126. $bioInstr = sprintf(
  127. _m('Describe yourself and your interests in %d character.',
  128. 'Describe yourself and your interests in %d characters.',
  129. $maxBio),
  130. $maxBio
  131. );
  132. } else {
  133. // TRANS: Tooltip for field label in form for profile settings.
  134. $bioInstr = _('Describe yourself and your interests.');
  135. }
  136. // TRANS: Text area label in form for profile settings where users can provide
  137. // TRANS: their biography.
  138. $this->textarea(
  139. 'bio',
  140. _('Bio'),
  141. ($this->trimmed('bio') ?: $this->scoped->getDescription()),
  142. $bioInstr
  143. );
  144. $this->elementEnd('li');
  145. $this->elementStart('li');
  146. // TRANS: Field label in form for profile settings.
  147. $this->input(
  148. 'location',
  149. _('Location'),
  150. ($this->trimmed('location') ?: $this->scoped->location),
  151. // TRANS: Tooltip for field label in form for profile settings.
  152. _('Where you are, like "City, State (or Region), Country".')
  153. );
  154. $this->elementEnd('li');
  155. if (common_config('location', 'share') == 'user') {
  156. $this->elementStart('li');
  157. // TRANS: Checkbox label in form for profile settings.
  158. $this->checkbox(
  159. 'sharelocation',
  160. _('Share my current location when posting notices'),
  161. ($this->arg('sharelocation') ?
  162. $this->boolean('sharelocation') : $this->scoped->shareLocation())
  163. );
  164. $this->elementEnd('li');
  165. }
  166. Event::handle('EndProfileFormData', array($this));
  167. $this->elementStart('li');
  168. // TRANS: Field label in form for profile settings.
  169. $this->input(
  170. 'tags',
  171. _('Tags'),
  172. ($this->trimmed('tags') ?: implode(' ', Profile_tag::getSelfTagsArray($this->scoped))),
  173. // TRANS: Tooltip for field label in form for profile settings.
  174. _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated.')
  175. );
  176. $this->elementEnd('li');
  177. $this->elementStart('li');
  178. $language = common_language();
  179. // TRANS: Dropdownlist label in form for profile settings.
  180. $this->dropdown(
  181. 'language',
  182. _('Language'),
  183. // TRANS: Tooltip for dropdown list label in form for profile settings.
  184. get_nice_language_list(),
  185. _('Preferred language.'),
  186. false,
  187. $language
  188. );
  189. $this->elementEnd('li');
  190. $timezone = common_timezone();
  191. $timezones = array();
  192. foreach (DateTimeZone::listIdentifiers() as $k => $v) {
  193. $timezones[$v] = $v;
  194. }
  195. $this->elementStart('li');
  196. // TRANS: Dropdownlist label in form for profile settings.
  197. $this->dropdown(
  198. 'timezone',
  199. _('Timezone'),
  200. // TRANS: Tooltip for dropdown list label in form for profile settings.
  201. $timezones,
  202. _('What timezone are you normally in?'),
  203. true,
  204. $timezone
  205. );
  206. $this->elementEnd('li');
  207. $this->elementStart('li');
  208. $this->checkbox(
  209. 'autosubscribe',
  210. // TRANS: Checkbox label in form for profile settings.
  211. _('Automatically subscribe to whoever '.
  212. 'subscribes to me (best for non-humans)'),
  213. ($this->arg('autosubscribe') ?
  214. $this->boolean('autosubscribe') : $user->autosubscribe)
  215. );
  216. $this->elementEnd('li');
  217. $this->elementStart('li');
  218. $this->dropdown(
  219. 'subscribe_policy',
  220. // TRANS: Dropdown field label on profile settings, for what policies to apply when someone else tries to subscribe to your updates.
  221. _('Subscription policy'),
  222. [
  223. // TRANS: Dropdown field option for following policy.
  224. User::SUBSCRIBE_POLICY_OPEN => _('Let anyone follow me'),
  225. // TRANS: Dropdown field option for following policy.
  226. User::SUBSCRIBE_POLICY_MODERATE => _('Ask me first'),
  227. ],
  228. // TRANS: Dropdown field title on group edit form.
  229. _('Whether other users need your permission to follow your updates.'),
  230. false,
  231. (empty($user->subscribe_policy) ? User::SUBSCRIBE_POLICY_OPEN : $user->subscribe_policy)
  232. );
  233. $this->elementEnd('li');
  234. }
  235. if (common_config('profile', 'allowprivate') || $user->private_stream) {
  236. $this->elementStart('li');
  237. $this->checkbox(
  238. 'private_stream',
  239. // TRANS: Checkbox label in profile settings.
  240. _('Make updates visible only to my followers'),
  241. ($this->arg('private_stream') ?
  242. $this->boolean('private_stream') : $user->private_stream)
  243. );
  244. $this->elementEnd('li');
  245. }
  246. $this->elementEnd('ul');
  247. // TRANS: Button to save input in profile settings.
  248. $this->submit('save', _m('BUTTON', 'Save'));
  249. $this->elementEnd('fieldset');
  250. $this->elementEnd('form');
  251. }
  252. /**
  253. * Handle a post
  254. *
  255. * Validate input and save changes. Reload the form with a success
  256. * or error message.
  257. *
  258. * @return void
  259. */
  260. protected function doPost()
  261. {
  262. if (Event::handle('StartProfileSaveForm', array($this))) {
  263. // $nickname will only be set if this changenick value is true.
  264. if (common_config('profile', 'changenick') == true) {
  265. try {
  266. $nickname = Nickname::normalize($this->trimmed('nickname'), true);
  267. } catch (NicknameTakenException $e) {
  268. // Abort only if the nickname is occupied by _another_ local user profile
  269. if (!$this->scoped->sameAs($e->profile)) {
  270. throw $e;
  271. }
  272. // Since the variable wasn't set before the exception was thrown, let's run
  273. // the normalize sequence again, but without in-use check this time.
  274. $nickname = Nickname::normalize($this->trimmed('nickname'));
  275. }
  276. }
  277. $fullname = $this->trimmed('fullname');
  278. $homepage = $this->trimmed('homepage');
  279. $bio = $this->trimmed('bio');
  280. $location = $this->trimmed('location');
  281. $autosubscribe = $this->boolean('autosubscribe');
  282. $subscribe_policy = $this->trimmed('subscribe_policy');
  283. $language = $this->trimmed('language');
  284. $timezone = $this->trimmed('timezone');
  285. $tagstring = $this->trimmed('tags');
  286. // Some validation
  287. if (!is_null($homepage) && (strlen($homepage) > 0) &&
  288. !common_valid_http_url($homepage)) {
  289. // TRANS: Validation error in form for profile settings.
  290. throw new ClientException(_('Homepage is not a valid URL.'));
  291. } elseif (!is_null($fullname) && mb_strlen($fullname) > 191) {
  292. // TRANS: Validation error in form for profile settings.
  293. throw new ClientException(_('Full name is too long (maximum 191 characters).'));
  294. } elseif (Profile::bioTooLong($bio)) {
  295. // TRANS: Validation error in form for profile settings.
  296. // TRANS: Plural form is used based on the maximum number of allowed
  297. // TRANS: characters for the biography (%d).
  298. throw new ClientException(sprintf(
  299. _m('Bio is too long (maximum %d character).',
  300. 'Bio is too long (maximum %d characters).',
  301. Profile::maxBio()),
  302. Profile::maxBio()
  303. ));
  304. } elseif (!is_null($location) && mb_strlen($location) > 191) {
  305. // TRANS: Validation error in form for profile settings.
  306. throw new ClientException(_('Location is too long (maximum 191 characters).'));
  307. } elseif (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
  308. // TRANS: Validation error in form for profile settings.
  309. throw new ClientException(_('Timezone not selected.'));
  310. } elseif (!is_null($language) && strlen($language) > 50) {
  311. // TRANS: Validation error in form for profile settings.
  312. throw new ClientException(_('Language is too long (maximum 50 characters).'));
  313. }
  314. $tags = array();
  315. $tag_priv = array();
  316. if (is_string($tagstring) && strlen($tagstring) > 0) {
  317. $tags = preg_split('/[\s,]+/', $tagstring);
  318. foreach ($tags as &$tag) {
  319. $private = @$tag[0] === '.';
  320. $tag = common_canonical_tag($tag);
  321. if (!common_valid_profile_tag($tag)) {
  322. // TRANS: Validation error in form for profile settings.
  323. // TRANS: %s is an invalid tag.
  324. throw new ClientException(sprintf(_('Invalid tag: "%s".'), $tag));
  325. }
  326. $tag_priv[$tag] = $private;
  327. }
  328. }
  329. $user = $this->scoped->getUser();
  330. $user->query('BEGIN');
  331. // Only allow setting private_stream if site policy allows it
  332. // (or user already _has_ a private stream, then you can unset it)
  333. if (common_config('profile', 'allowprivate') || $user->private_stream) {
  334. $private_stream = $this->boolean('private_stream');
  335. } else {
  336. // if not allowed, we set to the existing value
  337. $private_stream = (bool) $user->private_stream;
  338. }
  339. // $user->nickname is updated through Profile->update();
  340. if ((bool) $user->autosubscribe != $autosubscribe
  341. || (bool) $user->private_stream != $private_stream
  342. || $user->timezone != $timezone
  343. || $user->language != $language
  344. || $user->subscribe_policy != $subscribe_policy) {
  345. $original = clone($user);
  346. $user->autosubscribe = $autosubscribe;
  347. $user->language = $language;
  348. $user->private_stream = $private_stream;
  349. $user->subscribe_policy = $subscribe_policy;
  350. $user->timezone = $timezone;
  351. $result = $user->update($original);
  352. if ($result === false) {
  353. common_log_db_error($user, 'UPDATE', __FILE__);
  354. $user->query('ROLLBACK');
  355. // TRANS: Server error thrown when user profile settings could not be updated to
  356. // TRANS: automatically subscribe to any subscriber.
  357. throw new ServerException(_('Could not update user for autosubscribe or subscribe_policy.'));
  358. }
  359. // Re-initialize language environment if it changed
  360. common_init_language();
  361. }
  362. $original = clone($this->scoped);
  363. if (common_config('profile', 'changenick') == true && $this->scoped->getNickname() !== $nickname) {
  364. assert(Nickname::normalize($nickname)===$nickname);
  365. common_debug("Changing user nickname from '{$this->scoped->getNickname()}' to '{$nickname}'.");
  366. $this->scoped->nickname = $nickname;
  367. $this->scoped->profileurl = common_profile_url($this->scoped->getNickname());
  368. }
  369. $this->scoped->fullname = (mb_strlen($fullname)>0 ? $fullname : $this->scoped->nickname);
  370. $this->scoped->homepage = $homepage;
  371. $this->scoped->bio = $bio;
  372. $this->scoped->location = $location;
  373. $loc = Location::fromName($location);
  374. if (empty($loc)) {
  375. $this->scoped->lat = null;
  376. $this->scoped->lon = null;
  377. $this->scoped->location_id = null;
  378. $this->scoped->location_ns = null;
  379. } else {
  380. $this->scoped->lat = $loc->lat;
  381. $this->scoped->lon = $loc->lon;
  382. $this->scoped->location_id = $loc->location_id;
  383. $this->scoped->location_ns = $loc->location_ns;
  384. }
  385. if (common_config('location', 'share') == 'user') {
  386. $exists = false;
  387. $prefs = User_location_prefs::getKV('user_id', $this->scoped->getID());
  388. if (empty($prefs)) {
  389. $prefs = new User_location_prefs();
  390. $prefs->user_id = $this->scoped->getID();
  391. $prefs->created = common_sql_now();
  392. } else {
  393. $exists = true;
  394. $orig = clone($prefs);
  395. }
  396. $prefs->share_location = $this->boolean('sharelocation');
  397. if ($exists) {
  398. $result = $prefs->update($orig);
  399. } else {
  400. $result = $prefs->insert();
  401. }
  402. if ($result === false) {
  403. common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
  404. $user->query('ROLLBACK');
  405. // TRANS: Server error thrown when user profile location preference settings could not be updated.
  406. throw new ServerException(_('Could not save location prefs.'));
  407. }
  408. }
  409. common_debug('Old profile: ' . common_log_objstring($original), __FILE__);
  410. common_debug('New profile: ' . common_log_objstring($this->scoped), __FILE__);
  411. $result = $this->scoped->update($original);
  412. if ($result === false) {
  413. common_log_db_error($this->scoped, 'UPDATE', __FILE__);
  414. $user->query('ROLLBACK');
  415. // TRANS: Server error thrown when user profile settings could not be saved.
  416. throw new ServerException(_('Could not save profile.'));
  417. }
  418. // Set the user tags
  419. $result = Profile_tag::setSelfTags($this->scoped, $tags, $tag_priv);
  420. $user->query('COMMIT');
  421. Event::handle('EndProfileSaveForm', array($this));
  422. // TRANS: Confirmation shown when user profile settings are saved.
  423. return _('Settings saved.');
  424. }
  425. }
  426. public function showAside()
  427. {
  428. $this->elementStart('div', array('id' => 'aside_primary',
  429. 'class' => 'aside'));
  430. $this->elementStart('div', array('id' => 'account_actions',
  431. 'class' => 'section'));
  432. $this->elementStart('ul');
  433. if (Event::handle('StartProfileSettingsActions', array($this))) {
  434. if ($this->scoped->hasRight(Right::BACKUPACCOUNT)) {
  435. $this->elementStart('li');
  436. $this->element(
  437. 'a',
  438. ['href' => common_local_url('backupaccount')],
  439. // TRANS: Option in profile settings to create a backup of the account of the currently logged in user.
  440. _('Backup account')
  441. );
  442. $this->elementEnd('li');
  443. }
  444. if ($this->scoped->hasRight(Right::DELETEACCOUNT)) {
  445. $this->elementStart('li');
  446. $this->element(
  447. 'a',
  448. ['href' => common_local_url('deleteaccount')],
  449. // TRANS: Option in profile settings to delete the account of the currently logged in user.
  450. _('Delete account')
  451. );
  452. $this->elementEnd('li');
  453. }
  454. if ($this->scoped->hasRight(Right::RESTOREACCOUNT)) {
  455. $this->elementStart('li');
  456. $this->element(
  457. 'a',
  458. ['href' => common_local_url('restoreaccount')],
  459. // TRANS: Option in profile settings to restore the account of the currently logged in user from a backup.
  460. _('Restore account')
  461. );
  462. $this->elementEnd('li');
  463. }
  464. Event::handle('EndProfileSettingsActions', array($this));
  465. }
  466. $this->elementEnd('ul');
  467. $this->elementEnd('div');
  468. $this->elementEnd('div');
  469. }
  470. }