Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

userdata_xml.php

Go to the documentation of this file.
00001 <?php
00002 /* $Id: userdata_xml.php,v 1.2 2003/10/03 15:28:42 mikko Exp $ */
00003 /*
00004  * ImaComm - a web based photo album software
00005  * Copyright (C) 2003, Mikko Kokkonen
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or (at
00010  * your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00029 class Userdata_XML
00030 {
00031         var $minixml;
00032         
00033         function Userdata_XML()
00034         {
00035                 $this->prefix = realpath(dirname(__FILE__)."/../");
00036         }
00037         
00044         // {{{ createFile($_REQUEST)
00045         function createFile($vars)
00046         {
00047                 $this->file = $this->prefix."/conf/users.xml";
00048                 
00049                 $minixmlpath = $vars['minixml_path'];
00050                 $this->minixml = $minixmlpath;
00051                 if (!file_exists("$minixmlpath/minixml.inc.php"))
00052                 {
00053                         $this->file = null;
00054                         return null;
00055                 }
00056                 require_once "$minixmlpath/minixml.inc.php";
00057                 $doc = new MiniXMLDoc();
00058                 $root =& $doc->getRoot();
00059                 $users =& $root->createChild("users");
00060                 $users->attribute("version", "1.0");
00061 
00062                 $fp = @fopen($this->file, "w");
00063                 if ($fp)
00064                 {
00065                         fputs($fp, $doc->toString());
00066                         fclose($fp);
00067                 }
00068         }
00069         // }}}
00070 
00071         // {{{ addUser($username, $password, $class, $parameters, $opts)
00072         function addUser($username, $password, $class, $params, $opts)
00073         {
00074                 require_once ($this->minixml."/minixml.inc.php");
00075                 $doc = new MiniXMLDoc();
00076                 $doc->fromFile($this->file);
00077                 $root =& $doc->getRoot();
00078                 $users =& $root->getElement("users");
00079 
00080                 // Do we have childen $username?
00081                 if (!get_class($users))
00082                 {
00083                         $users =& $root->createChild("users");
00084                 }
00085                 if (method_exists($users, "numChildren"))
00086                 {
00087                         for ($c = 0; $c < $users->numChildren(); $c++)
00088                         {
00089                                 $user =& $users->xchildren[$c];
00090                                 $id = trim($user->attribute("id"));
00091                                 if ($id == $username)
00092                                 {
00093                                         $users->removeChild($user);
00094                                         break;
00095                                 }
00096                         }
00097                 }
00098                 
00099                 $user =& $users->createChild("user");
00100                 $user->attribute("id", $username);
00101                 $pw =& $user->createChild("password");
00102                 $pw->text($password);
00103                 $cls =& $user->createChild("class");
00104                 $cls->text($class);
00105 
00106                 if (is_array($param))
00107                 {
00108                         $names = array_keys($params);
00109                         for ($p = 0; $p < count($names); $p++)
00110                         {
00111                                 $param =& $user->createChild("parameter");
00112                                 $param->attribute("name", $names[$p]);
00113                                 $param->text($param[$names[$p]]);
00114                         }
00115                 }
00116                 if (is_array($opts))
00117                 {
00118                         $names = array_keys($opts);
00119                         for ($o = 0; $o < count($names); $o++)
00120                         {
00121                                 $options =& $user->createChild("option");
00122                                 $option->attribute("name", $names[$p]);
00123                                 $option->text($opts[$names[$p]]);
00124                         }
00125                 }
00126 
00127                 // And save output...
00128                 $fp = @fopen($this->file, "w");
00129                 if ($fp)
00130                 {
00131                         fputs($fp, $doc->toString());
00132                         fclose($fp);
00133                 }
00134                 else
00135                 {
00136                         die("Error: ".$this->file);
00137                 }
00138         }
00139         // }}}
00140 
00141         // {{{ loadData($username, $password)
00142         function loadData($file, $username, $password)
00143         {
00144                 global $settings;
00145                 
00146                 if (is_null($this->minixml))
00147                 {
00148                         $this->minixml = $settings->attribute("minixml_path");
00149                 }
00150                 // Load user data
00151                 require_once ($this->minixml."/minixml.inc.php");
00152                 $doc = new MiniXMLDoc();
00153                 $doc->fromFile($file);
00154                 $root =& $doc->getRoot();
00155                 $users =& $root->getElement("users");
00156 
00157                 // Find a correct user...
00158                 for ($c = 0; $c < $users->numChildren(); $c++)
00159                 {
00160                         $user =& $users->xchildren[$c];
00161                         $id = $user->attribute("id");
00162                         if ($id != $username)
00163                         {
00164                                 continue;
00165                         }
00166                         // Is the password OK?
00167                         $pwn =& $user->getElement("password");
00168                         $pw = trim($pwn->text());
00169                         // Do we have crypted password...
00170                         // How do we resolve that? Just presume that everything is MD5'ed!
00171                         if (preg_match('/^\$1\$/', $password))
00172                         {
00173                                 if ($password != $pw)
00174                                 {
00175                                         continue;
00176                                 }
00177                         }
00178                         elseif (crypt($password, $pw) != $pw)
00179                         {
00180                                 continue;
00181                         }
00182 
00183                         $this->password = $pw;
00184                         $class =& $user->getElement("class");
00185                         $this->class = trim($class->text());
00186 
00187                         // Get all options...
00188                         for ($o = 0; $o < $user->numChildren(); $o++)
00189                         {
00190                                 $child =& $user->xchildren[$o];
00191                                 if ($child->name() == "option")
00192                                 {
00193                                         $this->options[$child->attribute("name")] = $child->text();
00194                                 }
00195                                 elseif ($child->name() == "parameter")
00196                                 {
00197                                         $this->parameters[$child->attribute("name")] = $child->text();
00198                                 }
00199                         }
00200                         $this->allok = true;
00201                 }
00202         }
00203         // }}}
00204 }
00205 ?>

Generated on Sun Oct 19 11:08:01 2003 for ImaComm by doxygen1.3