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  * cron.php - this file is an entry point to execute scheduled jobs in Time Tracker.
 
  31  * It must be called externally (for example, from the system cron or task scheduler).
 
  33  * Internally, we store scheduled jobs in tt_cron table in db. The cron_spec field is in cron format.
 
  34  * Along with it, we store last and next timestamps for jobs, we use them as an execute condition.
 
  36  * Although cron_spec follows 5-field cron specification precisely, actual job timing depends on
 
  37  * how often cron.php is called. For example, an hourly ping will execute jobs no more than once
 
  38  * each hour, even if they are due more often. Configure whatever calls this file accordingly.
 
  41 require_once('initialize.php');
 
  42 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
 
  43 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
 
  44 import('ttFavReportHelper');
 
  45 import('ttReportHelper');
 
  47 $mdb2 = getConnection();
 
  50  $sql = "select c.id, c.cron_spec, c.report_id, c.email, c.report_condition from tt_cron c
 
  51    left join tt_fav_reports fr on (c.report_id = fr.id)
 
  52    where $now >= c.next and fr.status = 1
 
  53    and c.status = 1 and c.report_id is not null and c.email is not null";
 
  54 $res = $mdb2->query($sql);
 
  55 if (is_a($res, 'PEAR_Error'))
 
  58 while ($val = $res->fetchRow()) {
 
  59   // We have jobs to execute in user language.
 
  61   // Get favorite report details.
 
  62   $report = ttFavReportHelper::getReport($val['report_id']);
 
  63   if (!$report) continue; // Skip not found report.
 
  65   // Recycle global $user object, as user settings are specific for each report.
 
  66   $user = new ttUser(null, $report['user_id']);
 
  67   if (!$user->id) continue; // Skip not found user.
 
  68   // Recycle $i18n object because language is user-specific.
 
  69   $i18n->load($user->lang);
 
  71   // Check condition on a report.
 
  73   if ($val['report_condition'])
 
  74     $condition_ok = ttReportHelper::checkFavReportCondition($report, $val['report_condition']);
 
  76   // Email report if condition is okay.
 
  78     if (ttReportHelper::sendFavReport($report, $val['email']))
 
  79       echo "Report ".$val['report_id']. " sent to ".$val['email']."<br>";
 
  81       echo "Error while emailing report...<br>";
 
  84   // Calculate next execution time.
 
  85   $next = tdCron::getNextOccurrence($val['cron_spec'], $now + 60); // +60 sec is here to get us correct $next when $now is close to existing "next".
 
  86                                                                    // This is because the accuracy of tdcron class appears to be 1 minute.
 
  88   // Update last and next values in tt_cron.
 
  89   $sql = "update tt_cron set last = $now, next = $next where id = ".$val['id'];
 
  90   $affected = $mdb2->exec($sql);
 
  91   if (is_a($affected, 'PEAR_Error')) continue;