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.cc, c.subject, 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 $options = ttFavReportHelper::getReportOptions($val['report_id']);
63 if (!$options) continue; // Skip not found report.
65 // Recycle global $user object, as user settings are specific for each report.
66 $user = new ttUser(null, $options['user_id']);
67 if (!$user->id) continue; // Skip not found user.
69 // TODO: write a new function ttFavReportHelper::adjustOptions that will use
70 // a $user object recycled above. Put user handling below into it.
71 // Also adjust remaining options for potentially changed user access rights and group properties.
72 // For example, tracking mode may have changed, but fav report options are still old...
73 // This needs to be fixed.
74 $options = ttFavReportHelper::adjustOptions($options);
76 // Skip users with disabled Notifications plugin.
77 if (!$user->isPluginEnabled('no')) continue;
79 // Recycle $i18n object because language is user-specific.
80 $i18n->load($user->lang);
82 // Check condition on a report.
84 if ($val['report_condition'])
85 $condition_ok = ttReportHelper::checkFavReportCondition($options, $val['report_condition']);
87 // Email report if condition is okay.
89 if (ttReportHelper::sendFavReport($options, $val['subject'], $val['email'], $val['cc']))
90 echo "Report ".$val['report_id']. " sent.<br>";
92 echo "Error while emailing report...<br>";
95 // Calculate next execution time.
96 $next = tdCron::getNextOccurrence($val['cron_spec'], $now + 60); // +60 sec is here to get us correct $next when $now is close to existing "next".
97 // This is because the accuracy of tdcron class appears to be 1 minute.
99 // Update last and next values in tt_cron.
100 $sql = "update tt_cron set last = $now, next = $next where id = ".$val['id'];
101 $affected = $mdb2->exec($sql);
102 if (is_a($affected, 'PEAR_Error')) continue;