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 // Class ttOrgHelper contains helper functions that operate with organizations.
 
  30 // Organizations are collections of nested groups of users.
 
  33   // The getOrgs function returns an array of all active organizations on the server.
 
  34   static function getOrgs() {
 
  36     $mdb2 = getConnection();
 
  38     $sql =  "select id, name, created, lang from tt_groups".
 
  39             " where status = 1 and org_id = id and parent_id is NULL order by id desc";
 
  40     $res = $mdb2->query($sql);
 
  42     if (!is_a($res, 'PEAR_Error')) {
 
  43       while ($val = $res->fetchRow()) {
 
  44         $val['date'] = substr($val['created'], 0, 10); // Strip the time.
 
  52   // The getName function returns organization name (which is a name of its top group).
 
  53   static function getName($org_id) {
 
  54     $mdb2 = getConnection();
 
  56     $sql = "select name from tt_groups where id = $org_id and (status = 1 or status = 0) and parent_id is NULL";
 
  57     $res = $mdb2->query($sql);
 
  59     if (!is_a($res, 'PEAR_Error')) {
 
  60       $val = $res->fetchRow();
 
  66   // The getInactiveOrgs is a maintenance function that returns an array of inactive organization ids (max 25 for now).
 
  67   static function getInactiveOrgs() {
 
  68     $inactive_orgs = array();
 
  69     $mdb2 = getConnection();
 
  71     // Determine inactive organizations by querying the database for max access timestamp for its users.
 
  72     $cutoff_timestamp = $mdb2->quote(date('Y-m-d', strtotime('-1 year')));
 
  73     $sql = "select org_id from".
 
  74       " (select max(accessed) as last_access, org_id from tt_users where org_id > 0 group by org_id order by last_access, org_id) as t".
 
  75       " where last_access is null or last_access < $cutoff_timestamp limit 25"; // Max 25 orgs at a time for now...
 
  76     $res = $mdb2->query($sql);
 
  77     if (!is_a($res, 'PEAR_Error')) {
 
  78       while ($val = $res->fetchRow()) {
 
  79         $inactive_orgs[] = $val['org_id'];
 
  81       return $inactive_orgs;
 
  86   // deleteOrg deletes data for the entire organization from database permanently.
 
  87   static function deleteOrg($org_id) {
 
  89     // Delete all org files.
 
  90     ttOrgHelper::deleteOrgFiles($org_id);
 
  92     // Go one table at a time and remove all records with matching org_id.
 
  93     // The order is backwards to import (see ttOrgImportHelper). Remove groups last.
 
  94     // This leaves us with something partially working if an error occurs.
 
  95     $mdb2 = getConnection();
 
 103       'tt_predefined_expenses',
 
 105       'tt_custom_field_log',
 
 106       'tt_custom_field_options',
 
 111       'tt_user_project_binds',
 
 113       'tt_client_project_binds',
 
 115       'tt_project_task_binds',
 
 121     foreach($tables as $table) {
 
 122       $sql = "delete from $table where org_id = $org_id";
 
 123       $affected = $mdb2->exec($sql);
 
 124       if (is_a($affected, 'PEAR_Error')) return false;
 
 128   // deleteOrgFiles deletes files attached to all entities in the entire organization.
 
 129   static function deleteOrgFiles($org_id) {
 
 131     // Delete all org files from the database.
 
 132     $mdb2 = getConnection();
 
 133     $sql = "delete from tt_files where org_id = $org_id";
 
 134     $affected = $mdb2->exec($sql);
 
 135     if (is_a($affected, 'PEAR_Error'))
 
 138     if ($affected == 0) return true; // Do not call file storage utility.
 
 140     // Try to make a call to file storage facility.
 
 141     if (!defined('FILE_STORAGE_URI')) return true; // Nothing to do.
 
 143     $deleteorgfiles_uri = FILE_STORAGE_URI.'deleteorgfiles';
 
 146     $sql = "select param_value as site_id from tt_site_config where param_name = 'locker_id'";
 
 147     $res = $mdb2->query($sql);
 
 148     $val = $res->fetchRow();
 
 149     $site_id = $val['site_id'];
 
 150     if (!$site_id) return true; // Nothing to do.
 
 153     $sql = "select param_value as site_key from tt_site_config where param_name = 'locker_key'";
 
 154     $res = $mdb2->query($sql);
 
 155     $val = $res->fetchRow();
 
 156     $site_key = $val['site_key'];
 
 157     if (!$site_key) return true; // Can't continue without site key.
 
 160     $sql = "select group_key as org_key from tt_groups where id = $org_id";
 
 161     $res = $mdb2->query($sql);
 
 162     $val = $res->fetchRow();
 
 163     $org_key = $val['org_key'];
 
 164     if (!$site_key) return true; // Can't continue without org key.
 
 166     $curl_fields = array('site_id' => $site_id,
 
 167       'site_key' => $site_key,
 
 169       'org_key' => $org_key);
 
 171     // url-ify the data for the POST.
 
 172     foreach($curl_fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
 
 173     $fields_string = rtrim($fields_string, '&');
 
 178     // Set the url, number of POST vars, POST data.
 
 179     curl_setopt($ch, CURLOPT_URL, $deleteorgfiles_uri);
 
 180     curl_setopt($ch, CURLOPT_POST, true);
 
 181     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
 
 182     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
 184     // Execute a post request.
 
 185     $result = curl_exec($ch);
 
 190     // Many things can go wrong with a remote call to file storage facility.
 
 191     // By design, we ignore such errors.