<?php
/***************************************************************************
____ _ _ ____ _ _ _ _ _ _
| _ \| | | | _ \| |_ ___ ___ | |___| || | | | | |
| |_) | |_| | |_) | __/ _ \ / _ \| / __| || |_| | | |
| __/| _ | __/| || (_) | (_) | \__ \__ _| |_| |
|_| |_| |_|_| \__\___/ \___/|_|___/ |_| \___/
htaccess_generator.php - An .htaccess/ htpasswd counter
-------------------
begin : Fri Feb 21 2003
copyright : (C) 2001 PHPtools4U.com - laurent goussard
email : support@phptools4u.com
***************************************************************************/
/***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
***************************************************************************/
//error_reporting(E_ALL);
function gen_htaccess($vars){
Global $_SERVER;
$vars['htpasswd'] = (isset($vars['htpasswd'])) ? $vars['htpasswd'] : '.htpasswd';
$vars['domaine'] = (isset($vars['domaine'])) ? stripslashes($vars['domaine']) : 'Oui, qui c\'est ?';
$out = 'AuthUserFile ';
$out .= $vars['path'].$vars['htpasswd']."\n";
$out .= 'AuthName "'.$vars['domaine'].'"'."\n";
$out .= 'AuthType Basic'."\n\n";
$out .= '<limit GET POST>'."\n";
$out .= 'require valid-user'."\n";
$out .= '</limit>';
return $out;
}
function display_file($pass){
Global $_POST;
$out = 'Il semble que l\'écriture soit vérouillée sur ce serveur...<br>PHP n\'a pas pu écrire les fichier et vous allez devoir le faire vous-même :<br><br>';
$out .= 'Enregistrer le contenu ci-dessous dans un fichier .htaccess<br><i>(attention, le chemin vers le fichier '.$_POST['htpasswd'].' pointe vers un répertoire "export_htaccess" qui n\'existe surement pas... Il faudra modifier ce chemin...)</i><br>';
$out .= '<textarea name="access" cols="80" rows="10" class="trou">';
$out .= gen_htaccess($_POST);
$out .= '</textarea>';
$out .= '<br><br>';
$out .= 'Enregistrer le contenu ci-dessous dans un fichier '.$_POST['htpasswd'].'<br>';
$out .= '<textarea name="passwd" cols="80" rows="10" class="trou">';
$out .= $_POST['login'].':'.$pass['pwd'];
$out .= '</textarea><br>';
return $out;
}
function save_file($fichier, $contenu){
$fp = @fopen($fichier, "w+");
if(!$fp){
$out = FALSE;
}else{
fputs($fp, $contenu);
fclose($fp);
$out = $fichier.' enregistré';
}
return $out;
}
function set_pass(){
Global $_SERVER,$_POST;
if( ereg('win', strtolower($_SERVER['SERVER_SOFTWARE']) ) ){ // Windows
$pass['pwd'] = $_POST['passwd'];
$pass['msg'] = 'Mot de passe non encrypté (incompatibilité d\'encryption entre PHP et Apache sous Windows®...)';
}else{ // DES
$salt = substr($_POST['passwd'],0,2);
$pass['pwd'] = crypt($_POST['passwd'], $salt);
$pass['msg'] = 'Mot de passe encrypté en DES (serveur Unix)';
}
return $pass;
}
?>
<title>.htaccess</title>
<style type="text/css">
<!--
.titre {
border: 2px groove #FFFFFF;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
font-weight: bold;
color: #FFFFFF;
background-color: #333333;
text-align: center;
}
.normal {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
background-color: #EDECE9;
border: 2px groove #FFFFFF;
}
input {
border: 1px solid #999999;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
}
.msg {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
background-color: #F7DFDF;
border: 2px groove #FFFFFF;
}
.trou {
border: 1px solid #999999;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
}
-->
</style>
<body bgcolor="#D4D0C8" text="#333333">
<form action="<? echo $_SERVER['REQUEST_URI'] ?>" method="POST" name="generateur">
<table width="400" border="0" align="center" cellpadding="5" cellspacing="0" class="titre">
<tr>
<td>generateur de fichier .htaccess/.htpasswd</td>
</tr>
</table> <br>
<table width="400" border="0" align="center" cellpadding="2" cellspacing="0" class="normal">
<tr>
<td width="50%">nom du fichier de mot de passe </td>
<td align="center">
<input name="htpasswd" type="text" value=".htpasswd" size="30">
</td>
</tr>
</table>
<br>
<table width="400" border="0" align="center" cellpadding="2" cellspacing="0" class="normal">
<tr>
<td width="50%">domaine</td>
<td align="center">
<input name="domaine" type="text" value="Oui, qui c'est ?" size="30">
</td>
</tr>
</table>
<br>
<table width="400" border="0" align="center" cellpadding="2" cellspacing="0" class="normal">
<tr>
<td width="50%">login</td>
<td align="center">
<input name="login" type="text" size="30">
</td>
</tr>
</table>
<br>
<table width="400" border="0" align="center" cellpadding="2" cellspacing="0" class="normal">
<tr>
<td width="50%">mot de passe </td>
<td align="center">
<input name="passwd" type="text" size="30">
</td>
</tr>
</table>
<br>
<center>
<input name="Submit" type="reset" value="Rétablir" id="Submit2">
<input name="Submit" type="submit" value="Générer" id="Submit2">
</center>
</form>
<?php
if(isset($_GET['rep'])){
$_POST['path'] = $_GET['rep'];
}else{
$nom = 'export_htaccess';
if( ereg('win', strtolower($_SERVER['SERVER_SOFTWARE']) ) ){
$_POST['path'] = getcwd().'\\'.$nom.'\\';
}else{
$_POST['path'] = getcwd().'/'.$nom.'/';
}
if(!is_dir($_POST['path'])){
@mkdir($_POST['path']);
}
}
if(isset($_POST['login'])){
$pass = set_pass();
$res_htac = save_file($_POST['path'].'.htaccess', gen_htaccess($_POST));
$res_htpa = save_file($_POST['path'].$_POST['htpasswd'], $_POST['login'].':'.$pass['pwd']);
if($res_htac == FALSE){
$res_htac = display_file($pass);
}
echo ' <table width="400" border="0" align="center" cellpadding="2" cellspacing="0" class="msg">'."\n";
echo ' <tr> '."\n";
echo ' <td colspan="2">'."\n";
echo $res_htac;
echo '<br>';
echo $res_htpa;
echo '<br>';
echo '<i>'.$pass['msg'].'</i>';
echo '<br>';
echo ' </td>'."\n";
echo ' </tr>'."\n";
echo ' </table>'."\n";
}else{
$_POST['login'] = '';
$_POST['passwd'] = '';
}
?>