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 // +----------------------------------------------------------------------+
31 // ttAdmin class is used to perform admin tasks.
34 var $err = null; // Error object, passed to us as reference.
35 // We use it to communicate errors to caller.
38 function __construct(&$err = null) {
42 // getSubgroups rerurns an array of subgroups for a group.
43 function getSubgroups($group_id) {
44 $mdb2 = getConnection();
47 $sql = "select id from tt_groups where parent_id = $group_id";
48 $res = $mdb2->query($sql);
49 if (!is_a($res, 'PEAR_Error')) {
50 while ($val = $res->fetchRow()) {
57 // getUsers obtains user ids in a group.
58 function getUsers($group_id) {
59 $mdb2 = getConnection();
60 $sql = "select id from tt_users where group_id = $group_id";
61 $res = $mdb2->query($sql);
63 if (!is_a($res, 'PEAR_Error')) {
64 while ($val = $res->fetchRow()) {
71 // markUserDeleted marks a user and all things associated with user as deleted.
72 function markUserDeleted($user_id) {
73 $mdb2 = getConnection();
75 // Mark user binds as deleted.
76 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
77 $affected = $mdb2->exec($sql);
78 if (is_a($affected, 'PEAR_Error')) return false;
80 // Mark favorite reports as deleted.
81 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
82 $affected = $mdb2->exec($sql);
83 if (is_a($affected, 'PEAR_Error')) return false;
85 // Mark user as deleted.
87 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
88 $sql = "update tt_users set status = NULL $modified_part where id = $user_id";
89 $affected = $mdb2->exec($sql);
90 if (is_a($affected, 'PEAR_Error')) return false;
95 // The markTasksDeleted deletes task binds and marks the tasks as deleted for a group.
96 function markTasksDeleted($group_id) {
97 $mdb2 = getConnection();
98 $sql = "select id from tt_tasks where group_id = $group_id";
99 $res = $mdb2->query($sql);
100 if (is_a($res, 'PEAR_Error')) return false;
102 while ($val = $res->fetchRow()) {
103 // Delete task binds.
104 $task_id = $val['id'];
105 $sql = "delete from tt_project_task_binds where task_id = $task_id";
106 $affected = $mdb2->exec($sql);
107 if (is_a($affected, 'PEAR_Error')) return false;
109 // Mark task as deleted.
110 $sql = "update tt_tasks set status = NULL where id = $task_id";
111 $affected = $mdb2->exec($sql);
112 if (is_a($affected, 'PEAR_Error')) return false;
118 // markGroupDeleted marks a group and everything in it as deleted.
119 function markGroupDeleted($group_id) {
121 // Keep the logic simple by returning false on first error.
123 // Obtain subgroups and call self recursively on them.
124 $subgroups = $this->getSubgroups($group_id);
125 foreach($subgroups as $subgroup) {
126 if (!$this->markGroupDeleted($subgroup['id']))
130 // Now that we are done with subgroups, handle this group.
131 $users = $this->getUsers($group_id);
133 // Iterate through group users and mark them as deleted.
134 foreach ($users as $one_user) {
135 if (!$this->markUserDeleted($one_user['id']))
139 // Mark tasks deleted.
140 if (!$this->markTasksDeleted($group_id)) return false;
142 $mdb2 = getConnection();
144 // Mark roles deleted.
145 $sql = "update tt_roles set status = NULL where group_id = $group_id";
146 $affected = $mdb2->exec($sql);
147 if (is_a($affected, 'PEAR_Error')) return false;
149 // Mark projects deleted.
150 $sql = "update tt_projects set status = NULL where group_id = $group_id";
151 $affected = $mdb2->exec($sql);
152 if (is_a($affected, 'PEAR_Error')) return false;
154 // Mark clients deleted.
155 $sql = "update tt_clients set status = NULL where group_id = $group_id";
156 $affected = $mdb2->exec($sql);
157 if (is_a($affected, 'PEAR_Error')) return false;
159 // Mark invoices deleted.
160 $sql = "update tt_invoices set status = NULL where group_id = $group_id";
161 $affected = $mdb2->exec($sql);
162 if (is_a($affected, 'PEAR_Error')) return false;
164 // Mark custom fields deleted.
165 $sql = "update tt_custom_fields set status = NULL where group_id = $group_id";
166 $affected = $mdb2->exec($sql);
167 if (is_a($affected, 'PEAR_Error')) return false;
169 // Mark notifications deleted.
170 $sql = "update tt_cron set status = NULL where group_id = $group_id";
171 $affected = $mdb2->exec($sql);
172 if (is_a($affected, 'PEAR_Error')) return false;
174 // Note: we don't mark tt_log or tt_expense_items deleted here.
177 // 1) Users may mark some of them deleted during their work.
178 // If we mark all of them deleted here, we can't recover nicely
179 // as we'll lose track of what was accidentally deleted by user.
181 // 2) DB maintenance script (Clean up DB from inactive groups) should
182 // get rid of these items permanently eventually.
184 // Mark group deleted.
186 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
187 $sql = "update tt_groups set status = NULL $modified_part where id = $group_id";
188 $affected = $mdb2->exec($sql);
189 if (is_a($affected, 'PEAR_Error')) return false;
194 // validateGroupInfo validates group information entered by user.
195 function validateGroupInfo($fields) {
201 if (!ttValidString($fields['new_group_name'])) {
202 $this->err->add($i18n->get('error.field'), $i18n->get('label.group_name'));
205 if (!ttValidString($fields['user_name'])) {
206 $this->err->add($i18n->get('error.field'), $i18n->get('label.manager_name'));
209 if (!ttValidString($fields['new_login'])) {
210 $this->err->add($i18n->get('error.field'), $i18n->get('label.manager_login'));
214 // If we change login, it must be unique.
215 if ($fields['new_login'] != $fields['old_login']) {
216 if (ttUserHelper::getUserByLogin($fields['new_login'])) {
217 $this->err->add($i18n->get('error.user_exists'));
222 if (!$auth->isPasswordExternal() && ($fields['password1'] || $fields['password2'])) {
223 if (!ttValidString($fields['password1'])) {
224 $this->err->add($i18n->get('error.field'), $i18n->get('label.password'));
227 if (!ttValidString($fields['password2'])) {
228 $this->err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
231 if ($fields['password1'] !== $fields['password2']) {
232 $this->err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
236 if (!ttValidEmail($fields['email'], true)) {
237 $this->err->add($i18n->get('error.field'), $i18n->get('label.email'));
244 // updateGroup validates user input and updates the group with new information.
245 function updateGroup($group_id, $fields) {
246 if (!$this->validateGroupInfo($fields)) return false; // Can't continue as user input is invalid.
249 $mdb2 = getConnection();
251 // Update group name if it changed.
252 if ($fields['old_group_name'] != $fields['new_group_name']) {
253 $name_part = 'name = '.$mdb2->quote($fields['new_group_name']);
254 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
255 $sql = 'update tt_groups set '.$name_part.$modified_part.' where id = '.$group_id;
256 $affected = $mdb2->exec($sql);
257 if (is_a($affected, 'PEAR_Error')) return false;
260 // Update group manager.
261 $user_id = $fields['user_id'];
262 $login_part = 'login = '.$mdb2->quote($fields['new_login']);
263 if ($fields['password1'])
264 $password_part = ', password = md5('.$mdb2->quote($fields['password1']).')';
265 $name_part = ', name = '.$mdb2->quote($fields['user_name']);
266 $email_part = ', email = '.$mdb2->quote($fields['email']);
267 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
268 $sql = 'update tt_users set '.$login_part.$password_part.$name_part.$email_part.$modified_part.'where id = '.$user_id;
269 $affected = $mdb2->exec($sql);
270 if (is_a($affected, 'PEAR_Error')) return false;
275 // validateUserInfo validates account information entered by user.
276 function validateUserInfo($fields) {
283 if (!ttValidString($fields['name'])) {
284 $this->err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
287 if (!ttValidString($fields['login'])) {
288 $this->err->add($i18n->get('error.field'), $i18n->get('label.login'));
291 // If we change login, it must be unique.
292 if ($fields['login'] != $user->login) {
293 if (ttUserHelper::getUserByLogin($fields['login'])) {
294 $this->err->add($i18n->get('error.user_exists'));
298 if (!$auth->isPasswordExternal() && ($fields['password1'] || $fields['password2'])) {
299 if (!ttValidString($fields['password1'])) {
300 $this->err->add($i18n->get('error.field'), $i18n->get('label.password'));
303 if (!ttValidString($fields['password2'])) {
304 $this->err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
307 if ($fields['password1'] !== $fields['password2']) {
308 $this->err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
312 if (!ttValidEmail($fields['email'], true)) {
313 $this->err->add($i18n->get('error.field'), $i18n->get('label.email'));
320 // updateSelf validates user input and updates admin account with new information.
321 function updateSelf($fields) {
322 if (!$this->validateUserInfo($fields)) return false; // Can't continue as user input is invalid.
326 $mdb2 = getConnection();
329 $user_id = $user->id;
330 $login_part = 'login = '.$mdb2->quote($fields['login']);
331 if ($fields['password1'])
332 $password_part = ', password = md5('.$mdb2->quote($fields['password1']).')';
333 $name_part = ', name = '.$mdb2->quote($fields['name']);
334 $email_part = ', email = '.$mdb2->quote($fields['email']);
335 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
336 $sql = 'update tt_users set '.$login_part.$password_part.$name_part.$email_part.$modified_part.'where id = '.$user_id;
337 $affected = $mdb2->exec($sql);
338 if (is_a($affected, 'PEAR_Error')) {
339 $this->err->add($i18n->get('error.db'));
346 // getGroupDetails obtains group name and its top manager details.
347 static function getGroupDetails($group_id) {
349 $mdb2 = getConnection();
351 $sql = "select g.name as group_name, u.id as manager_id, u.name as manager_name, u.login as manager_login, u.email as manager_email".
353 " inner join tt_users u on (u.group_id = g.id)".
354 " inner join tt_roles r on (r.id = u.role_id and r.rank = 512)".
355 " where g.id = $group_id";
357 $res = $mdb2->query($sql);
358 if (!is_a($res, 'PEAR_Error')) {
359 $val = $res->fetchRow();