make list of tricks and what game and session we are more compact.
[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 $WIKI;
43
44 global $defaulttimezone;
45 foreach($users as $uid)
46   {
47     // get local time
48
49     $zone  = DB_get_user_timezone($uid);
50     date_default_timezone_set($zone);
51     $time = (int)(date("H"));
52
53     // calculate mod by digest-time
54     $PREF = DB_get_PREF($uid);
55     switch($PREF['digest'])
56       {
57       case 'digest-off':
58         break;
59       case 'digest-1h':
60         $every = 1;
61         break;
62       case 'digest-2h':
63         $every = 2;
64         break;
65       case 'digest-3h':
66         $every = 3;
67         break;
68       case 'digest-4h':
69         $every = 4;
70         break;
71       case 'digest-6h':
72         $every = 6;
73         break;
74       case 'digest-12h':
75         $every = 12;
76         break;
77       case 'digest-24h':
78         $every = 24;
79         break;
80       }
81
82     // make default time to send email 18:00
83     if( ( ($time-18) % $every) == 0 )
84       {
85         $email = DB_get_email('userid',$uid);
86
87         // get messages
88         $messages = DB_get_digest_message_by_email($email);
89
90         // add them together
91         if(sizeof($messages))
92           {
93             $text = array();
94             $i=0;
95             foreach($messages as $mess )
96               {
97                 $text[$i] = $mess[1]."\n++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
98                 $i++;
99               }
100
101             $text = implode("\n",$text);
102
103             // add header, footer, sent out
104             $name    = DB_get_name('userid',$uid);
105             $header  = "Hello $name\n\nHere is a digest of the latest emails:\n\n";
106
107             $footer  = "\nHave a nice day\n".
108               "   your E-Doko digester\n\n".
109               "-- \n".
110               "You can change your mail delivery mode in the preference menu.\n".
111               'web: http://doko.nubati.net   '.
112               "help, bugs, etc.: $WIKI";
113
114             $subject = "$EmailName Digest";
115
116             sendmail($email,$subject,$header.$text.$footer);
117           }
118
119         // delete all messages
120         foreach($messages as $mess )
121           DB_digest_delete_message($mess[0]);
122       }
123   } /* end foreach users */
124 ?>