Local.pm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package Cpanel::iContact::Provider::Local;
  2. use strict;
  3. use warnings;
  4. use parent 'Cpanel::iContact::Provider';
  5. use Try::Tiny;
  6. =encoding utf-8
  7. =head1 NAME
  8. Cpanel::iContact::Provider::Local - Backend for the Local iContact module
  9. =head1 SYNOPSIS
  10. use Cpanel::iContact::Provider::Local;
  11. my $notifier = Cpanel::iContact::Provider::Local->new();
  12. $notifier->send();
  13. =head1 DESCRIPTION
  14. Provide backend accessor for the Local iContact module.
  15. =cut
  16. =head2 send
  17. Sends off the notification over to /var/cpanel/iContact
  18. =over 2
  19. =item Input
  20. =over 3
  21. None
  22. =back
  23. =item Output
  24. =over 3
  25. Truthy value on success, exception on failure.
  26. =back
  27. =back
  28. =cut
  29. our $DIR = '/var/cpanel/iContact_notices';
  30. sub send {
  31. my ($self) = @_;
  32. my $args_hr = $self->{'args'};
  33. my $contact_hr = $self->{'contact'};
  34. my @errs;
  35. my $subject = $args_hr->{'subject'};
  36. my $text = ${ $args_hr->{'text_body'} };
  37. my $html = ${ $args_hr->{'html_body'} };
  38. $html =~ s:src=".*":src="/unprotected/cpanel/images/cp-logo.svg" style="width\:3rem":;
  39. require Text::Iconv;
  40. my $converter = Text::Iconv->new("UTF-8", "ASCII");
  41. $html = $converter->convert($html);
  42. # Send it
  43. my $time = time;
  44. $time =~ tr/ /-/;
  45. my $user = getpwuid($<);
  46. my $file = "$DIR/$user/$time.json";
  47. try {
  48. # Make the dir if it doesn't exist
  49. if( !-d "$DIR/$user" ) {
  50. my $path = '/';
  51. foreach my $component ( split( /\//, "$DIR/$user" ) ) {
  52. local $!;
  53. $path .= "$component/";
  54. mkdir( $path ) || do {
  55. die "Couldn't create $path: $!" if int $! != 17; # EEXISTS
  56. };
  57. }
  58. }
  59. require Cpanel::AdminBin::Serializer;
  60. require File::Slurper;
  61. File::Slurper::write_binary( $file, Cpanel::AdminBin::Serializer::Dump( { 'subject' => $subject, 'text' => $text, 'html' => $html } ) );
  62. }
  63. catch {
  64. require Cpanel::Exception;
  65. die Cpanel::Exception::create(
  66. 'ConnectionFailed',
  67. 'The system failed to save the message to “[_1]” due to an error: [_2]',
  68. [ $file, $_ ]
  69. );
  70. };
  71. return 1;
  72. }
  73. sub reap_older_than {
  74. my ($timestamp) = @_;
  75. return () unless -d $DIR;
  76. opendir(my $dh, $DIR) || die "Can't opendir $DIR: $!";
  77. my @actual_files = grep { !/^\./ && -f "$DIR/$_" } readdir($dh);
  78. closedir $dh;
  79. my %files_by_age = ();
  80. @files_by_age{@actual_files} = map { (stat "$DIR/$_")[9] } @actual_files;
  81. my @files_to_kill = grep { $files_by_age{$_} < $timestamp } @actual_files;
  82. foreach my $goner (@files_to_kill) { unlink "$DIR/$goner" or warn "Could not delete $DIR/$goner!"; }
  83. return @files_to_kill;
  84. }
  85. 1;