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 $group_id = null; // Group id, set after we create a group.
39 var $role_id = null; // Role id for top managers.
40 var $user_id = null; // User id after registration.
41 var $err = null; // Error object, passed to us as reference.
42 // We use it to communicate errors to caller.
45 function __construct($fields, &$err) {
46 $this->user_name = $fields['user_name'];
47 $this->login = $fields['login'];
48 $this->password1 = $fields['password1'];
49 $this->password2 = $fields['password2'];
50 $this->email = $fields['email'];
51 $this->group_name = $fields['group_name'];
52 $this->currency = $fields['currency'];
53 $this->lang = $fields['lang'];
54 if (!$this->lang) $this->lang = 'en';
57 // Validate passed in parameters.
64 if (!ttValidString($this->group_name, true))
65 $this->err->add($i18n->get('error.field'), $i18n->get('label.group_name'));
66 if (!ttValidString($this->currency, true))
67 $this->err->add($i18n->get('error.field'), $i18n->get('label.currency'));
68 if (!ttValidString($this->user_name))
69 $this->err->add($i18n->get('error.field'), $i18n->get('label.manager_name'));
70 if (!ttValidString($this->login))
71 $this->err->add($i18n->get('error.field'), $i18n->get('label.manager_login'));
72 if (!ttValidString($this->password1))
73 $this->err->add($i18n->get('error.field'), $i18n->get('label.password'));
74 if (!ttValidString($this->password2))
75 $this->err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
76 if ($this->password1 !== $this->password2)
77 $this->err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
78 if (!ttValidEmail($this->email, true))
79 $this->err->add($i18n->get('error.field'), $i18n->get('label.email'));
82 // The register function registers a user in Time Tracker.
84 if ($this->err->yes()) return false; // There are errors, do not proceed.
88 // Protection fom too many recent bot registrations from user IP.
89 if ($this->registeredRecently()) {
90 $this->err->add($i18n->get('error.access_denied'));
94 import('ttUserHelper');
95 if (ttUserHelper::getUserByLogin($this->login)) {
96 // User login already exists.
97 $this->err->add($i18n->get('error.user_exists'));
101 // Create a new group.
102 $this->group_id = $this->createGroup();
103 if (!$this->group_id) {
104 $this->err->add($i18n->get('error.db'));
108 import('ttRoleHelper');
109 if (!ttRoleHelper::createPredefinedRoles($this->group_id, $this->lang)) {
110 $err->add($i18n->get('error.db'));
113 $this->role_id = ttRoleHelper::getTopManagerRoleID();
114 $this->user_id = $this->createUser();
116 if (!$this->user_id) {
117 $err->add($i18n->get('error.db'));
121 if (!$this->setCreatedBy($this->user_id))
127 // The createGroup function creates a group in Time Tracker as part
128 // of user registration process. This is a top group for user as top manager.
129 function createGroup() {
130 $mdb2 = getConnection();
132 $name = $mdb2->quote($this->group_name);
133 $currency = $mdb2->quote($this->currency);
134 $lang = $mdb2->quote($this->lang);
136 $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
138 $sql = "insert into tt_groups (name, currency, lang, created, created_ip) values($name, $currency, $lang, $created, $created_ip)";
139 $affected = $mdb2->exec($sql);
141 if (!is_a($affected, 'PEAR_Error')) {
142 $group_id = $mdb2->lastInsertID('tt_groups', 'id');
148 // The createUser creates a user in database as part of registration process.
149 function createUser() {
150 $mdb2 = getConnection();
152 $login = $mdb2->quote($this->login);
153 $password = 'md5('.$mdb2->quote($this->password1).')';
154 $name = $mdb2->quote($this->user_name);
155 $email = $mdb2->quote($this->email);
157 $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
158 $values = "values($login, $password, $name, $this->group_id, $this->role_id, $email, $created, $created_ip)";
160 $sql = 'insert into tt_users (login, password, name, group_id, role_id, email, created, created_ip) '.$values;
161 $affected = $mdb2->exec($sql);
162 if (!is_a($affected, 'PEAR_Error')) {
163 $user_id = $mdb2->lastInsertID('tt_users', 'id');
169 // The setCreatedBy sets created_by field for both group and user to passed in user_id.
170 function setCreatedBy($user_id) {
171 if ($this->err->yes()) return false; // There are errors, do not proceed.
174 $mdb2 = getConnection();
177 $sql = "update tt_groups set created_by = $user_id where id = $this->group_id";
178 $affected = $mdb2->exec($sql);
179 if (is_a($affected, 'PEAR_Error')) {
180 $this->err->add($i18n->get('error.db'));
184 // Update top manager.
185 $sql = "update tt_users set created_by = $user_id where id = $user_id and group_id = $this->group_id";
186 $affected = $mdb2->exec($sql);
187 if (is_a($affected, 'PEAR_Error')) {
188 $this->err->add($i18n->get('error.db'));
195 // registeredRecently determines if we already have a successful recent registration from user IP.
196 // "recent" means "within the last minute" and is set in a query by the following condition:
197 // "and created > now() - interval 1 minute". Change if necessary.
198 function registeredRecently() {
199 $mdb2 = getConnection();
201 $ip_part = ' created_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']);
202 $sql = 'select created from tt_groups where '.$ip_part.' and created > now() - interval 1 minute';
203 $res = $mdb2->query($sql);
204 if (is_a($res, 'PEAR_Error'))
206 $val = $res->fetchRow();