source: alternc/trunk/bureau/class/m_hta.php @ 2998

Revision 2998, 11.4 KB checked in by xals, 2 years ago (diff)

Merged changesets 2970, 2972, 2984, 2986, 2987 and 2988 from branch stable 1.0 to trunk.

Line 
1<?php
2/*
3 $Id: m_hta.php,v 1.5 2004/11/29 17:15:37 anonymous Exp $
4 ----------------------------------------------------------------------
5 AlternC - Web Hosting System
6 Copyright (C) 2002 by the AlternC Development Team.
7 http://alternc.org/
8 ----------------------------------------------------------------------
9 Based on:
10 Valentin Lacambre's web hosting softwares: http://altern.org/
11 ----------------------------------------------------------------------
12 LICENSE
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License (GPL)
16 as published by the Free Software Foundation; either version 2
17 of the License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 GNU General Public License for more details.
23
24 To read the license please visit http://www.gnu.org/copyleft/gpl.html
25 ----------------------------------------------------------------------
26 Original Author of file:
27 Purpose of file:
28 ----------------------------------------------------------------------
29*/
30
31/**
32* This class handle folder web restricted access through .htaccess/.htpassword
33* files.
34*
35* Copyleft {@link http://alternc.net/ AlternC Team}
36*
37* @copyright    AlternC-Team 2002-11-01 http://alternc.org/
38*
39*/
40class m_hta {
41
42
43  /*---------------------------------------------------------------------------*/
44  /**
45   * Constructor
46   */
47  function m_webaccess() {
48  }
49
50
51  /**
52   * Password kind used in this class (hook for admin class)
53   */
54  function alternc_password_policy() {
55    return array("hta"=>"Protected folders passwords");
56  }
57
58
59  /*---------------------------------------------------------------------------*/
60  /**
61   * Create a protected folder (.htaccess et .htpasswd)
62   * @param string $dir Folder to protect (relative to user root)
63   * @return boolean TRUE if the folder has been protected, or FALSE if an error occurred
64   */
65  function CreateDir($dir) {
66    global $mem,$bro,$err,$L_ALTERNC_LOC;
67    $err->log("hta","createdir",$dir);
68    $absolute=$bro->convertabsolute($dir,0);
69    if (!$absolute) {
70      $err->raise("hta",8,$dir);
71      return false;
72    }
73    if (!file_exists($absolute)) {
74      @mkdir($absolute,00777);
75    }
76    if (!file_exists("$absolute/.htaccess")) {
77      if (!@touch("$absolute/.htaccess")) {
78        $err->raise("hta",12);
79        return false;
80      }
81      $file = @fopen("$absolute/.htaccess","r+");
82      if (!$file) {
83        $err->raise("hta",12);
84        return false;
85      }
86      fseek($file,0);
87      $param="AuthUserFile $absolute/.htpasswd\nAuthName \"Zone Protégée\"\nAuthType Basic\nrequire valid-user\n";
88      fwrite($file, $param);
89      fclose($file);
90    }
91    if (!file_exists("$absolute/.htpasswd")) {
92      if (!touch("$absolute/.htpasswd")) {
93        $err->raise("hta",12);
94        return false;
95      }
96      return true;
97    }
98    return true;
99  }
100
101
102  /*---------------------------------------------------------------------------*/
103  /**
104   * Returns the list of all user folder currently protected by a .htpasswd file
105   * @return array Array containing user folder list
106   */
107  function ListDir() {
108    global $err,$mem,$L_ALTERNC_LOC;
109    $err->log("hta","listdir");
110    $sortie=array();
111    $absolute="$L_ALTERNC_LOC/html/".substr($mem->user["login"],0,1)."/".$mem->user["login"];
112    exec("find $absolute -name .htpasswd | sort", $sortie);
113    if (!count($sortie)) {
114      $err->raise("hta",4);
115      return false;
116    }
117    for ($i=0;$i<count($sortie);$i++){
118      preg_match("/^".addslashes("$L_ALTERNC_LOC/html/")."\/.\/[^\/]*\/(.*)\/\.htpasswd/", $sortie[$i], $matches);
119      $r[$i]=$matches[1]."/";
120    }
121    return $r;
122  }
123
124
125  /*---------------------------------------------------------------------------*/
126  /**
127   * Tells if a folder is protected.
128   * @param string $dir Folder to check
129   * @return TRUE if the folder is protected, or FALSE if it is not
130   */
131  function is_protected($dir){
132    global $mem,$err,$L_ALTERNC_LOC;
133    $err->log("hta","is_protected",$dir);
134    $absolute="$L_ALTERNC_LOC/html/".substr($mem->user["login"],0,1)."/".$mem->user["login"]."/$dir";
135    $sortie=array();
136    if (file_exists("$absolute/.htpasswd")){
137      return true;
138    }
139    else {
140      return false;
141    }
142  }
143
144
145  /*---------------------------------------------------------------------------*/
146  /**
147   * Returns the list of login for a protected folder.
148   * @param string $dir The folder to lookup (relative to user root)
149   * @return array An array containing the list of logins from the .htpasswd file, or FALSE
150   */
151  function get_hta_detail($dir) {
152    global $mem,$err,$L_ALTERNC_LOC;
153    $err->log("hta","get_hta_detail");
154    $absolute="$L_ALTERNC_LOC/html/".substr($mem->user["login"],0,1)."/".$mem->user["login"]."/$dir";
155    if (file_exists("$absolute/.htaccess")) {
156      /*                if (!_reading_htaccess($absolute)) {
157                        return false;
158                        }
159      */        }
160    $file = @fopen("$absolute/.htpasswd","r");
161    $i=0;
162    $res=array();
163    if (!$file) {
164      return false;
165    }
166    // TODO: Tester la validité du .htpasswd
167    while (!feof($file)) {
168      $s=fgets($file,1024);
169      $t=explode(":",$s);
170      if ($t[0]!=$s) {
171        $res[$i]=$t[0];
172        $i=$i+1;
173      }
174    }
175    fclose($file);
176    return $res;
177  }
178
179
180  /*---------------------------------------------------------------------------*/
181  /**
182   * Unprotect a folder
183   * @param string $dir Folder to unprotect, relative to user root
184   * @return boolean TRUE if the folder has been unprotected, or FALSE if an error occurred
185   */
186  function DelDir($dir) {
187    global $mem,$bro,$err;
188    $err->log("hta","deldir",$dir);
189    $dir=$bro->convertabsolute($dir,0);
190    if (!$dir) {
191      $err->raise("hta",8,$dir);
192      return false;
193    }
194    if (!unlink("$dir/.htaccess")) {
195      $err->raise("hta",5,$dir);
196      return false;
197    }
198    if (!unlink("$dir/.htpasswd")) {
199      $err->raise("hta",6,$dir);
200      return false;
201    }
202    return true;
203  }
204
205
206  /*---------------------------------------------------------------------------*/
207  /**
208   * Add a user to a protected folder
209   * @param string $login The user login to add
210   * @param string $password The password to add (cleartext)
211   * @param string $dir The folder we add it to (relative to user root).
212   * @return boolean TRUE if the user has been added, or FALSE if an error occurred
213   */
214  function add_user($user,$password,$dir) {
215    global $err, $bro, $admin;
216    $err->log("hta","add_user",$user."/".$dir);
217    $absolute=$bro->convertabsolute($dir,0);
218    if (!file_exists($absolute)) {
219      $err->raise("hta",8,$dir);
220      return false;
221    }
222    if (checkloginmail($user)){
223      // Check this password against the password policy using common API :
224      if (is_callable(array($admin,"checkPolicy"))) {
225        if (!$admin->checkPolicy("hta",$user,$password)) {
226          return false; // The error has been raised by checkPolicy()
227        }
228      }
229
230      $file = @fopen("$absolute/.htpasswd","a+");
231      if (!$file) {
232        $err->raise("hta",12);
233        return false;
234      }
235      fseek($file,0);
236      while (!feof($file)) {
237        $s=fgets($file,1024);
238        $t=explode(":",$s);
239        if ($t[0]==$user) {
240          $err->raise("hta",10,$user);
241          return false;
242        }
243      }
244      fseek($file,SEEK_END);
245      if (substr($t[1],-1)!="\n") {
246        fwrite($file,"\n");
247      }
248      fwrite($file, "$user:"._md5cr($password)."\n");
249      fclose($file);
250      return true;
251    } else {
252      $err->raise("hta",11);
253      return false;
254    }
255  }
256
257
258  /*---------------------------------------------------------------------------*/
259  /**
260   * Delete a user from a protected folder.
261   * @param array $lst An array with login to delete.
262   * @param string $dir The folder, relative to user root, where we want to delete users.
263   * @return boolean TRUE if users has been deleted, or FALSE if an error occurred.
264   */
265  function del_user($lst,$dir) {
266    global $bro,$err;
267    $err->log("hta","del_user",$lst."/".$dir);
268    $absolute=$bro->convertabsolute($dir,0);
269    if (!file_exists($absolute)) {
270      $err->raise("hta",8,$dir);
271      return false;
272    }
273    touch("$absolute/.htpasswd.new");
274    $file = fopen("$absolute/.htpasswd","r");
275    $newf = fopen("$absolute/.htpasswd.new","a");
276    if (!$file || !$newf) {
277      $err->raise("hta",12);
278      return false;
279    }
280    reset($lst);
281    fseek($file,0);
282    while (!feof($file)) {
283      $s=fgets($file,1024);
284      $t=explode(":",$s);
285      if (!in_array($t[0],$lst) && ($t[0]!="\n")) {
286        fseek($newf,0);
287        fwrite($newf, "$s");
288      }
289    }
290    fclose($file);
291    fclose($newf);
292    unlink("$absolute/.htpasswd");
293    rename("$absolute/.htpasswd.new", "$absolute/.htpasswd");
294    return true;
295  }
296
297
298  /*---------------------------------------------------------------------------*/
299  /**
300   * Change the password of a user in a protected folder
301   * @param string $user The users whose password should be changed
302   * @param string $newpass The new password of this user
303   * @param string $dir The folder, relative to user root, in which we will change a password
304   * @return boolean TRUE if the password has been changed, or FALSE if an error occurred
305   */
306  function change_pass($user,$newpass,$dir) {
307    global $bro,$err,$admin;
308    $err->log("hta","change_pass",$user."/".$dir);
309    $absolute=$bro->convertabsolute($dir,0);
310    if (!file_exists($absolute)) {
311      $err->raise("hta",8,$dir);
312      return false;
313    }
314
315    // Check this password against the password policy using common API :
316    if (is_callable(array($admin,"checkPolicy"))) {
317      if (!$admin->checkPolicy("hta",$user,$password)) {
318        return false; // The error has been raised by checkPolicy()
319      }
320    }
321
322    touch("$absolute/.htpasswd.new");
323    $file = fopen("$absolute/.htpasswd","r");
324    $newf = fopen("$absolute/.htpasswd.new","a");
325    if (!$file || !$newf) {
326      $err->raise("hta",12);
327      return false;
328    }
329    while (!feof($file)) {
330      $s=fgets($file,1024);
331      $t=explode(":",$s);
332      if ($t[0]!=$user) {
333        fwrite($newf, "$s");
334      }
335    }
336    fwrite($newf, "$user:"._md5cr($newpass)."\n");
337    fclose($file);
338    fclose($newf);
339    unlink("$absolute/.htpasswd");
340    rename("$absolute/.htpasswd.new", "$absolute/.htpasswd");
341    return true;
342  }
343
344
345  /*---------------------------------------------------------------------------*/
346  /**
347   * Check that a .htaccess file is valid (for authentication)
348   * @param string $absolute Folder we want to check (relative to user root)
349   * @return boolean TRUE is the .htaccess is protecting this folder, or FALSE else
350   * @access private
351   */
352  function _reading_htaccess($absolute) {
353    global $err;
354    $err->log("hta","_reading_htaccess",$absolute);
355    $file = fopen("$absolute/.htaccess","r+");
356    $lignes=array(1,1,1);
357    $errr=0;
358    if (!$file) {
359      return false;
360    }
361    while (!feof($file) && !$errr) {
362      $s=fgets($file,1024);
363      if (substr($s,0,12)!="RewriteCond " && substr($s,0,14)!="ErrorDocument " && substr($s,0,12)!="RewriteRule " && substr($s,0,14)!="RewriteEngine " && trim($s)!="") {
364        $errr=1;
365      }
366      if (strtolower(trim($s))==strtolower("authuserfile $absolute/.htpasswd")) {
367        $lignes[0]=0;
368        $errr=0;
369      } // authuserfile
370      if (strtolower(trim($s))=="require valid-user") {
371        $lignes[1]=0;
372        $errr=0;
373      } //require
374      if (strtolower(trim($s))=="authtype basic") {
375        $lignes[2]=0;
376        $errr=0;
377      } //authtype
378    } // Reading config file
379    fclose($file);
380    if ($errr ||  in_array(0,$lignes)) {
381      $err->raise("hta",1);
382      return false;
383    }
384    return true;
385  } 
386
387} /* CLASS m_hta */
388
389
390
391?>
Note: See TracBrowser for help on using the repository browser.