Source for file RSDB.php

Documentation is available at RSDB.php


1 <?php
2 // Auth_Container_RSDB: An Extended Container for PEAR::Auth based on Auth_Container_DB
3 // Copyright (C) 2003 Lukas Feiler
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 /**This file contains the class Auth_Container_RSDB.
20 *
21 * @package Auth_Container_RSDB
22 * @author Lukas Feiler <lukas.feiler@chello.at>
23 * @copyright Lukas Feiler 2003
24 * @filesource
25 */
26
27 /**Auth_Container_RSDB extends Auth_Container_DB contained in DB.php.
28 */
29 require_once "Auth/Container/DB.php";
30
31
32 /**An extended container for PEAR::Auth based on Auth_Container_DB.
33 *
34 * Auth_Container_RSDB most important allows to activate/deactivate users
35 * so that they cannot log in any more but are still existing in the database.
36 *
37 * @author Lukas Feiler <lukas.feiler@chello.at>
38 * @version 0.1.9
39 * @copyright Lukas Feiler 2003
40 * @package Auth_Container_RSDB
41 */
42 class Auth_Container_RSDB extends Auth_Container_DB
43 {
44 // {{{ _setDefaults()
45
46 /**
47 * Set some default options. RSDB adds the options activatedcol and persistent!
48 *
49 * @access private
50 */
51 function _setDefaults()
52 {
53 $this->options['table'] = "auth";
54 $this->options['usernamecol'] = "username";
55 $this->options['passwordcol'] = "password";
56 $this->options['activatedcol'] = "1";
57 $this->options['dsn'] = "";
58 $this->options['db_fields'] = "";
59 $this->options['cryptType'] = "md5";
60 $this->options['persistent'] = true;
61 }
62 // }}}
63
64
65 // {{{ fetchData()
66
67 /**
68 * Get user information from database. RSDB appends ' AND $activatedcol=1' to the query.
69 *
70 * This function uses the given username to fetch
71 * the corresponding login data from the database
72 * table. If an account that matches the passed username
73 * and password is found, the function returns true.
74 * Otherwise it returns false.
75 *
76 * @param string Username
77 * @param string Password
78 * @return mixed Error object or boolean
79 */
80 function fetchData($username, $password)
81 {
82 // Prepare for a database query
83 $err = $this->_prepare();
84 if ($err !== true) {
85 return PEAR::raiseError($err->getMessage(), $err->getCode());
86 }
87
88 // Find if db_fileds contains a *, i so assume all col are selected
89 if (strstr($this->options['db_fields'], '*')) {
90 $sql_from = "*";
91 } else {
92 $sql_from = $this->options['usernamecol'] . ", ".$this->options['passwordcol'].$this->options['db_fields'];
93 }
94
95 if ($this->options['activatedcol'] == "") {
96 $activatedcol = 1;
97 } else {
98 $activatedcol = $this->options['activatedcol'];
99 }
100 $query = "SELECT ! FROM ! WHERE ! = ? AND ! = 1";
101 $query_params = array(
102 $sql_from,
103 $this->options['table'],
104 $this->options['usernamecol'],
105 $username,
106 $activatedcol
107 );
108 $res = $this->db->getRow($query, $query_params, DB_FETCHMODE_ASSOC);
109
110 if (DB::isError($res)) {
111 return PEAR::raiseError($res->getMessage(), $res->getCode());
112 }
113 if (!is_array($res)) {
114 $this->activeUser = '';
115 return false;
116 }
117 if ($this->verifyPassword(trim($password),
118 trim($res[$this->options['passwordcol']]),
119 $this->options['cryptType'])) {
120 // Store additional field values in the session
121 foreach ($res as $key => $value) {
122 if ($key == $this->options['passwordcol'] ||
123 $key == $this->options['usernamecol']) {
124 continue;
125 }
126 Auth::setAuthData($key, $value);
127 }
128
129 return true;
130 }
131
132 $this->activeUser = $res[$this->options['usernamecol']];
133 return false;
134 }
135
136
137 // }}}
138
139
140
141 // {{{ _connect()
142
143 /**
144 * Connect to database by using the given DSN string
145 *
146 * @access private
147 * @param string DSN string
148 * @return mixed Object on error, otherwise bool
149 */
150 function _connect($dsn)
151 {
152 if (is_string($dsn) || is_array($dsn)) {
153 $this->db = DB::Connect($dsn, $this->options['persistent']);
154 } elseif (get_parent_class($dsn) == "db_common") {
155 $this->db = $dsn;
156 } elseif (is_object($dsn) && DB::isError($dsn)) {
157 return PEAR::raiseError($dsn->code, PEAR_ERROR_DIE);
158 } else {
159 return PEAR::raiseError("The given dsn was not valid in file " . __FILE__ . " at line " . __LINE__,
160 41,
161 PEAR_ERROR_RETURN,
162 null,
163 null
164 );
165
166 }
167
168 if (DB::isError($this->db) || PEAR::isError($this->db)) {
169 return PEAR::raiseError($this->db->getMessage(), $this->db->getCode());
170 } else {
171 return true;
172 }
173 }
174
175 // }}}
176 }
177 ?>

Documentation generated on Mon, 8 Dec 2003 13:10:40 +0100 by phpDocumentor 1.2.3