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 // +----------------------------------------------------------------------+
 
  30   var $login = null;            // User login.
 
  31   var $name = null;             // User name.
 
  32   var $id = null;               // User id.
 
  33   var $group_id = null;         // Group id.
 
  34   var $role_id = null;          // Role id.
 
  35   var $role_name = null;        // Role name.
 
  36   var $rank = null;             // User role rank.
 
  37   var $client_id = null;        // Client id for client user role.
 
  38   var $behalf_id = null;        // User id, on behalf of whom we are working.
 
  39   var $behalf_name = null;      // User name, on behalf of whom we are working.
 
  40   var $email = null;            // User email.
 
  41   var $lang = null;             // Language.
 
  42   var $decimal_mark = null;     // Decimal separator.
 
  43   var $date_format = null;      // Date format.
 
  44   var $time_format = null;      // Time format.
 
  45   var $week_start = 0;          // Week start day.
 
  46   var $show_holidays = 0;       // Whether to show holidays in calendar.
 
  47   var $tracking_mode = 0;       // Tracking mode.
 
  48   var $project_required = 0;    // Whether project selection is required on time entires.
 
  49   var $task_required = 0;       // Whether task selection is required on time entires.
 
  50   var $record_type = 0;         // Record type (duration vs start and finish, or both).
 
  51   var $punch_mode = 0;          // Whether punch mode is enabled for user.
 
  52   var $allow_overlap = 0;       // Whether to allow overlapping time entries.
 
  53   var $future_entries = 0;      // Whether to allow creating future entries.
 
  54   var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
 
  55   var $bcc_email = null;        // Bcc email.
 
  56   var $allow_ip = null;         // Specification from where user is allowed access.
 
  57   var $currency = null;         // Currency.
 
  58   var $plugins = null;          // Comma-separated list of enabled plugins.
 
  59   var $config = null;           // Comma-separated list of miscellaneous config options.
 
  60   var $team = null;             // Team name.
 
  61   var $custom_logo = 0;         // Whether to use a custom logo for team.
 
  62   var $lock_spec = null;        // Cron specification for record locking.
 
  63   var $workday_minutes = 480;   // Number of work minutes in a regular day.
 
  64   var $rights = array();        // An array of user rights such as 'track_own_time', etc.
 
  65   var $is_client = false;       // Whether user is a client as determined by missing 'track_own_time' right.
 
  68   function __construct($login, $id = null) {
 
  69     if (!$login && !$id) {
 
  70       // nothing to initialize
 
  74     $mdb2 = getConnection();
 
  76     $sql = "SELECT u.id, u.login, u.name, u.group_id, u.role_id, r.rank, r.name as role_name, r.rights, u.client_id, u.email, g.name as group_name,
 
  77       g.currency, g.lang, g.decimal_mark, g.date_format, g.time_format, g.week_start,
 
  78       g.tracking_mode, g.project_required, g.task_required, g.record_type,
 
  79       g.bcc_email, g.allow_ip, g.plugins, g.config, g.lock_spec, g.workday_minutes, g.custom_logo
 
  80       FROM tt_users u LEFT JOIN tt_groups g ON (u.group_id = g.id) LEFT JOIN tt_roles r on (r.id = u.role_id) WHERE ";
 
  84       $sql .= "u.login = ".$mdb2->quote($login);
 
  85     $sql .= " AND u.status = 1";
 
  87     $res = $mdb2->query($sql);
 
  88     if (is_a($res, 'PEAR_Error')) {
 
  92     $val = $res->fetchRow();
 
  94       $this->login = $val['login'];
 
  95       $this->name = $val['name'];
 
  96       $this->id = $val['id'];
 
  97       $this->group_id = $val['group_id'];
 
  98       $this->role_id = $val['role_id'];
 
  99       $this->role_name = $val['role_name'];
 
 100       $this->rights = explode(',', $val['rights']);
 
 101       $this->is_client = !in_array('track_own_time', $this->rights);
 
 102       $this->rank = $val['rank'];
 
 103       $this->client_id = $val['client_id'];
 
 104       $this->email = $val['email'];
 
 105       $this->lang = $val['lang'];
 
 106       $this->decimal_mark = $val['decimal_mark'];
 
 107       $this->date_format = $val['date_format'];
 
 108       $this->time_format = $val['time_format'];
 
 109       $this->week_start = $val['week_start'];
 
 110       $this->tracking_mode = $val['tracking_mode'];
 
 111       $this->project_required = $val['project_required'];
 
 112       $this->task_required = $val['task_required'];
 
 113       $this->record_type = $val['record_type'];
 
 114       $this->bcc_email = $val['bcc_email'];
 
 115       $this->allow_ip = $val['allow_ip'];
 
 116       $this->team = $val['group_name'];
 
 117       $this->currency = $val['currency'];
 
 118       $this->plugins = $val['plugins'];
 
 119       $this->lock_spec = $val['lock_spec'];
 
 120       $this->workday_minutes = $val['workday_minutes'];
 
 121       $this->custom_logo = $val['custom_logo'];
 
 123       $this->config = $val['config'];
 
 124       $config_array = explode(',', $this->config);
 
 126       // Set user config options.
 
 127       $this->show_holidays = in_array('show_holidays', $config_array);
 
 128       $this->punch_mode = in_array('punch_mode', $config_array);
 
 129       $this->allow_overlap = in_array('allow_overlap', $config_array);
 
 130       $this->future_entries = in_array('future_entries', $config_array);
 
 131       $this->uncompleted_indicators = in_array('uncompleted_indicators', $config_array);
 
 133       // Set "on behalf" id and name.
 
 134       if (isset($_SESSION['behalf_id'])) {
 
 135           $this->behalf_id = $_SESSION['behalf_id'];
 
 136           $this->behalf_name = $_SESSION['behalf_name'];
 
 141   // The getActiveUser returns user id on behalf of whom the current user is operating.
 
 142   function getActiveUser() {
 
 143     return ($this->behalf_id ? $this->behalf_id : $this->id);
 
 146   // can - determines whether user has a right to do something.
 
 147   function can($do_something) {
 
 148     return in_array($do_something, $this->rights);
 
 151   // isAdmin - determines whether current user is admin (has right_administer_site).
 
 153     return $this->can('administer_site');
 
 156   // isManager - determines whether current user is team manager.
 
 157   // This is a legacy function that we are getting rid of by replacing with rights check.
 
 158   function isManager() {
 
 159     return $this->can('export_data'); // By default this is assigned to managers but not co-managers.
 
 160                                       // Which is sufficient for now until we refactor all calls
 
 161                                       // to this function and then remove it.
 
 164   // isCoManager - determines whether current user is team comanager.
 
 165   // This is a legacy function that we are getting rid of by replacing with rights check.
 
 166   function isCoManager() {
 
 167     return ($this->can('manage_users') && !$this->can('export_data'));
 
 170   // isClient - determines whether current user is a client.
 
 171   function isClient() {
 
 172     return $this->is_client;
 
 175   // canManageTeam - determines whether current user is manager or co-manager.
 
 176   // This is a legacy function that we are getting rid of by replacing with rights check.
 
 177   function canManageTeam() {
 
 178     return $this->can('manage_users'); // By default this is assigned to co-managers (an managers).
 
 179                                        // Which is sufficient for now until we refactor all calls
 
 180                                        // to this function and then remove it.
 
 183   // isPluginEnabled checks whether a plugin is enabled for user.
 
 184   function isPluginEnabled($plugin)
 
 186     return in_array($plugin, explode(',', $this->plugins));
 
 189   // getAssignedProjects - returns an array of assigned projects.
 
 190   function getAssignedProjects()
 
 193     $mdb2 = getConnection();
 
 195     // Do a query with inner join to get assigned projects.
 
 196     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
 
 197       inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
 
 198       where p.group_id = $this->group_id and p.status = 1 order by p.name";
 
 199     $res = $mdb2->query($sql);
 
 200     if (!is_a($res, 'PEAR_Error')) {
 
 201       while ($val = $res->fetchRow()) {
 
 208   // getAssignedTasks - returns an array of assigned tasks.
 
 209   function getAssignedTasks()
 
 211     // Start with projects;
 
 212     $projects = $this->getAssignedProjects();
 
 213     if (!$projects) return false;
 
 215     // Build an array of task ids.
 
 217     foreach($projects as $project) {
 
 218       $one_project_tasks = $project['tasks'] ? explode(',', $project['tasks']) : array();
 
 219       $task_ids = array_unique(array_merge($task_ids, $one_project_tasks));
 
 221     if (!$task_ids) return false;
 
 223     // Get task descriptions.
 
 225     $mdb2 = getConnection();
 
 226     $tasks = implode(',', $task_ids); // This is a comma-separated list of task ids.
 
 228     $sql = "select id, name, description from tt_tasks".
 
 229       " where group_id = $this->group_id and status = 1 and id in ($tasks) order by name";
 
 230     $res = $mdb2->query($sql);
 
 231     if (!is_a($res, 'PEAR_Error')) {
 
 232       while ($val = $res->fetchRow()) {
 
 239   // getAssignedClients - returns an array of clients assigned to own projects.
 
 240   function getAssignedClients()
 
 242     // Start with projects;
 
 243     $projects = $this->getAssignedProjects();
 
 244     if (!$projects) return false;
 
 245     $assigned_project_ids = array();
 
 246     foreach($projects as $project) {
 
 247       $assigned_project_ids[] = $project['id'];
 
 250     $mdb2 = getConnection();
 
 252     // Get active clients for group.
 
 254     $sql = "select id, name, address, projects from tt_clients where group_id = $this->group_id and status = 1";
 
 255     $res = $mdb2->query($sql);
 
 256     if (!is_a($res, 'PEAR_Error')) {
 
 257       while ($val = $res->fetchRow()) {
 
 258         $client_project_ids = $val['projects'] ? explode(',', $val['projects']) : array();
 
 259         if (array_intersect($assigned_project_ids, $client_project_ids))
 
 260           $clients[] = $val; // Add client if one of user projects is a client project, too.
 
 266   // isDateLocked checks whether a specifc date is locked for modifications.
 
 267   function isDateLocked($date)
 
 269     if (!$this->isPluginEnabled('lk'))
 
 270       return false; // Locking feature is disabled.
 
 272     if (!$this->lock_spec)
 
 273       return false; // There is no lock specification.
 
 275     if (!$this->behalf_id && $this->can('override_own_date_lock'))
 
 276       return false; // User is working as self and can override own date lock.
 
 278     if ($this->behalf_id && $this->can('override_date_lock'))
 
 279       return false; // User is working on behalf of someone else and can override date lock.
 
 281     require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
 
 282     require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
 
 284     // Calculate the last occurrence of a lock.
 
 285     $last = tdCron::getLastOccurrence($this->lock_spec, time());
 
 286     $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
 
 287     if ($date->before($lockdate))
 
 293   // canOverridePunchMode checks whether a user can override punch mode in a situation.
 
 294   function canOverridePunchMode()
 
 296     if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
 
 297       return false; // User is working as self and cannot override for self.
 
 299     if ($this->behalf_id && !$this->can('override_punch_mode'))
 
 300       return false; // User is working on behalf of someone else and cannot override.
 
 305   // getUsers obtains users in a group, as specififed by options.
 
 306   function getUsers($options) {
 
 308     $mdb2 = getConnection();
 
 310     $skipClients = !isset($options['include_clients']);
 
 311     $includeSelf = isset($options['include_self']);
 
 313     $select_part = 'select u.id, u.name';
 
 314     if (isset($options['include_login'])) $select_part .= ', u.login';
 
 315     if (!isset($options['include_clients'])) $select_part .= ', r.rights';
 
 316     if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
 
 318     $from_part = ' from tt_users u';
 
 321     if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
 
 322         $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
 
 324     $where_part = " where u.group_id = $this->group_id";
 
 325     if (isset($options['status']))
 
 326       $where_part .= ' and u.status = '.(int)$options['status'];
 
 328       $where_part .= ' and u.status is not null';
 
 330       $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
 
 332       if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
 
 335     $order_part = " order by upper(u.name)";
 
 337     $sql = $select_part.$from_part.$left_joins.$where_part.$order_part;
 
 338     $res = $mdb2->query($sql);
 
 339     $user_list = array();
 
 340     if (is_a($res, 'PEAR_Error'))
 
 343     while ($val = $res->fetchRow()) {
 
 345         $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
 
 347           continue; // Skip adding clients.
 
 352     if (isset($options['self_first'])) {
 
 353       // Put own entry at the front.
 
 354       $cnt = count($user_list);
 
 355       for($i = 0; $i < $cnt; $i++) {
 
 356         if ($user_list[$i]['id'] == $this->id) {
 
 357           $self = $user_list[$i]; // Found self.
 
 358           array_unshift($user_list, $self); // Put own entry at the front.
 
 359           array_splice($user_list, $i+1, 1); // Remove duplicate.
 
 366   // getUser function is used to manage users in group and returns user details.
 
 367   // At the moment, the function is used for user edits and deletes.
 
 368   function getUser($user_id) {
 
 369     if (!$this->can('manage_users')) return false;
 
 371     $mdb2 = getConnection();
 
 373     $sql =  "select u.id, u.name, u.login, u.role_id, u.status, u.rate, u.email from tt_users u".
 
 374             " left join tt_roles r on (u.role_id = r.id)".
 
 375             " where u.id = $user_id and u.group_id = $this->group_id and u.status is not null".
 
 376             " and (r.rank < $this->rank or (r.rank = $this->rank and u.id = $this->id))"; // Users with lesser roles or self.
 
 377     $res = $mdb2->query($sql);
 
 378     if (!is_a($res, 'PEAR_Error')) {
 
 379       $val = $res->fetchRow();
 
 385   // checkBehalfId checks whether behalf_id is appropriate.
 
 386   // On behalf user must be active and have lower rank.
 
 387   function checkBehalfId() {
 
 388     $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
 
 389     $users = $this->getUsers($options);
 
 390     foreach($users as $one_user) {
 
 391       if ($one_user['id'] == $this->behalf_id)
 
 397   // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
 
 400   // Needed for situations when user does not have do_own_something right.
 
 401   // Example: has view_charts but does not have view_own_charts.
 
 402   // In this case we still allow access to charts, but set behalf_id to someone else.
 
 403   function adjustBehalfId() {
 
 404     $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
 
 405     $users = $this->getUsers($options);
 
 406     foreach($users as $one_user) {
 
 407       // Fake loop to access first element.
 
 408       $this->behalf_id = $one_user['id'];
 
 409       $this->behalf_name = $one_user['name'];
 
 410       $_SESSION['behalf_id'] = $this->behalf_id;
 
 411       $_SESSION['behalf_name'] = $this->behalf_name;