register.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Register a new user account
  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 Login
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2008-2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
  30. /**
  31. * An action for registering a new user account
  32. *
  33. * @category Login
  34. * @package StatusNet
  35. * @author Evan Prodromou <evan@status.net>
  36. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  37. * @link http://status.net/
  38. */
  39. class RegisterAction extends Action
  40. {
  41. /**
  42. * Has there been an error?
  43. */
  44. var $error = null;
  45. /**
  46. * Have we registered?
  47. */
  48. var $registered = false;
  49. /**
  50. * Are we processing an invite?
  51. */
  52. var $invite = null;
  53. /**
  54. * Prepare page to run
  55. *
  56. *
  57. * @param $args
  58. * @return string title
  59. */
  60. protected function prepare(array $args=array())
  61. {
  62. parent::prepare($args);
  63. $this->code = $this->trimmed('code');
  64. if (empty($this->code)) {
  65. common_ensure_session();
  66. if (array_key_exists('invitecode', $_SESSION)) {
  67. $this->code = $_SESSION['invitecode'];
  68. }
  69. }
  70. if (common_config('site', 'inviteonly') && empty($this->code)) {
  71. // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
  72. $this->clientError(_('Sorry, only invited people can register.'));
  73. }
  74. if (!empty($this->code)) {
  75. $this->invite = Invitation::getKV('code', $this->code);
  76. if (!$this->invite instanceof Invitation) {
  77. // TRANS: Client error displayed when trying to register to an invite-only site without a valid invitation.
  78. $this->clientError(_('Sorry, invalid invitation code.'));
  79. }
  80. // Store this in case we need it
  81. common_ensure_session();
  82. $_SESSION['invitecode'] = $this->code;
  83. }
  84. return true;
  85. }
  86. /**
  87. * Title of the page
  88. *
  89. * @return string title
  90. */
  91. function title()
  92. {
  93. if ($this->registered) {
  94. // TRANS: Title for registration page after a succesful registration.
  95. return _('Registration successful');
  96. } else {
  97. // TRANS: Title for registration page.
  98. return _m('TITLE','Register');
  99. }
  100. }
  101. /**
  102. * Handle input, produce output
  103. *
  104. * Switches on request method; either shows the form or handles its input.
  105. *
  106. * Checks if registration is closed and shows an error if so.
  107. *
  108. * @param array $args $_REQUEST data
  109. *
  110. * @return void
  111. */
  112. function handle($args)
  113. {
  114. parent::handle($args);
  115. if (common_config('site', 'closed')) {
  116. // TRANS: Client error displayed when trying to register to a closed site.
  117. $this->clientError(_('Registration not allowed.'));
  118. } else if (common_logged_in()) {
  119. // TRANS: Client error displayed when trying to register while already logged in.
  120. $this->clientError(_('Already logged in.'));
  121. } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  122. $this->tryRegister();
  123. } else {
  124. $this->showForm();
  125. }
  126. }
  127. function showScripts()
  128. {
  129. parent::showScripts();
  130. $this->autofocus('nickname');
  131. }
  132. /**
  133. * Try to register a user
  134. *
  135. * Validates the input and tries to save a new user and profile
  136. * record. On success, shows an instructions page.
  137. *
  138. * @return void
  139. */
  140. function tryRegister()
  141. {
  142. if (Event::handle('StartRegistrationTry', array($this))) {
  143. $token = $this->trimmed('token');
  144. if (!$token || $token != common_session_token()) {
  145. // TRANS: Client error displayed when the session token does not match or is not given.
  146. $this->showForm(_('There was a problem with your session token. '.
  147. 'Try again, please.'));
  148. return;
  149. }
  150. $nickname = $this->trimmed('nickname');
  151. $email = $this->trimmed('email');
  152. $fullname = $this->trimmed('fullname');
  153. $homepage = $this->trimmed('homepage');
  154. $bio = $this->trimmed('bio');
  155. $location = $this->trimmed('location');
  156. // We don't trim these... whitespace is OK in a password!
  157. $password = $this->arg('password');
  158. $confirm = $this->arg('confirm');
  159. // invitation code, if any
  160. $code = $this->trimmed('code');
  161. if ($code) {
  162. $invite = Invitation::getKV($code);
  163. }
  164. if (common_config('site', 'inviteonly') && !($code && $invite)) {
  165. // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
  166. $this->clientError(_('Sorry, only invited people can register.'));
  167. }
  168. // Input scrubbing
  169. try {
  170. $nickname = Nickname::normalize($nickname, true);
  171. } catch (NicknameException $e) {
  172. $this->showForm($e->getMessage());
  173. return;
  174. }
  175. $email = common_canonical_email($email);
  176. if (!$this->boolean('license')) {
  177. // TRANS: Form validation error displayed when trying to register without agreeing to the site license.
  178. $this->showForm(_('You cannot register if you do not '.
  179. 'agree to the license.'));
  180. } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
  181. // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
  182. $this->showForm(_('Not a valid email address.'));
  183. } else if ($this->emailExists($email)) {
  184. // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
  185. $this->showForm(_('Email address already exists.'));
  186. } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
  187. !common_valid_http_url($homepage)) {
  188. // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
  189. $this->showForm(_('Homepage is not a valid URL.'));
  190. } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
  191. // TRANS: Form validation error displayed when trying to register with a too long full name.
  192. $this->showForm(_('Full name is too long (maximum 255 characters).'));
  193. } else if (Profile::bioTooLong($bio)) {
  194. // TRANS: Form validation error on registration page when providing too long a bio text.
  195. // TRANS: %d is the maximum number of characters for bio; used for plural.
  196. $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
  197. 'Bio is too long (maximum %d characters).',
  198. Profile::maxBio()),
  199. Profile::maxBio()));
  200. } else if (!is_null($location) && mb_strlen($location) > 255) {
  201. // TRANS: Form validation error displayed when trying to register with a too long location.
  202. $this->showForm(_('Location is too long (maximum 255 characters).'));
  203. } else if (strlen($password) < 6) {
  204. // TRANS: Form validation error displayed when trying to register with too short a password.
  205. $this->showForm(_('Password must be 6 or more characters.'));
  206. } else if ($password != $confirm) {
  207. // TRANS: Form validation error displayed when trying to register with non-matching passwords.
  208. $this->showForm(_('Passwords do not match.'));
  209. } else {
  210. try {
  211. $user = User::register(array('nickname' => $nickname,
  212. 'password' => $password,
  213. 'email' => $email,
  214. 'fullname' => $fullname,
  215. 'homepage' => $homepage,
  216. 'bio' => $bio,
  217. 'location' => $location,
  218. 'code' => $code));
  219. // success!
  220. if (!common_set_user($user)) {
  221. // TRANS: Server error displayed when saving fails during user registration.
  222. $this->serverError(_('Error setting user.'));
  223. }
  224. // this is a real login
  225. common_real_login(true);
  226. if ($this->boolean('rememberme')) {
  227. common_debug('Adding rememberme cookie for ' . $nickname);
  228. common_rememberme($user);
  229. }
  230. // Re-init language env in case it changed (not yet, but soon)
  231. common_init_language();
  232. Event::handle('EndRegistrationTry', array($this));
  233. $this->showSuccess();
  234. } catch (Exception $e) {
  235. // TRANS: Form validation error displayed when trying to register with an invalid username or password.
  236. $this->showForm($e->getMessage());
  237. }
  238. }
  239. }
  240. }
  241. /**
  242. * Does the given email address already exist?
  243. *
  244. * Checks a canonical email address against the database.
  245. *
  246. * @param string $email email address to check
  247. *
  248. * @return boolean true if the address already exists
  249. */
  250. function emailExists($email)
  251. {
  252. $email = common_canonical_email($email);
  253. if (!$email || strlen($email) == 0) {
  254. return false;
  255. }
  256. $user = User::getKV('email', $email);
  257. return is_object($user);
  258. }
  259. // overrrided to add entry-title class
  260. function showPageTitle() {
  261. if (Event::handle('StartShowPageTitle', array($this))) {
  262. $this->element('h1', array('class' => 'entry-title'), $this->title());
  263. }
  264. }
  265. // overrided to add h-entry, and content-inner class
  266. function showContentBlock()
  267. {
  268. $this->elementStart('div', array('id' => 'content', 'class' => 'h-entry'));
  269. $this->showPageTitle();
  270. $this->showPageNoticeBlock();
  271. $this->elementStart('div', array('id' => 'content_inner',
  272. 'class' => 'e-content'));
  273. // show the actual content (forms, lists, whatever)
  274. $this->showContent();
  275. $this->elementEnd('div');
  276. $this->elementEnd('div');
  277. }
  278. /**
  279. * Instructions or a notice for the page
  280. *
  281. * Shows the error, if any, or instructions for registration.
  282. *
  283. * @return void
  284. */
  285. function showPageNotice()
  286. {
  287. if ($this->registered) {
  288. return;
  289. } else if ($this->error) {
  290. $this->element('p', 'error', $this->error);
  291. } else {
  292. $instr =
  293. // TRANS: Page notice on registration page.
  294. common_markup_to_html(_('With this form you can create '.
  295. 'a new account. ' .
  296. 'You can then post notices and '.
  297. 'link up to friends and colleagues.'));
  298. $this->elementStart('div', 'instructions');
  299. $this->raw($instr);
  300. $this->elementEnd('div');
  301. }
  302. }
  303. /**
  304. * Wrapper for showing a page
  305. *
  306. * Stores an error and shows the page
  307. *
  308. * @param string $error Error, if any
  309. *
  310. * @return void
  311. */
  312. function showForm($error=null)
  313. {
  314. $this->error = $error;
  315. $this->showPage();
  316. }
  317. /**
  318. * Show the page content
  319. *
  320. * Either shows the registration form or, if registration was successful,
  321. * instructions for using the site.
  322. *
  323. * @return void
  324. */
  325. function showContent()
  326. {
  327. if ($this->registered) {
  328. $this->showSuccessContent();
  329. } else {
  330. $this->showFormContent();
  331. }
  332. }
  333. /**
  334. * Show the registration form
  335. *
  336. * @return void
  337. */
  338. function showFormContent()
  339. {
  340. $code = $this->trimmed('code');
  341. $invite = null;
  342. if ($code) {
  343. $invite = Invitation::getKV($code);
  344. }
  345. if (common_config('site', 'inviteonly') && !($code && $invite)) {
  346. // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
  347. $this->clientError(_('Sorry, only invited people can register.'));
  348. }
  349. $this->elementStart('form', array('method' => 'post',
  350. 'id' => 'form_register',
  351. 'class' => 'form_settings',
  352. 'action' => common_local_url('register')));
  353. $this->elementStart('fieldset');
  354. // TRANS: Fieldset legend on accout registration page.
  355. $this->element('legend', null, 'Account settings');
  356. $this->hidden('token', common_session_token());
  357. if ($this->code) {
  358. $this->hidden('code', $this->code);
  359. }
  360. $this->elementStart('ul', 'form_data');
  361. if (Event::handle('StartRegistrationFormData', array($this))) {
  362. $this->elementStart('li');
  363. // TRANS: Field label on account registration page.
  364. $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
  365. // TRANS: Field title on account registration page.
  366. _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
  367. $this->elementEnd('li');
  368. $this->elementStart('li');
  369. // TRANS: Field label on account registration page.
  370. $this->password('password', _('Password'),
  371. // TRANS: Field title on account registration page.
  372. _('6 or more characters.'));
  373. $this->elementEnd('li');
  374. $this->elementStart('li');
  375. // TRANS: Field label on account registration page. In this field the password has to be entered a second time.
  376. $this->password('confirm', _m('PASSWORD','Confirm'),
  377. // TRANS: Field title on account registration page.
  378. _('Same as password above.'));
  379. $this->elementEnd('li');
  380. $this->elementStart('li');
  381. if ($this->invite && $this->invite->address_type == 'email') {
  382. // TRANS: Field label on account registration page.
  383. $this->input('email', _m('LABEL','Email'), $this->invite->address,
  384. // TRANS: Field title on account registration page.
  385. _('Used only for updates, announcements, '.
  386. 'and password recovery.'));
  387. } else {
  388. // TRANS: Field label on account registration page.
  389. $this->input('email', _m('LABEL','Email'), $this->trimmed('email'),
  390. // TRANS: Field title on account registration page.
  391. _('Used only for updates, announcements, '.
  392. 'and password recovery.'));
  393. }
  394. $this->elementEnd('li');
  395. $this->elementStart('li');
  396. // TRANS: Field label on account registration page.
  397. $this->input('fullname', _('Full name'),
  398. $this->trimmed('fullname'),
  399. // TRANS: Field title on account registration page.
  400. _('Longer name, preferably your "real" name.'));
  401. $this->elementEnd('li');
  402. $this->elementStart('li');
  403. // TRANS: Field label on account registration page.
  404. $this->input('homepage', _('Homepage'),
  405. $this->trimmed('homepage'),
  406. // TRANS: Field title on account registration page.
  407. _('URL of your homepage, blog, '.
  408. 'or profile on another site.'));
  409. $this->elementEnd('li');
  410. $this->elementStart('li');
  411. $maxBio = Profile::maxBio();
  412. if ($maxBio > 0) {
  413. // TRANS: Text area title in form for account registration. Plural
  414. // TRANS: is decided by the number of characters available for the
  415. // TRANS: biography (%d).
  416. $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
  417. 'Describe yourself and your interests in %d characters.',
  418. $maxBio),
  419. $maxBio);
  420. } else {
  421. // TRANS: Text area title on account registration page.
  422. $bioInstr = _('Describe yourself and your interests.');
  423. }
  424. // TRANS: Text area label on account registration page.
  425. $this->textarea('bio', _('Bio'),
  426. $this->trimmed('bio'),
  427. $bioInstr);
  428. $this->elementEnd('li');
  429. $this->elementStart('li');
  430. // TRANS: Field label on account registration page.
  431. $this->input('location', _('Location'),
  432. $this->trimmed('location'),
  433. // TRANS: Field title on account registration page.
  434. _('Where you are, like "City, '.
  435. 'State (or Region), Country".'));
  436. $this->elementEnd('li');
  437. Event::handle('EndRegistrationFormData', array($this));
  438. $this->elementStart('li', array('id' => 'settings_rememberme'));
  439. // TRANS: Checkbox label on account registration page.
  440. $this->checkbox('rememberme', _('Remember me'),
  441. $this->boolean('rememberme'),
  442. // TRANS: Checkbox title on account registration page.
  443. _('Automatically login in the future; '.
  444. 'not for shared computers!'));
  445. $this->elementEnd('li');
  446. $attrs = array('type' => 'checkbox',
  447. 'id' => 'license',
  448. 'class' => 'checkbox',
  449. 'name' => 'license',
  450. 'value' => 'true');
  451. if ($this->boolean('license')) {
  452. $attrs['checked'] = 'checked';
  453. }
  454. $this->elementStart('li');
  455. $this->element('input', $attrs);
  456. $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
  457. $this->raw($this->licenseCheckbox());
  458. $this->elementEnd('label');
  459. $this->elementEnd('li');
  460. }
  461. $this->elementEnd('ul');
  462. // TRANS: Button text to register a user on account registration page.
  463. $this->submit('submit', _m('BUTTON','Register'));
  464. $this->elementEnd('fieldset');
  465. $this->elementEnd('form');
  466. }
  467. function licenseCheckbox()
  468. {
  469. $out = '';
  470. switch (common_config('license', 'type')) {
  471. case 'private':
  472. $out .= htmlspecialchars(sprintf(
  473. // TRANS: Copyright checkbox label in registration dialog, for private sites.
  474. // TRANS: %1$s is the StatusNet sitename.
  475. _('I understand that content and data of %1$s are private and confidential.'),
  476. common_config('site', 'name')));
  477. // fall through
  478. case 'allrightsreserved':
  479. if ($out != '') {
  480. $out .= ' ';
  481. }
  482. if (common_config('license', 'owner')) {
  483. $out .= htmlspecialchars(sprintf(
  484. // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
  485. // TRANS: %1$s is the license owner.
  486. _('My text and files are copyright by %1$s.'),
  487. common_config('license', 'owner')));
  488. } else {
  489. // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
  490. $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
  491. }
  492. // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
  493. $out .= ' ' . _('All rights reserved.');
  494. break;
  495. case 'cc': // fall through
  496. default:
  497. // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
  498. $message = _('My text and files are available under %s ' .
  499. 'except this private data: password, ' .
  500. 'email address, IM address, and phone number.');
  501. $link = '<a href="' .
  502. htmlspecialchars(common_config('license', 'url')) .
  503. '">' .
  504. htmlspecialchars(common_config('license', 'title')) .
  505. '</a>';
  506. $out .= sprintf(htmlspecialchars($message), $link);
  507. }
  508. return $out;
  509. }
  510. /**
  511. * Show some information about registering for the site
  512. *
  513. * Save the registration flag, run showPage
  514. *
  515. * @return void
  516. */
  517. function showSuccess()
  518. {
  519. $this->registered = true;
  520. $this->showPage();
  521. }
  522. /**
  523. * Show some information about registering for the site
  524. *
  525. * Gives some information and options for new registrees.
  526. *
  527. * @return void
  528. */
  529. function showSuccessContent()
  530. {
  531. if (Event::handle('StartRegisterSuccess', array($this))) {
  532. $nickname = $this->arg('nickname');
  533. $profileurl = common_local_url('showstream',
  534. array('nickname' => $nickname));
  535. $this->elementStart('div', 'success');
  536. // TRANS: Text displayed after successful account registration.
  537. // TRANS: %1$s is the registered nickname, %2$s is the profile URL.
  538. // TRANS: This message contains Markdown links in the form [link text](link)
  539. // TRANS: and variables in the form %%%%variable%%%%. Please mind the syntax.
  540. $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
  541. 'From here, you may want to...'. "\n\n" .
  542. '* Go to [your profile](%2$s) '.
  543. 'and post your first message.' . "\n" .
  544. '* Add a [Jabber/GTalk address]'.
  545. '(%%%%action.imsettings%%%%) '.
  546. 'so you can send notices '.
  547. 'through instant messages.' . "\n" .
  548. '* [Search for people](%%%%action.peoplesearch%%%%) '.
  549. 'that you may know or '.
  550. 'that share your interests. ' . "\n" .
  551. '* Update your [profile settings]'.
  552. '(%%%%action.profilesettings%%%%)'.
  553. ' to tell others more about you. ' . "\n" .
  554. '* Read over the [online docs](%%%%doc.help%%%%)'.
  555. ' for features you may have missed. ' . "\n\n" .
  556. 'Thanks for signing up and we hope '.
  557. 'you enjoy using this service.'),
  558. $nickname, $profileurl);
  559. $this->raw(common_markup_to_html($instr));
  560. $have_email = $this->trimmed('email');
  561. if ($have_email) {
  562. // TRANS: Instruction text on how to deal with the e-mail address confirmation e-mail.
  563. $emailinstr = _('(You should receive a message by email '.
  564. 'momentarily, with ' .
  565. 'instructions on how to confirm '.
  566. 'your email address.)');
  567. $this->raw(common_markup_to_html($emailinstr));
  568. }
  569. $this->elementEnd('div');
  570. Event::handle('EndRegisterSuccess', array($this));
  571. }
  572. }
  573. /**
  574. * Show the login group nav menu
  575. *
  576. * @return void
  577. */
  578. function showLocalNav()
  579. {
  580. if (common_logged_in()) {
  581. parent::showLocalNav();
  582. } else {
  583. $nav = new LoginGroupNav($this);
  584. $nav->show();
  585. }
  586. }
  587. /**
  588. * Show a bit of login context
  589. *
  590. * @return nothing
  591. */
  592. function showProfileBlock()
  593. {
  594. if (common_logged_in()) {
  595. parent::showProfileBlock();
  596. }
  597. }
  598. }