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('ttRoleHelper');
 
  31 // ttCronJobHelper class will be used to execute scheduled jobs in Time Tracker.
 
  33 // This is work in progress, NOT FINISHED.
 
  35 // Current complication is with global $user object and a difficulty to achieve
 
  36 // separation between user-specific tasks that utilize $user object and
 
  37 // cron jobs, that must work for ALL users. Specifically, report body building
 
  38 // heavily relies on $user.
 
  40 // An instance will be created in cron.php by an external call to it.
 
  41 // Then the exec method is called to execute all jobs that are due.
 
  42 class ttCronJobHelper {
 
  44   var $reports = array(); // A list of reports to email.
 
  46   // Constructor. Constructs a list of reports to send.
 
  47   function __construct() {
 
  48     $mdb2 = getConnection();
 
  51     // Note: review security implications each time this query is revised.
 
  53     //   1) There is no logged on user to reduce scope.
 
  54     //   2) Query may be executed by anonymous http get (of cron.php).
 
  56     // Therefore, in query make sure we obtain only relevant info.
 
  57     $sql = "select c.id, c.cron_spec, c.report_id, c.email, c.cc, c.subject, c.report_condition from tt_cron c".
 
  58       " inner join tt_fav_reports fr on".
 
  59       " (c.report_id = fr.id and c.group_id = fr.group_id and c.org_id = fr.org_id)". // Report for a correct group.
 
  60       " inner join tt_users u on (u.id = fr.user_id and u.status = 1)". // Report for an active user.
 
  61       " where $now >= c.next and fr.status = 1". // Due now.
 
  62       " and c.status = 1 and c.report_id is not null and c.email is not null";
 
  63     $res = $mdb2->query($sql);
 
  64     if (is_a($res, 'PEAR_Error')) return;
 
  65     while ($val = $res->fetchRow()) {
 
  66       $this->reports[] = $val;
 
  70   // exec - processes reports one at a time by sending each out.
 
  72     foreach ($this->reports as $report) {
 
  73       // Get favorite report details.
 
  74       $options = $this->getReportOptions($report['report_id']);
 
  75       if (!$options) continue; // Skip not found report.
 
  77       // Recycle global $user object, as user settings are specific for each report.
 
  79       $user = new ttUser(null, $options['user_id']);
 
  80       if (!$user->id) continue; // Skip not found user.
 
  82       // Avoid complications with impersonated users, possibly from subgroups.
 
  83       // Note: this may happen when cron.php is called by a browser who already impersonates.
 
  84       // This is not supposed to happen in automatic cron job.
 
  86         continue; // Skip processing on behalf situations entirely.
 
  88       // TODO: coding ongoing here. Not finished. Add other processing when ready.
 
  93   // getReportOptions - returns an array of fav report options from database data.
 
  94   private function getReportOptions($id) {
 
  95     $mdb2 = getConnection();
 
  97     $sql = "select * from tt_fav_reports where id = $id and status = 1";
 
  98     $res = $mdb2->query($sql);
 
  99     if (is_a(res, 'PEAR_Error')) return false;
 
 101     $val = $res->fetchRow();
 
 102     if (!$val) return false;
 
 104     // Drop things we don't need.
 
 106     unset($val['report_spec']);
 
 107     unset($val['status']);