2011-05-03 07:25:51 +00:00
< ? php
/*
$Id : m_authip . php
----------------------------------------------------------------------
LICENSE
This program is free software ; you can redistribute it and / or
modify it under the terms of the GNU General Public License ( GPL )
as published by the Free Software Foundation ; either version 2
of the License , or ( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
To read the license please visit http :// www . gnu . org / copyleft / gpl . html
----------------------------------------------------------------------
Original Author of file : Fufroma
----------------------------------------------------------------------
*/
/**
* Classe de gestion des IP authoris<EFBFBD> e
**/
class m_authip {
2011-05-16 11:57:13 +00:00
/*
* Retourne la liste des ip whitelist
*
* @ return array retourne un tableau index<EFBFBD> des ip de l ' utilisateur
*/
function list_ip_whitelist () {
global $mem ;
if ( ! $mem -> checkRight ()) return false ;
return $this -> list_ip ( true );
}
2011-05-03 19:03:43 +00:00
/*
* Retourne la liste des ip sp<EFBFBD> cifi<EFBFBD> es par cet utilisateur
*
* @ return array retourne un tableau index<EFBFBD> des ip de l ' utilisateur
*/
2011-05-16 11:57:13 +00:00
function list_ip ( $whitelist = false ) {
global $db , $mem ;
if ( $whitelist && $mem -> checkRight () ) {
$cuid = 0 ;
} else {
global $cuid ;
}
2011-05-03 07:25:51 +00:00
$r = array ();
$db -> query ( " SELECT * FROM authorised_ip WHERE uid=' $cuid '; " );
while ( $db -> next_record ()) {
$r [ $db -> f ( 'id' )] = $db -> Record ;
if ( ( checkip ( $db -> f ( 'ip' )) && $db -> f ( 'subnet' ) == 32 ) ||
( checkipv6 ( $db -> f ( 'ip' )) && $db -> f ( 'subnet' ) == 128 ) ) {
$r [ $db -> f ( 'id' )][ 'ip_human' ] = $db -> f ( 'ip' );
} else {
$r [ $db -> f ( 'id' )][ 'ip_human' ] = $db -> f ( 'ip' ) . " / " . $db -> f ( 'subnet' );
}
}
return $r ;
}
2011-05-16 11:57:13 +00:00
2011-05-03 19:03:43 +00:00
/*
* Supprime une IP des IP de l ' utilisateur
* et supprime les droits attach<EFBFBD> en cascade
*
* @ param integer $id id de la ligne <EFBFBD> supprimer
* @ return boolean Retourne FALSE si erreur , sinon TRUE
*/
2011-05-03 07:25:51 +00:00
function ip_delete ( $id ) {
global $db , $cuid ;
$id = intval ( $id );
$db -> query ( " SELECT id FROM authorised_ip_affected where authorised_ip_id =' $id '; " );
while ( $db -> next_record ()) {
$this -> ip_affected_delete ( $db -> f ( 'id' ));
}
if ( ! $db -> query ( " delete from authorised_ip where id=' $id ' and uid=' $cuid ' limit 1; " ) ) {
echo " query failed: " . $db -> Error ;
return false ;
}
return true ;
}
2011-06-03 17:08:44 +00:00
/*
* Liste les IP et subnet authoris<EFBFBD> s
* pour une classe donn<EFBFBD> e
*
* @ param string $s classe concern<EFBFBD> e
* @ return array Retourne un tableau
*/
function get_allowed ( $s ) {
global $db , $cuid ;
if ( ! $db -> query ( " select ai.ip, ai.subnet, ai.infos, aia.parameters from authorised_ip ai, authorised_ip_affected aia where aia.protocol=' $s ' and aia.authorised_ip_id = ai.id and ai.uid=' $cuid '; " ) ) {
echo " query failed: " . $db -> Error ;
return false ;
}
$r = Array ();
while ( $db -> next_record ()) {
$r [] = Array ( " ip " => $db -> f ( " ip " ), " subnet " => $db -> f ( " subnet " ), " infos " => $db -> f ( " infos " ), " parameters " => $db -> f ( " parameters " ));
}
return $r ;
}
function is_wl ( $ip ) {
global $db ;
if ( ! $db -> query ( " select ai.ip, ai.subnet from authorised_ip ai where ai.uid='0'; " ) ) {
echo " query failed: " . $db -> Error ;
return false ;
}
while ( $db -> next_record ()) {
if ( $this -> is_in_subnet ( $ip , $db -> f ( 'ip' ), $db -> f ( 'subnet' ) ) ) return true ;
}
return false ;
}
/*
* Retourne si l ' ip appartient au subnet .
*
*/
function is_in_subnet ( $o , $ip , $sub ) {
$o = inet_pton ( $o );
$ip = inet_pton ( $ip );
$sub = pow ( 2 , $sub );
if ( $o >= $ip && $o <= ( $ip + $sub ) ) return true ;
return false ;
}
2011-05-16 11:57:13 +00:00
/*
* Sauvegarde une IP dans les IP TOUJOURS authoris<EFBFBD> e
*
* @ param integer $id id de la ligne <EFBFBD> modifier . Si vide ou
* <EFBFBD> gal <EFBFBD> 0 , alors c ' est une insertion
* @ param string $ipsub IP ( v4 ou v6 ), potentiellement avec un subnet ( / 24 )
* @ param string $infos commentaire pour l ' utilisateur
* @ param integer $uid Si $uid = 0 et qu ' on est super - admin , insertion avec uid = 0
* ce qui correspond a une ip toujours authoris<EFBFBD> e
* @ return boolean Retourne FALSE si erreur , sinon TRUE
*/
function ip_save_whitelist ( $id , $ipsub , $infos ) {
global $mem ;
if ( ! $mem -> checkRight ()) return false ;
return $this -> ip_save ( $id , $ipsub , $infos , 0 );
}
2011-05-03 19:03:43 +00:00
/*
* Sauvegarde une IP dans les IP authoris<EFBFBD> e
*
* @ param integer $id id de la ligne <EFBFBD> modifier . Si vide ou
* <EFBFBD> gal <EFBFBD> 0 , alors c ' est une insertion
* @ param string $ipsub IP ( v4 ou v6 ), potentiellement avec un subnet ( / 24 )
* @ param string $infos commentaire pour l ' utilisateur
* @ param integer $uid Si $uid = 0 et qu ' on est super - admin , insertion avec uid = 0
* ce qui correspond a une ip toujours authoris<EFBFBD> e
* @ return boolean Retourne FALSE si erreur , sinon TRUE
*/
2011-05-03 07:25:51 +00:00
function ip_save ( $id , $ipsub , $infos , $uid = null ) {
global $db , $mem ;
// If we ask for uid=0, we have to check to be super-user
// else, juste use global cuid;
if ( $uid === 0 && $mem -> checkRight () ) {
$cuid = 0 ;
} else {
global $cuid ;
}
$id = intval ( $id );
$infos = mysql_real_escape_string ( $infos );
// Extract subnet from ipsub
$tmp = explode ( '/' , $ipsub );
$ip = $tmp [ 0 ];
$subnet = intval ( $tmp [ 1 ]);
// Error if $ip not an IP
if ( ! checkip ( $ip ) && ! checkipv6 ( $ip ) ) {
echo " Failed : not an IP address " ;
return false ;
}
// Check the subnet, if not defined, give a /32 or a /128
if ( ! $subnet ) {
if ( checkip ( $ip ) ) $subnet = 32 ;
else $subnet = 128 ;
}
// An IPv4 can't have subnet > 32
if ( checkip ( $ip ) && $subnet > 32 ) $subnet = 32 ;
if ( $id ) { // Update
2011-05-03 19:03:43 +00:00
$list_affected = $this -> list_affected ( $id );
foreach ( $list_affected as $k => $v ) {
$this -> call_hooks ( " authip_on_delete " , $k );
}
2011-05-03 07:25:51 +00:00
if ( ! $db -> query ( " update authorised_ip set ip=' $ip ', subnet=' $subnet ', infos=' $infos ' where id=' $id ' and uid=' $cuid ' ; " ) ) {
echo " query failed: " . $db -> Error ;
return false ;
}
2011-05-03 19:03:43 +00:00
foreach ( $list_affected as $k => $v ) {
$this -> call_hooks ( " authip_on_create " , $k );
}
2011-05-03 07:25:51 +00:00
} else { // Insert
if ( ! $db -> query ( " insert into authorised_ip (uid, ip, subnet, infos) values (' $cuid ', ' $ip ', ' $subnet ', ' $infos ' ); " ) ) {
echo " query failed: " . $db -> Error ;
return false ;
}
}
return true ;
}
2011-05-03 19:03:43 +00:00
/*
* Fonction appel<EFBFBD> e par Alternc lors de la suppression d ' un utilisateur
*
* @ return boolean Retourne TRUE
*/
2011-05-15 21:11:48 +00:00
function alternc_del_member () {
2011-05-15 21:17:29 +00:00
global $cuid , $db ;
2011-05-15 21:11:48 +00:00
$db -> query ( " SELECT id FROM authorised_ip WHERE uid =' $cuid '; " );
2011-05-03 07:25:51 +00:00
while ( $db -> next_record ()) {
$this -> ip_delete ( $db -> f ( 'id' ));
}
return true ;
}
2011-05-03 19:03:43 +00:00
/*
* Analyse les classes et r<EFBFBD> cup<EFBFBD> res les informations
* des classes voulant de la restriction IP
*
* @ return array Retourne un tableau compliqu<EFBFBD>
*/
2011-05-03 07:25:51 +00:00
function get_auth_class () {
2011-11-08 16:06:35 +00:00
global $hooks ;
2011-05-03 07:25:51 +00:00
$authclass = array ();
2011-11-08 16:06:35 +00:00
$authclass = $hooks -> invoke ( 'authip_class' );
2011-05-03 07:25:51 +00:00
2011-11-08 16:06:35 +00:00
// Je rajoute la class DANS l'objet parce que
// ca m'interesse
foreach ( $authclass as $k => $v ) {
$authclass [ $k ][ 'class' ] = $k ;
2011-05-03 07:25:51 +00:00
}
2011-11-08 16:06:35 +00:00
2011-05-03 07:25:51 +00:00
return $authclass ;
}
2011-05-03 19:03:43 +00:00
/*
* Enregistre ou modifie une affectation ip <=> ressource
* Nota : lance des hooks sur la classe correspondante pour
* informer de l ' <EFBFBD> dition/ cr<EFBFBD> ation
*
* @ param integer $authorised_ip_id id de l ' ip affect<EFBFBD>
* @ param string $protocol nom du protocole ( d<EFBFBD> finie dans la classe correspondante )
* @ param string $parameters information propre au protocole
* @ param integer $id pr<EFBFBD> sent si c ' est une <EFBFBD> dition
* @ return boolean Retourne FALSE si erreur , sinon TRUE
*/
2011-05-03 07:25:51 +00:00
function ip_affected_save ( $authorised_ip_id , $protocol , $parameters , $id = null ) {
global $db ;
$authorised_ip_id = intval ( $authorised_ip_id );
$protocol = mysql_real_escape_string ( $protocol );
$parameters = mysql_real_escape_string ( $parameters );
if ( $id ) {
$id = intval ( $id );
2011-05-03 19:03:43 +00:00
$this -> call_hooks ( " authip_on_delete " , $id );
2011-05-03 07:25:51 +00:00
if ( ! $db -> query ( " update authorised_ip_affected set authorised_ip_id=' $authorised_ip_id ', protocol=' $protocol ', parameters=' $parameters ' where id =' $id ' limit 1; " ) ) {
echo " query failed: " . $db -> Error ;
return false ;
}
2011-05-03 19:03:43 +00:00
$this -> call_hooks ( " authip_on_create " , $id );
2011-05-03 07:25:51 +00:00
} else {
if ( ! $db -> query ( " insert into authorised_ip_affected (authorised_ip_id, protocol, parameters) values (' $authorised_ip_id ', ' $protocol ', ' $parameters '); " ) ) {
echo " query failed: " . $db -> Error ;
return false ;
}
2011-05-03 19:03:43 +00:00
$this -> call_hooks ( " authip_on_create " , mysql_insert_id () );
2011-05-03 07:25:51 +00:00
}
return true ;
}
2011-05-03 19:03:43 +00:00
/*
* Supprime une affectation ip <=> ressource
* Nota : lance des hooks dans la classe correspondante
* pour informer de la suppression
*
* @ param integer $id id de la ligne <EFBFBD> supprimer
* @ return boolean Retourne FALSE si erreur , sinon TRUE
*/
2011-05-03 07:25:51 +00:00
function ip_affected_delete ( $id ) {
global $db ;
$id = intval ( $id );
2011-05-03 19:03:43 +00:00
// Call hooks
$this -> call_hooks ( " authip_on_delete " , $id );
2011-05-03 07:25:51 +00:00
if ( ! $db -> query ( " delete from authorised_ip_affected where id=' $id ' limit 1; " ) ) {
echo " query failed: " . $db -> Error ;
return false ;
}
return true ;
}
2011-05-03 19:03:43 +00:00
/*
* Appel les hooks demand<EFBFBD> avec en parametres les
* affectationt ip <=> ressource dont l ' id est en parametre
*
* @ param string $function nom de la fonction a rechercher et appeller dans les classes
* @ param integer $affectation_id id de l ' affectation correspondante
* @ return boolean Retourne TRUE
*/
function call_hooks ( $function , $affectation_id ) {
2011-11-08 16:06:35 +00:00
global $hooks ;
// On r<> cure l'objet dont on parle
2011-05-03 19:03:43 +00:00
$d = $this -> list_affected ();
$affectation = $d [ $affectation_id ];
2011-11-08 16:06:35 +00:00
// On en d<> duis la classe qui le concerne
2011-05-03 19:03:43 +00:00
$e = $this -> get_auth_class ();
$c = $e [ $affectation [ 'protocol' ]][ 'class' ];
2011-11-08 16:06:35 +00:00
// On appelle le hooks de cette classe
$hooks -> invoke ( $function , Array ( $affectation ), Array ( $c ) );
2011-05-03 19:03:43 +00:00
return true ;
}
/*
* Liste les affectation ip <=> ressource d ' un utilisateur
*
* @ return array Retourne un tableau de valeurs
*/
function list_affected ( $ip_id = null ) {
2011-05-03 07:25:51 +00:00
global $db , $cuid ;
$r = array ();
2011-05-03 19:03:43 +00:00
if ( is_null ( $ip_id ) ) {
$db -> query ( " select aia.* from authorised_ip_affected aia, authorised_ip ai where ai.uid=' $cuid ' and aia.authorised_ip_id = ai.id order by protocol, parameters; " );
} else {
$db -> query ( " select aia.* from authorised_ip_affected aia, authorised_ip ai where ai.uid=' $cuid ' and aia.authorised_ip_id = ' " . intval ( $ip_id ) . " ' order by protocol, parameters; " );
}
2011-05-03 07:25:51 +00:00
while ( $db -> next_record ()) {
2011-05-03 19:03:43 +00:00
$r [ $db -> f ( 'id' )] = $db -> Record ;
2011-05-03 07:25:51 +00:00
}
return $r ;
}
}; /* Classe m_authip */
2012-04-06 10:10:36 +00:00
?>