User management for the world wide web

Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

Apache.php

00001 <?php
00002 require_once("GenieGate/utils.php");
00003 /* Register self. */
00004 $this->addListener(new GenieGate_Api_Plugin_Apache($this->CFG)); 
00005 
00006 
00013 class GenieGate_Api_Plugin_Apache_FlatGroupFile {
00014     
00015     var $tree;
00016 
00020     function GenieGate_Api_Plugin_Apache_FlatGroupFile($filename){
00021         $this->tree = $this->parse($filename);
00022     }
00023     
00033     function parse($filename){
00034         $tree = array();
00035         if(! file_exists($filename)){
00036             return($tree);
00037         }
00038         $buffer = file($filename); // Slurp it all in.
00039         if(! is_array($buffer)){
00040             return($tree);
00041         }   
00042         foreach($buffer as $line){
00043             // Parse into a record, group: user user user
00044             $record = preg_split("/\:\s*|\s+/",trim($line));
00045             $gid = array_shift($record);        
00046             // Hmm.. multiple group: lines 
00047             // in the same file, this is perfectly legit.
00048             if(isset($tree[$gid])){
00049                 $tree[$gid] = array_merge($tree[$gid],$record);
00050             }else{
00051                 $tree[$gid] = $record;
00052             }
00053         }   
00054         return($tree);
00055     }
00056     
00064     function removeUid($uid) {
00065         foreach($this->tree as $gid => $members){
00066             // Search for user.
00067             $ix = array_search($uid,$members);
00068             // Remove the member.
00069             if($members[$ix] == $uid){
00070                 $this->tree[$gid][$ix] = FALSE;
00071             }
00072         }   
00073     }
00074     
00081     function removeGid($gid){
00082         unset($this->tree[$gid]);
00083     }
00084     
00091     function addUid($uid,$gid){
00092         if(is_array($this->tree[$gid])){
00093             array_push($this->tree[$gid],$uid); 
00094         }else{
00095             $this->tree[$gid] = array($uid);
00096         }   
00097     }
00098     
00106     function save($fh) {
00107         foreach($this->tree as $gid => $members){
00108             if($gid){
00109                 $buf = "$gid:";
00110                 foreach($this->tree[$gid] as $uid){
00111                     if($uid){
00112                         $buf .= $uid . " ";
00113                     }
00114                 }
00115                 fputs($fh,$buf . "\n");
00116             }
00117         }
00118     }
00119 }
00120 
00135 class GenieGate_Api_Plugin_Apache extends GenieGate_Api_Plugin {
00136 
00137   var $CFG;
00138 
00147   function GenieGate_Api_Plugin_Apache(&$conf){
00148     $this->CFG = $conf[ApachePlugin];
00149   }
00150 
00160   function setupUser($ua,$fields,$conf_id){
00161     $pf = $this->CFG[AuthUserFile];    
00162     if($pf) {
00163       $this->apache_replaceOrAddPw($pf,$fields[uid],$fields[password]);     
00164     }
00165   }
00166 
00167   // Change password.
00168   function userInfoChanged(&$um,$uid,&$old_values,&$new_values) {
00169     $pf = $this->CFG[AuthUserFile]; 
00170     if($pf){
00171       $this->apache_replaceOrAddPw($pf,$new_values[uid],$new_values[password]);      
00172     }
00173   }
00174 
00183   function userGroupsChanged(&$gm,$uid,&$oldg,&$newg) {    
00184     $gf = $this->CFG[AuthGroupFile];
00185     if(! $gf){
00186         return; // No Group file specified in conf, so ignore.
00187     }
00188     $twr = new GenieGate_TempWriter($gf); // this also locks it, does backup etc..
00189     $groups = new GenieGate_Api_Plugin_Apache_FlatGroupFile($gf);
00190     $groups->removeUid($uid);
00191     // Add member to each group.
00192     foreach($newg as $g){
00193         $groups->addUid($uid,$g);
00194     }
00195     $groups->save($twr->getWrite());
00196     $twr->complete(); 
00197   }
00198 
00199   // Remove Group ID, and all members. Standard
00200   // Plugin hook.
00201   function groupNameDelete(&$gm,$gid,&$info){
00202     $gf = $this->CFG[AuthGroupFile];
00203     if(! $gf){
00204         return; // No Group file specified in conf, so ignore.
00205     }
00206     $twr = new GenieGate_TempWriter($gf); 
00207     $groups = new GenieGate_Api_Plugin_Apache_FlatGroupFile($gf);
00208     $groups->removeGid($gid);
00209     $groups->save($twr->getWrite());
00210     $twr->complete();
00211   }
00212 
00223   function apache_replaceOrAddPw($pf,$un,$pw){ 
00224     $crypt_data = "$un:" . crypt($pw) . "\n";
00225 
00226     $twr = new GenieGate_TempWriter($pf);
00227     $rh = $twr->getRead();
00228     $wh = $twr->getWrite();
00229     while(! feof($rh)){
00230       $buffer = fgets($rh,4096);
00231       list($id,$pw) = explode(":",$buffer);
00232       if($id != $un){
00233         fputs($wh,$buffer); // Nope, but do save data.
00234       }else{
00235         if(strlen($crypt_data) > 0){
00236             fputs($wh,$crypt_data); // It's us, save our entry to tmp file.
00237             $crypt_data = "";
00238         }
00239       }
00240     }
00241     // Did we save it?
00242     if(strlen($crypt_data)){
00243       fputs($wh,$crypt_data); // Nope, must be a new entry!
00244     }
00245     $twr->complete(); // Finish editing.
00246   }
00247 
00251   function removeUser($ua,$uid,$fields) {
00252     $pf = $this->CFG[AuthUserFile];
00253     if(! $pf){
00254       return;
00255     }
00256     if(! file_exists($pf)){
00257       return;
00258     }
00259     
00260     $twr = new GenieGate_TempWriter($pf);
00261     $rh = $twr->getRead();
00262     $wh = $twr->getWrite();
00263     while(! feof($rh)){
00264       $buffer = fgets($rh,4096);
00265       list($id,$pw) = explode(":",$buffer);
00266       if($id != $uid){
00267     fputs($wh,$buffer); // Not us.
00268       }
00269     }
00270     $twr->complete();
00271   }
00272 }
00273 
00274 
00275 ?>

DoxyGen Documentation generated by DoxyGen