/usr/share/perl5/Lemonldap/NG/Common/Notification/File.pm is in liblemonldap-ng-common-perl 1.9.16-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ## @file
# File storage methods for notifications
## @class
# File storage methods for notifications
package Lemonldap::NG::Common::Notification::File;
use strict;
use MIME::Base64;
our $VERSION = '1.9.1';
## @method boolean prereq()
# Check if parameters are set and if storage directory exists.
# @return true if all is OK
sub prereq {
my $self = shift;
unless ( $self->{dirName} ) {
$Lemonldap::NG::Common::Notification::msg =
'"dirName" is required in "File" notification type !';
return 0;
}
if ( $self->{table} ) {
$self->{dirName} =~ s/\/conf\/?$//;
$self->{dirName} .= "/$self->{table}";
}
unless ( -d $self->{dirName} ) {
$Lemonldap::NG::Common::Notification::msg =
"Directory \"$self->{dirName}\" does not exist !";
return 0;
}
# Configure file name separator (_ by default)
$self->{fileNameSeparator} ||= "_";
1;
}
## @method hashref get(string uid,string ref)
# Returns notifications corresponding to the user $uid.
# If $ref is set, returns only notification corresponding to this reference.
# @param $uid UID
# @param $ref Notification reference
# @return hashref where keys are filenames and values are XML strings
sub get {
my ( $self, $uid, $ref ) = @_;
return () unless ($uid);
my $fns = $self->{fileNameSeparator};
my $identifier = &getIdentifier( $self, $uid, $ref );
opendir D, $self->{dirName};
my @notif = grep /^\d{8}${fns}${identifier}\S*\.xml$/, readdir(D);
closedir D;
my $files;
foreach my $file (@notif) {
unless ( open F, $self->{dirName} . "/$file" ) {
$self->lmLog( "Unable to read notification $self->{dirName}/$file",
'error' );
next;
}
$files->{$file} = join( '', <F> );
}
return $files;
}
## @method hashref getAll()
# Return all messages not notified.
# @return hashref where keys are internal reference and values are hashref with
# keys date, uid and ref.
sub getAll {
my $self = shift;
opendir D, $self->{dirName};
my @notif;
my $fns = $self->{fileNameSeparator};
@notif = grep /^\S*\.xml$/, readdir(D);
my %h = map {
/^(\d{8})${fns}([^\s${fns}]+)${fns}([^\s${fns}]+)(?:${fns}([^\s${fns}]+))?\.xml$/
? (
$_ => {
date => $1,
uid => $2,
ref => decode_base64($3),
condition => decode_base64( $4 // '' )
}
)
: ()
} @notif;
return \%h;
}
## @method boolean delete(string myref)
# Mark a notification as done.
# @param $myref identifier returned by get() or getAll()
sub delete {
my ( $self, $myref ) = @_;
my $new = ( $myref =~ /(.*?)(?:\.xml)$/ )[0] . '.done';
return rename( $self->{dirName} . "/$myref", $self->{dirName} . "/$new" );
}
## @method boolean purge(string myref)
# Purge notification (really delete record)
# @param $myref identifier returned by get() or getAll()
# @return true if something was deleted
sub purge {
my ( $self, $myref ) = @_;
return unlink( $self->{dirName} . "/$myref" );
}
## @method boolean newNotif(string date, string uid, string ref, string xml)
# Insert a new notification
# @param date Date
# @param uid UID
# @param ref Reference of the notification
# @param xml XML notification
# @return true if succeed
sub newNotif {
my ( $self, $date, $uid, $ref, $condition, $xml ) = @_;
my $fns = $self->{fileNameSeparator};
$date =~ s/-//g;
return ( 0, "Bad date" ) unless ( $date =~ /^\d{8}/ );
my $filename =
$self->{dirName}
. "/${date}${fns}${uid}${fns}"
. encode_base64( $ref, '' );
$filename .= "${fns}" . encode_base64( $condition, '' ) if $condition;
$filename .= ".xml";
return ( 0, 'This notification still exists' ) if ( -e $filename );
my $old = ( $filename =~ /(.*?)(?:\.xml)$/ )[0] . '.done';
return ( 0, 'This notification has been done' ) if ( -e $old );
open my $F, ">$filename" or return ( 0, "Unable to create $filename ($!)" );
binmode($F);
$xml->toFH($F);
return ( 0, "Unable to close $filename ($!)" ) unless ( close $F );
return 1;
}
## @method hashref getDone()
# Returns a list of notification that have been done
# @return hashref where keys are internal reference and values are hashref with
# keys notified, uid and ref.
sub getDone {
my ($self) = @_;
opendir D, $self->{dirName};
my @notif;
my $fns = $self->{fileNameSeparator};
@notif = grep /^\d{8}${fns}\S*\.done$/, readdir(D);
my $res;
foreach my $file (@notif) {
my ( $u, $r ) =
( $file =~
/^\d+${fns}([^${fns}]+)${fns}([^${fns}]+)${fns}?([^${fns}]+)\.done$/
);
die unless ( -f "$self->{dirName}/$file" );
my $time = ( stat("$self->{dirName}/$file") )[10];
$res->{$file} = {
'uid' => $u,
'ref' => decode_base64($r),
'notified' => $time,
};
}
return $res;
}
## @method string getIdentifier(string uid, string ref, string date)
# Get notification identifier
# @param $uid uid
# @param $ref ref
# @param $date date
# @return the notification identifier
sub getIdentifier {
my ( $self, $uid, $ref, $date ) = @_;
my $result;
# Special fix to manage purge from notification explorer
return $date if $date;
my $fns = $self->{fileNameSeparator};
if ($date) {
$result .= $date . $fns;
}
$result .= $uid;
if ($ref) {
my $tmp = encode_base64( $ref, '' );
$result .= $fns . $tmp;
}
return $result;
}
1;
|