Important alert: (current site time 7/15/2013 11:22:05 AM EDT)
 

VB icon

htaccess

Email
Submitted on: 8/13/2002 3:46:22 PM
By: Sven Wagener 
Level: Advanced
User Rating: By 3 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 27590
 
     The htaccess class manages the htaccess functions of Apache Webservers.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: htaccess
// Description:The htaccess class manages the htaccess functions of Apache Webservers.
// By: Sven Wagener
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=707&lngWId=8//for details.//**************************************

<?
/**
* Class for handling htaccess of Apache
* @authorSven Wagener <sven.wagener@intertribe.de>
* @copyright Intertribe - Internetservices Germany
* @include 	 Funktion:_include_
*/
class htaccess{
var $fHtaccess=""; // path and filename for htaccess file
var $fHtgroup=""; // path and filename for htgroup file
var $fPasswd="";// path and filename for passwd file
var $authType="Basic"; // Default authentification type
var $authName="Internal area"; // Default authentification name
/**
* Initialising class htaccess
*/
function htaccess(){
}
/**
* Sets the filename and path of .htaccess to work with
* @param string	$filenamethe name of htaccess file
*/
function setFHtaccess($filename){
$this->fHtaccess=$filename;
}
/**
* Sets the filename and path of the htgroup file for the htaccess file
* @param string	$filenamethe name of htgroup file
*/
function setFHtgroup($filename){
$this->fHtgroup=$filename;
}
/**
* Sets the filename and path of the password file for the htaccess file
* @param string	$filenamethe name of htgroup file
*/
function setFPasswd($filename){
$this->fPasswd=$filename;
}
/**
* Adds a user to the password file
* @param string $username Username
* @param string $password Password for Username
* @param string $groupGroupname for User (optional)
* @return boolean $created Returns true if user have been created otherwise false
*/
function addUser($username,$password,$group){
// checking if user already exists
$file=@fopen($this->fPasswd,"r");
$isAlready=false;
while($line=@fgets($file,200)){
$lineArr=explode(":",$line);
if($username==$lineArr[0]){
$isAlready=true;
 }
}
if($isAlready==false){
$file=fopen($this->fPasswd,"a");
$password=crypt($password);
$newLine=$username.":".$password."\n";
fputs($file,$newLine);
fclose($file);
return true;
}else{
return false;
}
}
/**
* Adds a group to the htgroup file
* @param string $groupname Groupname
*/
function addGroup($groupname){
$file=fopen($this->fHtgroup,"a");
fclose($file);
}
/**
* Deletes a user in the password file
* @param string $username Username to delete
* @return boolean $deletedReturns true if user have been deleted otherwise false
*/
function delUser($username){
// Reading names from file
$file=fopen($path.$this->fPasswd,"r");
$i=0;
while($line=fgets($file,200)){
$lineArr=explode(":",$line);
if($username!=$lineArr[0]){
$newUserlist[$i][0]=$lineArr[0];
$newUserlist[$i][1]=$lineArr[1];
$i++;
}else{
$deleted=true;
}
}
fclose($file);
// Writing names back to file (without the user to delete)
$file=fopen($path.$this->fPasswd,"w");
for($i=0;$i<count($newUserlist);$i++){
fputs($file,$newUserlist[$i][0].":".$newUserlist[$i][0]."\n");
}
fclose($file);
if($deleted==true){
return true;
}else{
return false;
}
}
/**
* Returns an array of all users in a password file
* @return array $users All usernames of a password file in an array
* @see setFPasswd()
*/
function getUsers(){
}
/**
* Sets a password to the given username
* @param string $username The name of the User for changing password
* @param string $password New Password for the User
* @return boolean $isSet Returns true if password have been set
*/
function setPasswd($username,$new_password){
// Reading names from file
$newUserlist="";
$file=fopen($this->fPasswd,"r");
$x=0;
for($i=0;$line=fgets($file,200);$i++){
$lineArr=explode(":",$line);
if($username!=$lineArr[0] && $lineArr[0]!="" && $lineArr[1]!=""){
$newUserlist[$i][0]=$lineArr[0];
$newUserlist[$i][1]=$lineArr[1];
$x++;
}else if($lineArr[0]!="" && $lineArr[1]!=""){
$newUserlist[$i][0]=$lineArr[0];
$newUserlist[$i][1]=crypt($new_password)."\n";
$isSet=true;
$x++;
}
}
fclose($file);
unlink($this->fPasswd);
/// Writing names back to file (with new password)
$file=fopen($this->fPasswd,"w");
for($i=0;$i<count($newUserlist);$i++){
$content=$newUserlist[$i][0].":".$newUserlist[$i][1];
fputs($file,$content);
}
fclose($file);
if($isSet==true){
return true;
}else{
return false;
}
}
/**
* Sets the Authentification type for Login
* @param string $authtype Authentification type as string
*/
function setAuthType($authtype){
$this->authType=$authtype;
}
/**
* Sets the Authentification Name (Name of the login area)
* @param string $authname Name of the login area
 	*/
function setAuthName($authname){
$this->authName=$authname;
}
/**
* Writes the htaccess file to the given Directory and protects it
* @see setFhtaccess()
 	*/
function addLogin(){
$file=fopen($this->fHtaccess,"w+");
fputs($file,"Order allow,deny\n");
fputs($file,"Allow from all\n");
fputs($file,"AuthType".$this->authType."\n");
fputs($file,"AuthUserFile".$this->fPasswd."\n\n");
fputs($file,"AuthName\"".$this->authName."\"\n");
fputs($file,"require valid-user\n");
fclose($file);
}
/**
* Deletes the protection of the given directory
* @see setFhtaccess()
*/
function delLogin(){
unlink($this->fHtaccess);
}
}
?>


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/14/2002 5:12:58 AMDave Lambert

Very useful. Thanks!
(If this comment was disrespectful, please report it.)

 
8/23/2002 10:38:28 AMJames

hi nice code but too complicated for me . . i'm a php virgin n was wondering if the author could right me a php script that would check htaccess username and password when a user enter his username and password from a form
(If this comment was disrespectful, please report it.)

 
5/18/2003 4:16:44 AMtroubleshootsdotnet

James is the perfect example of ignorance in effect. The code is commented, and it does exactly what he wanted in a script. Sigh.


:)
(If this comment was disrespectful, please report it.)

 
6/12/2003 1:20:24 PM

Hmmm i cant seem to get it to work :( I get a blank screen and thats it. It does nothing???

Can smebody help me out?

Thanks in advance

xander
(If this comment was disrespectful, please report it.)

 
3/17/2004 9:21:48 AMOliver Johnson Schofield

An absolutely masterful piece of coding.

Well done.
(If this comment was disrespectful, please report it.)

 
12/10/2005 11:23:16 PMShakil

I m very new in PHP.
But I think, i can understand this.
Thanks.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.