profilesettings.php 22 KB

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