| [ Index ] |
|
Code source de Plume CMS 1.2.2 |
1 <?php 2 3 /** 4 * Class for handling htaccess of Apache 5 * @author Sven Wagener <sven.wagener@intertribe.de> 6 * @copyright Intertribe - Internetservices Germany 7 * @include Funktion:_include_ 8 * 9 * @contributor loic d'Anterroches 10 */ 11 12 class htaccess 13 { 14 var $fHtaccess=''; // path and filename for htaccess file 15 var $fHtgroup=''; // path and filename for htgroup file 16 var $fPasswd=''; // path and filename for passwd file 17 18 var $authType='Basic'; // Default authentification type 19 var $authName='Internal area'; // Default authentification name 20 21 var $defaultName='No name'; // Default name of the user 22 var $defaultEmail='No email'; // Default email 23 24 25 /** 26 * Initialising class htaccess 27 */ 28 function htaccess() 29 { 30 } 31 32 /** 33 * Sets the filename and path of .htaccess to work with 34 * @param string $filename the name of htaccess file 35 */ 36 function setFHtaccess($filename) 37 { 38 $this->fHtaccess=$filename; 39 } 40 41 /** 42 * Sets the filename and path of the htgroup file for the htaccess file 43 * @param string $filename the name of htgroup file 44 */ 45 function setFHtgroup($filename) 46 { 47 $this->fHtgroup=$filename; 48 } 49 50 /** 51 * Sets the filename and path of the password file for the htaccess file 52 * @param string $filename the name of htpasswd file 53 */ 54 function setFPasswd($filename) 55 { 56 $this->fPasswd=$filename; 57 } 58 59 /** 60 * Return true if both a .htaccess and a .htpasswd are in the folder 61 * @param string folder 62 */ 63 function isSecured($folder) 64 { 65 $folder = preg_replace('#(/)*$#', '', $folder); 66 if (file_exists($folder.'/.htaccess')) { 67 $this->setFHtaccess($folder.'/.htaccess'); 68 $this->setFPasswd($folder.'/.htpasswd'); 69 return true; 70 } 71 return false; 72 } 73 74 /** 75 * Adds a user to the password file 76 * @param string $username Username 77 * @param string $password Password for Username 78 * @param string $name Real name of the user (optional) 79 * @param string $email Email of the user (optional) 80 * @param string $group Groupname for User (optional) 81 * @return boolean $created Returns true if user have been created otherwise false 82 */ 83 function addUser($username,$password,$name='',$email='',$group='') 84 { 85 // checking if user already exists 86 $file=@fopen($this->fPasswd,'r'); 87 $isAlready=false; 88 while($line=@fgets($file,200)){ 89 $lineArr=explode(':',$line); 90 if($username==$lineArr[0]){ 91 $isAlready=true; 92 } 93 } 94 fclose($file); 95 96 if($isAlready==false){ 97 $file=fopen($this->fPasswd,'a'); 98 99 $password=crypt($password); 100 $newLine=$username.':'.$password; 101 if (strlen($name)) $newLine .= ':'.str_replace(':',' ',$name); 102 if (strlen($email)) $newLine .= ':'.str_replace(':',' ',$email); 103 $newLine .= "\n"; 104 fputs($file,$newLine); 105 fclose($file); 106 return true; 107 }else{ 108 return false; 109 } 110 } 111 112 /** 113 * Adds a group to the htgroup file 114 * @param string $groupname Groupname 115 */ 116 function addGroup($groupname) 117 { 118 $file=fopen($this->fHtgroup,'a'); 119 fclose($file); 120 } 121 122 /** 123 * Deletes a user in the password file 124 * @param string $username Username to delete 125 * @return boolean $deleted Returns true if user have been deleted otherwise false 126 */ 127 function delUser($username) 128 { 129 // Reading names from file 130 $file=fopen($this->fPasswd,'r'); 131 $i=0; 132 while($line=fgets($file,200)){ 133 $lineArr=explode(':',$line); 134 if($username!=$lineArr[0]){ 135 $newUserlist[$i][0]=$lineArr[0]; 136 $newUserlist[$i][1]=$lineArr[1]; 137 $i++; 138 }else{ 139 $deleted=true; 140 } 141 } 142 fclose($file); 143 144 // Writing names back to file (without the user to delete) 145 $file=fopen($this->fPasswd,"w"); 146 for($i=0;$i<count($newUserlist);$i++){ 147 fputs($file,$newUserlist[$i][0].":".$newUserlist[$i][1]."\n"); 148 } 149 fclose($file); 150 151 if($deleted==true){ 152 return true; 153 }else{ 154 return false; 155 } 156 } 157 158 /** 159 * Returns the name of the zone 160 * @return string 161 */ 162 function getZoneName() 163 { 164 if (file_exists($this->fHtaccess)) { 165 $f = fopen($this->fHtaccess, 'r'); 166 $file = fread($f, filesize($this->fHtaccess)); 167 fclose($f); 168 $match = '/AuthName\s+\"([^"]*)\"/mU'; 169 if (preg_match($match,$file,$res)) { 170 return $res[1]; 171 } 172 return ''; 173 } 174 return false; 175 } 176 177 /** 178 * Returns an array of all users in a password file 179 * @return array $users All usernames of a password file in an array 180 * @see setFPasswd() 181 */ 182 function getUsers() 183 { 184 $users = array(); 185 if (file_exists($this->fPasswd)) { 186 $file=fopen($this->fPasswd,'r'); 187 $x=0; 188 for($i=0;$line=fgets($file,200);$i++){ 189 $lineArr=explode(':',$line); 190 if($lineArr[0]!='' && !empty($lineArr[1])) { 191 $users[] = $lineArr[0]; 192 } 193 } 194 fclose($file); 195 } 196 return $users; 197 } 198 199 /** 200 * Sets a password to the given username 201 * @param string $username The name of the User for changing password 202 * @param string $password New Password for the User 203 * @return boolean $isSet Returns true if password have been set 204 */ 205 function setPasswd($username,$new_password) 206 { 207 // Reading names from file 208 $newUserlist=""; 209 $file=fopen($this->fPasswd,"r"); 210 $x=0; 211 for($i=0;$line=fgets($file,200);$i++){ 212 $lineArr=explode(":",$line); 213 if($username!=$lineArr[0] && $lineArr[0]!="" && $lineArr[1]!=""){ 214 $newUserlist[$i][0]=$lineArr[0]; 215 $newUserlist[$i][1]=$lineArr[1]; 216 $x++; 217 }else if($lineArr[0]!="" && $lineArr[1]!=""){ 218 $newUserlist[$i][0]=$lineArr[0]; 219 $newUserlist[$i][1]=crypt($new_password)."\n"; 220 $isSet=true; 221 $x++; 222 } 223 } 224 fclose($file); 225 226 unlink($this->fPasswd); 227 228 /// Writing names back to file (with new password) 229 $file=fopen($this->fPasswd,"w"); 230 for($i=0;$i<count($newUserlist);$i++){ 231 $content=$newUserlist[$i][0].":".$newUserlist[$i][1]; 232 fputs($file,$content); 233 } 234 fclose($file); 235 236 if($isSet==true){ 237 return true; 238 }else{ 239 return false; 240 } 241 } 242 243 /** 244 * Sets the Authentification type for Login 245 * @param string $authtype Authentification type as string 246 */ 247 function setAuthType($authtype) 248 { 249 $this->authType=$authtype; 250 } 251 252 /** 253 * Sets the Authentification Name (Name of the login area) 254 * @param string $authname Name of the login area 255 */ 256 function setAuthName($authname) 257 { 258 if (!empty($authname)) 259 $this->authName=$authname; 260 } 261 262 /** 263 * Writes the htaccess file to the given Directory and protects it 264 * @see setFhtaccess() 265 */ 266 function addLogin() 267 { 268 echo $this->fHtaccess; 269 $file=fopen($this->fHtaccess,'w'); 270 fputs($file,"Order allow,deny\n"); 271 fputs($file,"Allow from all\n"); 272 fputs($file,"AuthType ".$this->authType."\n"); 273 fputs($file,"AuthUserFile ".$this->fPasswd."\n\n"); 274 fputs($file,"AuthName \"".$this->authName."\"\n"); 275 fputs($file,"require valid-user\n"); 276 fclose($file); 277 @touch($this->fPasswd, time()); 278 } 279 280 /** 281 * Deletes the protection of the given directory 282 * @see setFhtaccess() 283 */ 284 function delLogin() 285 { 286 @unlink($this->fHtaccess); 287 } 288 } 289 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 11:57:01 2007 | par Balluche grâce à PHPXref 0.7 |
|