]>
jfr.im git - uguu.git/blob - src/Classes/expireChecker.php
3 namespace Pomf\Uguu\Classes
;
10 public string $timeUnit ;
12 public function checkDB (): bool | array {
13 if ( is_int ( $this- > CONFIG
[ 'expireTime' ])) {
14 $this- > timeUnit
= strtoupper ( $this- > CONFIG
[ 'expireTimeUnit' ]);
15 if (! in_array ( $this- > timeUnit
, [ 'SECONDS' , 'MINUTES' , 'HOURS' , 'DAYS' , 'WEEKS' , 'MONTHS' , 'YEARS' ])){
16 $this- > timeUnit
= "HOURS" ;
18 $query = match ( $this- > dbType
) {
19 'pgsql' => 'SELECT id, filename FROM files WHERE date < EXTRACT(epoch from NOW() - INTERVAL \' ' . $this- > CONFIG
[ 'expireTime' ] . ' ' . $this- > timeUnit
. ' \' )' ,
20 default => 'SELECT id, filename FROM files WHERE date <= strftime( \' %s \' , datetime( \' now \' , \' -' . $this- > CONFIG
[ 'expireTime' ] . ' ' . $this- > timeUnit
. ' \' ));'
22 $q = $this- > DB
-> prepare ( $query );
24 $result = $q- > fetchAll ( PDO
:: FETCH_ASSOC
);
30 foreach ( $result as $array ){
31 $returnArray [ 'ids' ][] = $array [ 'id' ];
32 $returnArray [ 'filenames' ][] = $array [ 'filename' ];
40 public function cleanRateLimitDB (): void {
41 $query = match ( $this- > dbType
) {
42 'pgsql' => 'DELETE FROM ratelimit WHERE time < EXTRACT(epoch from NOW() - INTERVAL \' 24 HOURS \' )' ,
43 default => 'DELETE FROM ratelimit WHERE time <= strftime( \' %s \' , datetime( \' now \' , \' -24 HOURS \' ));'
45 $q = $this- > DB
-> prepare ( $query );
49 public function deleteFiles ( array $filenames ): void {
50 foreach ( $filenames as $filename ) {
51 unlink ( $this- > CONFIG
[ 'FILES_ROOT' ] . $filename );
55 public function deleteFromDB ( array $ids ): void {
56 foreach ( $ids as $id ) {
57 $query = match ( $this- > dbType
) {
58 'pgsql' => 'DELETE FROM files WHERE id = (:id)' ,
59 default => 'DELETE FROM files WHERE id = (:id)'
61 $q = $this- > DB
-> prepare ( $query );
62 $q- > bindValue ( ':id' , $id );
69 * Reads the config.json file and populates the CONFIG property with the settings
70 * Also assembles the PDO DB connection and registers error handlers.
73 public function __construct ()
75 $this- > response
= new Response ( 'json' );
76 if (! file_exists ( __DIR__
. '/../config.json' )) {
77 $this- > response
-> error ( 500 , 'Cant read settings file.' );
79 $this- > CONFIG
= json_decode (
80 file_get_contents ( __DIR__
. '/../config.json' ),
83 ini_set ( 'display_errors' , 0 );
84 $this- > dbType
= $this- > CONFIG
[ 'DB_MODE' ];
86 $this- > CONFIG
[ 'DB_MODE' ] . ':' . $this- > CONFIG
[ 'DB_PATH' ],
87 $this- > CONFIG
[ 'DB_USER' ],
88 $this- > CONFIG
[ 'DB_PASS' ]