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.
 
 
 
 
 
 

368 lines
12 KiB

  1. <?php
  2. require_once('include/config.php');
  3. require_once('include/network.php');
  4. require_once('include/plugin.php');
  5. require_once('include/text.php');
  6. require_once('include/pgettext.php');
  7. require_once('include/datetime.php');
  8. function create_user($arr) {
  9. // Required: { username, nickname, email } or { openid_url }
  10. $a = get_app();
  11. $result = array('success' => false, 'user' => null, 'password' => '', 'message' => '');
  12. $using_invites = get_config('system','invitation_only');
  13. $num_invites = get_config('system','number_invites');
  14. $invite_id = ((x($arr,'invite_id')) ? notags(trim($arr['invite_id'])) : '');
  15. $username = ((x($arr,'username')) ? notags(trim($arr['username'])) : '');
  16. $nickname = ((x($arr,'nickname')) ? notags(trim($arr['nickname'])) : '');
  17. $email = ((x($arr,'email')) ? notags(trim($arr['email'])) : '');
  18. $openid_url = ((x($arr,'openid_url')) ? notags(trim($arr['openid_url'])) : '');
  19. $photo = ((x($arr,'photo')) ? notags(trim($arr['photo'])) : '');
  20. $password = ((x($arr,'password')) ? trim($arr['password']) : '');
  21. $blocked = ((x($arr,'blocked')) ? intval($arr['blocked']) : 0);
  22. $verified = ((x($arr,'verified')) ? intval($arr['verified']) : 0);
  23. $publish = ((x($arr,'profile_publish_reg') && intval($arr['profile_publish_reg'])) ? 1 : 0);
  24. $netpublish = ((strlen(get_config('system','directory_submit_url'))) ? $publish : 0);
  25. $tmp_str = $openid_url;
  26. if($using_invites) {
  27. if(! $invite_id) {
  28. $result['message'] .= t('An invitation is required.') . EOL;
  29. return $result;
  30. }
  31. $r = q("select * from register where `hash` = '%s' limit 1", dbesc($invite_id));
  32. if(! results($r)) {
  33. $result['message'] .= t('Invitation could not be verified.') . EOL;
  34. return $result;
  35. }
  36. }
  37. if((! x($username)) || (! x($email)) || (! x($nickname))) {
  38. if($openid_url) {
  39. if(! validate_url($tmp_str)) {
  40. $result['message'] .= t('Invalid OpenID url') . EOL;
  41. return $result;
  42. }
  43. $_SESSION['register'] = 1;
  44. $_SESSION['openid'] = $openid_url;
  45. require_once('library/openid.php');
  46. $openid = new LightOpenID;
  47. $openid->identity = $openid_url;
  48. $openid->returnUrl = $a->get_baseurl() . '/openid';
  49. $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
  50. $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
  51. try {
  52. $authurl = $openid->authUrl();
  53. } catch (Exception $e){
  54. $result['message'] .= t("We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID."). EOL . EOL . t("The error message was:") . $e->getMessage() . EOL;
  55. return $result;
  56. }
  57. goaway($authurl);
  58. // NOTREACHED
  59. }
  60. notice( t('Please enter the required information.') . EOL );
  61. return;
  62. }
  63. if(! validate_url($tmp_str))
  64. $openid_url = '';
  65. $err = '';
  66. // collapse multiple spaces in name
  67. $username = preg_replace('/ +/',' ',$username);
  68. if(mb_strlen($username) > 48)
  69. $result['message'] .= t('Please use a shorter name.') . EOL;
  70. if(mb_strlen($username) < 3)
  71. $result['message'] .= t('Name too short.') . EOL;
  72. // I don't really like having this rule, but it cuts down
  73. // on the number of auto-registrations by Russian spammers
  74. // Using preg_match was completely unreliable, due to mixed UTF-8 regex support
  75. // $no_utf = get_config('system','no_utf');
  76. // $pat = (($no_utf) ? '/^[a-zA-Z]* [a-zA-Z]*$/' : '/^\p{L}* \p{L}*$/u' );
  77. // So now we are just looking for a space in the full name.
  78. $loose_reg = get_config('system','no_regfullname');
  79. if(! $loose_reg) {
  80. $username = mb_convert_case($username,MB_CASE_TITLE,'UTF-8');
  81. if(! strpos($username,' '))
  82. $result['message'] .= t("That doesn't appear to be your full \x28First Last\x29 name.") . EOL;
  83. }
  84. if(! allowed_email($email))
  85. $result['message'] .= t('Your email domain is not among those allowed on this site.') . EOL;
  86. if((! valid_email($email)) || (! validate_email($email)))
  87. $result['message'] .= t('Not a valid email address.') . EOL;
  88. // Disallow somebody creating an account using openid that uses the admin email address,
  89. // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
  90. $adminlist = explode(",", str_replace(" ", "", strtolower($a->config['admin_email'])));
  91. //if((x($a->config,'admin_email')) && (strcasecmp($email,$a->config['admin_email']) == 0) && strlen($openid_url)) {
  92. if((x($a->config,'admin_email')) && in_array(strtolower($email), $adminlist) && strlen($openid_url)) {
  93. $r = q("SELECT * FROM `user` WHERE `email` = '%s' LIMIT 1",
  94. dbesc($email)
  95. );
  96. if(count($r))
  97. $result['message'] .= t('Cannot use that email.') . EOL;
  98. }
  99. $nickname = $arr['nickname'] = strtolower($nickname);
  100. if(! preg_match("/^[a-z][a-z0-9\-\_]*$/",$nickname))
  101. $result['message'] .= t('Your "nickname" can only contain "a-z", "0-9", "-", and "_", and must also begin with a letter.') . EOL;
  102. $r = q("SELECT `uid` FROM `user`
  103. WHERE `nickname` = '%s' LIMIT 1",
  104. dbesc($nickname)
  105. );
  106. if(count($r))
  107. $result['message'] .= t('Nickname is already registered. Please choose another.') . EOL;
  108. // Check deleted accounts that had this nickname. Doesn't matter to us,
  109. // but could be a security issue for federated platforms.
  110. $r = q("SELECT * FROM `userd`
  111. WHERE `username` = '%s' LIMIT 1",
  112. dbesc($nickname)
  113. );
  114. if(count($r))
  115. $result['message'] .= t('Nickname was once registered here and may not be re-used. Please choose another.') . EOL;
  116. if(strlen($result['message'])) {
  117. return $result;
  118. }
  119. $new_password = ((strlen($password)) ? $password : autoname(6) . mt_rand(100,9999));
  120. $new_password_encoded = hash('whirlpool',$new_password);
  121. $result['password'] = $new_password;
  122. require_once('include/crypto.php');
  123. $keys = new_keypair(4096);
  124. if($keys === false) {
  125. $result['message'] .= t('SERIOUS ERROR: Generation of security keys failed.') . EOL;
  126. return $result;
  127. }
  128. $default_service_class = get_config('system','default_service_class');
  129. if(! $default_service_class)
  130. $default_service_class = '';
  131. $prvkey = $keys['prvkey'];
  132. $pubkey = $keys['pubkey'];
  133. /**
  134. *
  135. * Create another keypair for signing/verifying
  136. * salmon protocol messages. We have to use a slightly
  137. * less robust key because this won't be using openssl
  138. * but the phpseclib. Since it is PHP interpreted code
  139. * it is not nearly as efficient, and the larger keys
  140. * will take several minutes each to process.
  141. *
  142. */
  143. $sres = new_keypair(512);
  144. $sprvkey = $sres['prvkey'];
  145. $spubkey = $sres['pubkey'];
  146. $r = q("INSERT INTO `user` ( `guid`, `username`, `password`, `email`, `openid`, `nickname`,
  147. `pubkey`, `prvkey`, `spubkey`, `sprvkey`, `register_date`, `verified`, `blocked`, `timezone`, `service_class` )
  148. VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 'UTC', '%s' )",
  149. dbesc(generate_user_guid()),
  150. dbesc($username),
  151. dbesc($new_password_encoded),
  152. dbesc($email),
  153. dbesc($openid_url),
  154. dbesc($nickname),
  155. dbesc($pubkey),
  156. dbesc($prvkey),
  157. dbesc($spubkey),
  158. dbesc($sprvkey),
  159. dbesc(datetime_convert()),
  160. intval($verified),
  161. intval($blocked),
  162. dbesc($default_service_class)
  163. );
  164. if($r) {
  165. $r = q("SELECT * FROM `user`
  166. WHERE `username` = '%s' AND `password` = '%s' LIMIT 1",
  167. dbesc($username),
  168. dbesc($new_password_encoded)
  169. );
  170. if($r !== false && count($r)) {
  171. $u = $r[0];
  172. $newuid = intval($r[0]['uid']);
  173. }
  174. }
  175. else {
  176. $result['message'] .= t('An error occurred during registration. Please try again.') . EOL ;
  177. return $result;
  178. }
  179. /**
  180. * if somebody clicked submit twice very quickly, they could end up with two accounts
  181. * due to race condition. Remove this one.
  182. */
  183. $r = q("SELECT `uid` FROM `user`
  184. WHERE `nickname` = '%s' ",
  185. dbesc($nickname)
  186. );
  187. if((count($r) > 1) && $newuid) {
  188. $result['message'] .= t('Nickname is already registered. Please choose another.') . EOL;
  189. q("DELETE FROM `user` WHERE `uid` = %d LIMIT 1",
  190. intval($newuid)
  191. );
  192. return $result;
  193. }
  194. if(x($newuid) !== false) {
  195. $r = q("INSERT INTO `profile` ( `uid`, `profile-name`, `is-default`, `name`, `photo`, `thumb`, `publish`, `net-publish` )
  196. VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, %d ) ",
  197. intval($newuid),
  198. t('default'),
  199. 1,
  200. dbesc($username),
  201. dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
  202. dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
  203. intval($publish),
  204. intval($netpublish)
  205. );
  206. if($r === false) {
  207. $result['message'] .= t('An error occurred creating your default profile. Please try again.') . EOL;
  208. // Start fresh next time.
  209. $r = q("DELETE FROM `user` WHERE `uid` = %d",
  210. intval($newuid));
  211. return $result;
  212. }
  213. $r = q("INSERT INTO `contact` ( `uid`, `created`, `self`, `name`, `nick`, `photo`, `thumb`, `micro`, `blocked`, `pending`, `url`, `nurl`,
  214. `request`, `notify`, `poll`, `confirm`, `poco`, `name-date`, `uri-date`, `avatar-date`, `closeness` )
  215. VALUES ( %d, '%s', 1, '%s', '%s', '%s', '%s', '%s', 0, 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', 0 ) ",
  216. intval($newuid),
  217. datetime_convert(),
  218. dbesc($username),
  219. dbesc($nickname),
  220. dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
  221. dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
  222. dbesc($a->get_baseurl() . "/photo/micro/{$newuid}.jpg"),
  223. dbesc($a->get_baseurl() . "/profile/$nickname"),
  224. dbesc(normalise_link($a->get_baseurl() . "/profile/$nickname")),
  225. dbesc($a->get_baseurl() . "/dfrn_request/$nickname"),
  226. dbesc($a->get_baseurl() . "/dfrn_notify/$nickname"),
  227. dbesc($a->get_baseurl() . "/dfrn_poll/$nickname"),
  228. dbesc($a->get_baseurl() . "/dfrn_confirm/$nickname"),
  229. dbesc($a->get_baseurl() . "/poco/$nickname"),
  230. dbesc(datetime_convert()),
  231. dbesc(datetime_convert()),
  232. dbesc(datetime_convert())
  233. );
  234. // Create a group with no members. This allows somebody to use it
  235. // right away as a default group for new contacts.
  236. require_once('include/group.php');
  237. group_add($newuid, t('Friends'));
  238. $r = q("SELECT id FROM `group` WHERE uid = %d AND name = '%s'",
  239. intval($newuid),
  240. dbesc(t('Friends'))
  241. );
  242. if($r && count($r)) {
  243. $def_gid = $r[0]['id'];
  244. q("UPDATE user SET def_gid = %d WHERE uid = %d",
  245. intval($r[0]['id']),
  246. intval($newuid)
  247. );
  248. }
  249. if(get_config('system', 'newuser_private') && $def_gid) {
  250. q("UPDATE user SET allow_gid = '%s' WHERE uid = %d",
  251. dbesc("<" . $def_gid . ">"),
  252. intval($newuid)
  253. );
  254. }
  255. }
  256. // if we have no OpenID photo try to look up an avatar
  257. if(! strlen($photo))
  258. $photo = avatar_img($email);
  259. // unless there is no avatar-plugin loaded
  260. if(strlen($photo)) {
  261. require_once('include/Photo.php');
  262. $photo_failure = false;
  263. $filename = basename($photo);
  264. $img_str = fetch_url($photo,true);
  265. // guess mimetype from headers or filename
  266. $type = guess_image_type($photo,true);
  267. $img = new Photo($img_str, $type);
  268. if($img->is_valid()) {
  269. $img->scaleImageSquare(175);
  270. $hash = photo_new_resource();
  271. $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 4 );
  272. if($r === false)
  273. $photo_failure = true;
  274. $img->scaleImage(80);
  275. $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 5 );
  276. if($r === false)
  277. $photo_failure = true;
  278. $img->scaleImage(48);
  279. $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 6 );
  280. if($r === false)
  281. $photo_failure = true;
  282. if(! $photo_failure) {
  283. q("UPDATE `photo` SET `profile` = 1 WHERE `resource-id` = '%s' ",
  284. dbesc($hash)
  285. );
  286. }
  287. }
  288. }
  289. call_hooks('register_account', $newuid);
  290. $result['success'] = true;
  291. $result['user'] = $u;
  292. return $result;
  293. }