| 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 | |
|---|
| 27 | /* |
|---|
| 28 | * Retourne la liste des ip spécifiées par cet utilisateur |
|---|
| 29 | * |
|---|
| 30 | * @return array retourne un tableau indexé des ip de l'utilisateur |
|---|
| 31 | */ |
|---|
| 32 | function list_ip() { |
|---|
| 33 | global $db, $cuid; |
|---|
| 34 | |
|---|
| 35 | $r = array(); |
|---|
| 36 | $db->query("SELECT * FROM authorised_ip WHERE uid='$cuid';"); |
|---|
| 37 | while ($db->next_record()) { |
|---|
| 38 | $r[$db->f('id')]=$db->Record; |
|---|
| 39 | if ( (checkip($db->f('ip')) && $db->f('subnet') == 32) || |
|---|
| 40 | (checkipv6($db->f('ip')) && $db->f('subnet') == 128) ) { |
|---|
| 41 | $r[$db->f('id')]['ip_human']=$db->f('ip'); |
|---|
| 42 | } else { |
|---|
| 43 | $r[$db->f('id')]['ip_human']=$db->f('ip')."/".$db->f('subnet'); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | } |
|---|
| 47 | return $r; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /* |
|---|
| 51 | * Supprime une IP des IP de l'utilisateur |
|---|
| 52 | * et supprime les droits attaché en cascade |
|---|
| 53 | * |
|---|
| 54 | * @param integer $id id de la ligne à supprimer |
|---|
| 55 | * @return boolean Retourne FALSE si erreur, sinon TRUE |
|---|
| 56 | */ |
|---|
| 57 | function ip_delete($id) { |
|---|
| 58 | global $db, $cuid; |
|---|
| 59 | $id=intval($id); |
|---|
| 60 | |
|---|
| 61 | $db->query("SELECT id FROM authorised_ip_affected where authorised_ip_id ='$id';"); |
|---|
| 62 | while ($db->next_record()) { |
|---|
| 63 | $this->ip_affected_delete($db->f('id')); |
|---|
| 64 | } |
|---|
| 65 | if (! $db->query("delete from authorised_ip where id='$id' and uid='$cuid' limit 1;") ) { |
|---|
| 66 | echo "query failed: ".$db->Error; |
|---|
| 67 | return false; |
|---|
| 68 | } |
|---|
| 69 | return true; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /* |
|---|
| 73 | * Sauvegarde une IP dans les IP authorisée |
|---|
| 74 | * |
|---|
| 75 | * @param integer $id id de la ligne à modifier. Si vide ou |
|---|
| 76 | * égal à 0, alors c'est une insertion |
|---|
| 77 | * @param string $ipsub IP (v4 ou v6), potentiellement avec un subnet ( /24) |
|---|
| 78 | * @param string $infos commentaire pour l'utilisateur |
|---|
| 79 | * @param integer $uid Si $uid=0 et qu'on est super-admin, insertion avec uid=0 |
|---|
| 80 | * ce qui correspond a une ip toujours authorisée |
|---|
| 81 | * @return boolean Retourne FALSE si erreur, sinon TRUE |
|---|
| 82 | */ |
|---|
| 83 | function ip_save($id, $ipsub, $infos, $uid=null) { |
|---|
| 84 | global $db, $mem; |
|---|
| 85 | |
|---|
| 86 | // If we ask for uid=0, we have to check to be super-user |
|---|
| 87 | // else, juste use global cuid; |
|---|
| 88 | if ($uid === 0 && $mem->checkRight() ) { |
|---|
| 89 | $cuid=0; |
|---|
| 90 | } else { |
|---|
| 91 | global $cuid; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | $id=intval($id); |
|---|
| 95 | $infos=mysql_real_escape_string($infos); |
|---|
| 96 | |
|---|
| 97 | // Extract subnet from ipsub |
|---|
| 98 | $tmp=explode('/',$ipsub); |
|---|
| 99 | $ip=$tmp[0]; |
|---|
| 100 | $subnet=intval($tmp[1]); |
|---|
| 101 | |
|---|
| 102 | // Error if $ip not an IP |
|---|
| 103 | if ( ! checkip($ip) && ! checkipv6($ip) ) { |
|---|
| 104 | echo "Failed : not an IP address"; |
|---|
| 105 | return false; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | // Check the subnet, if not defined, give a /32 or a /128 |
|---|
| 109 | if ( ! $subnet ) { |
|---|
| 110 | if ( checkip($ip) ) $subnet=32; |
|---|
| 111 | else $subnet=128; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | // An IPv4 can't have subnet > 32 |
|---|
| 115 | if (checkip($ip) && $subnet > 32 ) $subnet=32; |
|---|
| 116 | |
|---|
| 117 | if ($id) { // Update |
|---|
| 118 | $list_affected = $this->list_affected($id); |
|---|
| 119 | foreach($list_affected as $k => $v) { |
|---|
| 120 | $this->call_hooks("authip_on_delete", $k ); |
|---|
| 121 | } |
|---|
| 122 | if (! $db->query("update authorised_ip set ip='$ip', subnet='$subnet', infos='$infos' where id='$id' and uid='$cuid' ;") ) { |
|---|
| 123 | echo "query failed: ".$db->Error; |
|---|
| 124 | return false; |
|---|
| 125 | } |
|---|
| 126 | foreach($list_affected as $k => $v) { |
|---|
| 127 | $this->call_hooks("authip_on_create", $k ); |
|---|
| 128 | } |
|---|
| 129 | } else { // Insert |
|---|
| 130 | if (! $db->query("insert into authorised_ip (uid, ip, subnet, infos) values ('$cuid', '$ip', '$subnet', '$infos' );") ) { |
|---|
| 131 | echo "query failed: ".$db->Error; |
|---|
| 132 | return false; |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | return true; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | /* |
|---|
| 139 | * Fonction appelée par Alternc lors de la suppression d'un utilisateur |
|---|
| 140 | * |
|---|
| 141 | * @return boolean Retourne TRUE |
|---|
| 142 | */ |
|---|
| 143 | function alternc_del_member() { |
|---|
| 144 | global $cuid,$db; |
|---|
| 145 | $db->query("SELECT id FROM authorised_ip WHERE uid ='$cuid';"); |
|---|
| 146 | while ($db->next_record()) { |
|---|
| 147 | $this->ip_delete($db->f('id')); |
|---|
| 148 | } |
|---|
| 149 | return true; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | /* |
|---|
| 154 | * Analyse les classes et récupéres les informations |
|---|
| 155 | * des classes voulant de la restriction IP |
|---|
| 156 | * |
|---|
| 157 | * @return array Retourne un tableau compliqué |
|---|
| 158 | */ |
|---|
| 159 | function get_auth_class() { |
|---|
| 160 | global $classes; |
|---|
| 161 | $authclass=array(); |
|---|
| 162 | |
|---|
| 163 | foreach ($classes as $c) { |
|---|
| 164 | global $$c; |
|---|
| 165 | if ( method_exists($$c, "authip_class") ) { |
|---|
| 166 | $a=$$c->authip_class(); |
|---|
| 167 | $a['class']=$c; |
|---|
| 168 | $authclass[$a['protocol']]=$a; |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | return $authclass; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /* |
|---|
| 175 | * Enregistre ou modifie une affectation ip<=>ressource |
|---|
| 176 | * Nota : lance des hooks sur la classe correspondante pour |
|---|
| 177 | * informer de l'édition/création |
|---|
| 178 | * |
|---|
| 179 | * @param integer $authorised_ip_id id de l'ip affecté |
|---|
| 180 | * @param string $protocol nom du protocole (définie dans la classe correspondante) |
|---|
| 181 | * @param string $parameters information propre au protocole |
|---|
| 182 | * @param integer $id présent si c'est une édition |
|---|
| 183 | * @return boolean Retourne FALSE si erreur, sinon TRUE |
|---|
| 184 | */ |
|---|
| 185 | function ip_affected_save($authorised_ip_id, $protocol, $parameters, $id=null) { |
|---|
| 186 | global $db; |
|---|
| 187 | $authorised_ip_id=intval($authorised_ip_id); |
|---|
| 188 | $protocol=mysql_real_escape_string($protocol); |
|---|
| 189 | $parameters=mysql_real_escape_string($parameters); |
|---|
| 190 | |
|---|
| 191 | if ($id) { |
|---|
| 192 | $id=intval($id); |
|---|
| 193 | $this->call_hooks("authip_on_delete", $id ); |
|---|
| 194 | if (! $db->query("update authorised_ip_affected set authorised_ip_id='$authorised_ip_id', protocol='$protocol', parameters='$parameters' where id ='$id' limit 1;") ) { |
|---|
| 195 | echo "query failed: ".$db->Error; |
|---|
| 196 | return false; |
|---|
| 197 | } |
|---|
| 198 | $this->call_hooks("authip_on_create", $id ); |
|---|
| 199 | } else { |
|---|
| 200 | if (! $db->query("insert into authorised_ip_affected (authorised_ip_id, protocol, parameters) values ('$authorised_ip_id', '$protocol', '$parameters');") ) { |
|---|
| 201 | echo "query failed: ".$db->Error; |
|---|
| 202 | return false; |
|---|
| 203 | } |
|---|
| 204 | $this->call_hooks("authip_on_create", mysql_insert_id() ); |
|---|
| 205 | } |
|---|
| 206 | return true; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | /* |
|---|
| 210 | * Supprime une affectation ip<=>ressource |
|---|
| 211 | * Nota : lance des hooks dans la classe correspondante |
|---|
| 212 | * pour informer de la suppression |
|---|
| 213 | * |
|---|
| 214 | * @param integer $id id de la ligne à supprimer |
|---|
| 215 | * @return boolean Retourne FALSE si erreur, sinon TRUE |
|---|
| 216 | */ |
|---|
| 217 | function ip_affected_delete($id) { |
|---|
| 218 | global $db; |
|---|
| 219 | $id=intval($id); |
|---|
| 220 | |
|---|
| 221 | // Call hooks |
|---|
| 222 | $this->call_hooks("authip_on_delete", $id ); |
|---|
| 223 | |
|---|
| 224 | if (! $db->query("delete from authorised_ip_affected where id='$id' limit 1;") ) { |
|---|
| 225 | echo "query failed: ".$db->Error; |
|---|
| 226 | return false; |
|---|
| 227 | } |
|---|
| 228 | return true; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | /* |
|---|
| 233 | * Appel les hooks demandé avec en parametres les |
|---|
| 234 | * affectationt ip<=>ressource dont l'id est en parametre |
|---|
| 235 | * |
|---|
| 236 | * @param string $function nom de la fonction a rechercher et appeller dans les classes |
|---|
| 237 | * @param integer $affectation_id id de l'affectation correspondante |
|---|
| 238 | * @return boolean Retourne TRUE |
|---|
| 239 | */ |
|---|
| 240 | function call_hooks($function, $affectation_id) { |
|---|
| 241 | $d = $this->list_affected(); |
|---|
| 242 | $affectation = $d[$affectation_id]; |
|---|
| 243 | $e = $this->get_auth_class(); |
|---|
| 244 | $c = $e[$affectation['protocol']]['class']; |
|---|
| 245 | global $$c; |
|---|
| 246 | if ( method_exists($$c, $function) ) { |
|---|
| 247 | $$c->$function($affectation); |
|---|
| 248 | } |
|---|
| 249 | return true; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | /* |
|---|
| 253 | * Liste les affectation ip<=>ressource d'un utilisateur |
|---|
| 254 | * |
|---|
| 255 | * @return array Retourne un tableau de valeurs |
|---|
| 256 | */ |
|---|
| 257 | function list_affected($ip_id=null) { |
|---|
| 258 | global $db, $cuid; |
|---|
| 259 | |
|---|
| 260 | $r = array(); |
|---|
| 261 | if ( is_null($ip_id) ) { |
|---|
| 262 | $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;"); |
|---|
| 263 | } else { |
|---|
| 264 | $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;"); |
|---|
| 265 | } |
|---|
| 266 | while ($db->next_record()) { |
|---|
| 267 | $r[$db->f('id')]=$db->Record; |
|---|
| 268 | } |
|---|
| 269 | return $r; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | }; /* Classe m_authip */ |
|---|