filters.statusnotify.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @file
  4. * A filter for compressing bckups with zip, gz bzip etc.
  5. */
  6. /**
  7. * A filter to send a notification email on success or failure of backup.
  8. *
  9. * @ingroup backup_migrate_filters
  10. */
  11. class backup_migrate_filter_statusnotify extends backup_migrate_filter {
  12. /**
  13. * Get the default backup settings for this filter.
  14. */
  15. function backup_settings_default() {
  16. return array(
  17. 'notify_success_enable' => FALSE,
  18. 'notify_failure_enable' => FALSE,
  19. 'notify_success_email' => variable_get('site_mail', ''),
  20. 'notify_failure_email' => variable_get('site_mail', ''),
  21. );
  22. }
  23. /**
  24. * Get the form for the settings for this filter.
  25. */
  26. function backup_settings_form($settings) {
  27. $form = array();
  28. $form['advanced']['notify_success_enable'] = array(
  29. "#type" => 'checkbox',
  30. "#title" => t("Send an email if backup succeeds"),
  31. "#default_value" => @$settings['notify_success_enable'],
  32. );
  33. $form['advanced']['notify_success_email_wrapper'] = array(
  34. '#type' => 'backup_migrate_dependent',
  35. '#dependencies' => array(
  36. 'filters[notify_success_enable]' => TRUE,
  37. ),
  38. );
  39. $form['advanced']['notify_success_email_wrapper']['notify_success_email'] = array(
  40. "#type" => "textfield",
  41. "#title" => t("Email Address for Success Notices"),
  42. "#default_value" => @$settings['notify_success_email'],
  43. );
  44. $form['advanced']['notify_failure_enable'] = array(
  45. "#type" => 'checkbox',
  46. "#title" => t("Send an email if backup fails"),
  47. "#default_value" => @$settings['notify_failure_enable'],
  48. );
  49. $form['advanced']['notify_failure_email_wrapper'] = array(
  50. '#type' => 'backup_migrate_dependent',
  51. '#dependencies' => array(
  52. 'filters[notify_failure_enable]' => TRUE,
  53. ),
  54. );
  55. $form['advanced']['notify_failure_email_wrapper']['notify_failure_email'] = array(
  56. "#type" => "textfield",
  57. "#title" => t("Email Address for Failure Notices"),
  58. "#default_value" => @$settings['notify_failure_email'],
  59. );
  60. return $form;
  61. }
  62. /**
  63. * Send the success email.
  64. */
  65. function backup_succeed($settings) {
  66. if (@$settings->filters['notify_success_enable'] && $to = @$settings->filters['notify_success_email']) {
  67. $messages = $this->get_messages();
  68. $subject = t('!site backup succeeded', array('!site' => variable_get('site_name', 'Drupal site')));
  69. if ($messages = $this->get_messages()) {
  70. $body = t("The site backup has completed successfully with the following messages:\n!messages", array('!messages' => $messages));
  71. }
  72. else {
  73. $body = t("The site backup has completed successfully.\n");
  74. }
  75. mail($settings->filters['notify_success_email'], $subject, $body);
  76. }
  77. }
  78. /**
  79. * Send the failure email.
  80. */
  81. function backup_fail($settings) {
  82. if (@$settings->filters['notify_failure_enable'] && $to = @$settings->filters['notify_failure_email']) {
  83. $messages = $this->get_messages();
  84. $subject = t('!site backup failed', array('!site' => variable_get('site_name', 'Drupal site')));
  85. if ($messages = $this->get_messages()) {
  86. $body = t("The site backup has failed with the following messages:\n!messages", array('!messages' => $messages));
  87. }
  88. else {
  89. $body = t("The site backup has failed for an unknown reason.");
  90. }
  91. mail($settings->filters['notify_failure_email'], $subject, $body);
  92. }
  93. }
  94. /**
  95. * Render the messages and errors for the email.
  96. */
  97. function get_messages() {
  98. $out = "";
  99. $messages = _backup_migrate_messages();
  100. foreach ($messages as $message) {
  101. $out .= strip_tags(t($message['message'], $message['replace'])) . "\n";
  102. }
  103. return $out;
  104. }
  105. }