<?php
$__tmp_access__ = array("authtype"=>"AuthType", "authname"=>"AuthName",
"authuserfile"=>"AuthUserFile",
"require"=>"Require");
class apache_auth
{
private $htaccess = '.htaccess';
private $htpasswd = '.htpasswd';
public $authname = '"Login and password"';
public $authtype = "Basic";
public $require = "valid-user";
public $authuserfile = "";
public $htaccesspath = "";
public $htaccess_save = false;
public $htpasswd_save = false;
public $userlist = array(); public $htaccesslist = array();
public function __construct($path = __PATH__)
{
{
$data_file = file("{$path}{$this->htaccess}"); foreach($data_file as $item)
{
list
($name, $value) = explode(' ',$item); if($name == "authuserfile")
$this->authuserfile = trim($value); else
if($name == "authname")
$this->authname = trim($value); else
if($name == "authtype")
$this->authtype = trim($value); else
if($name == "require")
$this->require = trim($value); else $this->htaccesslist[]="{$item}";
}
$this->loadhtpasswd();
} else
{
$this->htaccess_save = true;
$this->authuserfile = "{$path}/{$this->htpasswd}";
$this->htaccesspath = "{$path}/{$this->htaccess}";
}
}
public function savehtaccess($newpath = "")
{
$path = $newpath != "" ? $newpath : $this->htaccesspath;
if($f)
{
foreach($__tmp_access__ as $name => $value)
fwrite($f,"{$value} {$this->$name}r\n");
foreach($this->htaccesslist as $item)
$this->htaccess_save = false;
} else die("nie mogę utworzyć pliku {$path}"); }
public function loadhtpasswd()
{
{
$dane = file($this->authuserfile); foreach($dane as $item)
{
list
($username, $password) = explode(':',$item); $this->userlist[$username]=$password;
}
}
}
public function savehtpasswd($newpath = "")
{
$path = $newpath != "" ? $newpath : $this->authuserfile;
if($f)
{
foreach($this->userlist as $name => $password)
fwrite($f,"$name:$passwordr\n"); if($newpath == $path)
$this->htaccess_save = true;
$this->htpasswd_save = false;
} else die("nie mogę utworzyć pliku {$path}"); }
public function adduser($username, $password)
{
if(!$this->userexists($username))
{
$this->userlist[$username] = crypt($password,CRYPT_STD_DES
); $this->htpasswd_save = true;
}
}
public function checkuser($username, $password)
{
return $this->userlist[$username] == crypt($password,CRYPT_STD_DES
); }
public function changepassword($username, $password)
{
if($this->userexists($username))
{
$this->userlist[$username] = crypt($password,CRYPT_STD_DES
); $this->htpasswd_save = true;
}
}
public function deluser($username)
{
foreach($this->userlist as $user => $password)
if($user != $username)
$tab[$user]=$password;
$this->userlist = $tab;
$this->htpasswd_save = true;
}
public function userexists($username)
{
return !empty($this->userlist[$username]); }
}
?>
info z rozwiązaniem do posta
"katalog na hasło" aby użytkownikowi taki katalog podlinkować tak by się nie kapnął to wystarczy do strony dodac obrazek z katalogu zabezpieczonego hasłem <img href="http://user:pass@domena.pl/img.jpg" />, w js i = new Images(); i.src='http://user:pass@domena.pl/img.jpg' i po tym można śmiało dawać odnośniki do innych plików
wiadome użytkownicy zaawansowani się kapną jak to jest pod linkowywanie i poznają zaraz użytkowników i hasła, wiec w dołączoną wyżej klasą można łatwo na tym operować by zmieniać użytkowników i im hasła (IMG:
http://forum.php.pl/style_emoticons/default/smile.gif) . bardzo dobrze rozwiązuje to problem o który często widzę posty, czyli udostępnianie plików w sieci zalogowanym użytkownikom
Ten post edytował okitoki 7.05.2009, 18:17:16