44841f503f9df391585b78e29f7a825d1a257750
[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 // ttRegistrator class is used to register a user in Time Tracker.
30 class ttRegistrator {
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.
43
44   // Constructor.
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';
55     $this->err = $err;
56
57     // Validate passed in parameters.
58     $this->validate();
59   }
60
61   function validate() {
62     global $i18n;
63
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'));
80   }
81
82   // The register function registers a user in Time Tracker.
83   function register() {
84     if ($this->err->yes()) return false; // There are errors, do not proceed.
85
86     global $i18n;
87     global $user;
88
89     // Protection fom too many recent bot registrations from user IP.
90     if (!$user->can('administer_site')) { // No problems for site admin.
91       if ($this->registeredRecently()) {
92         $this->err->add($i18n->get('error.access_denied'));
93         return false;
94       }
95     }
96
97     import('ttUserHelper');
98     if (ttUserHelper::getUserByLogin($this->login)) {
99       // User login already exists.
100       $this->err->add($i18n->get('error.user_exists'));
101       return false;
102     }
103
104     // Create a new group.
105     $this->group_id = $this->createGroup();
106     if (!$this->group_id) {
107       $this->err->add($i18n->get('error.db'));
108       return false;
109     }
110
111     import('ttRoleHelper');
112     if (!ttRoleHelper::createPredefinedRoles($this->group_id, $this->lang)) {
113       $err->add($i18n->get('error.db'));
114       return false;
115     }
116     $this->role_id = ttRoleHelper::getTopManagerRoleID();
117     $this->user_id = $this->createUser();
118
119     if (!$this->user_id) {
120       $err->add($i18n->get('error.db'));
121       return false;
122     }
123
124     if (!$this->setCreatedBy($this->user_id))
125       return false;
126
127     return true;
128   }
129
130   // The createGroup function creates a group in Time Tracker as part
131   // of user registration process. This is a top group for user as top manager.
132   function createGroup() {
133     $mdb2 = getConnection();
134
135     $name = $mdb2->quote($this->group_name);
136     $currency = $mdb2->quote($this->currency);
137     $lang = $mdb2->quote($this->lang);
138     $created = 'now()';
139     $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
140
141     $sql = "insert into tt_groups (name, currency, lang, created, created_ip) values($name, $currency, $lang, $created, $created_ip)";
142     $affected = $mdb2->exec($sql);
143     if (is_a($affected, 'PEAR_Error')) return false;
144
145     $group_id = $mdb2->lastInsertID('tt_groups', 'id');
146
147     // Update org_id with group_id.
148     $sql = "update tt_groups set org_id = $group_id where org_id is NULL and id = $group_id";
149     $affected = $mdb2->exec($sql);
150     if (is_a($affected, 'PEAR_Error')) return false;
151
152     return $group_id;
153   }
154
155   // The createUser creates a user in database as part of registration process.
156   function createUser() {
157     $mdb2 = getConnection();
158
159     $login = $mdb2->quote($this->login);
160     $password = 'md5('.$mdb2->quote($this->password1).')';
161     $name = $mdb2->quote($this->user_name);
162     $email = $mdb2->quote($this->email);
163     $created = 'now()';
164     $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
165     $values = "values($login, $password, $name, $this->group_id, $this->group_id, $this->role_id, $email, $created, $created_ip)";
166
167     $sql = 'insert into tt_users (login, password, name, group_id, org_id, role_id, email, created, created_ip) '.$values;
168     $affected = $mdb2->exec($sql);
169     if (!is_a($affected, 'PEAR_Error')) {
170       $user_id = $mdb2->lastInsertID('tt_users', 'id');
171       return $user_id;
172     }
173     return false;
174   }
175
176   // The setCreatedBy sets created_by field for both group and user to passed in user_id.
177   function setCreatedBy($user_id) {
178     if ($this->err->yes()) return false; // There are errors, do not proceed.
179
180     global $i18n;
181     $mdb2 = getConnection();
182
183     // Update group.
184     $sql = "update tt_groups set created_by = $user_id where id = $this->group_id";
185     $affected = $mdb2->exec($sql);
186     if (is_a($affected, 'PEAR_Error')) {
187       $this->err->add($i18n->get('error.db'));
188       return false;
189     }
190
191     // Update top manager.
192     $sql = "update tt_users set created_by = $user_id where id = $user_id and group_id = $this->group_id";
193     $affected = $mdb2->exec($sql);
194     if (is_a($affected, 'PEAR_Error')) {
195       $this->err->add($i18n->get('error.db'));
196       return false;
197     }
198
199     return true;
200   }
201
202   // registeredRecently determines if we already have a successful recent registration from user IP.
203   // "recent" means "within the last minute" and is set in a query by the following condition:
204   // "and created > now() - interval 1 minute". Change if necessary.
205   function registeredRecently() {
206     $mdb2 = getConnection();
207
208     $ip_part = ' created_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']);
209     $sql = 'select created from tt_groups where '.$ip_part.' and created > now() - interval 1 minute';
210     $res = $mdb2->query($sql);
211     if (is_a($res, 'PEAR_Error'))
212       return false;
213     $val = $res->fetchRow();
214     if ($val)
215       return true;
216
217     return false;
218   }
219 }