profilesettings.php 21 KB

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