| [2930] | 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | $Id: m_authip.php |
|---|
| 4 | ---------------------------------------------------------------------- |
|---|
| 5 | LICENSE |
|---|
| 6 | |
|---|
| 7 | This program is free software; you can redistribute it and/or |
|---|
| 8 | modify it under the terms of the GNU General Public License (GPL) |
|---|
| 9 | as published by the Free Software Foundation; either version 2 |
|---|
| 10 | of the License, or (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | This program is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | To read the license please visit http://www.gnu.org/copyleft/gpl.html |
|---|
| 18 | ---------------------------------------------------------------------- |
|---|
| 19 | Original Author of file: Fufroma |
|---|
| 20 | ---------------------------------------------------------------------- |
|---|
| 21 | */ |
|---|
| 22 | /** |
|---|
| 23 | * Classe de gestion des IP authorisée |
|---|
| 24 | **/ |
|---|
| 25 | class m_authip { |
|---|
| 26 | |
|---|
| [2931] | 27 | /* |
|---|
| [2936] | 28 | * Retourne la liste des ip whitelist |
|---|
| 29 | * |
|---|
| 30 | * @return array retourne un tableau indexé des ip de l'utilisateur |
|---|
| 31 | */ |
|---|
| 32 | function list_ip_whitelist() { |
|---|
| 33 | global $mem; |
|---|
| 34 | if (!$mem->checkRight()) return false; |
|---|
| 35 | return $this->list_ip(true); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | /* |
|---|
| [2931] | 39 | * Retourne la liste des ip spécifiées par cet utilisateur |
|---|
| 40 | * |
|---|
| 41 | * @return array retourne un tableau indexé des ip de l'utilisateur |
|---|
| 42 | */ |
|---|
| [2936] | 43 | function list_ip($whitelist=false) { |
|---|
| 44 | global $db, $mem; |
|---|
| 45 | |
|---|
| 46 | if ($whitelist && $mem->checkRight() ) { |
|---|
| 47 | $cuid=0; |
|---|
| 48 | } else { |
|---|
| 49 | global $cuid; |
|---|
| 50 | } |
|---|
| [2930] | 51 | |
|---|
| 52 | $r = array(); |
|---|
| 53 | $db->query("SELECT * FROM authorised_ip WHERE uid='$cuid';"); |
|---|
| 54 | while ($db->next_record()) { |
|---|
| 55 | $r[$db->f('id')]=$db->Record; |
|---|
| 56 | if ( (checkip($db->f('ip')) && $db->f('subnet') == 32) || |
|---|
| 57 | (checkipv6($db->f('ip')) && $db->f('subnet') == 128) ) { |
|---|
| 58 | $r[$db->f('id')]['ip_human']=$db->f('ip'); |
|---|
| 59 | } else { |
|---|
| 60 | $r[$db->f('id')]['ip_human']=$db->f('ip')."/".$db->f('subnet'); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | } |
|---|
| 64 | return $r; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| [2936] | 67 | |
|---|
| 68 | |
|---|
| [2931] | 69 | /* |
|---|
| 70 | * Supprime une IP des IP de l'utilisateur |
|---|
| 71 | * et supprime les droits attaché en cascade |
|---|
| 72 | * |
|---|
| 73 | * @param integer $id id de la ligne à supprimer |
|---|
| 74 | * @return boolean Retourne FALSE si erreur, sinon TRUE |
|---|
| 75 | */ |
|---|
| [2930] | 76 | function ip_delete($id) { |
|---|
| 77 | global $db, $cuid; |
|---|
| 78 | $id=intval($id); |
|---|
| 79 | |
|---|
| 80 | $db->query("SELECT id FROM authorised_ip_affected where authorised_ip_id ='$id';"); |
|---|
| 81 | while ($db->next_record()) { |
|---|
| 82 | $this->ip_affected_delete($db->f('id')); |
|---|
| 83 | } |
|---|
| 84 | if (! $db->query("delete from authorised_ip where id='$id' and uid='$cuid' limit 1;") ) { |
|---|
| 85 | echo "query failed: ".$db->Error; |
|---|
| 86 | return false; |
|---|
| 87 | } |
|---|
| 88 | return true; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| [2931] | 91 | /* |
|---|
| [2936] | 92 | * Sauvegarde une IP dans les IP TOUJOURS authorisée |
|---|
| 93 | * |
|---|
| 94 | * @param integer $id id de la ligne à modifier. Si vide ou |
|---|
| 95 | * égal à 0, alors c'est une insertion |
|---|
| 96 | * @param string $ipsub IP (v4 ou v6), potentiellement avec un subnet ( /24) |
|---|
| 97 | * @param string $infos commentaire pour l'utilisateur |
|---|
| 98 | * @param integer $uid Si $uid=0 et qu'on est super-admin, insertion avec uid=0 |
|---|
| 99 | * ce qui correspond a une ip toujours authorisée |
|---|
| 100 | * @return boolean Retourne FALSE si erreur, sinon TRUE |
|---|
| 101 | */ |
|---|
| 102 | function ip_save_whitelist($id, $ipsub, $infos) { |
|---|
| 103 | global $mem; |
|---|
| 104 | if (!$mem->checkRight()) return false; |
|---|
| 105 | return $this->ip_save($id, $ipsub, $infos, 0); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /* |
|---|
| [2931] | 109 | * Sauvegarde une IP dans les IP authorisée |
|---|
| 110 | * |
|---|
| 111 | * @param integer $id id de la ligne à modifier. Si vide ou |
|---|
| 112 | * égal à 0, alors c'est une insertion |
|---|
| 113 | * @param string $ipsub IP (v4 ou v6), potentiellement avec un subnet ( /24) |
|---|
| 114 | * @param string $infos commentaire pour l'utilisateur |
|---|
| 115 | * @param integer $uid Si $uid=0 et qu'on est super-admin, insertion avec uid=0 |
|---|
| 116 | * ce qui correspond a une ip toujours authorisée |
|---|
| 117 | * @return boolean Retourne FALSE si erreur, sinon TRUE |
|---|
| 118 | */ |
|---|
| [2930] | 119 | function ip_save($id, $ipsub, $infos, $uid=null) { |
|---|
| 120 | global $db, $mem; |
|---|
| 121 | |
|---|
| 122 | // If we ask for uid=0, we have to check to be super-user |
|---|
| 123 | // else, juste use global cuid; |
|---|
| 124 | if ($uid === 0 && $mem->checkRight() ) { |
|---|
| 125 | $cuid=0; |
|---|
| 126 | } else { |
|---|
| 127 | global $cuid; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | $id=intval($id); |
|---|
| 131 | $infos=mysql_real_escape_string($infos); |
|---|
| 132 | |
|---|
| 133 | // Extract subnet from ipsub |
|---|
| 134 | $tmp=explode('/',$ipsub); |
|---|
| 135 | $ip=$tmp[0]; |
|---|
| 136 | $subnet=intval($tmp[1]); |
|---|
| 137 | |
|---|
| 138 | // Error if $ip not an IP |
|---|
| 139 | if ( ! checkip($ip) && ! checkipv6($ip) ) { |
|---|
| 140 | echo "Failed : not an IP address"; |
|---|
| 141 | return false; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | // Check the subnet, if not defined, give a /32 or a /128 |
|---|
| 145 | if ( ! $subnet ) { |
|---|
| 146 | if ( checkip($ip) ) $subnet=32; |
|---|
| 147 | else $subnet=128; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | // An IPv4 can't have subnet > 32 |
|---|
| 151 | if (checkip($ip) && $subnet > 32 ) $subnet=32; |
|---|
| 152 | |
|---|
| 153 | if ($id) { // Update |
|---|
| [2931] | 154 | $list_affected = $this->list_affected($id); |
|---|
| 155 | foreach($list_affected as $k => $v) { |
|---|
| 156 | $this->call_hooks("authip_on_delete", $k ); |
|---|
| 157 | } |
|---|
| [2930] | 158 | if (! $db->query("update authorised_ip set ip='$ip', subnet='$subnet', infos='$infos' where id='$id' and uid='$cuid' ;") ) { |
|---|
| 159 | echo "query failed: ".$db->Error; |
|---|
| 160 | return false; |
|---|
| 161 | } |
|---|
| [2931] | 162 | foreach($list_affected as $k => $v) { |
|---|
| 163 | $this->call_hooks("authip_on_create", $k ); |
|---|
| 164 | } |
|---|
| [2930] | 165 | } else { // Insert |
|---|
| 166 | if (! $db->query("insert into authorised_ip (uid, ip, subnet, infos) values ('$cuid', '$ip', '$subnet', '$infos' );") ) { |
|---|
| 167 | echo "query failed: ".$db->Error; |
|---|
| 168 | return false; |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | return true; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| [2931] | 174 | /* |
|---|
| 175 | * Fonction appelée par Alternc lors de la suppression d'un utilisateur |
|---|
| 176 | * |
|---|
| 177 | * @return boolean Retourne TRUE |
|---|
| 178 | */ |
|---|
| [2934] | 179 | function alternc_del_member() { |
|---|
| [2935] | 180 | global $cuid,$db; |
|---|
| [2934] | 181 | $db->query("SELECT id FROM authorised_ip WHERE uid ='$cuid';"); |
|---|
| [2930] | 182 | while ($db->next_record()) { |
|---|
| 183 | $this->ip_delete($db->f('id')); |
|---|
| 184 | } |
|---|
| 185 | return true; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| [2931] | 189 | /* |
|---|
| 190 | * Analyse les classes et récupéres les informations |
|---|
| 191 | * des classes voulant de la restriction IP |
|---|
| 192 | * |
|---|
| 193 | * @return array Retourne un tableau compliqué |
|---|
| 194 | */ |
|---|
| [2930] | 195 | function get_auth_class() { |
|---|
| 196 | global $classes; |
|---|
| 197 | $authclass=array(); |
|---|
| 198 | |
|---|
| 199 | foreach ($classes as $c) { |
|---|
| 200 | global $$c; |
|---|
| 201 | if ( method_exists($$c, "authip_class") ) { |
|---|
| 202 | $a=$$c->authip_class(); |
|---|
| 203 | $a['class']=$c; |
|---|
| 204 | $authclass[$a['protocol']]=$a; |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | return $authclass; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| [2931] | 210 | /* |
|---|
| 211 | * Enregistre ou modifie une affectation ip<=>ressource |
|---|
| 212 | * Nota : lance des hooks sur la classe correspondante pour |
|---|
| 213 | * informer de l'édition/création |
|---|
| 214 | * |
|---|
| 215 | * @param integer $authorised_ip_id id de l'ip affecté |
|---|
| 216 | * @param string $protocol nom du protocole (définie dans la classe correspondante) |
|---|
| 217 | * @param string $parameters information propre au protocole |
|---|
| 218 | * @param integer $id présent si c'est une édition |
|---|
| 219 | * @return boolean Retourne FALSE si erreur, sinon TRUE |
|---|
| 220 | */ |
|---|
| [2930] | 221 | function ip_affected_save($authorised_ip_id, $protocol, $parameters, $id=null) { |
|---|
| 222 | global $db; |
|---|
| 223 | $authorised_ip_id=intval($authorised_ip_id); |
|---|
| 224 | $protocol=mysql_real_escape_string($protocol); |
|---|
| 225 | $parameters=mysql_real_escape_string($parameters); |
|---|
| 226 | |
|---|
| 227 | if ($id) { |
|---|
| 228 | $id=intval($id); |
|---|
| [2931] | 229 | $this->call_hooks("authip_on_delete", $id ); |
|---|
| [2930] | 230 | if (! $db->query("update authorised_ip_affected set authorised_ip_id='$authorised_ip_id', protocol='$protocol', parameters='$parameters' where id ='$id' limit 1;") ) { |
|---|
| 231 | echo "query failed: ".$db->Error; |
|---|
| 232 | return false; |
|---|
| 233 | } |
|---|
| [2931] | 234 | $this->call_hooks("authip_on_create", $id ); |
|---|
| [2930] | 235 | } else { |
|---|
| 236 | if (! $db->query("insert into authorised_ip_affected (authorised_ip_id, protocol, parameters) values ('$authorised_ip_id', '$protocol', '$parameters');") ) { |
|---|
| 237 | echo "query failed: ".$db->Error; |
|---|
| 238 | return false; |
|---|
| 239 | } |
|---|
| [2931] | 240 | $this->call_hooks("authip_on_create", mysql_insert_id() ); |
|---|
| [2930] | 241 | } |
|---|
| 242 | return true; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| [2931] | 245 | /* |
|---|
| 246 | * Supprime une affectation ip<=>ressource |
|---|
| 247 | * Nota : lance des hooks dans la classe correspondante |
|---|
| 248 | * pour informer de la suppression |
|---|
| 249 | * |
|---|
| 250 | * @param integer $id id de la ligne à supprimer |
|---|
| 251 | * @return boolean Retourne FALSE si erreur, sinon TRUE |
|---|
| 252 | */ |
|---|
| [2930] | 253 | function ip_affected_delete($id) { |
|---|
| 254 | global $db; |
|---|
| 255 | $id=intval($id); |
|---|
| [2931] | 256 | |
|---|
| 257 | // Call hooks |
|---|
| 258 | $this->call_hooks("authip_on_delete", $id ); |
|---|
| 259 | |
|---|
| [2930] | 260 | if (! $db->query("delete from authorised_ip_affected where id='$id' limit 1;") ) { |
|---|
| 261 | echo "query failed: ".$db->Error; |
|---|
| 262 | return false; |
|---|
| 263 | } |
|---|
| 264 | return true; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| [2931] | 268 | /* |
|---|
| 269 | * Appel les hooks demandé avec en parametres les |
|---|
| 270 | * affectationt ip<=>ressource dont l'id est en parametre |
|---|
| 271 | * |
|---|
| 272 | * @param string $function nom de la fonction a rechercher et appeller dans les classes |
|---|
| 273 | * @param integer $affectation_id id de l'affectation correspondante |
|---|
| 274 | * @return boolean Retourne TRUE |
|---|
| 275 | */ |
|---|
| 276 | function call_hooks($function, $affectation_id) { |
|---|
| 277 | $d = $this->list_affected(); |
|---|
| 278 | $affectation = $d[$affectation_id]; |
|---|
| 279 | $e = $this->get_auth_class(); |
|---|
| 280 | $c = $e[$affectation['protocol']]['class']; |
|---|
| 281 | global $$c; |
|---|
| 282 | if ( method_exists($$c, $function) ) { |
|---|
| 283 | $$c->$function($affectation); |
|---|
| 284 | } |
|---|
| 285 | return true; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | /* |
|---|
| 289 | * Liste les affectation ip<=>ressource d'un utilisateur |
|---|
| 290 | * |
|---|
| 291 | * @return array Retourne un tableau de valeurs |
|---|
| 292 | */ |
|---|
| 293 | function list_affected($ip_id=null) { |
|---|
| [2930] | 294 | global $db, $cuid; |
|---|
| 295 | |
|---|
| 296 | $r = array(); |
|---|
| [2931] | 297 | if ( is_null($ip_id) ) { |
|---|
| 298 | $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;"); |
|---|
| 299 | } else { |
|---|
| 300 | $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;"); |
|---|
| 301 | } |
|---|
| [2930] | 302 | while ($db->next_record()) { |
|---|
| [2931] | 303 | $r[$db->f('id')]=$db->Record; |
|---|
| [2930] | 304 | } |
|---|
| 305 | return $r; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | }; /* Classe m_authip */ |
|---|