2016-08-18 20:39:31 +00:00
< ? php
/* --------------------------------------------------------------------
Chevereto
http :// chevereto . com /
@ author Rodolfo Berrios A . < http :// rodolfoberrios . com />
< inbox @ rodolfoberrios . com >
Copyright ( C ) Rodolfo Berrios A . All rights reserved .
2018-04-17 21:25:26 +00:00
2016-08-18 20:39:31 +00:00
BY USING THIS SOFTWARE YOU DECLARE TO ACCEPT THE CHEVERETO EULA
http :// chevereto . com / license
--------------------------------------------------------------------- */
namespace CHV ;
use G , Exception ;
class Ip_ban {
public static function getSingle ( $args = []) {
try {
$args = array_merge ([
'ip' => G\get_client_ip ()
], $args );
2018-04-17 21:25:26 +00:00
2016-08-18 20:39:31 +00:00
$db = DB :: getInstance ();
2018-04-17 21:25:26 +00:00
$query = 'SELECT * FROM ' . DB :: getTable ( 'ip_bans' ) . ' WHERE ' ;
2016-08-18 20:39:31 +00:00
if ( $args [ 'id' ]) {
2018-04-17 21:25:26 +00:00
$query .= 'ip_ban_id = :id;' ;
2016-08-18 20:39:31 +00:00
} else {
2018-04-17 21:25:26 +00:00
$query .= ':ip LIKE ip_ban_ip AND (ip_ban_expires_gmt > :now OR ip_ban_expires_gmt IS NULL) ORDER BY ip_ban_id DESC;' ; // wilcard are stored as % but displayed as *
2016-08-18 20:39:31 +00:00
}
2018-04-17 21:25:26 +00:00
2016-08-18 20:39:31 +00:00
$db -> query ( $query );
2018-04-17 21:25:26 +00:00
2016-08-18 20:39:31 +00:00
if ( $args [ 'id' ]) {
$db -> bind ( ':id' , $args [ 'id' ]);
} else {
$db -> bind ( ':ip' , $args [ 'ip' ]);
$db -> bind ( ':now' , G\datetimegmt ());
}
2018-04-17 21:25:26 +00:00
$ip_ban = $db -> fetchSingle ();
if ( $ip_ban ) {
$ip_ban = DB :: formatRow ( $ip_ban , 'ip_ban' );
self :: fill ( $ip_ban );
return $ip_ban ;
2016-08-18 20:39:31 +00:00
} else {
return false ;
}
} catch ( Exception $e ) {
throw new Ip_banException ( $e -> getMessage (), 400 );
}
}
2018-04-17 21:25:26 +00:00
2016-08-18 20:39:31 +00:00
public static function getAll () {
try {
$ip_bans_raw = DB :: get ( 'ip_bans' , 'all' );
$ip_bans = [];
if ( $ip_bans_raw ) {
foreach ( $ip_bans_raw as $ip_ban ) {
2018-04-17 21:25:26 +00:00
$idx = $ip_ban [ 'ip_ban_id' ];
$ip_bans [ $idx ] = DB :: formatRow ( $ip_ban , 'ip_ban' );
self :: fill ( $ip_bans [ $idx ]);
2016-08-18 20:39:31 +00:00
}
}
return $ip_bans ;
} catch ( Exception $e ) {
throw new Ip_banException ( $e -> getMessage (), 400 );
}
}
2018-04-17 21:25:26 +00:00
2016-08-18 20:39:31 +00:00
public static function delete ( $args = []) {
try {
return DB :: delete ( 'ip_bans' , $args );
} catch ( Exception $e ) {
throw new Ip_banException ( $e -> getMessage (), 400 );
}
}
2018-04-17 21:25:26 +00:00
2016-08-18 20:39:31 +00:00
public static function update ( $where = [], $values = []) {
try {
2018-04-17 21:25:26 +00:00
if ( $values [ 'ip' ]) {
$values [ 'ip' ] = str_replace ( '*' , '%' , $values [ 'ip' ]);
}
2016-08-18 20:39:31 +00:00
return DB :: update ( 'ip_bans' , $values , $where );
} catch ( Exception $e ) {
throw new Ip_banException ( $e -> getMessage (), 400 );
}
}
2018-04-17 21:25:26 +00:00
2016-08-18 20:39:31 +00:00
public static function insert ( $args = []) {
try {
2018-04-17 21:25:26 +00:00
$args [ 'ip' ] = str_replace ( '*' , '%' , $args [ 'ip' ]);
2016-08-18 20:39:31 +00:00
return DB :: insert ( 'ip_bans' , $args );
} catch ( Exception $e ) {
throw new Ip_banException ( $e -> getMessage (), 400 );
}
}
2018-04-17 21:25:26 +00:00
public static function fill ( & $ip_ban ) {
$ip_ban [ 'ip' ] = str_replace ( '%' , '*' , $ip_ban [ 'ip' ]);
}
public static function validateIP ( $ip , $wilcards = TRUE ) {
$validate = TRUE ;
if ( $wilcards ) {
$base_ip = str_replace ( '*' , '0' , $ip );
if ( ! G\is_valid_ip ( $ip ) && ! G\is_valid_ip ( $base_ip )) {
$validate = FALSE ;
}
} else {
if ( ! G\is_valid_ip ( $ip )) {
$validate = FALSE ;
}
}
if ( ! $validate ) {
throw new Ip_banException ( 'Invalid IP address' );
}
return TRUE ;
}
2016-08-18 20:39:31 +00:00
}
class Ip_banException extends Exception {}