b122d015f5a4622108b621ff9fdfccbf44469701
[timetracker.git] / WEB-INF / lib / ttRegistrator.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 import('ttUserHelper');
30 import('ttRoleHelper');
31
32 // ttRegistrator class is used to register a user in Time Tracker.
33 class ttRegistrator {
34   var $user_name = null;  // User name.
35   var $login = null;      // User login.
36   var $password = null;   // User password.
37   var $email = null;      // User email.
38   var $group_name = null; // Group name.
39   var $currency = null;   // Currency.
40   var $lang = null;       // Language.
41   var $created_by_id = null; // User, who uses the instance.
42                              // Currently, there are 2 possibilities:
43                              // 1) Self-registration, or null here.
44                              // 2) Registration by admin with a user_id.
45   var $group_id = null;   // Group id, set after we create a group.
46   var $org_id = null;     // Organization id, the same as group_id (top group in org).
47   var $role_id = null;    // Role id for top managers.
48   var $user_id = null;    // User id after registration.
49   var $err = null;        // Error object, passed to us as reference.
50                           // We use it to communicate errors to caller.
51
52   // Constructor.
53   function __construct($fields, &$err) {
54     $this->user_name = $fields['user_name'];
55     $this->login = $fields['login'];    
56     $this->password1 = $fields['password1'];
57     $this->password2 = $fields['password2'];
58     $this->email = $fields['email'];
59     $this->group_name = $fields['group_name'];
60     $this->currency = $fields['currency'];
61     $this->lang = $fields['lang'];
62     if (!$this->lang) $this->lang = 'en';
63     $this->created_by_id = (int) $fields['created_by_id'];
64     $this->err = $err;
65
66     // Validate passed in parameters.
67     $this->validate();
68   }
69
70   function validate() {
71     global $i18n;
72
73     if (!ttValidString($this->group_name))
74       $this->err->add($i18n->get('error.field'), $i18n->get('label.group_name'));
75     if (!ttValidString($this->currency, true))
76       $this->err->add($i18n->get('error.field'), $i18n->get('label.currency'));
77     if (!ttValidString($this->user_name))
78       $this->err->add($i18n->get('error.field'), $i18n->get('label.manager_name'));
79     if (!ttValidString($this->login))
80       $this->err->add($i18n->get('error.field'), $i18n->get('label.manager_login'));
81     if (!ttValidString($this->password1))
82       $this->err->add($i18n->get('error.field'), $i18n->get('label.password'));
83     if (!ttValidString($this->password2))
84       $this->err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
85     if ($this->password1 !== $this->password2)
86       $this->err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
87     if (!ttValidEmail($this->email, true))
88       $this->err->add($i18n->get('error.field'), $i18n->get('label.email'));
89     if (!ttUserHelper::canAdd())
90       $this->err->add($i18n->get('error.user_count'));
91   }
92
93   // The register function registers a user in Time Tracker.
94   function register() {
95     if ($this->err->yes()) return false; // There are errors, do not proceed.
96
97     global $i18n;
98     global $user;
99
100     // Protection from too many recent bot registrations from user IP.
101     if (!$this->created_by_id) { // No problems for logged in user (site admin).
102       if ($this->registeredRecently()) {
103         $this->err->add($i18n->get('error.access_denied'));
104         return false;
105       }
106     }
107
108     import('ttUserHelper');
109     if (ttUserHelper::getUserByLogin($this->login)) {
110       // User login already exists.
111       $this->err->add($i18n->get('error.user_exists'));
112       return false;
113     }
114
115     // Create a new group.
116     $this->group_id = $this->createGroup();
117     $this->org_id = $this->group_id;
118     if (!$this->group_id) {
119       $this->err->add($i18n->get('error.db'));
120       return false;
121     }
122
123     if (!ttRoleHelper::createPredefinedRoles($this->group_id, $this->lang)) {
124       $err->add($i18n->get('error.db'));
125       return false;
126     }
127     $this->role_id = ttRoleHelper::getTopManagerRoleID();
128     $this->user_id = $this->createUser();
129
130     if (!$this->user_id) {
131       $this->err->add($i18n->get('error.db'));
132       return false;
133     }
134
135     // Set created_by appropriately.
136     $created_by = $this->created_by_id ? $this->created_by_id : $this->user_id;
137     if (!$this->setCreatedBy($created_by))
138       return false;
139
140     return true;
141   }
142
143   // The createGroup function creates a group in Time Tracker as part
144   // of user registration process. This is a top group for user as top manager.
145   function createGroup() {
146     $mdb2 = getConnection();
147
148     $group_key = $mdb2->quote(ttRandomString());
149     $name = $mdb2->quote($this->group_name);
150     $currency = $mdb2->quote($this->currency);
151     $lang = $mdb2->quote($this->lang);
152     $created = 'now()';
153     $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
154
155     $sql = "insert into tt_groups (group_key, name, currency, lang, created, created_ip)".
156       " values($group_key, $name, $currency, $lang, $created, $created_ip)";
157     $affected = $mdb2->exec($sql);
158     if (is_a($affected, 'PEAR_Error')) return false;
159
160     $group_id = $mdb2->lastInsertID('tt_groups', 'id');
161
162     // Update org_id with group_id.
163     $sql = "update tt_groups set org_id = $group_id where org_id is NULL and id = $group_id";
164     $affected = $mdb2->exec($sql);
165     if (is_a($affected, 'PEAR_Error')) return false;
166
167     return $group_id;
168   }
169
170   // The createUser creates a user in database as part of registration process.
171   function createUser() {
172     $mdb2 = getConnection();
173
174     $login = $mdb2->quote($this->login);
175     $password = 'md5('.$mdb2->quote($this->password1).')';
176     $name = $mdb2->quote($this->user_name);
177     $email = $mdb2->quote($this->email);
178     $created = 'now()';
179     $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
180     $values = "values($login, $password, $name, $this->group_id, $this->org_id, $this->role_id, $email, $created, $created_ip)";
181
182     $sql = 'insert into tt_users (login, password, name, group_id, org_id, role_id, email, created, created_ip) '.$values;
183     $affected = $mdb2->exec($sql);
184     if (!is_a($affected, 'PEAR_Error')) {
185       $user_id = $mdb2->lastInsertID('tt_users', 'id');
186       return $user_id;
187     }
188     return false;
189   }
190
191   // The setCreatedBy sets created_by field for both group and user to passed in user_id.
192   private function setCreatedBy($user_id) {
193     if ($this->err->yes()) return false; // There are errors, do not proceed.
194
195     global $i18n;
196     $mdb2 = getConnection();
197
198     // Update group.
199     $sql = "update tt_groups set created_by = $user_id where id = $this->group_id and org_id = $this->org_id";
200     $affected = $mdb2->exec($sql);
201     if (is_a($affected, 'PEAR_Error')) {
202       $this->err->add($i18n->get('error.db'));
203       return false;
204     }
205
206     // Update top manager.
207     $sql = "update tt_users set created_by = $user_id where id = $this->user_id and group_id = $this->group_id and org_id = $this->org_id";
208     $affected = $mdb2->exec($sql);
209     if (is_a($affected, 'PEAR_Error')) {
210       $this->err->add($i18n->get('error.db'));
211       return false;
212     }
213
214     return true;
215   }
216
217   // registeredRecently determines if we already have a successful recent registration from user IP.
218   // "recent" means "within the last minute" and is set in a query by the following condition:
219   // "and created > now() - interval 1 minute". Change if necessary.
220   function registeredRecently() {
221     $mdb2 = getConnection();
222
223     $ip_part = ' created_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']);
224     $sql = 'select created from tt_groups where '.$ip_part.' and created > now() - interval 1 minute';
225     $res = $mdb2->query($sql);
226     if (is_a($res, 'PEAR_Error'))
227       return false;
228     $val = $res->fetchRow();
229     if ($val)
230       return true;
231
232     return false;
233   }
234 }