Put in Diego's change so blog posts appear.
[elgg.git] / units / invite / invite_actions.php
blobb4b72b5057ff170aa8b1acd5abfc3731b7393d85
1 <?php
2 global $USER;
3 global $CFG;
5 // Kill all old invitations
6 delete_records_select('invitations',"added < ?",array(time() - (86400 * 7)));
8 // Get site name
9 $sitename = $CFG->sitename;
11 $action = optional_param('action');
12 switch ($action) {
14 // Add a new invite code
15 case "invite_invite":
16 $invite = new StdClass;
17 $invite->name = trim(optional_param('invite_name'));
18 $invite->email = trim(optional_param('invite_email'));
19 if (!empty($invite->name) && !empty($invite->email)) {
20 if (logged_on || ($CFG->publicinvite == true)) {
21 if (($CFG->maxusers == 0 || (count_users('person') < $CFG->maxusers))) {
22 if (validate_email(stripslashes($invite->email))) {
23 $strippedname = stripslashes($invite->name); // for the message text.
24 $invitations = count_records('invitations','email',$invite->email);
25 if ($invitations == 0) {
26 if (!$account = get_record('users','email',$invite->email)) {
27 $invite->code = 'i' . substr(base_convert(md5(time() . $USER->username), 16, 36), 0, 7);
28 $invite->added = time();
29 $invite->owner = $USER->ident;
30 insert_record('invitations',$invite);
31 $url = url . "_invite/join.php?invitecode=" . $invite->code;
32 if (!logged_on) {
33 $invitetext = '';
34 $greetingstext = sprintf(__gettext("Thank you for registering with %s."),$sitename);
35 $subjectline = sprintf(__gettext("%s account verification"),$sitename);
36 $from_email = email;
37 } else {
38 $invitetext = trim(optional_param('invite_text'));
39 if (!empty($invitetext)) {
40 $invitetext = __gettext("They included the following message:") . "\n\n----------\n" . $invitetext . "\n----------";
42 $greetingstext = $USER->name . " " . __gettext("has invited you to join") ." $sitename, ". __gettext("a social network.") ."";
43 $subjectline = $USER->name . " " . __gettext("has invited you to join") ." $sitename";
44 $from_email = $USER->email;
46 $emailmessage = sprintf(__gettext("Dear %s,\n\n%s %s\n\nTo join, visit the following URL:\n\n\t%s\n\nYour email address has not been passed onto any third parties, and will be removed from our system within seven days.\n\nRegards,\n\nThe %s team."),$strippedname,$greetingstext,$invitetext,$url, $sitename);
47 $emailmessage = wordwrap($emailmessage);
48 $messages[] = sprintf(__gettext("Your invitation was sent to %s at %s. It will be valid for seven days."),$strippedname,$invite->email);
49 email_to_user($invite,null,$subjectline,$emailmessage);
50 } else {
51 $messages[] = sprintf(__gettext("User %s already has that email address. Invitation not sent."),$account->username);
53 } else {
54 $messages[] = __gettext("Someone with that email address has already been invited to the system. ");
56 } else {
57 $messages[] = __gettext("Invitation failed: The email address was not valid.");
59 } else {
60 $messages[] = __gettext("Error: This community has reached its maximum number of users.");
62 } else {
63 $messages[] = __gettext("Invitation failed: you are not logged in.");
65 } else {
66 $messages[] = __gettext("Invitation failed: you must specify both a name and an email address.");
68 break;
69 // Join using an invitation
70 case "invite_join":
71 $name = trim(optional_param('join_name'));
72 $code = trim(optional_param('invitecode'));
73 $over13 = optional_param('over13');
74 $username = trim(optional_param('join_username'));
75 $password1 = trim(optional_param('join_password1'));
76 $password2 = trim(optional_param('join_password2'));
77 if (isset($name) && isset($code)) {
78 if (!($CFG->maxusers == 0 || (count_users('person') < $CFG->maxusers))) {
79 $messages[] = __gettext("Unfortunately this community has reached its account limit and you are unable to join at this time.");
80 break;
82 if (empty($over13)) {
83 $messages[] = __gettext("You must indicate that you are at least 13 years old to join.");
84 break;
86 if (!$details = get_record('invitations','code',$code)) {
87 $messages[] = __gettext("Error! Invalid invite code.");
88 break;
90 if ($password1 != $password2 || strlen($password1) < 6 || strlen($password2) > 16) {
91 $messages[] = __gettext("Error! Invalid password. Your passwords must match and be between 6 and 16 characters in length.");
92 break;
94 if (!preg_match("/^[A-Za-z0-9]{3,12}$/",$username)) {
95 $messages[] = __gettext("Error! Your username must contain letters and numbers only, cannot be blank, and must be between 3 and 12 characters in length.");
96 break;
98 $username = strtolower($username);
99 if (record_exists('users','username',$username)) {
100 $messages[] = __gettext("The username '$username' is already taken by another user. You will need to pick a different one.");
101 break;
103 $displaypassword = $password1;
104 $u = new StdClass;
105 $u->name = $name;
106 $u->password = md5($password1);
107 $u->email = $details->email;
108 $u->username = $username;
109 $u = plugin_hook("user","create",$u);
111 if (!empty($u)) {
112 $ident = insert_record('users',$u);
113 $u->ident = $ident;
114 // Calendar code is in the wrong place!
115 global $function;
116 if(isset($function["calendar:init"])) {
117 $c = new StdClass;
118 $c->owner = $ident;
119 insert_record('calendar',$c);
121 $owner = (int)$details->owner;
122 if ($owner != -1) { // invited by someone - set up mutual friendship
123 $f = new StdClass;
124 $f->owner = $owner;
125 $f->friend = $ident;
126 insert_record('friends',$f);
127 $f->owner = $ident;
128 $f->friend = $owner;
129 insert_record('friends',$f);
131 // make them friend the news user
132 $f = new StdClass;
133 $f->owner = $ident;
134 $f->friend = 1;
135 insert_record('friends',$f);
137 $u = plugin_hook("user","publish",$u);
139 $rssresult = run("weblogs:rss:publish", array($ident, false));
140 $rssresult = run("files:rss:publish", array($ident, false));
141 $rssresult = run("profile:rss:publish", array($ident, false));
142 $_SESSION['messages'][] = __gettext("Your account was created! You can now log in using the username and password you supplied. You have been sent an email containing these details for reference purposes.");
143 delete_records('invitations','code',$code);
144 email_to_user($u,null,sprintf(__gettext("Your %s account"),$sitename),
145 sprintf(__gettext("Thanks for joining %s!\n\nFor your records, your %s username and password are:\n\n\t")
146 .__gettext("Username: %s\n\tPassword: %s\n\nYou can log in at any time by visiting %s and entering these details into the login form.\n\n")
147 .__gettext("We hope you enjoy using the system.\n\nRegards,\n\nThe %s Team")
148 ,$sitename,$sitename,$username,$displaypassword,url,$sitename));
149 header("Location: " . $CFG->wwwroot);
150 exit();
154 break;
156 // Request a new password
157 case "invite_password_request":
158 $username = optional_param('password_request_name');
159 if (!empty($username)) {
160 if ($user = get_record('users','username',trim($username),'user_type','person')) {
161 $pwreq = new StdClass;
162 $pwreq->code = 'i' . substr(base_convert(md5(time() . $username), 16, 36), 0, 7);
163 $pwreq->owner = $user->ident;
164 insert_record('password_requests',$pwreq);
165 $url = url . "_invite/new_password.php?passwordcode=" . $pwreq->code;
166 email_to_user($user,null,sprintf(__gettext("Verify your %s account password request"),$sitename),
167 sprintf(__gettext("A request has been received to generate your account at %s a new password.\n\n")
168 .__gettext("To confirm this request and receive a new password by email, please click the following link:\n\n\t%s\n\n")
169 .__gettext("Please let us know if you have any further problems.\n\nRegards,\n\nThe %s Team")
170 ,$sitename,$url,$sitename));
171 $messages[] = __gettext("Your verification email was sent. Please check your inbox.");
172 } else {
173 $messages[] = __gettext("No user with that username was found.");
176 break;