]>
jfr.im git - uguu.git/blob - src/Classes/expireChecker.php
5 * @copyright Copyright (c) 2022-2024 Go Johansson (nokonoko) <neku@pomf.se>
7 * Note that this was previously distributed under the MIT license 2015-2022.
9 * If you are a company that wants to use Uguu I urge you to contact me to
10 * solve any potential license issues rather then using pre-2022 code.
12 * A special thanks goes out to the open source community around the world
13 * for supporting and being the backbone of projects like Uguu.
15 * This project can be found at <https://github.com/nokonoko/Uguu>.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <https://www.gnu.org/licenses/>.
31 namespace Pomf\Uguu\Classes
;
38 public string $dbType ;
40 public string $timeUnit ;
42 * @var \Pomf\Uguu\Classes\Response
44 private Response
$response ;
46 public function checkDB (): bool | array
48 if ( is_int ( $this- > CONFIG
[ 'expireTime' ])) {
49 $this- > timeUnit
= strtoupper ( $this- > CONFIG
[ 'expireTimeUnit' ]);
50 if (! in_array ( $this- > timeUnit
, [ 'SECONDS' , 'MINUTES' , 'HOURS' , 'DAYS' , 'WEEKS' , 'MONTHS' , 'YEARS' ])) {
51 $this- > timeUnit
= "HOURS" ;
53 $query = match ( $this- > dbType
) {
54 'pgsql' => 'SELECT id, filename FROM files WHERE date < EXTRACT(epoch from NOW() - INTERVAL \' ' . $this- > CONFIG
[ 'expireTime' ] . ' ' . $this- > timeUnit
. ' \' )' ,
55 default => 'SELECT id, filename FROM files WHERE date <= strftime( \' %s \' , datetime( \' now \' , \' -' . $this- > CONFIG
[ 'expireTime' ] . ' ' . $this- > timeUnit
. ' \' ));'
57 $q = $this- > DB
-> prepare ( $query );
59 $result = $q- > fetchAll ( PDO
:: FETCH_ASSOC
);
65 foreach ( $result as $array ) {
66 $returnArray [ 'ids' ][] = $array [ 'id' ];
67 $returnArray [ 'filenames' ][] = $array [ 'filename' ];
75 public function cleanRateLimitDB (): void
77 $query = match ( $this- > dbType
) {
78 'pgsql' => 'DELETE FROM ratelimit WHERE time < EXTRACT(epoch from NOW() - INTERVAL \' 24 HOURS \' )' ,
79 default => 'DELETE FROM ratelimit WHERE time <= strftime( \' %s \' , datetime( \' now \' , \' -24 HOURS \' ));'
81 $q = $this- > DB
-> prepare ( $query );
86 public function deleteFiles ( array $filenames ): void
88 foreach ( $filenames as $filename ) {
89 unlink ( $this- > CONFIG
[ 'FILES_ROOT' ] . $filename );
93 public function deleteFromDB ( array $ids ): void
95 foreach ( $ids as $id ) {
96 $query = match ( $this- > dbType
) {
97 'pgsql' => 'DELETE FROM files WHERE id = (:id)' ,
98 default => 'DELETE FROM files WHERE id = (:id)'
100 $q = $this- > DB
-> prepare ( $query );
101 $q- > bindValue ( ':id' , $id );
108 * Reads the config.json file and populates the CONFIG property with the settings
109 * Also assembles the PDO DB connection and registers error handlers.
112 public function __construct ()
114 $this- > response
= new Response ( 'json' );
115 if (! file_exists ( __DIR__
. '/../config.json' )) {
116 $this- > response
-> error ( 500 , 'Cant read settings file.' );
118 $this- > CONFIG
= json_decode (
119 file_get_contents ( __DIR__
. '/../config.json' ),
122 ini_set ( 'display_errors' , 0 );
123 $this- > dbType
= $this- > CONFIG
[ 'DB_MODE' ];
125 $this- > CONFIG
[ 'DB_MODE' ] . ':' . $this- > CONFIG
[ 'DB_PATH' ],
126 $this- > CONFIG
[ 'DB_USER' ],
127 $this- > CONFIG
[ 'DB_PASS' ],