Issues (404)

scripts/alertgonemps.php (3 issues)

1
<?php
2
/*
3
 * Name: alertgonemps.php
4
 * Description: Mailer for those whose MP has gone
5
 * $Id: alertgonemps.php,v 1.2 2009-06-16 09:12:09 matthew Exp $
6
 */
7
8
function mlog($message) {
9
    print $message;
10
}
11
12
include_once '../www/includes/easyparliament/init.php';
13
ini_set('memory_limit', -1);
14
include_once INCLUDESPATH . 'easyparliament/member.php';
15
16
$global_start = getmicrotime();
17
$db = new ParlDB();
18
19
$nomail = false;
20
$onlyemail = '';
21
$fromemail = '';
22
$fromflag = false;
23
$toemail = '';
24
$template = 'alert_gone';
25
for ($k = 1; $k < $argc; $k++) {
26
    if ($argv[$k] == '--nomail') {
27
        $nomail = true;
28
    }
29
    if (preg_match('#^--only=(.*)$#', $argv[$k], $m)) {
30
        $onlyemail = $m[1];
31
    }
32
    if (preg_match('#^--from=(.*)$#', $argv[$k], $m)) {
33
        $fromemail = $m[1];
34
    }
35
    if (preg_match('#^--to=(.*)$#', $argv[$k], $m)) {
36
        $toemail = $m[1];
37
    }
38
    if (preg_match('#^--template=(.*)$#', $argv[$k], $m)) {
39
        $template = $m[1];
40
        # Tee hee
41
        $template = "../../../../../../../../../../home/twfy-live/email-alert-templates/alert_mailout_$template";
42
    }
43
}
44
45
#if (DEVSITE)
46
#	$nomail = true;
47
48
if ($nomail) {
49
    mlog("NOT SENDING EMAIL\n");
50
}
51
if (($fromemail && $onlyemail) || ($toemail && $onlyemail)) {
52
    mlog("Can't have both from/to and only!\n");
53
    exit;
54
}
55
56
$active = 0;
57
$queries = 0;
58
$unregistered = 0;
59
$registered = 0;
60
$sentemails = 0;
61
62
$LIVEALERTS = new ALERT();
63
64
$current = ['email' => '', 'token' => ''];
65
$email_text = [];
66
$globalsuccess = 1;
67
68
# Fetch all confirmed, non-deleted alerts
69
$confirmed = 1;
70
$deleted = 0;
71
$alertdata = $LIVEALERTS->fetch($confirmed, $deleted);
72
$alertdata = $alertdata['data'];
73
74
$outof = count($alertdata);
75
$members = [];
76
$start_time = time();
77
foreach ($alertdata as $alertitem) {
78
    $active++;
79
    $email = $alertitem['email'];
80
    if ($onlyemail && $email != $onlyemail) {
81
        continue;
82
    }
83
    if ($fromemail && strtolower($email) == $fromemail) {
84
        $fromflag = true;
85
    }
86
    if ($fromemail && !$fromflag) {
87
        continue;
88
    }
89
    if ($toemail && strtolower($email) >= $toemail) {
90
        continue;
91
    }
92
    $criteria_raw = $alertitem['criteria'];
93
94
    if (!strstr($criteria_raw, 'speaker:')) {
95
        continue;
96
    }
97
98
    preg_match('#speaker:(\d+)#', $criteria_raw, $m);
99
    $person_id = $m[1];
100
    if (!isset($members[$person_id])) {
101
        $queries++;
102
        $members[$person_id] = new MEMBER(['person_id' => $person_id]);
103
    }
104
    $member = $members[$person_id];
105
    if ($member->current_member_anywhere()) {
106
        continue;
107
    }
108
109
    if ($email != $current['email']) {
110
        if ($email_text) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $email_text of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
111
            write_and_send_email($current, $email_text, $template);
0 ignored issues
show
The call to write_and_send_email() has too few arguments starting with template. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
            /** @scrutinizer ignore-call */ 
112
            write_and_send_email($current, $email_text, $template);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
112
        }
113
        $current['email'] = $email;
114
        $current['token'] = $alertitem['alert_id'] . '-' . $alertitem['registrationtoken'];
115
        $email_text = [];
116
        $q = $db->query('SELECT user_id FROM users WHERE email = :email', [
117
            ':email' => $email])->first();
118
        if ($q) {
119
            $user_id = $q['user_id'];
120
            $registered++;
121
        } else {
122
            $user_id = 0;
123
            $unregistered++;
124
        }
125
        mlog("\nEMAIL: $email, uid $user_id; memory usage : " . memory_get_usage() . "\n");
126
    }
127
128
    $lh = $member->left_house();
129
    $lh = array_shift($lh);
130
    $text = '* ' . $member->full_name() . ', left ' . $lh['date_pretty'];
131
    if (!in_array($text, $email_text)) {
132
        $email_text[] = $text;
133
    }
134
}
135
if ($email_text) {
0 ignored issues
show
$email_text is of type array|string[], thus it always evaluated to false.
Loading history...
136
    write_and_send_email($current, $email_text, $template);
137
}
138
139
mlog("\n");
140
141
$sss = "Active alerts: $active\nEmail lookups: $registered registered, $unregistered unregistered\nQuery lookups: $queries\nSent emails: $sentemails\n";
142
if ($globalsuccess) {
143
    $sss .= 'Everything went swimmingly, in ';
144
} else {
145
    $sss .= 'Something went wrong! Total time: ';
146
}
147
$sss .= (getmicrotime() - $global_start) . "\n\n";
148
mlog($sss);
149
mlog(date('r') . "\n");
150
151
function write_and_send_email($current, $data, $template) {
152
    global $globalsuccess, $sentemails, $nomail, $start_time;
153
154
    $sentemails++;
155
    mlog("SEND $sentemails : Sending email to $current[email] ... ");
156
    $d = ['to' => $current['email'], 'template' => $template];
157
    $m = [
158
        'DATA' => join("\n", $data),
159
        'MANAGE' => 'https://www.theyworkforyou.com/D/' . $current['token'],
160
        'ALERT_IS' => count($data) == 1 ? 'alert is' : 'alerts are',
161
        'ALERTS' => count($data) == 1 ? 'an alert' : 'some alerts',
162
    ];
163
    if (!$nomail) {
164
        $success = send_template_email($d, $m, true);
165
        mlog("sent ... ");
166
        # sleep if time between sending mails is less than a certain number of seconds on average
167
        if (((time() - $start_time) / $sentemails) < 0.5) { # number of seconds per mail not to be quicker than
168
            mlog("pausing ... ");
169
            sleep(1);
170
        }
171
    } else {
172
        mlog(join('', $data));
173
        $success = 1;
174
    }
175
    mlog("done\n");
176
    if (!$success) {
177
        $globalsuccess = 0;
178
    }
179
}
180