Improved protection from mass bot registrations.
[timetracker.git] / WEB-INF / lib / ttTeamHelper.class.php
index 5347449..b94f643 100644 (file)
@@ -33,76 +33,6 @@ import('ttInvoiceHelper');
 // Class ttTeamHelper - contains helper functions that operate with groups.
 class ttTeamHelper {
 
-  // The getUserCount function returns number of active users in a group.
-  static function getUserCount($group_id) {
-    $mdb2 = getConnection();
-
-    $sql = "select count(id) as cnt from tt_users where group_id = $group_id and status = 1";
-    $res = $mdb2->query($sql);
-
-    if (!is_a($res, 'PEAR_Error')) {
-      $val = $res->fetchRow();
-      return $val['cnt'];
-    }
-    return false;
-  }
-
-  // The getUsersForClient obtains all active and inactive users in a group that are relevant to a client.
-  static function getUsersForClient() {
-    global $user;
-    $mdb2 = getConnection();
-
-    $sql = "select u.id, u.name from tt_user_project_binds upb".
-      " inner join tt_client_project_binds cpb on (upb.project_id = cpb.project_id and cpb.client_id = $user->client_id)".
-      " inner join tt_users u on (u.id = upb.user_id)".
-      " where (u.status = 1 or u.status = 0)".
-      " group by u.id".
-      " order by upper(u.name)";
-    $res = $mdb2->query($sql);
-    $user_list = array();
-    if (is_a($res, 'PEAR_Error'))
-      return false;
-    while ($val = $res->fetchRow()) {
-      $user_list[] = $val;
-    }
-    return $user_list;
-  }
-
-  // The getActiveUsers obtains all active users in a given group.
-  static function getActiveUsers($options = null) {
-    global $user;
-    global $i18n;
-    $mdb2 = getConnection();
-
-    if (isset($options['getAllFields']))
-      $sql = "select u.*, r.name as role_name, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id) where u.group_id = $user->group_id and u.status = 1 order by upper(u.name)";
-    else
-      $sql = "select id, name from tt_users where group_id = $user->group_id and status = 1 order by upper(name)";
-    $res = $mdb2->query($sql);
-    $user_list = array();
-    if (is_a($res, 'PEAR_Error'))
-      return false;
-    while ($val = $res->fetchRow()) {
-      // Localize top manager role name, as it is not localized in db.
-      if ($val['rank'] == 512)
-        $val['role_name'] = $i18n->get('role.top_manager.label');
-      $user_list[] = $val;
-    }
-
-    if (isset($options['putSelfFirst'])) {
-      // Put own entry at the front.
-      $cnt = count($user_list);
-      for($i = 0; $i < $cnt; $i++) {
-        if ($user_list[$i]['id'] == $user->id) {
-          $self = $user_list[$i]; // Found self.
-          array_unshift($user_list, $self); // Put own entry at the front.
-          array_splice($user_list, $i+1, 1); // Remove duplicate.
-        }
-      }
-    }
-    return $user_list;
-  }
-
   // The swapRolesWith swaps existing user role with that of another user.
   static function swapRolesWith($user_id) {
     global $user;
@@ -117,7 +47,7 @@ class ttTeamHelper {
     if (!$val['id'] || !$val['role_id'])
       return false;
 
-    $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
+    $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
 
     // Promote user.
     $sql = "update tt_users set role_id = $user->role_id".$modified_part." where id = $user_id and group_id = $user->group_id";
@@ -153,21 +83,6 @@ class ttTeamHelper {
     return $user_list;
   }
 
-  // The getUsers obtains all active and inactive (but not deleted) users in a group.
-  static function getUsers() {
-    global $user;
-    $mdb2 = getConnection();
-    $sql = "select id, name from tt_users where group_id = $user->group_id and (status = 1 or status = 0) order by upper(name)";
-    $res = $mdb2->query($sql);
-    $user_list = array();
-    if (is_a($res, 'PEAR_Error'))
-      return false;
-    while ($val = $res->fetchRow()) {
-      $user_list[] = $val;
-    }
-    return $user_list;
-  }
-
   // The getInactiveUsers obtains all inactive users in a group.
   static function getInactiveUsers($group_id, $all_fields = false) {
     $mdb2 = getConnection();
@@ -187,60 +102,6 @@ class ttTeamHelper {
     return false;
   }
 
-  // The getAllUsers obtains all users in a group.
-  static function getAllUsers($group_id, $all_fields = false) {
-    $mdb2 = getConnection();
-    if ($all_fields)
-      $sql = "select * from tt_users where group_id = $group_id order by upper(name)";
-    else
-      $sql = "select id, name from tt_users where group_id = $group_id order by upper(name)";
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $result[] = $val;
-      }
-      return $result;
-    }
-    return false;
-  }
-
-  // getActiveProjects - returns an array of active projects for a group.
-  static function getActiveProjects($group_id)
-  {
-    $result = array();
-    $mdb2 = getConnection();
-
-    $sql = "select id, name, description, tasks from tt_projects
-      where group_id = $group_id and status = 1 order by upper(name)";
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $result[] = $val;
-      }
-    }
-    return $result;
-  }
-
-  // getInactiveProjects - returns an array of inactive projects for a group.
-  static function getInactiveProjects($group_id)
-  {
-    $result = array();
-    $mdb2 = getConnection();
-
-    $sql = "select id, name, description, tasks from tt_projects
-      where group_id = $group_id and status = 0 order by upper(name)";
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $result[] = $val;
-      }
-    }
-    return $result;
-  }
-
   // The getAllProjects obtains all projects in a group.
   static function getAllProjects($group_id, $all_fields = false) {
     $mdb2 = getConnection();
@@ -260,60 +121,6 @@ class ttTeamHelper {
     return false;
   }
 
-  // getActiveTasks - returns an array of active tasks for a group.
-  static function getActiveTasks($group_id)
-  {
-    $result = array();
-    $mdb2 = getConnection();
-
-    $sql = "select id, name, description from tt_tasks where group_id = $group_id and status = 1 order by upper(name)";
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $result[] = $val;
-      }
-    }
-    return $result;
-  }
-
-  // getInactiveTasks - returns an array of inactive tasks for a group.
-  static function getInactiveTasks($group_id)
-  {
-    $result = array();
-    $mdb2 = getConnection();
-
-    $sql = "select id, name, description from tt_tasks
-      where group_id = $group_id and status = 0 order by upper(name)";
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $result[] = $val;
-      }
-    }
-    return $result;
-  }
-
-  // The getAllTasks obtains all tasks in a group.
-  static function getAllTasks($group_id, $all_fields = false) {
-    $mdb2 = getConnection();
-
-    if ($all_fields)
-      $sql = "select * from tt_tasks where group_id = $group_id order by status, upper(name)";
-    else
-      $sql = "select id, name from tt_tasks where group_id = $group_id order by status, upper(name)";
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $result[] = $val;
-      }
-      return $result;
-    }
-    return false;
-  }
-
   // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
   // "Relevant" means that client roles are filtered out if Client plugin is disabled.
   static function getActiveRolesForUser()
@@ -322,7 +129,14 @@ class ttTeamHelper {
     $result = array();
     $mdb2 = getConnection();
 
-    $sql = "select id, name, description, rank, rights from tt_roles where group_id = $user->group_id and rank < $user->rank and status = 1 order by rank";
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
+
+    // Determine max rank. If we are working in on behalf group
+    // then rank restriction does not apply.
+    $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
+
+    $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and org_id = $org_id and rank < $max_rank and status = 1 order by rank";
     $res = $mdb2->query($sql);
     $result = array();
     if (!is_a($res, 'PEAR_Error')) {
@@ -380,7 +194,14 @@ class ttTeamHelper {
     $result = array();
     $mdb2 = getConnection();
 
-    $sql = "select id, name, description, rank, rights from tt_roles where group_id = $user->group_id and rank < $user->rank and status = 0 order by rank";
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
+
+    // Determine max rank. If we are working in on behalf group
+    // then rank restriction does not apply.
+    $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
+
+    $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and org_id = $org_id and rank < $max_rank and status = 0 order by rank";
     $res = $mdb2->query($sql);
     $result = array();
     if (!is_a($res, 'PEAR_Error')) {
@@ -394,48 +215,6 @@ class ttTeamHelper {
     return $result;
   }
 
-  // The getActiveClients returns an array of active clients for a group.
-  static function getActiveClients($group_id, $all_fields = false)
-  {
-    $result = array();
-    $mdb2 = getConnection();
-
-    if ($all_fields)
-      $sql = "select * from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
-    else
-      $sql = "select id, name from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
-
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $result[] = $val;
-      }
-    }
-    return $result;
-  }
-
-  // The getInactiveClients returns an array of inactive clients for a group.
-  static function getInactiveClients($group_id, $all_fields = false)
-  {
-    $result = array();
-    $mdb2 = getConnection();
-
-    if ($all_fields)
-      $sql = "select * from tt_clients where group_id = $group_id and status = 0 order by upper(name)";
-    else
-      $sql = "select id, name from tt_clients where group_id = $group_id and status = 0 order by upper(name)";
-
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-       $result[] = $val;
-      }
-    }
-    return $result;
-  }
-
   // The getAllClients obtains all clients in a group.
   static function getAllClients($group_id, $all_fields = false) {
     $mdb2 = getConnection();
@@ -456,38 +235,6 @@ class ttTeamHelper {
     return false;
   }
 
-  // The getActiveInvoices returns an array of active invoices for a group.
-  static function getActiveInvoices($localizeDates = true)
-  {
-    global $user;
-    $addPaidStatus = $user->isPluginEnabled('ps');
-
-    $result = array();
-    $mdb2 = getConnection();
-
-    if ($user->isClient())
-      $client_part = " and i.client_id = $user->client_id";
-
-    $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
-      left join tt_clients c on (c.id = i.client_id)
-      where i.status = 1 and i.group_id = $user->group_id $client_part order by i.name";
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      $dt = new DateAndTime(DB_DATEFORMAT);
-      while ($val = $res->fetchRow()) {
-        if ($localizeDates) {
-          $dt->parseVal($val['date']);
-          $val['date'] = $dt->toString($user->date_format);
-        }
-        if ($addPaidStatus)
-          $val['paid'] = ttInvoiceHelper::isPaid($val['id']);
-        $result[] = $val;
-      }
-    }
-    return $result;
-  }
-
   // The getAllInvoices returns an array of all invoices for a group.
   static function getAllInvoices()
   {
@@ -508,35 +255,14 @@ class ttTeamHelper {
     return $result;
   }
 
-  // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
-  static function getRecentInvoices($group_id, $client_id)
-  {
-    global $user;
-
-    $result = array();
-    $mdb2 = getConnection();
-
-    $sql = "select i.id, i.name from tt_invoices i
-      left join tt_clients c on (c.id = i.client_id)
-      where i.group_id = $group_id and i.status = 1 and c.id = $client_id
-      order by i.id desc limit 3";
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      $dt = new DateAndTime(DB_DATEFORMAT);
-      while ($val = $res->fetchRow()) {
-        $result[] = $val;
-      }
-    }
-    return $result;
-  }
-
   // getUserToProjectBinds - obtains all user to project binds for a group.
   static function getUserToProjectBinds($group_id) {
     $mdb2 = getConnection();
 
     $result = array();
-    $sql = "select * from tt_user_project_binds where user_id in (select id from tt_users where group_id = $group_id) order by user_id, status, project_id";
+    $sql = "select * from tt_user_project_binds".
+      " where user_id in (select id from tt_users where group_id = $group_id)".
+      " and group_id = $group_id order by user_id, status, project_id";
     $res = $mdb2->query($sql);
     $result = array();
     if (!is_a($res, 'PEAR_Error')) {
@@ -633,47 +359,6 @@ class ttTeamHelper {
     return false;
   }
 
-  // getPredefinedExpenses - obtains predefined expenses for a group.
-  static function getPredefinedExpenses($group_id) {
-    global $user;
-    $replaceDecimalMark = ('.' != $user->decimal_mark);
-
-    $mdb2 = getConnection();
-
-    $result = array();
-    $sql = "select id, name, cost from tt_predefined_expenses where group_id = $group_id";
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        if ($replaceDecimalMark)
-          $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
-        $result[] = $val;
-      }
-      return $result;
-    }
-    return false;
-  }
-
-  // getNotifications - obtains notification descriptions for a group.
-  static function getNotifications($group_id) {
-    $mdb2 = getConnection();
-
-    $result = array();
-    $sql = "select c.id, c.cron_spec, c.email, c.report_condition, fr.name from tt_cron c
-      left join tt_fav_reports fr on (fr.id = c.report_id)
-      where c.group_id = $group_id and c.status = 1 and fr.status = 1";
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $result[] = $val;
-      }
-      return $result;
-    }
-    return false;
-  }
-
   // getMonthlyQuotas - obtains monthly quotas for a group.
   static function getMonthlyQuotas($group_id) {
     $mdb2 = getConnection();
@@ -691,178 +376,6 @@ class ttTeamHelper {
     return false;
   }
 
-  // The insert function creates a new group.
-  static function insert($fields) {
-
-    global $user;
-    $mdb2 = getConnection();
-
-    // Start with group name and currency.
-    $columns = 'name, currency';
-    $values = $mdb2->quote(trim($fields['name'])).', '.$mdb2->quote(trim($fields['currency']));
-
-    if ($fields['decimal_mark']) {
-      $columns .= ', decimal_mark';
-      $values .= ', '.$mdb2->quote($fields['decimal_mark']);
-    }
-
-    $lang = $fields['lang'];
-    if (!$lang) {
-      global $i18n;
-      $lang = $i18n->lang;
-    }
-    $columns .= ', lang';
-    $values .= ', '.$mdb2->quote($lang);
-
-    if ($fields['date_format'] || defined('DATE_FORMAT_DEFAULT')) {
-      $date_format = $fields['date_format'] ? $fields['date_format'] : DATE_FORMAT_DEFAULT;
-      $columns .= ', date_format';
-      $values .= ', '.$mdb2->quote($date_format);
-    }
-
-    if ($fields['time_format'] || defined('TIME_FORMAT_DEFAULT')) {
-      $time_format = $fields['time_format'] ? $fields['time_format'] : TIME_FORMAT_DEFAULT;
-      $columns .= ', time_format';
-      $values .= ', '.$mdb2->quote($time_format);
-    }
-
-    if ($fields['week_start'] || defined('WEEK_START_DEFAULT')) {
-      $week_start = $fields['week_start'] ? $fields['week_start'] : WEEK_START_DEFAULT;
-      $columns .= ', week_start';
-      $values .= ', '.(int)$week_start;
-    }
-
-    if ($fields['tracking_mode']) {
-      $columns .= ', tracking_mode';
-      $values .= ', '.(int)$fields['tracking_mode'];
-    }
-
-    if ($fields['project_required']) {
-      $columns .= ', project_required';
-      $values .= ', '.(int)$fields['project_required'];
-    }
-
-    if ($fields['task_required']) {
-      $columns .= ', task_required';
-      $values .= ', '.(int)$fields['task_required'];
-    }
-
-    if ($fields['record_type']) {
-      $columns .= ', record_type';
-      $values .= ', '.(int)$fields['record_type'];
-    }
-
-    if ($fields['bcc_email']) {
-      $columns .= ', bcc_email';
-      $values .= ', '.$mdb2->quote($fields['bcc_email']);
-    }
-
-    if ($fields['plugins']) {
-      $columns .= ', plugins';
-      $values .= ', '.$mdb2->quote($fields['plugins']);
-    }
-
-    if ($fields['lock_spec']) {
-      $columns .= ', lock_spec';
-      $values .= ', '.$mdb2->quote($fields['lock_spec']);
-    }
-
-    if ($fields['workday_minutes']) {
-      $columns .= ', workday_minutes';
-      $values .= ', '.(int)$fields['workday_minutes'];
-    }
-
-    if ($fields['config']) {
-      $columns .= ', config';
-      $values .= ', '.$mdb2->quote($fields['config']);
-    }
-
-    $columns .= ', created, created_ip, created_by';
-    $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id);
-
-    $sql = "insert into tt_groups ($columns) values($values)";
-    $affected = $mdb2->exec($sql);
-
-    if (!is_a($affected, 'PEAR_Error')) {
-      $group_id = $mdb2->lastInsertID('tt_groups', 'id');
-      return $group_id;
-    }
-
-    return false;
-  }
-
-  // The getInactiveGroups is a maintenance function that returns an array of inactive group ids (max 100).
-  static function getInactiveGroups() {
-    $inactive_groups = array();
-    $mdb2 = getConnection();
-
-    // Get all group ids for groups created or modified more than 8 months ago.
-    // $ts = date('Y-m-d', strtotime('-1 year'));
-    $ts = $mdb2->quote(date('Y-m-d', strtotime('-8 month')));
-    $sql =  "select id from tt_groups where created < $ts and (modified is null or modified < $ts) order by id";
-    $res = $mdb2->query($sql);
-
-    $count = 0;
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $group_id = $val['id'];
-        if (ttTeamHelper::isGroupActive($group_id) == false) {
-          $count++;
-          $inactive_groups[] = $group_id;
-          // Limit the array size for perfomance by allowing this operation on small chunks only.
-          if ($count >= 100) break;
-        }
-      }
-      return $inactive_groups;
-    }
-    return false;
-  }
-
-  // The isGroupActive determines if a group is using Time Tracker or abandoned it.
-  static function isGroupActive($group_id) {
-    $users = array();
-
-    $mdb2 = getConnection();
-    $sql = "select id from tt_users where group_id = $group_id";
-    $res = $mdb2->query($sql);
-    if (is_a($res, 'PEAR_Error')) die($res->getMessage());
-    while ($val = $res->fetchRow()) {
-      $users[] = $val['id'];
-    }
-    $user_list = implode(',', $users); // This is a comma-separated list of user ids.
-    if (!$user_list)
-      return false; // No users in group.
-
-    $count = 0;
-    $ts = date('Y-m-d', strtotime('-2 years'));
-    $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and created > '$ts'";
-    $res = $mdb2->query($sql);
-    if (!is_a($res, 'PEAR_Error')) {
-      if ($val = $res->fetchRow()) {
-        $count = $val['cnt'];
-      }
-    }
-
-    if ($count == 0)
-      return false;  // No time entries for the last 2 years.
-
-    if ($count <= 5) {
-      // We will consider a group inactive if it has 5 or less time entries made more than 1 year ago.
-      $count_last_year = 0;
-      $ts = date('Y-m-d', strtotime('-1 year'));
-      $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and created > '$ts'";
-      $res = $mdb2->query($sql);
-      if (!is_a($res, 'PEAR_Error')) {
-        if ($val = $res->fetchRow()) {
-          $count_last_year = $val['cnt'];
-        }
-        if ($count_last_year == 0)
-          return false;  // No time entries for the last year and only a few entries before that.
-      }
-    }
-    return true;
-  }
-
   // The delete function permanently deletes all data for a group.
   static function delete($group_id) {
     $mdb2 = getConnection();
@@ -907,34 +420,25 @@ class ttTeamHelper {
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error')) return false;
 
-    // Delete group.
-    $sql = "delete from tt_groups where id = $group_id";
+    // Delete cron entries.
+    $sql = "delete from tt_cron where group_id = $group_id";
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error')) return false;
 
-    return true;
-  }
-
-  // The markTasksDeleted deletes task binds and marks the tasks as deleted for a group.
-  static function markTasksDeleted($group_id) {
-    $mdb2 = getConnection();
-    $sql = "select id from tt_tasks where group_id = $group_id";
-    $res = $mdb2->query($sql);
-    if (is_a($res, 'PEAR_Error')) return false;
-
-    while ($val = $res->fetchRow()) {
+    // Delete predefined expenses.
+    $sql = "delete from tt_predefined_expenses where group_id = $group_id";
+    $affected = $mdb2->exec($sql);
+    if (is_a($affected, 'PEAR_Error')) return false;
 
-      // Delete task binds.
-      $task_id = $val['id'];
-      $sql = "delete from tt_project_task_binds where task_id = $task_id";
-      $affected = $mdb2->exec($sql);
-      if (is_a($affected, 'PEAR_Error')) return false;
+    // Delete monthly quotas.
+    $sql = "delete from tt_monthly_quotas where group_id = $group_id";
+    $affected = $mdb2->exec($sql);
+    if (is_a($affected, 'PEAR_Error')) return false;
 
-      // Mark task as deleted.
-      $sql = "update tt_tasks set status = NULL where id = $task_id";
-      $affected = $mdb2->exec($sql);
-      if (is_a($affected, 'PEAR_Error')) return false;
-    }
+    // Delete group.
+    $sql = "delete from tt_groups where id = $group_id";
+    $affected = $mdb2->exec($sql);
+    if (is_a($affected, 'PEAR_Error')) return false;
 
     return true;
   }