profilesettings.php 22 KB

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