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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 // ttRegistrator class is used to register a user in Time Tracker.
31 var $user_name = null; // User name.
32 var $login = null; // User login.
33 var $password = null; // User password.
34 var $email = null; // User email.
35 var $group_name = null; // Group name.
36 var $currency = null; // Currency.
37 var $lang = null; // Language.
38 var $created_by_id = null; // User, who uses the instance.
39 // Currently, there are 2 possibilities:
40 // 1) Self-registration, or null here.
41 // 2) Registration by admin with a user_id.
42 var $group_id = null; // Group id, set after we create a group.
43 var $org_id = null; // Organization id, the same as group_id (top group in org).
44 var $role_id = null; // Role id for top managers.
45 var $user_id = null; // User id after registration.
46 var $err = null; // Error object, passed to us as reference.
47 // We use it to communicate errors to caller.
50 function __construct($fields, &$err) {
51 $this->user_name = $fields['user_name'];
52 $this->login = $fields['login'];
53 $this->password1 = $fields['password1'];
54 $this->password2 = $fields['password2'];
55 $this->email = $fields['email'];
56 $this->group_name = $fields['group_name'];
57 $this->currency = $fields['currency'];
58 $this->lang = $fields['lang'];
59 if (!$this->lang) $this->lang = 'en';
60 $this->created_by_id = (int) $fields['created_by_id'];
63 // Validate passed in parameters.
70 if (!ttValidString($this->group_name))
71 $this->err->add($i18n->get('error.field'), $i18n->get('label.group_name'));
72 if (!ttValidString($this->currency, true))
73 $this->err->add($i18n->get('error.field'), $i18n->get('label.currency'));
74 if (!ttValidString($this->user_name))
75 $this->err->add($i18n->get('error.field'), $i18n->get('label.manager_name'));
76 if (!ttValidString($this->login))
77 $this->err->add($i18n->get('error.field'), $i18n->get('label.manager_login'));
78 if (!ttValidString($this->password1))
79 $this->err->add($i18n->get('error.field'), $i18n->get('label.password'));
80 if (!ttValidString($this->password2))
81 $this->err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
82 if ($this->password1 !== $this->password2)
83 $this->err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
84 if (!ttValidEmail($this->email, true))
85 $this->err->add($i18n->get('error.field'), $i18n->get('label.email'));
88 // The register function registers a user in Time Tracker.
90 if ($this->err->yes()) return false; // There are errors, do not proceed.
95 // Protection from too many recent bot registrations from user IP.
96 if (!$this->created_by_id) { // No problems for logged in user (site admin).
97 if ($this->registeredRecently()) {
98 $this->err->add($i18n->get('error.access_denied'));
103 import('ttUserHelper');
104 if (ttUserHelper::getUserByLogin($this->login)) {
105 // User login already exists.
106 $this->err->add($i18n->get('error.user_exists'));
110 // Create a new group.
111 $this->group_id = $this->createGroup();
112 $this->org_id = $this->group_id;
113 if (!$this->group_id) {
114 $this->err->add($i18n->get('error.db'));
118 import('ttRoleHelper');
119 if (!ttRoleHelper::createPredefinedRoles($this->group_id, $this->lang)) {
120 $err->add($i18n->get('error.db'));
123 $this->role_id = ttRoleHelper::getTopManagerRoleID();
124 $this->user_id = $this->createUser();
126 if (!$this->user_id) {
127 $err->add($i18n->get('error.db'));
131 // Set created_by appropriately.
132 $created_by = $this->created_by_id ? $this->created_by_id : $this->user_id;
133 if (!$this->setCreatedBy($created_by))
139 // The createGroup function creates a group in Time Tracker as part
140 // of user registration process. This is a top group for user as top manager.
141 function createGroup() {
142 $mdb2 = getConnection();
144 $name = $mdb2->quote($this->group_name);
145 $currency = $mdb2->quote($this->currency);
146 $lang = $mdb2->quote($this->lang);
148 $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
150 $sql = "insert into tt_groups (name, currency, lang, created, created_ip) values($name, $currency, $lang, $created, $created_ip)";
151 $affected = $mdb2->exec($sql);
152 if (is_a($affected, 'PEAR_Error')) return false;
154 $group_id = $mdb2->lastInsertID('tt_groups', 'id');
156 // Update org_id with group_id.
157 $sql = "update tt_groups set org_id = $group_id where org_id is NULL and id = $group_id";
158 $affected = $mdb2->exec($sql);
159 if (is_a($affected, 'PEAR_Error')) return false;
164 // The createUser creates a user in database as part of registration process.
165 function createUser() {
166 $mdb2 = getConnection();
168 $login = $mdb2->quote($this->login);
169 $password = 'md5('.$mdb2->quote($this->password1).')';
170 $name = $mdb2->quote($this->user_name);
171 $email = $mdb2->quote($this->email);
173 $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
174 $values = "values($login, $password, $name, $this->group_id, $this->org_id, $this->role_id, $email, $created, $created_ip)";
176 $sql = 'insert into tt_users (login, password, name, group_id, org_id, role_id, email, created, created_ip) '.$values;
177 $affected = $mdb2->exec($sql);
178 if (!is_a($affected, 'PEAR_Error')) {
179 $user_id = $mdb2->lastInsertID('tt_users', 'id');
185 // The setCreatedBy sets created_by field for both group and user to passed in user_id.
186 private function setCreatedBy($user_id) {
187 if ($this->err->yes()) return false; // There are errors, do not proceed.
190 $mdb2 = getConnection();
193 $sql = "update tt_groups set created_by = $user_id where id = $this->group_id and org_id = $this->org_id";
194 $affected = $mdb2->exec($sql);
195 if (is_a($affected, 'PEAR_Error')) {
196 $this->err->add($i18n->get('error.db'));
200 // Update top manager.
201 $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";
202 $affected = $mdb2->exec($sql);
203 if (is_a($affected, 'PEAR_Error')) {
204 $this->err->add($i18n->get('error.db'));
211 // registeredRecently determines if we already have a successful recent registration from user IP.
212 // "recent" means "within the last minute" and is set in a query by the following condition:
213 // "and created > now() - interval 1 minute". Change if necessary.
214 function registeredRecently() {
215 $mdb2 = getConnection();
217 $ip_part = ' created_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']);
218 $sql = 'select created from tt_groups where '.$ip_part.' and created > now() - interval 1 minute';
219 $res = $mdb2->query($sql);
220 if (is_a($res, 'PEAR_Error'))
222 $val = $res->fetchRow();