changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / auth / auth_phplib.php
1 <?php
2 /*
3    This auth method uses phplib to authenticate users and set the ewiki_author.
4    It also initializes the phplib perm object.
5
6    you'll need:
7     - EWIKI_PROTECTED_MODE
8         - EWIKI_AUTH_DEFAULT_RING as a minimum
9             - plugins/auth_perm_ring.php for better support
10     - phplib-7.4-pre2 or compatible installed version
11
12    You can only load __one__ auth method plugin!
13
14    * To use you must add the following line to the end of your layout:
15          <?php page_close() ?>  
16    * You may also want to add some code to your layout to display the login and logout 
17      links selectively this code is what I use:
18          * 
19          if($perm->have_perm(EWIKI_PHPLIB_ALLUSERS_PERM)){
20         ?>
21                 Welcome <?php echo($GLOBALS["ewiki_author"]); ?><br />
22                 » <A HREF='<?php echo(EWIKI_SCRIPT); ?>Logout'>Logout</A><br />
23         <?php
24                 }else{
25         ?>
26                 » <A HREF='<?php echo(EWIKI_SCRIPT); ?>Login'>Login</A><br />
27         <?php
28                 };
29         ?>
30 */
31
32 //There must be one group that includes all valid wiki users, this can be used for
33 // determing whether to display a login or logout link
34 define("EWIKI_PHPLIB_ALLUSERS_PERM","user");
35
36 #-- Connect to login query plugin
37 #-- Thereby requiring that the user have a name to edit pages
38 #-- permissions are not required for any particular edit 
39 #-- the user permission is checked below only to see if they are logged in 
40 #-- with an account other than nobody.
41 $ewiki_plugins["auth_query"][0] = "ewiki_auth_query_phplib";
42
43 #-- Login/Logout pages
44 $ewiki_plugins["page"]["LogOut"] = "ewiki_page_phplib_logout";
45 $ewiki_plugins["page"]["LogIn"] = "ewiki_page_phplib_login";
46 $ewiki_plugins["page"]["ChangePassword"] = "ewiki_page_phplib_chpw";
47
48
49   page_open(array("sess" => "Example_Session", "auth" => "My_Auth", "perm" => "Example_Perm", "user" => "My_User"));
50
51   @$GLOBALS["ewiki_author"]=$auth->auth["uname"];
52
53   //echo($_REQUEST["cancel_login"]);
54   if((!$perm->have_perm(EWIKI_PHPLIB_ALLUSERS_PERM))){  
55         define("EWIKI_AUTO_EDIT",0);    
56   }
57   
58 #-- text data
59 $ewiki_t["en"]["CANNOTCHANGEPAGE"] = "This page cannot be changed.  Perhaps you can <A href='".EWIKI_SCRIPT."LogOut'>LogIn</A> to and change it then.";
60 $ewiki_t["en"]["RESTRICTED_ACCESS"] = "You must be authenticated to use this part of the wiki.  If you have an account you can <A href='".EWIKI_SCRIPT."LogOut'>LogIn</A>.";
61 $ewiki_t["en"]["NOTLOGGEDIN"] = "You are not logged in.  You must <A href='".EWIKI_SCRIPT."LogIn'>LogIn</A> to access some features of this site.";
62 @$ewiki_t["en"]["LOGGEDIN"] = "You have logged in as ".$auth->auth['uname'].".  You must <A href='".EWIKI_SCRIPT."LogOut'>LogOut</A> to login again.";
63 @$ewiki_t["en"]["LOGINFORM"]='
64 <h1>Test for Login</h1>
65
66 Welcome!
67
68 Please identify yourself with a username and a password:<br />
69
70 <form action='.$auth->url().' method=post>
71 <table border=0 bgcolor="#eeeeee" align="center" cellspacing=0 cellpadding=4>
72  <tr valign=top align=left>
73   <td>Username:</td>
74   <td><input type="text" name="username"
75   value="'.htmlentities($auth->auth["uname"]).'"
76   size=32 maxlength=32></td>
77  </tr>
78  <tr valign=top align=left>
79   <td>Password:</td>
80   <td><input type="password" name="password" size=32 maxlength=32></td>
81  </tr>
82  
83  <tr>
84   <td>&nbsp;</td>
85   <td align=right><input type="submit" name="cancel_login" value="Cancel">
86   <input type="submit" name="submit" value="Login now"></td>
87  </tr>
88 </table>
89
90
91 </table>
92 </form>
93 ';
94 $ewiki_t["en"]["BADOLDPW"]='You have misentered your old password and been logged out.  '.
95         "Please <A href='".EWIKI_SCRIPT."LogIn'>LogIn</A> again to resume your session.";
96 $ewiki_t["en"]["PWCHNGD"]='Your password has been changed.';
97 $ewiki_t["en"]["NOMATCH"]='Your new password does not match with your retyping of it.';
98 $ewiki_t["en"]["CHPWFORM"]='
99 <h1>Change Password:</h1>
100
101 Please enter your old password once and your new password twice in the blanks below:<br />
102
103 <form action='.$auth->url().' method=post>
104 <table border=0 bgcolor="#eeeeee" align="center" cellspacing=0 cellpadding=4>
105  <tr valign=top align=left>
106   <td>Old Password:</td>
107   <td><input type="password" name="oldpassword" size=32 maxlength=32></td>
108  </tr>
109  <tr valign=top align=left>
110   <td>New Password:</td>
111   <td><input type="password" name="newpassword1" size=32 maxlength=32></td>
112  </tr>
113  <tr valign=top align=left>
114   <td>Repeat New Password:</td>
115   <td><input type="password" name="newpassword2" size=32 maxlength=32></td>
116  </tr>
117  
118  <tr>
119   <td>&nbsp;</td>
120   <td align=right><input type="submit" name="cancel_pwchng" value="Cancel">
121   <input type="submit" name="submit" value="Change Password"></td>
122  </tr>
123 </table>
124
125
126 </table>
127 </form>
128 ';
129  
130 function ewiki_page_phplib_chpw($id=0, $data=0) { 
131         global $user;
132
133         if(!isset($_REQUEST['oldpassword'])){
134                 return(ewiki_t("CHPWFORM"));
135         }else{
136                 return($user->changepw($_REQUEST['oldpassword'],$_REQUEST['newpassword1'],$_REQUEST['newpassword2']).ewiki_t("CHPWFORM"));
137         }
138         
139         
140 }
141  
142 function ewiki_page_phplib_login($id=0, $data=0) {
143         global $auth,$sess,$perm;
144
145         //if we did not just submit user data
146         if(isset($_REQUEST['username'])){
147                 if($perm->have_perm(EWIKI_PHPLIB_ALLUSERS_PERM)){
148                         return( ewiki_t("LOGGEDIN") );          
149                 }else{
150                         return( ewiki_t("NOTLOGGEDIN") );                                       
151                 }
152         }
153            $auth->unauth();  # We have to relogin, so clear current auth info
154            $auth->nobody = false; # We are forcing login, so default auth is 
155                              # disabled
156            $auth->auth["uid"] = "form";
157        $auth->auth["exp"] = 0x7fffffff;
158        $auth->auth["refresh"] = 0x7fffffff;
159        $sess->freeze();         
160            
161   return( ewiki_t("LOGINFORM") );
162
163  
164
165  
166 function ewiki_page_phplib_logout($id=0, $data=0) {
167         global $auth;
168
169   $auth->logout();  
170   page_close();
171 /**
172  *    return(  "<h1>Logout</h1> 
173   You have been logged in as <b>".$auth->auth["uname"]."</b> with
174   <b>".$auth->auth["perm"]."</b> permission.You have been logged out.");
175  */
176    return(  "<h1>Logout</h1> You have been logged out.");
177  }
178 function ewiki_auth_query_phplib(&$output, $force_query=0) {
179
180    global $auth,$perm,$sess, $ewiki_author, $ewiki_ring;
181         
182   if($_REQUEST["cancel_login"]=="Cancel"){
183         return(false);
184   }
185         
186         //attempt login if user not in group user
187    if(!$perm->have_perm(EWIKI_PHPLIB_ALLUSERS_PERM)){
188         $auth->unauth();  # We have to relogin, so clear current auth info
189         $auth->nobody = false; # We are forcing login, so default auth is 
190                              # disabled
191     $auth->auth["uid"] = "form";
192     $auth->auth["exp"] = 0x7fffffff;
193     $auth->auth["refresh"] = 0x7fffffff;
194     $sess->freeze();                                                     
195                                                                 
196         $output=ewiki_t("LOGINFORM");
197         return(false);
198    }
199    
200    //If we have a valid user (in group user) return sucess
201    if ($perm->have_perm(EWIKI_PHPLIB_ALLUSERS_PERM)) {
202         $ewiki_ring=1;   // priviliged but ordinary user
203    }
204    else {
205         $ewiki_ring=3;   // every other stupid, browse-only access
206    }
207
208    
209         //If we have a valid user (in group user) return sucess
210    return($perm->have_perm(EWIKI_PHPLIB_ALLUSERS_PERM));
211 }
212
213 class My_User extends User {
214   var $classname = "My_User";
215   var $register_globals = false;                
216  
217   var $magic          = "Abracadabra";     ## ID seed
218   var $that_class     = "Example_CT_Sql";  ## name of data storage container class
219   
220   function changepw($oldpw,$newpw1,$newpw2){
221         global $auth;
222         
223         if(!$auth->check_login($auth->auth['uname'],$oldpw)){
224                 $auth->unauth();  # Invalid password, log them out              
225                 return(ewiki_t("BADOLDPW"));    
226         }elseif($reason=$this->is_pw_invalid($newpw1)){
227                 return($reason);
228         }elseif($newpw1!=$newpw2){
229                 return(ewiki_t("NOMATCH"));
230         }       
231         $auth->db->query(sprintf("UPDATE %s SET password='%s'".
232                 "       where user_id = '%s' ",
233         $auth->database_table,
234         addslashes($newpw1),    
235         addslashes($this->id)
236         ));
237         return(ewiki_t("PWCHNGD"));
238
239          
240         
241   }
242   
243         function is_pw_invalid($pw){
244                 return($pw=='');
245         }
246
247 }
248
249
250 class My_Auth extends Auth {
251   var $classname = "My_Auth";
252   
253   var $mode = "log";              ## "log" for login only systems,
254 #  var $classname      = "Example_Auth";
255
256   var $lifetime       = 15;
257
258   var $database_class = "DB_Example";
259   var $database_table = "auth_user";
260   var $nobody    = true;
261   
262   function auth_loginform() {
263     global $sess;
264     global $_PHPLIB;
265
266     include($_PHPLIB["libdir"] . "loginform.ihtml");
267   }
268   function check_login($username,$password){
269     $uid = false;
270         
271       $this->db->query(sprintf("select user_id, perms ".
272                              "        from %s ".
273                              "       where username = '%s' ".
274                              "         and password = '%s'",
275                           $this->database_table,
276                           addslashes($username),
277                           addslashes($password)));
278
279     while($this->db->next_record()) {
280       $uid = $this->db->f("user_id");
281       $this->auth["perm"] = $this->db->f("perms");
282     }
283         
284         return $uid;
285   }
286   
287   function auth_validatelogin() {
288     global $HTTP_POST_VARS;
289         
290     if(isset($HTTP_POST_VARS["username"])) {
291       $this->auth["uname"] = $HTTP_POST_VARS["username"];        ## This provides access for "loginform.ihtml"
292     }
293     
294     
295         return $this->check_login($HTTP_POST_VARS["username"],$HTTP_POST_VARS["password"]);
296         
297
298      
299   }  
300   
301 }
302
303
304
305 ?>