Issue #10730: Use crypto_api for generating nonces and improve hashing
[mantis/radio.git] / core / email_queue_api.php
blob5eb148b83a9ff78e9dbc3f03bbda9e6fc0103f14
1 <?php
2 # MantisBT - A PHP based bugtracking system
4 # MantisBT is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
9 # MantisBT is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Email Queue API
20 * @package CoreAPI
21 * @subpackage EmailQueueAPI
22 * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
23 * @copyright Copyright (C) 2002 - 2010 MantisBT Team - mantisbt-dev@lists.sourceforge.net
24 * @link http://www.mantisbt.org
26 * @uses constant_api.php
27 * @uses database_api.php
28 * @uses error_api.php
29 * @uses lang_api.php
30 * @uses utility_api.php
33 require_api( 'constant_inc.php' );
34 require_api( 'database_api.php' );
35 require_api( 'error_api.php' );
36 require_api( 'lang_api.php' );
37 require_api( 'utility_api.php' );
39 /**
40 * EmailData Structure Definition
41 * @package MantisBT
42 * @subpackage classes
44 class EmailData {
45 // properties set during creation
46 var $email = '';
47 var $subject = '';
48 var $body = '';
49 var $metadata = array(
50 'headers' => array(),
53 // auto-populated properties
54 var $email_id = 0;
55 var $submitted = '';
58 /**
59 * Return a copy of the bug structure with all the instvars prepared for db insertion
60 * @param EmailData $p_email_data
61 * @return EmailData
63 function email_queue_prepare_db( $p_email_data ) {
64 $p_email_data->email_id = db_prepare_int( $p_email_data->email_id );
66 return $p_email_data;
69 /**
70 * Add to email queue
71 * @param EmailData $p_email_data
72 * @return int
74 function email_queue_add( $p_email_data ) {
75 $t_email_data = email_queue_prepare_db( $p_email_data );
77 # email cannot be blank
78 if( is_blank( $t_email_data->email ) ) {
79 error_parameters( lang_get( 'email' ) );
80 trigger_error( ERROR_EMPTY_FIELD, ERROR );
83 # subject cannot be blank
84 if( is_blank( $t_email_data->subject ) ) {
85 error_parameters( lang_get( 'subject' ) );
86 trigger_error( ERROR_EMPTY_FIELD, ERROR );
89 # body cannot be blank
90 if( is_blank( $t_email_data->body ) ) {
91 error_parameters( lang_get( 'body' ) );
92 trigger_error( ERROR_EMPTY_FIELD, ERROR );
95 $t_email_table = db_get_table( 'email' );
97 $c_email = $t_email_data->email;
98 $c_subject = $t_email_data->subject;
99 $c_body = $t_email_data->body;
100 $c_metadata = serialize( $t_email_data->metadata );
102 $query = "INSERT INTO $t_email_table
103 ( email,
104 subject,
105 body,
106 submitted,
107 metadata)
108 VALUES
109 ( " . db_param() . ",
110 " . db_param() . ",
111 " . db_param() . ",
112 " . db_param() . ",
113 " . db_param() . "
115 db_query_bound( $query, Array( $c_email, $c_subject, $c_body, db_now(), $c_metadata ) );
117 return db_insert_id( $t_email_table, 'email_id' );
121 * Convert email db row to EmailData object
122 * @param array $p_row
123 * @return bool|EmailData
125 function email_queue_row_to_object( $p_row ) {
126 # typically this function takes as an input the result of db_fetch_array() which can be false.
127 if( $p_row === false ) {
128 return false;
131 $t_row = $p_row;
132 $t_row['metadata'] = unserialize( $t_row['metadata'] );
134 $t_email_data = new EmailData;
136 $t_row_keys = array_keys( $t_row );
137 $t_vars = get_object_vars( $t_email_data );
139 # Check each variable in the class
140 foreach( $t_vars as $t_var => $t_value ) {
141 # If we got a field from the DB with the same name
142 if( in_array( $t_var, $t_row_keys, true ) ) {
144 # Store that value in the object
145 $t_email_data->$t_var = $t_row[$t_var];
149 return $t_email_data;
153 * Get Corresponding EmailData object
154 * @param int $p_email_id
155 * @return bool|EmailData
157 function email_queue_get( $p_email_id ) {
158 $c_email_id = db_prepare_int( $p_email_id );
159 $t_email_table = db_get_table( 'email' );
161 $query = 'SELECT * FROM ' . $t_email_table . ' WHERE email_id=' . db_param();
162 $result = db_query_bound( $query, Array( $c_email_id ) );
164 $t_row = db_fetch_array( $result );
166 return email_queue_row_to_object( $t_row );
170 * Delete entry from email queue
171 * @param int $p_email_id
172 * @return null
174 function email_queue_delete( $p_email_id ) {
175 $c_email_id = db_prepare_int( $p_email_id );
176 $t_email_table = db_get_table( 'email' );
178 $query = 'DELETE FROM ' . $t_email_table . ' WHERE email_id=' . db_param();
179 db_query_bound( $query, Array( $c_email_id ) );
183 * Get array of email queue id's
184 * @return array
186 function email_queue_get_ids() {
187 $t_email_table = db_get_table( 'email' );
189 $query = 'SELECT email_id FROM ' . $t_email_table . ' ORDER BY email_id DESC';
190 $result = db_query_bound( $query );
192 $t_ids = array();
193 while(( $t_row = db_fetch_array( $result ) ) !== false ) {
194 $t_ids[] = $t_row['email_id'];
197 return $t_ids;