Failed attempt to write ttCronJobHelper, some improvements to cron.php.
[timetracker.git] / WEB-INF / lib / ttCronJobHelper.class.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 import('ttRoleHelper');
30
31 // ttCronJobHelper class will be used to execute scheduled jobs in Time Tracker.
32 //
33 // This is work in progress, NOT FINISHED.
34 //
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.
39 //
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 {
43
44   var $reports = array(); // A list of reports to email.
45
46   // Constructor. Constructs a list of reports to send.
47   function __construct() {
48     $mdb2 = getConnection();
49     $now = time();
50
51     // Note: review security implications each time this query is revised.
52     // Because:
53     //   1) There is no logged on user to reduce scope.
54     //   2) Query may be executed by anonymous http get (of cron.php).
55     //
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;
67     }
68   }
69
70   // exec - processes reports one at a time by sending each out.
71   function exec() {
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.
76
77       // Recycle global $user object, as user settings are specific for each report.
78       unset($user);
79       $user = new ttUser(null, $options['user_id']);
80       if (!$user->id) continue; // Skip not found user.
81
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.
85       if ($user->behalf_id)
86         continue; // Skip processing on behalf situations entirely.
87
88       // TODO: coding ongoing here. Not finished. Add other processing when ready.
89
90     }
91   }
92
93   // getReportOptions - returns an array of fav report options from database data.
94   private function getReportOptions($id) {
95     $mdb2 = getConnection();
96
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;
100
101     $val = $res->fetchRow();
102     if (!$val) return false;
103
104     // Drop things we don't need.
105     unset($val['id']);
106     unset($val['report_spec']);
107     unset($val['status']);
108
109     return $val;
110   }
111 }