summaryrefslogtreecommitdiffstats
path: root/digest.php
blob: c93117af9d376844f80436aa31b11171a11cf7bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
error_reporting(E_ALL);

include_once("config.php");                /* needs to be first in list, since other includes use this */
include_once("./include/output.php");      /* html output only */
include_once("./include/db.php");          /* database only */
include_once("./include/functions.php");   /* the rest */

/* make sure that user has set all variables in config.php */
config_check();

/* open the database */
if(DB_open()<0)
  exit();

/* only callable via cron or CLI */
if(isset($_SERVER['REMOTE_ADDR']))
  exit();

/* get userid for users that have digest set != digest-off */
$users = DB_get_digest_users();

global $defaulttimezone;
foreach($users as $uid)
  {
    // get local time

    $zone  = DB_get_user_timezone($uid);
    date_default_timezone_set($zone);
    $time = (int)(date("H"));

    // calculate mod by digest-time
    $PREF = DB_get_PREF($uid);
    switch($PREF['digest'])
      {
      case 'digest-off':
	break;
      case 'digest-1h':
	$every = 1;
	break;
      case 'digest-2h':
	$every = 2;
	break;
      case 'digest-3h':
	$every = 3;
	break;
      case 'digest-4h':
	$every = 4;
	break;
      case 'digest-6h':
	$every = 6;
	break;
      case 'digest-12h':
	$every = 12;
	break;
      case 'digest-24h':
	$every = 24;
	break;
      }

    // make default time to send email 18:00
    if( ( ($time-18) % $every) == 0 )
      {
	$email = DB_get_email('userid',$uid);

	// get messages
	$messages = DB_get_digest_message_by_email($email);

	// add them together
	if(sizeof($messages))
	  {
	    $text = array();
	    $i=0;
	    foreach($messages as $mess )
	      {
		$text[$i] = $mess[1]."\n++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
		$i++;
	      }

	    $text = implode("\n",$text);

	    // add header, footer, sent out
	    $name    = DB_get_name('userid',$uid);
	    $header  = "Hello $name\n\nHere is a digest of the latest emails:\n\n";

	    $footer  = "\nHave a nice day\n".
	      "   your E-Doko digester\n\n".
	      "-- \n".
	      "You can change your mail delivery mode in the preference menu.\n".
	      'web: http://doko.nubati.net   '.
	      'help: http://wiki.nubati.net/EmailDoko   '.
	      'bugs: http://wiki.nubati.net/EmailDokoIssues';

	    $subject = "$EmailName Digest";

	    sendmail($email,$subject,$header.$text.$footer);
	  }

	// delete all messages
	foreach($messages as $mess )
	  DB_digest_delete_message($mess[0]);
      }
  } /* end foreach users */
?>