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 * from tt_cron where $now >= next 
 
  51   and status = 1 and report_id is not null and email is not null";
 
  52 $res = $mdb2->query($sql);
 
  53 if (is_a($res, 'PEAR_Error'))
 
  56 while ($val = $res->fetchRow()) {
 
  57   // We have jobs to execute in user language.
 
  59   // Get favorite report details.
 
  60   $report = ttFavReportHelper::getReport($val['report_id']);
 
  61   if (!$report) continue;
 
  63   // Recycle global $user and $i18n objects, as user settings and language are specific for each report.
 
  64   $user = new ttUser(null, $report['user_id']);
 
  65   $i18n->load($user->lang);
 
  67   // Check condition on a report.
 
  69   if ($val['report_condition'])
 
  70     $condition_ok = ttReportHelper::checkFavReportCondition($report, $val['report_condition']);
 
  72   // Email report if condition is okay.
 
  74     if (ttReportHelper::sendFavReport($report, $val['email']))
 
  75       echo "Report ".$val['report_id']. " sent to ".$val['email']."<br>";
 
  77       echo "Error while emailing report...<br>";
 
  80   // Calculate next execution time.
 
  81   $next = tdCron::getNextOccurrence($val['cron_spec'], $now + 60); // +60 sec is here to get us correct $next when $now is close to existing "next".
 
  82                                                                    // This is because the accuracy of tdcron class appears to be 1 minute.
 
  84   // Update last and next values in tt_cron.
 
  85   $sql = "update tt_cron set last = $now, next = $next where id = ".$val['id'];
 
  86   $affected = $mdb2->exec($sql);
 
  87   if (is_a($affected, 'PEAR_Error')) continue;