Merge branch 'master' of /home/arun/nubati.net/git/e-DoKo
[e-DoKo.git] / digest.php
1 <?php
2 /* Copyright 2006, 2007, 2008, 2009, 2010 Arun Persaud <arun@nubati.net>
3  *
4  *   This file is part of e-DoKo.
5  *
6  *   e-DoKo is free software: you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, either version 3 of the License, or
9  *   (at your option) any later version.
10  *
11  *   e-DoKo is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with e-DoKo.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 error_reporting(E_ALL);
22
23 include_once("config.php");                /* needs to be first in list, since other includes use this */
24 include_once("./include/output.php");      /* html output only */
25 include_once("./include/db.php");          /* database only */
26 include_once("./include/functions.php");   /* the rest */
27
28 /* make sure that user has set all variables in config.php */
29 config_check();
30
31 /* open the database */
32 if(DB_open()<0)
33   exit();
34
35 /* only callable via cron or CLI */
36 if(isset($_SERVER['REMOTE_ADDR']))
37   exit();
38
39 /* get userid for users that have digest set != digest-off */
40 $users = DB_get_digest_users();
41
42 global $defaulttimezone;
43 foreach($users as $uid)
44   {
45     // get local time
46
47     $zone  = DB_get_user_timezone($uid);
48     date_default_timezone_set($zone);
49     $time = (int)(date("H"));
50
51     // calculate mod by digest-time
52     $PREF = DB_get_PREF($uid);
53     switch($PREF['digest'])
54       {
55       case 'digest-off':
56         break;
57       case 'digest-1h':
58         $every = 1;
59         break;
60       case 'digest-2h':
61         $every = 2;
62         break;
63       case 'digest-3h':
64         $every = 3;
65         break;
66       case 'digest-4h':
67         $every = 4;
68         break;
69       case 'digest-6h':
70         $every = 6;
71         break;
72       case 'digest-12h':
73         $every = 12;
74         break;
75       case 'digest-24h':
76         $every = 24;
77         break;
78       }
79
80     // make default time to send email 18:00
81     if( ( ($time-18) % $every) == 0 )
82       {
83         $email = DB_get_email('userid',$uid);
84
85         // get messages
86         $messages = DB_get_digest_message_by_email($email);
87
88         // add them together
89         if(sizeof($messages))
90           {
91             $text = array();
92             $i=0;
93             foreach($messages as $mess )
94               {
95                 $text[$i] = $mess[1]."\n++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
96                 $i++;
97               }
98
99             $text = implode("\n",$text);
100
101             // add header, footer, sent out
102             $name    = DB_get_name('userid',$uid);
103             $header  = "Hello $name\n\nHere is a digest of the latest emails:\n\n";
104
105             $footer  = "\nHave a nice day\n".
106               "   your E-Doko digester\n\n".
107               "-- \n".
108               "You can change your mail delivery mode in the preference menu.\n".
109               'web: http://doko.nubati.net   '.
110               'help: http://wiki.nubati.net/EmailDoko   '.
111               'bugs: http://wiki.nubati.net/EmailDokoIssues';
112
113             $subject = "$EmailName Digest";
114
115             sendmail($email,$subject,$header.$text.$footer);
116           }
117
118         // delete all messages
119         foreach($messages as $mess )
120           DB_digest_delete_message($mess[0]);
121       }
122   } /* end foreach users */
123 ?>