/
email_queue_api.php
209 lines (172 loc) · 5.24 KB
/
email_queue_api.php
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* Email Queue API
*
* @package CoreAPI
* @subpackage EmailQueueAPI
* @copyright Copyright 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*
* @uses constant_api.php
* @uses database_api.php
* @uses error_api.php
* @uses lang_api.php
* @uses utility_api.php
*/
require_api( 'constant_inc.php' );
require_api( 'database_api.php' );
require_api( 'error_api.php' );
require_api( 'lang_api.php' );
require_api( 'utility_api.php' );
/**
* EmailData Structure Definition
*/
class EmailData {
/**
* Email address
*/
public $email = '';
/**
* Subject text
*/
public $subject = '';
/**
* Body text
*/
public $body = '';
/**
* Meta Data array
*/
public $metadata = array(
'headers' => array(),
);
/**
* Email ID
*/
public $email_id = 0;
/**
* Submitted
*/
public $submitted = '';
};
/**
* Return a copy of the bug structure with all the instvars prepared for database insertion
* @param EmailData $p_email_data Email Data structure to store.
* @return EmailData
*/
function email_queue_prepare_db( EmailData $p_email_data ) {
$p_email_data->email_id = (int)$p_email_data->email_id;
return $p_email_data;
}
/**
* Add to email queue
* @param EmailData $p_email_data Email Data structure.
* @return integer
*/
function email_queue_add( EmailData $p_email_data ) {
$t_email_data = email_queue_prepare_db( $p_email_data );
# email cannot be blank
if( is_blank( $t_email_data->email ) ) {
error_parameters( lang_get( 'email' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
# subject cannot be blank
if( is_blank( $t_email_data->subject ) ) {
error_parameters( lang_get( 'subject' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
# body cannot be blank
if( is_blank( $t_email_data->body ) ) {
error_parameters( lang_get( 'body' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
$t_email_table = db_get_table( 'email' );
$c_email = $t_email_data->email;
$c_subject = $t_email_data->subject;
$c_body = $t_email_data->body;
$c_metadata = serialize( $t_email_data->metadata );
$t_query = 'INSERT INTO ' . $t_email_table . '
( email, subject, body, submitted, metadata)
VALUES
(' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ')';
db_query_bound( $t_query, array( $c_email, $c_subject, $c_body, db_now(), $c_metadata ) );
$t_id = db_insert_id( $t_email_table, 'email_id' );
log_event( LOG_EMAIL, 'message #' . $t_id . ' queued' );
return $t_id;
}
/**
* Convert email db row to EmailData object
* @param array|false $p_row Database result row to convert.
* @return boolean|EmailData
*/
function email_queue_row_to_object( $p_row ) {
# typically this function takes as an input the result of db_fetch_array() which can be false.
if( $p_row === false ) {
return false;
}
$t_row = $p_row;
$t_row['metadata'] = unserialize( $t_row['metadata'] );
$t_email_data = new EmailData;
$t_row_keys = array_keys( $t_row );
$t_vars = get_object_vars( $t_email_data );
# Check each variable in the class
foreach( $t_vars as $t_var => $t_value ) {
# If we got a field from the DB with the same name
if( in_array( $t_var, $t_row_keys, true ) ) {
# Store that value in the object
$t_email_data->$t_var = $t_row[$t_var];
}
}
return $t_email_data;
}
/**
* Get Corresponding EmailData object
* @param integer $p_email_id An email identifier.
* @return boolean|EmailData
*/
function email_queue_get( $p_email_id ) {
$t_email_table = db_get_table( 'email' );
$t_query = 'SELECT * FROM ' . $t_email_table . ' WHERE email_id=' . db_param();
$t_result = db_query_bound( $t_query, array( $p_email_id ) );
$t_row = db_fetch_array( $t_result );
return email_queue_row_to_object( $t_row );
}
/**
* Delete entry from email queue
* @param integer $p_email_id Email queue identifier.
* @return void
*/
function email_queue_delete( $p_email_id ) {
$t_email_table = db_get_table( 'email' );
$t_query = 'DELETE FROM ' . $t_email_table . ' WHERE email_id=' . db_param();
db_query_bound( $t_query, array( $p_email_id ) );
log_event( LOG_EMAIL, 'message #' . $p_email_id . ' deleted from queue' );
}
/**
* Get array of email queue id's
* @return array
*/
function email_queue_get_ids() {
$t_email_table = db_get_table( 'email' );
$t_query = 'SELECT email_id FROM ' . $t_email_table . ' ORDER BY email_id ASC';
$t_result = db_query_bound( $t_query );
$t_ids = array();
while( ( $t_row = db_fetch_array( $t_result ) ) !== false ) {
$t_ids[] = $t_row['email_id'];
}
return $t_ids;
}