posaune
[timetracker.git] / cron.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 /*
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).
32  * 
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.
35  * 
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.
39  */
40
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');
46
47 $mdb2 = getConnection();
48 $now = time();
49
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   " inner join tt_fav_reports fr on".
52   " (c.report_id = fr.id and c.group_id = fr.group_id and c.org_id = fr.org_id)". // Report for a correct group.
53   " inner join tt_users u on (u.id = fr.user_id and u.status = 1)". // Report for an active user.
54   " where $now >= c.next and fr.status = 1". // Due now.
55   " and c.status = 1 and c.report_id is not null and c.email is not null";
56 $res = $mdb2->query($sql);
57 if (is_a($res, 'PEAR_Error'))
58   exit();
59
60 while ($val = $res->fetchRow()) {
61   // We have jobs to execute in user language.
62
63   // Get favorite report details.
64   $options = ttFavReportHelper::getReportOptions($val['report_id']);
65   if (!$options) continue; // Skip not found report.
66
67   // Recycle global $user object, as user settings are specific for each report.
68   $user = new ttUser(null, $options['user_id']);
69   if (!$user->id) continue; // Skip not found user.
70
71   // Avoid complications with impersonated users, possibly from subgroups.
72   // Note: this may happen when cron.php is called by a browser who already impersonates.
73   // This is not supposed to happen in automatic cron job.
74   if ($user->behalf_id)
75     continue; // Skip processing on behalf situations entirely.
76
77   // TODO: write a new function ttFavReportHelper::adjustOptions that will use
78   // a $user object recycled above. Put user handling below into it.
79   // Also adjust remaining options for potentially changed user access rights and group properties.
80   // For example, tracking mode may have changed, but fav report options are still old...
81   // This needs to be fixed.
82   $options = ttFavReportHelper::adjustOptions($options);
83
84   // Skip users with disabled Notifications plugin.
85   if (!$user->isPluginEnabled('no')) continue;
86
87   // Recycle $i18n object because language is user-specific.
88   $i18n->load($user->lang);
89
90   // Check condition on a report.
91   $condition_ok = true;
92   if ($val['report_condition'])
93     $condition_ok = ttReportHelper::checkFavReportCondition($options, $val['report_condition']);
94
95   // Email report if condition is okay.
96   if ($condition_ok) {
97     if (ttReportHelper::sendFavReport($options, $val['subject'], $val['email'], $val['cc']))
98       echo "Report ".$val['report_id']. " sent.<br>";
99     else
100       echo "Error while emailing report...<br>";
101   }
102
103   // Calculate next execution time.
104   $next = tdCron::getNextOccurrence($val['cron_spec'], $now + 60); // +60 sec is here to get us correct $next when $now is close to existing "next".
105                                                                    // This is because the accuracy of tdcron class appears to be 1 minute.
106   // Update last and next values in tt_cron.
107   $sql = "update tt_cron set last = $now, next = $next where id = ".$val['id'];
108   $affected = $mdb2->exec($sql);
109   if (is_a($affected, 'PEAR_Error')) continue;
110 }
111
112 echo "Done!";