| [1] | 1 | <?php |
|---|
| 2 | /* |
|---|
| [789] | 3 | $Id: m_ftp.php,v 1.12 2005/12/18 09:51:32 benjamin Exp $ |
|---|
| [1] | 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: Benjamin Sonntag |
|---|
| 20 | Purpose of file: Manage FTP accounts |
|---|
| 21 | ---------------------------------------------------------------------- |
|---|
| 22 | */ |
|---|
| 23 | /** |
|---|
| 24 | * Classe de gestion des comptes FTP de l'hébergé. |
|---|
| 25 | * |
|---|
| [102] | 26 | * Cette classe permet de gérer les Comptes FTP d'un membre hébergé.<br /> |
|---|
| [1] | 27 | * Copyleft {@link http://alternc.net/ AlternC Team} |
|---|
| 28 | * |
|---|
| 29 | * @copyright AlternC-Team 2002-11-01 http://alternc.net/ |
|---|
| 30 | * |
|---|
| 31 | */ |
|---|
| 32 | class m_ftp { |
|---|
| 33 | |
|---|
| 34 | /* ----------------------------------------------------------------- */ |
|---|
| 35 | /** |
|---|
| 36 | * Constructeur |
|---|
| 37 | */ |
|---|
| [222] | 38 | function m_ftp() { |
|---|
| [1] | 39 | } |
|---|
| 40 | |
|---|
| 41 | /* ----------------------------------------------------------------- */ |
|---|
| [222] | 42 | /** |
|---|
| 43 | * Quota name |
|---|
| 44 | */ |
|---|
| 45 | function alternc_quota_names() { |
|---|
| 46 | return "ftp"; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | /* ----------------------------------------------------------------- */ |
|---|
| [1] | 50 | /** Retourne la liste des comptes FTP du compte hébergé |
|---|
| 51 | * Retourne la liste des comptes FTP sous forme de tableau indexé de |
|---|
| 52 | * tableaus associatifs comme suit : |
|---|
| 53 | * $a["id"]= ID du compte ftp |
|---|
| 54 | * $a["login"]= Nom de login du compte |
|---|
| 55 | * $a["pass"]= Mot de passe du compte |
|---|
| 56 | * $a["dir"]= Dossier relatif à la racine du compte de l'utilisateur |
|---|
| 57 | * @return array Retourne le tableau des comptes ou FALSE si une erreur s'est produite. |
|---|
| 58 | */ |
|---|
| 59 | function get_list() { |
|---|
| [222] | 60 | global $db,$err,$cuid; |
|---|
| [1] | 61 | $err->log("ftp","get_list"); |
|---|
| 62 | $r=array(); |
|---|
| [1004] | 63 | $db->query("SELECT id, name, homedir FROM ftpusers WHERE uid='$cuid' ORDER BY homedir;"); |
|---|
| [1] | 64 | if ($db->num_rows()) { |
|---|
| 65 | while ($db->next_record()) { |
|---|
| 66 | // On passe /var/alternc/html/u/user |
|---|
| 67 | $tr=preg_match("/^\/var\/alternc\/html\/.\/[^\/]*\/(.*)$/", $db->f("homedir"),$match); /* " */ |
|---|
| 68 | $r[]=array( |
|---|
| 69 | "id"=>$db->f("id"), |
|---|
| 70 | "login"=>$db->f("name"), |
|---|
| 71 | "dir"=>$match[1] |
|---|
| 72 | ); |
|---|
| 73 | } |
|---|
| 74 | return $r; |
|---|
| 75 | } else { |
|---|
| 76 | $err->raise("ftp",1); |
|---|
| 77 | return false; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | /* ----------------------------------------------------------------- */ |
|---|
| 82 | /** Retourne les détails d'un compte FTP (voir get_list) |
|---|
| 83 | * Le tableau est celui du compte d'id spécifié |
|---|
| 84 | * @param integer $id Numéro du compte dont on souhaite obtenir les détails |
|---|
| 85 | * @return array Tableau associatif contenant les infos du comptes ftp |
|---|
| 86 | */ |
|---|
| 87 | function get_ftp_details($id) { |
|---|
| [222] | 88 | global $db,$err,$cuid; |
|---|
| [1] | 89 | $err->log("ftp","get_ftp_details",$id); |
|---|
| 90 | $r=array(); |
|---|
| [1004] | 91 | $db->query("SELECT id, name, homedir FROM ftpusers WHERE uid='$cuid' AND id='$id';"); |
|---|
| [1] | 92 | if ($db->num_rows()) { |
|---|
| 93 | $db->next_record(); |
|---|
| 94 | $tr=preg_match("/^\/var\/alternc\/html\/.\/[^\/]*\/(.*)$/", $db->f("homedir"),$match); /*"*/ |
|---|
| 95 | $lg=explode("_",$db->f("name")); |
|---|
| 96 | if (!is_array($lg)) { |
|---|
| 97 | $lg[0]=$db->f("name"); |
|---|
| 98 | $lg[1]=""; |
|---|
| 99 | } |
|---|
| 100 | return array( |
|---|
| [1804] | 101 | "id" => $db->f("id"), |
|---|
| 102 | "prefixe" => $lg[0], |
|---|
| 103 | "login" => isset($lg[1]) ? $lg[1] : "", |
|---|
| 104 | "dir" => $match[1] |
|---|
| [1] | 105 | ); |
|---|
| 106 | } else { |
|---|
| 107 | $err->raise("ftp",2); |
|---|
| 108 | return false; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /* ----------------------------------------------------------------- */ |
|---|
| 113 | /** Retourne la liste des prefixes utilisables par le compte courant |
|---|
| 114 | * @return array tableau contenant la liste des prefixes (domaines + login) |
|---|
| 115 | * du compte actuel. |
|---|
| 116 | */ |
|---|
| 117 | function prefix_list() { |
|---|
| [222] | 118 | global $db,$mem,$cuid; |
|---|
| [1] | 119 | $r=array(); |
|---|
| 120 | $r[]=$mem->user["login"]; |
|---|
| [222] | 121 | $db->query("SELECT domaine FROM domaines WHERE compte='$cuid' ORDER BY domaine;"); |
|---|
| [1] | 122 | while ($db->next_record()) { |
|---|
| 123 | $r[]=$db->f("domaine"); |
|---|
| 124 | } |
|---|
| 125 | return $r; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /* ----------------------------------------------------------------- */ |
|---|
| 129 | /** Affiche (ECHO) la liste des prefixes disponibles sous forme de champs d'option |
|---|
| 130 | * Les champs sont affichés sous la forme <option>prefixe</option>... |
|---|
| 131 | * La valeur $current se voit affublée de la balise SELECTED. |
|---|
| 132 | * @param string $current Prefixe sélectionné par défaut |
|---|
| 133 | * @return boolean TRUE. |
|---|
| 134 | */ |
|---|
| 135 | function select_prefix_list($current) { |
|---|
| 136 | $r=$this->prefix_list(); |
|---|
| 137 | reset($r); |
|---|
| 138 | while (list($key,$val)=each($r)) { |
|---|
| [105] | 139 | if ($current==$val) $c=" selected=\"selected\""; else $c=""; |
|---|
| [1] | 140 | echo "<option$c>$val</option>"; |
|---|
| 141 | } |
|---|
| 142 | return true; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /* ----------------------------------------------------------------- */ |
|---|
| 146 | /** Modifie les paramètres du comptes FTP $id. |
|---|
| 147 | * @param integer $id Numéro du compte dont on veut modifier les paramètres |
|---|
| 148 | * @param string $prefixe Prefixe du compte FTP |
|---|
| 149 | * @param string $login login ajouté au préfixe ($prefixe_$login) |
|---|
| 150 | * @param string $pass mot de passe |
|---|
| 151 | * @param string $dir Répertoire racine du compte |
|---|
| 152 | * @return boolean TRUE si le compte a été modifié, FALSE si une erreur est survenue. |
|---|
| 153 | */ |
|---|
| 154 | function put_ftp_details($id,$prefixe,$login,$pass,$dir) { |
|---|
| [222] | 155 | global $mem,$db,$err,$bro,$cuid; |
|---|
| [1] | 156 | $err->log("ftp","put_ftp_details",$id); |
|---|
| [222] | 157 | $db->query("SELECT count(*) AS cnt FROM ftpusers WHERE id='$id' and uid='$cuid';"); |
|---|
| [1] | 158 | $db->next_record(); |
|---|
| 159 | if (!$db->f("cnt")) { |
|---|
| 160 | $err->raise("ftp",2); |
|---|
| 161 | return false; |
|---|
| 162 | } |
|---|
| 163 | $dir=$bro->convertabsolute($dir); |
|---|
| 164 | if (substr($dir,0,1)=="/") { |
|---|
| 165 | $dir=substr($dir,1); |
|---|
| 166 | } |
|---|
| 167 | $r=$this->prefix_list(); |
|---|
| 168 | if (!in_array($prefixe,$r)) { |
|---|
| 169 | $err->raise("ftp",3); |
|---|
| 170 | return false; |
|---|
| 171 | } |
|---|
| 172 | $lo=$mem->user["login"]; |
|---|
| 173 | $l=substr($lo,0,1); |
|---|
| 174 | if ($login) $login="_".$login; |
|---|
| [222] | 175 | $db->query("SELECT COUNT(*) AS cnt FROM ftpusers WHERE id!='$id' AND name='$prefixe$login';"); |
|---|
| [1] | 176 | $db->next_record(); |
|---|
| 177 | if ($db->f("cnt")) { |
|---|
| 178 | $err->raise("ftp",4); |
|---|
| 179 | return false; |
|---|
| 180 | } |
|---|
| 181 | $absolute="/var/alternc/html/".substr($mem->user["login"],0,1)."/".$mem->user["login"]."/$dir"; |
|---|
| 182 | if (!file_exists($absolute)) { |
|---|
| 183 | system("/bin/mkdir -p $absolute"); |
|---|
| 184 | } |
|---|
| 185 | if (!is_dir($absolute)) { |
|---|
| 186 | $err->raise("ftp",6); |
|---|
| 187 | return false; |
|---|
| 188 | } |
|---|
| [1004] | 189 | if (trim($pass)!="********") { |
|---|
| 190 | $db->query("UPDATE ftpusers SET name='".$prefixe.$login."', password='', encrypted_password=ENCRYPT('$pass'), homedir='/var/alternc/html/$l/$lo/$dir', uid='$cuid' WHERE id='$id';"); |
|---|
| 191 | } else { |
|---|
| 192 | $db->query("UPDATE ftpusers SET name='".$prefixe.$login."', homedir='/var/alternc/html/$l/$lo/$dir', uid='$cuid' WHERE id='$id';"); |
|---|
| 193 | } |
|---|
| [1] | 194 | return true; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /* ----------------------------------------------------------------- */ |
|---|
| 198 | /** Efface le compte ftp spécifié. |
|---|
| 199 | * @param integer $id Numéro du compte FTP à supprimer. |
|---|
| 200 | * @return boolean TRUE si le compte a été effacé, FALSE sinon. |
|---|
| 201 | */ |
|---|
| 202 | function delete_ftp($id) { |
|---|
| [430] | 203 | global $db,$err,$cuid; |
|---|
| [1] | 204 | $err->log("ftp","delete_ftp",$id); |
|---|
| [222] | 205 | $db->query("SELECT name FROM ftpusers WHERE id='$id' and uid='$cuid';"); |
|---|
| [1] | 206 | $db->next_record(); |
|---|
| 207 | $name=$db->f("name"); |
|---|
| 208 | if (!$name) { |
|---|
| 209 | $err->raise("ftp",2); |
|---|
| 210 | return false; |
|---|
| 211 | } |
|---|
| [222] | 212 | $db->query("DELETE FROM ftpusers WHERE id='$id'"); |
|---|
| [1] | 213 | return $name; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | /* ----------------------------------------------------------------- */ |
|---|
| 217 | /** Crée un nouveau compte FTP. |
|---|
| 218 | * @param string $prefixe Prefixe au login |
|---|
| 219 | * @param string $login Login ftp (login=prefixe_login) |
|---|
| 220 | * @param string $pass Mot de passe FTP |
|---|
| 221 | * @param string $dir Répertoire racine du compte relatif à la racine du membre |
|---|
| 222 | * @return boolean TRUE si le compte a été créé, FALSE sinon. |
|---|
| 223 | * |
|---|
| 224 | */ |
|---|
| 225 | function add_ftp($prefixe,$login,$pass,$dir) { |
|---|
| [222] | 226 | global $mem,$db,$err,$quota,$bro,$cuid; |
|---|
| [1] | 227 | $err->log("ftp","add_ftp",$prefixe."_".$login); |
|---|
| 228 | $dir=$bro->convertabsolute($dir); |
|---|
| 229 | if (substr($dir,0,1)=="/") { |
|---|
| 230 | $dir=substr($dir,1); |
|---|
| 231 | } |
|---|
| 232 | $r=$this->prefix_list(); |
|---|
| 233 | if (!in_array($prefixe,$r) || $prefixe=="") { |
|---|
| 234 | $err->raise("ftp",3); |
|---|
| 235 | return false; |
|---|
| 236 | } |
|---|
| 237 | if ($login) $login="_".$login; |
|---|
| 238 | $db->query("SELECT count(*) AS cnt FROM ftpusers WHERE name='".$prefixe.$login."'"); |
|---|
| 239 | $db->next_record(); |
|---|
| 240 | if ($db->f("cnt")) { |
|---|
| 241 | $err->raise("ftp",4); |
|---|
| 242 | return false; |
|---|
| 243 | } |
|---|
| [222] | 244 | $db->query("SELECT login FROM membres WHERE uid='$cuid';"); |
|---|
| [1] | 245 | $db->next_record(); |
|---|
| 246 | $lo=$db->f("login"); |
|---|
| 247 | $l=substr($lo,0,1); |
|---|
| 248 | $absolute="/var/alternc/html/".substr($mem->user["login"],0,1)."/".$mem->user["login"]."/$dir"; |
|---|
| 249 | if (!file_exists($absolute)) { |
|---|
| 250 | system("/bin/mkdir -p $absolute"); |
|---|
| 251 | } |
|---|
| 252 | if (!is_dir($absolute)) { |
|---|
| 253 | $err->raise("ftp",6); |
|---|
| 254 | return false; |
|---|
| 255 | } |
|---|
| 256 | if ($quota->cancreate("ftp")) { |
|---|
| [1004] | 257 | $db->query("INSERT INTO ftpusers (name,password, encrypted_password,homedir,uid) VALUES ('".$prefixe.$login."', '', ENCRYPT('$pass'), '/var/alternc/html/$l/$lo/$dir', '$cuid')"); |
|---|
| [1] | 258 | return true; |
|---|
| 259 | } else { |
|---|
| 260 | $err->raise("ftp",5); |
|---|
| 261 | return false; |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | /* ----------------------------------------------------------------- */ |
|---|
| 266 | /** Retourne TRUE si $dir possède un compte FTP |
|---|
| 267 | * @param string $dir Dossier à tester, relatif à la racine du compte courant |
|---|
| 268 | * @return boolean retourne TRUE si $dir à un compte FTP, FALSE sinon. |
|---|
| 269 | */ |
|---|
| 270 | function is_ftp($dir) { |
|---|
| 271 | global $mem,$db,$err; |
|---|
| 272 | $err->log("ftp","is_ftp",$dir); |
|---|
| [222] | 273 | $lo=$mem->user["login"]; |
|---|
| [1] | 274 | $l=substr($lo,0,1); |
|---|
| 275 | if (substr($dir,0,1)=="/") $dir=substr($dir,1); |
|---|
| 276 | $db->query("SELECT id FROM ftpusers WHERE homedir='/var/alternc/html/$l/$lo/$dir';"); |
|---|
| 277 | if ($db->num_rows()) { |
|---|
| 278 | $db->next_record(); |
|---|
| 279 | return $db->f("id"); |
|---|
| 280 | } else { |
|---|
| 281 | return false; |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | /* ----------------------------------------------------------------- */ |
|---|
| 286 | /** Fonction appellée par domains quand un deomaine est supprimé pour le membre |
|---|
| 287 | * @param string $dom Domaine à détruire. |
|---|
| 288 | * @access private |
|---|
| 289 | */ |
|---|
| [45] | 290 | function alternc_del_domain($dom) { |
|---|
| [430] | 291 | global $db,$err,$cuid; |
|---|
| [1] | 292 | $err->log("ftp","del_dom",$dom); |
|---|
| [222] | 293 | $db->query("SELECT COUNT(*) AS cnt FROM ftpusers WHERE uid='$cuid' AND name LIKE '$dom%'"); |
|---|
| [1] | 294 | $db->next_record(); |
|---|
| 295 | $cnt=$db->Record["cnt"]; |
|---|
| [222] | 296 | $db->query("DELETE FROM ftpusers WHERE uid='$cuid' AND name LIKE '$dom%'"); |
|---|
| [1] | 297 | return true; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | /* ----------------------------------------------------------------- */ |
|---|
| 301 | /** Fonction appellée par membres quand un membre est effacé. |
|---|
| 302 | * @param integer $uid Numéro de membre effacé. |
|---|
| 303 | * @access private |
|---|
| 304 | */ |
|---|
| [222] | 305 | function alternc_del_member() { |
|---|
| [430] | 306 | global $db,$err,$cuid; |
|---|
| [222] | 307 | $err->log("ftp","del_member"); |
|---|
| 308 | $db->query("DELETE FROM ftpusers WHERE uid='$cuid'"); |
|---|
| [1] | 309 | return true; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | /* ----------------------------------------------------------------- */ |
|---|
| [1804] | 313 | /** |
|---|
| [428] | 314 | * Returns the used quota for the $name service for the current user. |
|---|
| [1804] | 315 | * @param $name string name of the quota |
|---|
| [428] | 316 | * @return integer the number of service used or false if an error occured |
|---|
| [427] | 317 | * @access private |
|---|
| 318 | */ |
|---|
| [428] | 319 | function alternc_get_quota($name) { |
|---|
| [427] | 320 | global $db,$err,$cuid; |
|---|
| [428] | 321 | if ($name=="ftp") { |
|---|
| 322 | $err->log("ftp","getquota"); |
|---|
| 323 | $db->query("SELECT COUNT(*) AS cnt FROM ftpusers WHERE uid='$cuid'"); |
|---|
| 324 | $db->next_record(); |
|---|
| 325 | return $db->f("cnt"); |
|---|
| 326 | } else return false; |
|---|
| [427] | 327 | } |
|---|
| 328 | |
|---|
| [789] | 329 | |
|---|
| 330 | /* ----------------------------------------------------------------- */ |
|---|
| 331 | /** |
|---|
| 332 | * Exporte toutes les informations ftp du compte AlternC |
|---|
| 333 | * @access private |
|---|
| [1804] | 334 | * EXPERIMENTAL 'sid' function ;) |
|---|
| [789] | 335 | */ |
|---|
| 336 | function alternc_export() { |
|---|
| 337 | global $db,$err; |
|---|
| 338 | $err->log("ftp","export"); |
|---|
| 339 | $f=$this->get_list(); |
|---|
| 340 | $str="<ftp>\n"; |
|---|
| 341 | foreach ($f as $d) { |
|---|
| 342 | $str.=" <account>\n"; |
|---|
| 343 | $str.=" <login>".xml_entities($s[login])."</login>\n"; |
|---|
| 344 | $str.=" <pass>".xml_entities($s[pass])."</pass>\n"; |
|---|
| 345 | $str.=" <dir>".xml_entities($s[dir])."</dir>\n"; |
|---|
| 346 | $str.=" </account>\n"; |
|---|
| 347 | } |
|---|
| 348 | $str.="</ftp>\n"; |
|---|
| 349 | return $str; |
|---|
| 350 | } |
|---|
| [1804] | 351 | |
|---|
| 352 | |
|---|
| [1] | 353 | } /* Class m_ftp */ |
|---|
| 354 | |
|---|
| 355 | ?> |
|---|