Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
9.7KB

  1. <?php
  2. /**
  3. * @file mod/register.php
  4. */
  5. use Friendica\App;
  6. use Friendica\BaseModule;
  7. use Friendica\Content\Text\BBCode;
  8. use Friendica\Core\Addon;
  9. use Friendica\Core\Config;
  10. use Friendica\Core\L10n;
  11. use Friendica\Core\Logger;
  12. use Friendica\Core\PConfig;
  13. use Friendica\Core\Renderer;
  14. use Friendica\Core\System;
  15. use Friendica\Core\Worker;
  16. use Friendica\Model;
  17. use Friendica\Module\Tos;
  18. use Friendica\Util\Strings;
  19. function register_post(App $a)
  20. {
  21. BaseModule::checkFormSecurityTokenRedirectOnError('/register', 'register');
  22. $verified = 0;
  23. $blocked = 1;
  24. $arr = ['post' => $_POST];
  25. Addon::callHooks('register_post', $arr);
  26. $max_dailies = intval(Config::get('system', 'max_daily_registrations'));
  27. if ($max_dailies) {
  28. $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day");
  29. if ($r && $r[0]['total'] >= $max_dailies) {
  30. return;
  31. }
  32. }
  33. switch (Config::get('config', 'register_policy')) {
  34. case REGISTER_OPEN:
  35. $blocked = 0;
  36. $verified = 1;
  37. break;
  38. case REGISTER_APPROVE:
  39. $blocked = 1;
  40. $verified = 0;
  41. break;
  42. default:
  43. case REGISTER_CLOSED:
  44. if (empty($_SESSION['authenticated']) && empty($_SESSION['administrator'])) {
  45. notice(L10n::t('Permission denied.') . EOL);
  46. return;
  47. }
  48. $blocked = 1;
  49. $verified = 0;
  50. break;
  51. }
  52. $netpublish = !empty($_POST['profile_publish_reg']);
  53. $arr = $_POST;
  54. $arr['blocked'] = $blocked;
  55. $arr['verified'] = $verified;
  56. $arr['language'] = L10n::detectLanguage();
  57. try {
  58. $result = Model\User::create($arr);
  59. } catch (Exception $e) {
  60. notice($e->getMessage());
  61. return;
  62. }
  63. $user = $result['user'];
  64. if ($netpublish && intval(Config::get('config', 'register_policy')) !== REGISTER_APPROVE) {
  65. $url = $a->getBaseUrl() . '/profile/' . $user['nickname'];
  66. Worker::add(PRIORITY_LOW, "Directory", $url);
  67. }
  68. $using_invites = Config::get('system', 'invitation_only');
  69. $num_invites = Config::get('system', 'number_invites');
  70. $invite_id = (!empty($_POST['invite_id']) ? Strings::escapeTags(trim($_POST['invite_id'])) : '');
  71. if (intval(Config::get('config', 'register_policy')) === REGISTER_OPEN) {
  72. if ($using_invites && $invite_id) {
  73. Model\Register::deleteByHash($invite_id);
  74. PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites);
  75. }
  76. // Only send a password mail when the password wasn't manually provided
  77. if (empty($_POST['password1']) || empty($_POST['confirm'])) {
  78. $res = Model\User::sendRegisterOpenEmail(
  79. $user,
  80. Config::get('config', 'sitename'),
  81. $a->getBaseUrl(),
  82. $result['password']
  83. );
  84. if ($res) {
  85. info(L10n::t('Registration successful. Please check your email for further instructions.') . EOL);
  86. $a->internalRedirect();
  87. } else {
  88. notice(
  89. L10n::t('Failed to send email message. Here your accout details:<br> login: %s<br> password: %s<br><br>You can change your password after login.',
  90. $user['email'],
  91. $result['password'])
  92. . EOL
  93. );
  94. }
  95. } else {
  96. info(L10n::t('Registration successful.') . EOL);
  97. $a->internalRedirect();
  98. }
  99. } elseif (intval(Config::get('config', 'register_policy')) === REGISTER_APPROVE) {
  100. if (!strlen(Config::get('config', 'admin_email'))) {
  101. notice(L10n::t('Your registration can not be processed.') . EOL);
  102. $a->internalRedirect();
  103. }
  104. Model\Register::createForApproval($user['uid'], Config::get('system', 'language'), $_POST['permonlybox']);
  105. // invite system
  106. if ($using_invites && $invite_id) {
  107. Model\Register::deleteByHash($invite_id);
  108. PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites);
  109. }
  110. // send email to admins
  111. $admin_mail_list = "'" . implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email'))))) . "'";
  112. $adminlist = q("SELECT uid, language, email FROM user WHERE email IN (%s)",
  113. $admin_mail_list
  114. );
  115. // send notification to admins
  116. foreach ($adminlist as $admin) {
  117. notification([
  118. 'type' => NOTIFY_SYSTEM,
  119. 'event' => 'SYSTEM_REGISTER_REQUEST',
  120. 'source_name' => $user['username'],
  121. 'source_mail' => $user['email'],
  122. 'source_nick' => $user['nickname'],
  123. 'source_link' => $a->getBaseUrl() . "/admin/users/",
  124. 'link' => $a->getBaseUrl() . "/admin/users/",
  125. 'source_photo' => $a->getBaseUrl() . "/photo/avatar/" . $user['uid'] . ".jpg",
  126. 'to_email' => $admin['email'],
  127. 'uid' => $admin['uid'],
  128. 'language' => $admin['language'] ? $admin['language'] : 'en',
  129. 'show_in_notification_page' => false
  130. ]);
  131. }
  132. // send notification to the user, that the registration is pending
  133. Model\User::sendRegisterPendingEmail(
  134. $user,
  135. Config::get('config', 'sitename'),
  136. $a->getBaseURL(),
  137. $result['password']
  138. );
  139. info(L10n::t('Your registration is pending approval by the site owner.') . EOL);
  140. $a->internalRedirect();
  141. }
  142. return;
  143. }
  144. function register_content(App $a)
  145. {
  146. // logged in users can register others (people/pages/groups)
  147. // even with closed registrations, unless specifically prohibited by site policy.
  148. // 'block_extended_register' blocks all registrations, period.
  149. $block = Config::get('system', 'block_extended_register');
  150. if (local_user() && ($block)) {
  151. notice("Permission denied." . EOL);
  152. return;
  153. }
  154. if ((!local_user()) && (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED)) {
  155. notice("Permission denied." . EOL);
  156. return;
  157. }
  158. $max_dailies = intval(Config::get('system', 'max_daily_registrations'));
  159. if ($max_dailies) {
  160. $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day");
  161. if ($r && $r[0]['total'] >= $max_dailies) {
  162. Logger::log('max daily registrations exceeded.');
  163. notice(L10n::t('This site has exceeded the number of allowed daily account registrations. Please try again tomorrow.') . EOL);
  164. return;
  165. }
  166. }
  167. if (!empty($_SESSION['theme'])) {
  168. unset($_SESSION['theme']);
  169. }
  170. if (!empty($_SESSION['mobile-theme'])) {
  171. unset($_SESSION['mobile-theme']);
  172. }
  173. $username = defaults($_REQUEST, 'username' , '');
  174. $email = defaults($_REQUEST, 'email' , '');
  175. $openid_url = defaults($_REQUEST, 'openid_url', '');
  176. $nickname = defaults($_REQUEST, 'nickname' , '');
  177. $photo = defaults($_REQUEST, 'photo' , '');
  178. $invite_id = defaults($_REQUEST, 'invite_id' , '');
  179. $noid = Config::get('system', 'no_openid');
  180. if ($noid) {
  181. $fillwith = '';
  182. $fillext = '';
  183. $oidlabel = '';
  184. } else {
  185. $fillwith = L10n::t("You may \x28optionally\x29 fill in this form via OpenID by supplying your OpenID and clicking 'Register'.");
  186. $fillext = L10n::t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
  187. $oidlabel = L10n::t("Your OpenID \x28optional\x29: ");
  188. }
  189. if (Config::get('system', 'publish_all')) {
  190. $profile_publish = '<input type="hidden" name="profile_publish_reg" value="1" />';
  191. } else {
  192. $publish_tpl = Renderer::getMarkupTemplate("profile_publish.tpl");
  193. $profile_publish = Renderer::replaceMacros($publish_tpl, [
  194. '$instance' => 'reg',
  195. '$pubdesc' => L10n::t('Include your profile in member directory?'),
  196. '$yes_selected' => '',
  197. '$no_selected' => ' checked="checked"',
  198. '$str_yes' => L10n::t('Yes'),
  199. '$str_no' => L10n::t('No'),
  200. ]);
  201. }
  202. $r = q("SELECT COUNT(*) AS `contacts` FROM `contact`");
  203. $passwords = !$r[0]["contacts"];
  204. $tpl = Renderer::getMarkupTemplate("register.tpl");
  205. $arr = ['template' => $tpl];
  206. Addon::callHooks('register_form', $arr);
  207. $tpl = $arr['template'];
  208. $tos = new Tos();
  209. $o = Renderer::replaceMacros($tpl, [
  210. '$invitations' => Config::get('system', 'invitation_only'),
  211. '$permonly' => intval(Config::get('config', 'register_policy')) === REGISTER_APPROVE,
  212. '$permonlybox' => ['permonlybox', L10n::t('Note for the admin'), '', L10n::t('Leave a message for the admin, why you want to join this node')],
  213. '$invite_desc' => L10n::t('Membership on this site is by invitation only.'),
  214. '$invite_label' => L10n::t('Your invitation code: '),
  215. '$invite_id' => $invite_id,
  216. '$regtitle' => L10n::t('Registration'),
  217. '$registertext' => BBCode::convert(Config::get('config', 'register_text', '')),
  218. '$fillwith' => $fillwith,
  219. '$fillext' => $fillext,
  220. '$oidlabel' => $oidlabel,
  221. '$openid' => $openid_url,
  222. '$namelabel' => L10n::t('Your Full Name ' . "\x28" . 'e.g. Joe Smith, real or real-looking' . "\x29" . ': '),
  223. '$addrlabel' => L10n::t("Your Email Address: \x28Initial information will be send there, so this has to be an existing address.\x29"),
  224. '$passwords' => $passwords,
  225. '$password1' => ['password1', L10n::t('New Password:'), '', L10n::t('Leave empty for an auto generated password.')],
  226. '$password2' => ['confirm', L10n::t('Confirm:'), '', ''],
  227. '$nickdesc' => L10n::t('Choose a profile nickname. This must begin with a text character. Your profile address on this site will then be \'<strong>nickname@%s</strong>\'.', $a->getHostName()),
  228. '$nicklabel' => L10n::t('Choose a nickname: '),
  229. '$photo' => $photo,
  230. '$publish' => $profile_publish,
  231. '$regbutt' => L10n::t('Register'),
  232. '$username' => $username,
  233. '$email' => $email,
  234. '$nickname' => $nickname,
  235. '$sitename' => $a->getHostName(),
  236. '$importh' => L10n::t('Import'),
  237. '$importt' => L10n::t('Import your profile to this friendica instance'),
  238. '$showtoslink' => Config::get('system', 'tosdisplay'),
  239. '$tostext' => L10n::t('Terms of Service'),
  240. '$showprivstatement' => Config::get('system', 'tosprivstatement'),
  241. '$privstatement' => $tos->privacy_complete,
  242. '$baseurl' => System::baseurl(),
  243. '$form_security_token' => BaseModule::getFormSecurityToken("register"),
  244. '$explicit_content' => Config::get('system', 'explicit_content', false),
  245. '$explicit_content_note' => L10n::t('Note: This node explicitly contains adult content')
  246. ]);
  247. return $o;
  248. }