Alternc  latest
Alternc logiel libre pour l'hébergement
 All Classes Namespaces Files Functions Variables Pages
m_hta Class Reference

This class handle folder web restricted access through .htaccess/.htpassword files. More...

Public Member Functions

 m_webaccess ()
 Constructor. More...
 
 alternc_password_policy ()
 Password kind used in this class (hook for admin class) More...
 
 hook_menu ()
 
 CreateDir ($dir)
 Create a protected folder (.htaccess et .htpasswd) More...
 
 ListDir ()
 Returns the list of all user folder currently protected by a .htpasswd file. More...
 
 is_protected ($dir)
 Tells if a folder is protected. More...
 
 get_hta_detail ($dir)
 Returns the list of login for a protected folder. More...
 
 DelDir ($dir, $skip=false)
 Unprotect a folder. More...
 
 add_user ($user, $password, $dir)
 Add a user to a protected folder. More...
 
 del_user ($lst, $dir)
 Delete a user from a protected folder. More...
 
 change_pass ($user, $newpass, $dir)
 Change the password of a user in a protected folder. More...
 

Private Member Functions

 _reading_htaccess ($absolute)
 Check that a .htaccess file is valid (for authentication) More...
 

Detailed Description

This class handle folder web restricted access through .htaccess/.htpassword files.

Copyleft AlternC Team

Definition at line 41 of file m_hta.php.

Member Function Documentation

m_hta::_reading_htaccess (   $absolute)
private

Check that a .htaccess file is valid (for authentication)

Parameters
globalm_err $err
type$absolute
string$absoluteFolder we want to check (relative to user root)
Returns
boolean TRUE is the .htaccess is protecting this folder, or FALSE else

Definition at line 426 of file m_hta.php.

References $err.

426  {
427  global $err;
428  $err->log("hta", "_reading_htaccess", $absolute);
429  $file = fopen("$absolute/.htaccess", "r+");
430  $lignes = array(1, 1, 1);
431  $errr = 0;
432  if (!$file) {
433  return false;
434  }
435  while (!feof($file) && !$errr) {
436  $s = fgets($file, 1024);
437  if (substr($s, 0, 12) != "RewriteCond " && substr($s, 0, 14) != "ErrorDocument " && substr($s, 0, 12) != "RewriteRule " && substr($s, 0, 14) != "RewriteEngine " && trim($s) != "") {
438  $errr = 1;
439  }
440  if (strtolower(trim($s)) == strtolower("authuserfile $absolute/.htpasswd")) {
441  $lignes[0] = 0;
442  $errr = 0;
443  } // authuserfile
444  if (strtolower(trim($s)) == "require valid-user") {
445  $lignes[1] = 0;
446  $errr = 0;
447  } //require
448  if (strtolower(trim($s)) == "authtype basic") {
449  $lignes[2] = 0;
450  $errr = 0;
451  } //authtype
452  } // Reading config file
453  fclose($file);
454  if ($errr || in_array(0, $lignes)) {
455  $err->raise("hta", _("An incompatible .htaccess file exists in this folder"));
456  return false;
457  }
458  return true;
459  }
$err
Definition: bootstrap.php:72
m_hta::add_user (   $user,
  $password,
  $dir 
)

Add a user to a protected folder.

Parameters
globalm_err $err
globalm_bro $bro
globalm_admin $admin
string$user
string$password
string$dir
string$passwordThe password to add (cleartext)
string$dirThe folder we add it to (relative to user root).
Returns
boolean TRUE if the user has been added, or FALSE if an error occurred

Definition at line 280 of file m_hta.php.

References $bro, $err, $password, $t, $user, _md5cr(), and checkloginmail().

280  {
281  global $err, $bro, $admin;
282  $err->log("hta", "add_user", $user . "/" . $dir);
283  if (empty($user)) {
284  $err->raise('hta', _("Please enter a user"));
285  return false;
286  }
287  if (empty($password)) {
288  $err->raise('hta', _("Please enter a password"));
289  return false;
290  }
291  $absolute = $bro->convertabsolute($dir, 0);
292  if (!file_exists($absolute)) {
293  $err->raise("hta", printf(("The folder '%s' does not exist"), $dir));
294  return false;
295  }
296  // @todo delete cf!. functions.php checkloginemail definition
297  if (checkloginmail($user)) {
298  // Check this password against the password policy using common API :
299  if (is_callable(array($admin, "checkPolicy"))) {
300  if (!$admin->checkPolicy("hta", $user, $password)) {
301  return false; // The error has been raised by checkPolicy()
302  }
303  }
304 
305  $file = @fopen("$absolute/.htpasswd", "a+");
306  if (!$file) {
307  $err->raise("hta", _("File already exist"));
308  return false;
309  }
310  fseek($file, 0);
311  while (!feof($file)) {
312  $s = fgets($file, 1024);
313  $t = explode(":", $s);
314  if ($t[0] == $user) {
315  $err->raise("hta", _("The user '%s' already exist for this folder"), $user);
316  return false;
317  }
318  }
319  fseek($file, SEEK_END);
320  if (empty($t[1]) || substr($t[1], -1) != "\n") {
321  fwrite($file, "\n");
322  }
323  fwrite($file, "$user:" . _md5cr($password) . "\n");
324  fclose($file);
325  return true;
326  } else {
327  $err->raise("hta", _("Please enter a valid username"));
328  return false;
329  }
330  }
$user
Definition: bootstrap.php:84
$bro
Definition: bootstrap.php:151
checkloginmail($mail)
Check a login mail, cf http://www.bortzmeyer.org/arreter-d-interdire-des-adresses-legales.html.
Definition: functions.php:226
_md5cr($pass, $salt="")
Hashe un mot de passe en clair en MD5 avec un salt al�atoire.
Definition: functions.php:513
$err
Definition: bootstrap.php:72
$t
Definition: dom_editdns.php:58
$password
Definition: bootstrap.php:85
m_hta::alternc_password_policy ( )

Password kind used in this class (hook for admin class)

Returns
array

Definition at line 55 of file m_hta.php.

55  {
56  return array("hta" => "Protected folders passwords");
57  }
m_hta::change_pass (   $user,
  $newpass,
  $dir 
)

Change the password of a user in a protected folder.

Parameters
string$userThe users whose password should be changed
string$newpassThe new password of this user
string$dirThe folder, relative to user root, in which we will change a password
Returns
boolean TRUE if the password has been changed, or FALSE if an error occurred

Definition at line 380 of file m_hta.php.

References $bro, $err, $t, $user, and _md5cr().

380  {
381  global $bro, $err, $admin;
382  $err->log("hta", "change_pass", $user . "/" . $dir);
383  $absolute = $bro->convertabsolute($dir, 0);
384  if (!file_exists($absolute)) {
385  $err->raise("hta", printf(_("The folder '%s' does not exist"), $dir));
386  return false;
387  }
388 
389  // Check this password against the password policy using common API :
390  if (is_callable(array($admin, "checkPolicy"))) {
391  if (!$admin->checkPolicy("hta", $user, $newpass)) {
392  return false; // The error has been raised by checkPolicy()
393  }
394  }
395 
396  touch("$absolute/.htpasswd.new");
397  $file = fopen("$absolute/.htpasswd", "r");
398  $newf = fopen("$absolute/.htpasswd.new", "a");
399  if (!$file || !$newf) {
400  $err->raise("hta", _("File already exist"));
401  return false;
402  }
403  while (!feof($file)) {
404  $s = fgets($file, 1024);
405  $t = explode(":", $s);
406  if ($t[0] != $user) {
407  fwrite($newf, "$s");
408  }
409  }
410  fwrite($newf, "$user:" . _md5cr($newpass) . "\n");
411  fclose($file);
412  fclose($newf);
413  unlink("$absolute/.htpasswd");
414  rename("$absolute/.htpasswd.new", "$absolute/.htpasswd");
415  return true;
416  }
$user
Definition: bootstrap.php:84
$bro
Definition: bootstrap.php:151
_md5cr($pass, $salt="")
Hashe un mot de passe en clair en MD5 avec un salt al�atoire.
Definition: functions.php:513
$err
Definition: bootstrap.php:72
$t
Definition: dom_editdns.php:58
m_hta::CreateDir (   $dir)

Create a protected folder (.htaccess et .htpasswd)

Parameters
string$dirFolder to protect (relative to user root)
Returns
boolean TRUE if the folder has been protected, or FALSE if an error occurred
Parameters
globalm_mem $mem
globalm_bro $bro
globalm_err $err
string$dir
Returns
boolean

Definition at line 85 of file m_hta.php.

References $bro, and $err.

85  {
86  global $bro, $err;
87  $err->log("hta", "createdir", $dir);
88  $absolute = $bro->convertabsolute($dir, 0);
89  if (!$absolute) {
90  $err->raise("hta", printf(_("The folder '%s' does not exist"), $dir));
91  return false;
92  }
93  if (!file_exists($absolute)) {
94  @mkdir($absolute, 00777);
95  }
96  if (!file_exists("$absolute/.htaccess")) {
97  if (!@touch("$absolute/.htaccess")) {
98  $err->raise("hta", _("File already exist"));
99  return false;
100  }
101  $file = @fopen("$absolute/.htaccess", "r+");
102  if (!$file) {
103  $err->raise("hta", _("File already exist"));
104  return false;
105  }
106  fseek($file, 0);
107  $param = "AuthUserFile \"$absolute/.htpasswd\"\nAuthName \"" . _("Restricted area") . "\"\nAuthType Basic\nrequire valid-user\n";
108  fwrite($file, $param);
109  fclose($file);
110  }
111  if (!file_exists("$absolute/.htpasswd")) {
112  if (!touch("$absolute/.htpasswd")) {
113  $err->raise("hta", _("File already exist"));
114  return false;
115  }
116  return true;
117  }
118  return true;
119  }
$bro
Definition: bootstrap.php:151
$err
Definition: bootstrap.php:72
m_hta::del_user (   $lst,
  $dir 
)

Delete a user from a protected folder.

Parameters
globalm_bro $bro
globalm_err $err
array$lstAn array with login to delete.
string$dirThe folder, relative to user root, where we want to delete users.
Returns
boolean TRUE if users has been deleted, or FALSE if an error occurred.

Definition at line 341 of file m_hta.php.

References $bro, $err, and $t.

341  {
342  global $bro, $err;
343  $err->log("hta", "del_user", $lst . "/" . $dir);
344  $absolute = $bro->convertabsolute($dir, 0);
345  if (!file_exists($absolute)) {
346  $err->raise("hta", printf(_("The folder '%s' does not exist"), $dir));
347  return false;
348  }
349  touch("$absolute/.htpasswd.new");
350  $file = fopen("$absolute/.htpasswd", "r");
351  $newf = fopen("$absolute/.htpasswd.new", "a");
352  if (!$file || !$newf) {
353  $err->raise("hta", _("File already exist"));
354  return false;
355  }
356  reset($lst);
357  fseek($file, 0);
358  while (!feof($file)) {
359  $s = fgets($file, 1024);
360  $t = explode(":", $s);
361  if (!in_array($t[0], $lst) && ($t[0] != "\n")) {
362  fseek($newf, 0);
363  fwrite($newf, "$s");
364  }
365  }
366  fclose($file);
367  fclose($newf);
368  unlink("$absolute/.htpasswd");
369  rename("$absolute/.htpasswd.new", "$absolute/.htpasswd");
370  return true;
371  }
$bro
Definition: bootstrap.php:151
$err
Definition: bootstrap.php:72
$t
Definition: dom_editdns.php:58
m_hta::DelDir (   $dir,
  $skip = false 
)

Unprotect a folder.

Parameters
globalm_mem $mem
globalm_bro $bro
globalm_err $err
string$dirFolder to unprotect, relative to user root
boolean$skipFor testing purpose mainly, skips the full user path search
Returns
boolean TRUE if the folder has been unprotected, or FALSE if an error occurred

Definition at line 216 of file m_hta.php.

References $bro, $err, and $key.

216  {
217  global $bro, $err;
218  $err->log("hta", "deldir", $dir);
219  $dir = $bro->convertabsolute($dir, $skip);
220  if (!$dir) {
221  $err->raise("hta", printf(("The folder '%s' does not exist"), $dir));
222  return false;
223  }
224  $htaccess_file = "$dir/.htaccess";
225  if (!is_readable($htaccess_file)) {
226  $err->raise("hta", printf(_("I cannot read the file '%s'"), $htaccess_file));
227  }
228  $fileLines = file($htaccess_file);
229  $patternList = array(
230  "AuthUserFile.*$",
231  "AuthName.*$",
232  "AuthType Basic.*$",
233  "require valid-user.*$"
234  );
235  $count_lines = 0;
236  foreach ($fileLines as $key => $line) {
237  foreach ($patternList as $pattern) {
238  if (preg_match("/" . $pattern . "/", $line)) {
239  $count_lines++;
240  unset($fileLines[$key]);
241  }
242  }
243  }
244  // If no changes
245  if (!$count_lines) {
246  $err->raise("hta", printf(_("Unexpected: No changes made to '%s'"), $htaccess_file));
247  }
248  // If file is empty, remove it
249  if (!count($fileLines)) {
250  if (!unlink($htaccess_file)) {
251  $err->raise("hta", printf(_("I could not delete the file '%s'"), $htaccess_file));
252  }
253  } else {
254  file_put_contents($htaccess_file, implode("\n", $fileLines));
255  }
256  $htpasswd_file = "$dir/.htpasswd";
257  if (!is_writable($htpasswd_file)) {
258  $err->raise("hta", printf(_("I cannot read the file '%s'"), $htpasswd_file));
259  } else if (!unlink($htpasswd_file)) {
260  $err->raise("hta", printf(_("I cannot delete the file '%s/.htpasswd'"), $dir));
261  return false;
262  }
263 
264  return true;
265  }
$bro
Definition: bootstrap.php:151
$err
Definition: bootstrap.php:72
if(empty($_POST['key'])||empty($_POST['val'])) $key
Definition: tempovars.php:14
m_hta::get_hta_detail (   $dir)

Returns the list of login for a protected folder.

Parameters
globalm_mem $mem
globalm_err $err
string$dirThe folder to lookup (relative to user root)
Returns
array An array containing the list of logins from the .htpasswd file, or FALSE

Definition at line 177 of file m_hta.php.

References $err, $i, $mem, $res, $t, and ALTERNC_HTML.

177  {
178  global $mem, $err;
179  $err->log("hta", "get_hta_detail");
180  $absolute = ALTERNC_HTML . "/" . substr($mem->user["login"], 0, 1) . "/" . $mem->user["login"] . "/$dir";
181  if (file_exists("$absolute/.htaccess")) {
182  /* if (!_reading_htaccess($absolute)) {
183  return false;
184  }
185  */
186  }
187  $file = @fopen("$absolute/.htpasswd", "r");
188  $i = 0;
189  $res = array();
190  if (!$file) {
191  return false;
192  }
193  // TODO: Tester la validité du .htpasswd
194  while (!feof($file)) {
195  $s = fgets($file, 1024);
196  $t = explode(":", $s);
197  if ($t[0] != $s) {
198  $res[$i] = $t[0];
199  $i = $i + 1;
200  }
201  }
202  fclose($file);
203  return $res;
204  }
$err
Definition: bootstrap.php:72
const ALTERNC_HTML
Definition: bootstrap.php:10
$res
Definition: index.php:127
$t
Definition: dom_editdns.php:58
$mem
Definition: bootstrap.php:71
m_hta::hook_menu ( )
Returns
array

Definition at line 63 of file m_hta.php.

63  {
64  $obj = array(
65  'title' => _("Protected folders"),
66  'ico' => 'images/password.png',
67  'link' => 'hta_list.php',
68  'pos' => 50,
69  );
70 
71  return $obj;
72  }
m_hta::is_protected (   $dir)

Tells if a folder is protected.

Parameters
globalm_mem $mem
globalm_err $err
string$dirFolder to check
Returns
boolean If the folder is protected, or FALSE if it is not

Definition at line 158 of file m_hta.php.

References $err, $mem, and ALTERNC_HTML.

158  {
159  global $mem, $err;
160  $err->log("hta", "is_protected", $dir);
161  $absolute = ALTERNC_HTML . "/" . substr($mem->user["login"], 0, 1) . "/" . $mem->user["login"] . "/$dir";
162  if (file_exists("$absolute/.htpasswd")) {
163  return true;
164  } else {
165  return false;
166  }
167  }
$err
Definition: bootstrap.php:72
const ALTERNC_HTML
Definition: bootstrap.php:10
$mem
Definition: bootstrap.php:71
m_hta::ListDir ( )

Returns the list of all user folder currently protected by a .htpasswd file.

Parameters
globalm_err $err
globalm_mem $mem
Returns
array Array containing user folder list

Definition at line 128 of file m_hta.php.

References $err, $i, $mem, $r, and ALTERNC_HTML.

128  {
129  global$err, $mem;
130  $err->log("hta", "listdir");
131  $sortie = array();
132  $absolute = ALTERNC_HTML . "/" . substr($mem->user["login"], 0, 1) . "/" . $mem->user["login"];
133  exec("find " . escapeshellarg($absolute) . " -name .htpasswd|sort", $sortie);
134  if (!count($sortie)) {
135  $err->raise("hta", _("No protected folder"));
136  return false;
137  }
138  $pattern = "/^" . preg_quote(ALTERNC_HTML, "/") . "\/.\/[^\/]*\/(.*)\/\.htpasswd/";
139 
140  $r = array();
141  for ($i = 0; $i < count($sortie); $i++) {
142  $matches = array();
143  preg_match($pattern, $sortie[$i], $matches);
144  $tmpm = isset($matches[1]) ? '/' . $matches[1] : '';
145  $r[$i] = $tmpm . "/";
146  }
147  return $r;
148  }
$r
Definition: aws_add.php:77
$err
Definition: bootstrap.php:72
const ALTERNC_HTML
Definition: bootstrap.php:10
$mem
Definition: bootstrap.php:71
m_hta::m_webaccess ( )

Constructor.

Definition at line 46 of file m_hta.php.

46  {
47 
48  }

The documentation for this class was generated from the following file: