Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP5] Katalog na hasło cd........ problem rozwiązany
Forum PHP.pl > Inne > Oceny
okitoki
  1. <?php
  2. define('__PATH__',dirname($_SERVER['SCRIPT_FILENAME']));
  3. $__tmp_access__ = array("authtype"=>"AuthType",
  4.                                    "authname"=>"AuthName",
  5.                                    "authuserfile"=>"AuthUserFile",
  6.                                    "require"=>"Require");
  7.  
  8.  
  9. class apache_auth
  10. {
  11.     private $htaccess = '.htaccess';
  12.     private $htpasswd = '.htpasswd';
  13.    public $authname = '"Login and password"';
  14.    public $authtype = "Basic";
  15.    public $require = "valid-user";
  16.    public $authuserfile = "";
  17.    public $htaccesspath = "";
  18.    public $htaccess_save = false;
  19.    public $htpasswd_save = false;
  20.    public $userlist = array();
  21.    public $htaccesslist = array();
  22.    
  23.     public function __construct($path = __PATH__)
  24.     {
  25.         if(file_exists("{$path}{$this->htaccess}"))
  26.         {
  27.             $data_file = file("{$path}{$this->htaccess}");
  28.             foreach($data_file as $item)
  29.             {
  30.              $item=str_replace("r",'',$item);
  31.              $item=str_replace("\n",'',$item);
  32.              list($name, $value) = explode(' ',$item);
  33.             $name = trim(strtolower($name));
  34.              if($name == "authuserfile")
  35.                  $this->authuserfile = trim($value);
  36.              else    
  37.              if($name == "authname")
  38.                  $this->authname = trim($value);
  39.              else
  40.              if($name == "authtype")
  41.                  $this->authtype = trim($value);
  42.              else
  43.              if($name == "require")
  44.                  $this->require = trim($value);
  45.              else $this->htaccesslist[]="{$item}";    
  46.             }
  47.             $this->loadhtpasswd();
  48.         } else
  49.         {
  50.             $this->htaccess_save = true;
  51.             $this->authuserfile = "{$path}/{$this->htpasswd}";
  52.             $this->htaccesspath = "{$path}/{$this->htaccess}";
  53.         }
  54.     }
  55.    
  56.     public function savehtaccess($newpath = "")
  57.     {
  58.         global $__tmp_access__;
  59.         $path = $newpath != "" ? $newpath : $this->htaccesspath;
  60.         $f = fopen($path,"w+");
  61.         if($f)
  62.         {
  63.             foreach($__tmp_access__ as $name => $value)
  64.                  fwrite($f,"{$value} {$this->$name}r\n");
  65.              
  66.             foreach($this->htaccesslist as $item)
  67.               fwrite($f,"$itemr\n");
  68.              
  69.             fclose($f);
  70.             $this->htaccess_save = false;
  71.         } else die("nie mogę utworzyć pliku {$path}");
  72.     }
  73.    
  74.     public function loadhtpasswd()
  75.     {
  76.         if(file_exists($this->authuserfile))
  77.         {
  78.             $dane = file($this->authuserfile);
  79.             foreach($dane as $item)
  80.             {
  81.                 list($username, $password) = explode(':',$item);
  82.                 $password=str_replace("r",'',$password);
  83.                 $password=str_replace("\n",'',$password);
  84.                 $this->userlist[$username]=$password;
  85.             }
  86.            
  87.         }
  88.     }
  89.    
  90.     public function savehtpasswd($newpath = "")
  91.     {
  92.         $path = $newpath != "" ? $newpath : $this->authuserfile;
  93.         $f = fopen($path,"w+");
  94.         if($f)
  95.         {
  96.             foreach($this->userlist as $name => $password)
  97.               fwrite($f,"$name:$passwordr\n");
  98.             fclose($f);
  99.             if($newpath == $path)
  100.                 $this->htaccess_save = true;
  101.             $this->htpasswd_save = false;
  102.         } else die("nie mogę utworzyć pliku {$path}");
  103.     }
  104.    
  105.     public function adduser($username, $password)
  106.     {
  107.         if(!$this->userexists($username))
  108.         {
  109.             $this->userlist[$username] = crypt($password,CRYPT_STD_DES);
  110.             $this->htpasswd_save = true;
  111.         }
  112.     }
  113.    
  114.     public function checkuser($username, $password)
  115.     {
  116.         return $this->userlist[$username] == crypt($password,CRYPT_STD_DES);
  117.     }
  118.    
  119.     public function changepassword($username, $password)
  120.     {
  121.         if($this->userexists($username))
  122.         {
  123.             $this->userlist[$username] = crypt($password,CRYPT_STD_DES);
  124.             $this->htpasswd_save = true;
  125.         }
  126.      }
  127.    
  128.     public function deluser($username)
  129.     {
  130.         $tab = array();
  131.         foreach($this->userlist as $user => $password)
  132.            if($user != $username)
  133.              $tab[$user]=$password;
  134.         $this->userlist = $tab;
  135.         $this->htpasswd_save = true;
  136.     }
  137.    
  138.     public function userexists($username)
  139.     {
  140.         return !empty($this->userlist[$username]);
  141.     }
  142.    
  143.    
  144. }
  145. ?>


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 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
tiraeth
Przenoszę na Oceny.
in5ane
Może jakiś praktyczny przykład pokażesz?
okitoki
Cytat(in5ane @ 9.05.2009, 16:23:58 ) *
Może jakiś praktyczny przykład pokażesz?



to znaczy ?

Kod
require_once 'apache_auth.php';

$ht = new apache_auth("/home/pawel/www/tajne");
$ht->adduser('test','test1'); //dodawanie użytkownika
$ht->checkuser('test','test1'); // czy jest taki login i hasło
$ht->changepassword('test','nowe haslo'); //zmana hasla
$ht->userexists('test');// czy jest uzytkownik
$ht->deluser('test');// kasowanie uzytownika
$ht->savehtpasswd(); // zapisz plik .htpasswd
$ht->savehtaccess(); // zapisz plik .htaccess
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.