File: Expire.pm

package info (click to toggle)
pnopaste 1.8-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 344 kB
  • sloc: perl: 1,056; sh: 55; sql: 39; makefile: 8
file content (119 lines) | stat: -rw-r--r-- 2,224 bytes parent folder | download | duplicates (3)
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
package Expire;

# This file is part of the pnopaste program
# Copyright (C) 2008-2021 Patrick Matthäi <pmatthaei@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

use warnings;
use strict;
use lib::Database;


our %Time_List = (
	'6h'	=> 21600,
	'1d'	=> 86400,
	'1w'	=> 604800,
	'1m'	=> 2419200,
	'never'	=> 0
);

# Standard choice.
our $Standard = '1w';


### Builds an array of the times.
sub Time_List {
	my @List = sort {
		$Time_List{$b} <=> $Time_List{$a}
	} keys %Time_List;

	return @List;
}


### Creates the time list with option tags for HTML.
sub Build_Time_List_HTML {
	my @List = Time_List();

	my $String = '';

	foreach my $Tmp (@List){
		if($Tmp eq $Standard){
			$String .= '<option selected="selected">' . $Tmp . '</option>' . "\n";
		} else {
			$String .= '<option>' . $Tmp . '</option>' . "\n";
		}
	}

	return $String;
}


### Checks if the expire time is valid.
sub Valid_Time {
	my($Time) = @_;

	if(!exists $Time_List{$Time}){
		return 0;
	}

	return 1;
}


### Get the expire date of a code.
sub Get_Expire {
	my($ID) = @_;

	my $Query = 'SELECT time,expires FROM nopaste WHERE id = ? LIMIT 1';
	$Query = $Database::dbh->prepare($Query);
	$Query->execute($ID);

	my $Result = $Query->fetchrow_hashref();

	my $Expiretime = $Result->{'expires'};
	my $Createtime = $Result->{'time'};

	# It will never expires.
	if(!$Expiretime){
		return 'never';
	}

	$Expiretime += $Createtime;
	$Expiretime = localtime($Expiretime);

	return $Expiretime;
}


### Deletes expired entrys.
sub Delete_Old_Entrys {

	my $Query = 'DELETE FROM nopaste WHERE expires != 0 AND time + expires < ?';
	$Query = $Database::dbh->prepare($Query);
	$Query->execute(time());

}


### Gets default setting for cleanup option.
sub Get_Cleanup {
	my $Default = 1;

	my $Query = 'SELECT cleanup FROM settings LIMIT 1';
	$Query = $Database::dbh->prepare($Query);
	$Query->execute();

	my $List = $Query->fetchrow_hashref();
	if(defined $List->{'cleanup'}){
		$Default = $List->{'cleanup'};
	}

	return $Default;
}

1;