PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/email_queue_api.php

https://github.com/fusenigk/mantisbt-1
PHP | 198 lines | 95 code | 32 blank | 71 comment | 5 complexity | 39835f273cd55a39bf7c68045d6535ab MD5 | raw file
  1. <?php
  2. # MantisBT - A PHP based bugtracking system
  3. # MantisBT is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # MantisBT is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Email Queue API
  17. *
  18. * @package CoreAPI
  19. * @subpackage EmailQueueAPI
  20. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  21. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  22. * @link http://www.mantisbt.org
  23. *
  24. * @uses constant_api.php
  25. * @uses database_api.php
  26. * @uses error_api.php
  27. * @uses lang_api.php
  28. * @uses utility_api.php
  29. */
  30. require_api( 'constant_inc.php' );
  31. require_api( 'database_api.php' );
  32. require_api( 'error_api.php' );
  33. require_api( 'lang_api.php' );
  34. require_api( 'utility_api.php' );
  35. /**
  36. * EmailData Structure Definition
  37. * @package MantisBT
  38. * @subpackage classes
  39. */
  40. class EmailData {
  41. // properties set during creation
  42. var $email = '';
  43. var $subject = '';
  44. var $body = '';
  45. var $metadata = array(
  46. 'headers' => array(),
  47. );
  48. // auto-populated properties
  49. var $email_id = 0;
  50. var $submitted = '';
  51. };
  52. /**
  53. * Return a copy of the bug structure with all the instvars prepared for db insertion
  54. * @param EmailData $p_email_data
  55. * @return EmailData
  56. */
  57. function email_queue_prepare_db( $p_email_data ) {
  58. $p_email_data->email_id = db_prepare_int( $p_email_data->email_id );
  59. return $p_email_data;
  60. }
  61. /**
  62. * Add to email queue
  63. * @param EmailData $p_email_data
  64. * @return int
  65. */
  66. function email_queue_add( $p_email_data ) {
  67. $t_email_data = email_queue_prepare_db( $p_email_data );
  68. # email cannot be blank
  69. if( is_blank( $t_email_data->email ) ) {
  70. error_parameters( lang_get( 'email' ) );
  71. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  72. }
  73. # subject cannot be blank
  74. if( is_blank( $t_email_data->subject ) ) {
  75. error_parameters( lang_get( 'subject' ) );
  76. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  77. }
  78. # body cannot be blank
  79. if( is_blank( $t_email_data->body ) ) {
  80. error_parameters( lang_get( 'body' ) );
  81. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  82. }
  83. $t_email_table = db_get_table( 'email' );
  84. $c_email = $t_email_data->email;
  85. $c_subject = $t_email_data->subject;
  86. $c_body = $t_email_data->body;
  87. $c_metadata = serialize( $t_email_data->metadata );
  88. $query = "INSERT INTO $t_email_table
  89. ( email,
  90. subject,
  91. body,
  92. submitted,
  93. metadata)
  94. VALUES
  95. ( " . db_param() . ",
  96. " . db_param() . ",
  97. " . db_param() . ",
  98. " . db_param() . ",
  99. " . db_param() . "
  100. )";
  101. db_query_bound( $query, Array( $c_email, $c_subject, $c_body, db_now(), $c_metadata ) );
  102. return db_insert_id( $t_email_table, 'email_id' );
  103. }
  104. /**
  105. * Convert email db row to EmailData object
  106. * @param array $p_row
  107. * @return bool|EmailData
  108. */
  109. function email_queue_row_to_object( $p_row ) {
  110. # typically this function takes as an input the result of db_fetch_array() which can be false.
  111. if( $p_row === false ) {
  112. return false;
  113. }
  114. $t_row = $p_row;
  115. $t_row['metadata'] = unserialize( $t_row['metadata'] );
  116. $t_email_data = new EmailData;
  117. $t_row_keys = array_keys( $t_row );
  118. $t_vars = get_object_vars( $t_email_data );
  119. # Check each variable in the class
  120. foreach( $t_vars as $t_var => $t_value ) {
  121. # If we got a field from the DB with the same name
  122. if( in_array( $t_var, $t_row_keys, true ) ) {
  123. # Store that value in the object
  124. $t_email_data->$t_var = $t_row[$t_var];
  125. }
  126. }
  127. return $t_email_data;
  128. }
  129. /**
  130. * Get Corresponding EmailData object
  131. * @param int $p_email_id
  132. * @return bool|EmailData
  133. */
  134. function email_queue_get( $p_email_id ) {
  135. $c_email_id = db_prepare_int( $p_email_id );
  136. $t_email_table = db_get_table( 'email' );
  137. $query = 'SELECT * FROM ' . $t_email_table . ' WHERE email_id=' . db_param();
  138. $result = db_query_bound( $query, Array( $c_email_id ) );
  139. $t_row = db_fetch_array( $result );
  140. return email_queue_row_to_object( $t_row );
  141. }
  142. /**
  143. * Delete entry from email queue
  144. * @param int $p_email_id
  145. * @return null
  146. */
  147. function email_queue_delete( $p_email_id ) {
  148. $c_email_id = db_prepare_int( $p_email_id );
  149. $t_email_table = db_get_table( 'email' );
  150. $query = 'DELETE FROM ' . $t_email_table . ' WHERE email_id=' . db_param();
  151. db_query_bound( $query, Array( $c_email_id ) );
  152. }
  153. /**
  154. * Get array of email queue id's
  155. * @return array
  156. */
  157. function email_queue_get_ids() {
  158. $t_email_table = db_get_table( 'email' );
  159. $query = 'SELECT email_id FROM ' . $t_email_table . ' ORDER BY email_id DESC';
  160. $result = db_query_bound( $query );
  161. $t_ids = array();
  162. while(( $t_row = db_fetch_array( $result ) ) !== false ) {
  163. $t_ids[] = $t_row['email_id'];
  164. }
  165. return $t_ids;
  166. }