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 import('ttTeamHelper');
 
  31 // Class ttUserHelper contains helper functions for operations with users.
 
  34   // The getUserDetails function returns user details.
 
  35   static function getUserDetails($user_id) {
 
  37     $mdb2 = getConnection();
 
  39     $sql =  "select * from tt_users where id = $user_id";
 
  40     $res = $mdb2->query($sql);
 
  42     if (!is_a($res, 'PEAR_Error')) {
 
  43       $val = $res->fetchRow();
 
  49   // The getUserName function returns user name.
 
  50   static function getUserName($user_id) {
 
  51     $mdb2 = getConnection();
 
  53     $sql = "select name from tt_users where id = $user_id and (status = 1 or status = 0)";
 
  54     $res = $mdb2->query($sql);
 
  56     if (!is_a($res, 'PEAR_Error')) {
 
  57       $val = $res->fetchRow();
 
  63   // The getUserByLogin function obtains data for a user, who is identified by login.
 
  64   static function getUserByLogin($login) {
 
  65     $mdb2 = getConnection();
 
  67     $sql = "select id, name from tt_users where login = ".$mdb2->quote($login)." and (status = 1 or status = 0)";
 
  68     $res = $mdb2->query($sql);
 
  69     if (!is_a($res, 'PEAR_Error')) {
 
  70       if ($val = $res->fetchRow()) {
 
  77   // The getUserByEmail function is a helper function that tries to obtain user details identified by email.
 
  78   // This function works only when one such active user exists.
 
  79   static function getUserByEmail($email) {
 
  80     $mdb2 = getConnection();
 
  82     $sql = "select login, count(*) as cnt from tt_users where email = ".$mdb2->quote($email)." and status = 1 group by email";
 
  83     $res = $mdb2->query($sql);
 
  85     if (is_a($res, 'PEAR_Error'))
 
  88     $val = $res->fetchRow();
 
  89     if (1 <> $val['cnt']) {
 
  90       // We either have no users or multiple users with a given email.
 
  96   // The getUserIdByTmpRef obtains user id from a temporary reference (used for password resets).
 
  97   static function getUserIdByTmpRef($ref) {
 
  98     $mdb2 = getConnection();
 
 100     $sql = "select user_id from tt_tmp_refs where ref = ".$mdb2->quote($ref);
 
 101     $res = $mdb2->query($sql);
 
 103     if (!is_a($res, 'PEAR_Error')) {
 
 104       $val = $res->fetchRow();
 
 105       return $val['user_id'];
 
 110   // insert - inserts a user into database.
 
 111   static function insert($fields, $hash = true) {
 
 112     $mdb2 = getConnection();
 
 114     $password = $mdb2->quote($fields['password']);
 
 116       $password = 'md5('.$password.')';
 
 117     $email = isset($fields['email']) ? $fields['email'] : '';
 
 118     $team_id = (int) $fields['team_id'];
 
 119     $role = (int) $fields['role'];
 
 120     $rate = str_replace(',', '.', isset($fields['rate']) ? $fields['rate'] : 0);
 
 123     if (array_key_exists('status', $fields)) { // Key exists and may be NULL during migration of deleted acounts.
 
 124       $status_f = ', status';
 
 125       $status_v = ', '.$mdb2->quote($fields['status']);
 
 128     $sql = "insert into tt_users (name, login, password, team_id, role, role_id, client_id, rate, email $status_f) values (".
 
 129       $mdb2->quote($fields['name']).", ".$mdb2->quote($fields['login']).
 
 130       ", $password, $team_id, $role, ".$mdb2->quote($fields['role_id']).", ".$mdb2->quote($fields['client_id']).", $rate, ".$mdb2->quote($email)." $status_v)";
 
 131     $affected = $mdb2->exec($sql);
 
 133     // Now deal with project assignment.
 
 134     if (!is_a($affected, 'PEAR_Error')) {
 
 135       $sql = "SELECT LAST_INSERT_ID() AS last_id";
 
 136       $res = $mdb2->query($sql);
 
 137       $val = $res->fetchRow();
 
 138       $last_id = $val['last_id'];
 
 140       $projects = isset($fields['projects']) ? $fields['projects'] : array();
 
 141       if (count($projects) > 0) {
 
 142         // We have at least one project assigned. Insert corresponding entries in tt_user_project_binds table.
 
 143         foreach($projects as $p) {
 
 144           if(!isset($p['rate']))
 
 147             $p['rate'] = str_replace(',', '.', $p['rate']);
 
 149           $sql = "insert into tt_user_project_binds (project_id, user_id, rate, status) values(".$p['id'].",".$last_id.",".$p['rate'].", 1)";
 
 150           $affected = $mdb2->exec($sql);
 
 158   // update - updates a user in database.
 
 159   static function update($user_id, $fields) {
 
 161     $mdb2 = getConnection();
 
 164     if (!$user_id || !isset($fields['login']))
 
 167     // Prepare query parts.
 
 168     if (isset($fields['password']))
 
 169       $pass_part = ', password = md5('.$mdb2->quote($fields['password']).')';
 
 170     if (right_assign_roles & $user->rights) {
 
 171       if (isset($fields['role'])) {
 
 172         $role = (int) $fields['role'];
 
 173         $role_part = ", role = $role";
 
 175       if (isset($fields['role_id'])) {
 
 176         $role_id = (int) $fields['role_id'];
 
 177         $role_id_part = ", role_id = $role_id";
 
 179       if (array_key_exists('client_id', $fields)) // Could be NULL.
 
 180         $client_part = ", client_id = ".$mdb2->quote($fields['client_id']);
 
 183     if (array_key_exists('rate', $fields)) {
 
 184       $rate = str_replace(',', '.', isset($fields['rate']) ? $fields['rate'] : 0);
 
 185       if($rate == '') $rate = 0;
 
 186       $rate_part = ", rate = ".$mdb2->quote($rate); 
 
 189     if (isset($fields['status'])) {
 
 190       $status = (int) $fields['status']; 
 
 191       $status_part = ", status = $status";
 
 194     $sql = "update tt_users set login = ".$mdb2->quote($fields['login']).
 
 195       "$pass_part, name = ".$mdb2->quote($fields['name']).
 
 196       "$role_part $role_id_part $client_part $rate_part $status_part, email = ".$mdb2->quote($fields['email']).
 
 197       " where id = $user_id";
 
 198     $affected = $mdb2->exec($sql);
 
 199     if (is_a($affected, 'PEAR_Error')) return false;
 
 201     if (array_key_exists('projects', $fields)) {
 
 202       // Deal with project assignments.
 
 203       // Note: we cannot simply delete old project binds and insert new ones because it screws up reporting
 
 204       // (when looking for cost while entries for de-assigned projects exist).
 
 205       // Therefore, we must iterate through all projects and only delete the binds when no time entries are present,
 
 206       // otherwise de-activate the bind (set its status to inactive). This will keep the bind
 
 207       // and its rate in database for reporting.
 
 209       $all_projects = ttTeamHelper::getAllProjects($user->team_id);
 
 210       $assigned_projects = isset($fields['projects']) ? $fields['projects'] : array();
 
 212       foreach($all_projects as $p) {
 
 213         // Determine if a project is assigned.
 
 215         $project_id = $p['id'];
 
 217         if (count($assigned_projects) > 0) {
 
 218           foreach ($assigned_projects as $ap) {
 
 219             if ($project_id == $ap['id']) {
 
 223                 $rate = str_replace(",",".",$rate);
 
 231           ttUserHelper::deleteBind($user_id, $project_id);
 
 233           // Here we need to either update or insert new tt_user_project_binds record.
 
 234           // Determine if a record exists.
 
 235           $sql = "select id from tt_user_project_binds where user_id = $user_id and project_id = $project_id";
 
 236           $res = $mdb2->query($sql);
 
 237           if (is_a($res, 'PEAR_Error')) die ($res->getMessage());
 
 238           if ($val = $res->fetchRow()) {
 
 239             // Record exists. Update it.
 
 240             $sql = "update tt_user_project_binds set status = 1, rate = $rate where id = ".$val['id'];
 
 241             $affected = $mdb2->exec($sql);
 
 242             if (is_a($affected, 'PEAR_Error')) die ($affected->getMessage());
 
 244             // Record does not exist. Insert it.
 
 245             ttUserHelper::insertBind($user_id, $project_id, $rate, 1);
 
 253   // markDeleted - marks user and its associated things as deleted.
 
 254   static function markDeleted($user_id) {
 
 255     $mdb2 = getConnection();
 
 258     // Preliminary checks. Only managers, co-managers, and admin can do this.
 
 259     if (!$user->canManageTeam() && !$user->isAdmin())
 
 262     // Tho logic is different depending on who is doing the operation.
 
 263     // Co-manager and admin - mark user deleted.
 
 264     // Manager - mark user deleted. If manager is the only account in team, mark team items deleted.
 
 267     if ($user->isAdmin()) {
 
 268       // Mark user binds as deleted.
 
 269       $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
 
 270       $affected = $mdb2->exec($sql);
 
 271       if (is_a($affected, 'PEAR_Error'))
 
 274       // Mark favorite reports as deleted.
 
 275       $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
 
 276       $affected = $mdb2->exec($sql);
 
 277       if (is_a($affected, 'PEAR_Error'))
 
 280       // Mark user as deleted.
 
 281       $sql = "update tt_users set status = NULL where id = $user_id";
 
 282       $affected = $mdb2->exec($sql);
 
 283       if (is_a($affected, 'PEAR_Error'))
 
 286     } elseif ($user->isCoManager()) {
 
 287       // Mark user binds as deleted.
 
 288       $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
 
 289       $affected = $mdb2->exec($sql);
 
 290       if (is_a($affected, 'PEAR_Error'))
 
 293       // Mark favorite reports as deleted.
 
 294       $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
 
 295       $affected = $mdb2->exec($sql);
 
 296       if (is_a($affected, 'PEAR_Error'))
 
 299       // Mark user as deleted.
 
 300       $sql = "update tt_users set status = NULL where id = $user_id and team_id = ".$user->team_id;
 
 301       $affected = $mdb2->exec($sql);
 
 302       if (is_a($affected, 'PEAR_Error'))
 
 305     } elseif ($user->isManager()) {
 
 306       $user_count = ttTeamHelper::getUserCount($user->team_id);
 
 308       // Marking deleted a manager with active users is not allowed.
 
 309        if (($user_id == $user->id) && ($user_count > 1))
 
 312       if (1 == $user_count) {
 
 313         // Mark tasks deleted.
 
 314         if (!ttTeamHelper::markTasksDeleted($user->team_id))
 
 317         // Mark projects deleted.
 
 318         $sql = "update tt_projects set status = NULL where team_id = $user->team_id";
 
 319         $affected = $mdb2->exec($sql);
 
 320         if (is_a($affected, 'PEAR_Error'))
 
 323         // Mark clients deleted.
 
 324         $sql = "update tt_clients set status = NULL where team_id = $user->team_id";
 
 325         $affected = $mdb2->exec($sql);
 
 326         if (is_a($affected, 'PEAR_Error'))
 
 329         // Mark custom fields deleted.
 
 330         $sql = "update tt_custom_fields set status = NULL where team_id = $user->team_id";
 
 331         $affected = $mdb2->exec($sql);
 
 332         if (is_a($affected, 'PEAR_Error'))
 
 335         // Mark team deleted.
 
 336         $sql = "update tt_teams set status = NULL where id = $user->team_id";
 
 337         $affected = $mdb2->exec($sql);
 
 338         if (is_a($affected, 'PEAR_Error'))
 
 342       // Mark user binds as deleted.
 
 343       $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
 
 344       $affected = $mdb2->exec($sql);
 
 345       if (is_a($affected, 'PEAR_Error'))
 
 348       // Mark favorite reports as deleted.
 
 349       $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
 
 350       $affected = $mdb2->exec($sql);
 
 351       if (is_a($affected, 'PEAR_Error'))
 
 354       // Mark user as deleted.
 
 355       $sql = "update tt_users set status = NULL where id = $user_id and team_id = ".$user->team_id;
 
 356       $affected = $mdb2->exec($sql);
 
 357       if (is_a($affected, 'PEAR_Error'))
 
 364   // The delete function permanently deletes a user and all associated data.
 
 365   static function delete($user_id) {
 
 366     $mdb2 = getConnection();
 
 368     // Delete custom field log entries for user, if we have them.
 
 369     $sql = "delete from tt_custom_field_log where log_id in
 
 370       (select id from tt_log where user_id = $user_id)";
 
 371     $affected = $mdb2->exec($sql);
 
 372     if (is_a($affected, 'PEAR_Error'))
 
 375     // Delete log entries for user.
 
 376     $sql = "delete from tt_log where user_id = $user_id";
 
 377     $affected = $mdb2->exec($sql);
 
 378     if (is_a($affected, 'PEAR_Error'))
 
 381     // Delete expense items for user.
 
 382     $sql = "delete from tt_expense_items where user_id = $user_id";
 
 383     $affected = $mdb2->exec($sql);
 
 384     if (is_a($affected, 'PEAR_Error'))
 
 387     // Delete user binds.
 
 388     $sql = "delete from tt_user_project_binds where user_id = $user_id";
 
 389     $affected = $mdb2->exec($sql);
 
 390     if (is_a($affected, 'PEAR_Error'))
 
 393     // Clean up tt_config table.
 
 394     $sql = "delete from tt_config where user_id = $user_id";
 
 395     $affected = $mdb2->exec($sql);
 
 396     if (is_a($affected, 'PEAR_Error'))
 
 399     // Clean up tt_fav_reports table.
 
 400     $sql = "delete from tt_fav_reports where user_id = $user_id";
 
 401     $affected = $mdb2->exec($sql);
 
 402     if (is_a($affected, 'PEAR_Error'))
 
 406     $sql = "delete from tt_users where id = $user_id";
 
 407     $affected = $mdb2->exec($sql);    
 
 408     if (is_a($affected, 'PEAR_Error'))
 
 414   // The saveTmpRef saves a temporary reference for user that is used to reset user password.
 
 415   static function saveTmpRef($ref, $user_id) {
 
 416     $mdb2 = getConnection();
 
 418     $sql = "delete from tt_tmp_refs where timestamp + 86400 < now()";
 
 419     $affected = $mdb2->exec($sql);
 
 421     $sql = "insert into tt_tmp_refs (ref, user_id) values(".$mdb2->quote($ref).", $user_id)";
 
 422     $affected = $mdb2->exec($sql);
 
 425   // The setPassword function updates password for user.
 
 426   static function setPassword($user_id, $password) {
 
 427     $mdb2 = getConnection();
 
 429     $sql = "update tt_users set password = md5(".$mdb2->quote($password).") where id = $user_id";
 
 430     $affected = $mdb2->exec($sql);
 
 432     return (!is_a($affected, 'PEAR_Error'));
 
 435   // insertBind - inserts a user to project bind into tt_user_project_binds table.
 
 436   static function insertBind($user_id, $project_id, $rate, $status) {
 
 437     $mdb2 = getConnection();
 
 439     $sql = "insert into tt_user_project_binds (user_id, project_id, rate, status)
 
 440       values($user_id, $project_id, ".$mdb2->quote($rate).", $status)";
 
 441     $affected = $mdb2->exec($sql);
 
 442     return (!is_a($affected, 'PEAR_Error'));
 
 445   // deleteBind - deactivates user to project bind when time entries exist,
 
 446   // otherwise deletes it entirely.
 
 447   static function deleteBind($user_id, $project_id) {
 
 448     $mdb2 = getConnection();
 
 450     $sql = "select count(*) as cnt from tt_log where 
 
 451       user_id = $user_id and project_id = $project_id and status = 1";
 
 452     $res = $mdb2->query($sql);
 
 453     if (is_a($res, 'PEAR_Error')) die ($res->getMessage());
 
 456     $val = $res->fetchRow();
 
 457     $count = $val['cnt'];
 
 460       // Deactivate user bind.
 
 461       $sql = "select id from tt_user_project_binds where user_id = $user_id and project_id = $project_id";
 
 462        $res = $mdb2->query($sql);
 
 463        if (is_a($res, 'PEAR_Error')) die ($res->getMessage());
 
 464        if ($val = $res->fetchRow()) {
 
 465          $sql = "update tt_user_project_binds set status = 0 where id = ".$val['id'];
 
 466          $affected = $mdb2->exec($sql);
 
 467          if (is_a($affected, 'PEAR_Error')) die ($res->getMessage());
 
 471       $sql = "delete from tt_user_project_binds where user_id = $user_id and project_id = $project_id";
 
 472       $affected = $mdb2->exec($sql);
 
 473       if (is_a($affected, 'PEAR_Error')) die ($res->getMessage());