File: validator.inc.php

package info (click to toggle)
zoph 0.6-2.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 1,716 kB
  • ctags: 2,283
  • sloc: php: 8,554; perl: 1,601; sh: 760; sql: 382; python: 338; makefile: 71
file content (222 lines) | stat: -rw-r--r-- 7,680 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
 * This file is part of Zoph.
 *
 * Zoph is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * A class to validate a user.
 */
class validator {

    var $username;
    var $password;

    /*
     * The constructor.
     */
    function validator($username, $password) {
        $this->username = $username;
        $this->password = $password;
    }

    /*
     * Validate a user.
     */
    function validate() {
        global $VALIDATOR;
        return $this->$VALIDATOR();
    }

    /*
     * Validate users using Zoph's database.
     */
    function default_validate() {
        $user = null;

        // No username or password are given, and a default user is defined
        // let's login as that...
        if (!$this->username && !$this->password && DEFAULT_USER) {
            $user = new user(DEFAULT_USER);
        } else {

            $query =
                "select user_id from " . DB_PREFIX . "users where " .
                "user_name = '" .  escape_string($this->username) . "' and " .
                "password = password('" . escape_string($this->password) . "')";

            $result = mysql_query($query);

            if (mysql_num_rows($result) == 1) {
                $row = mysql_fetch_array($result);

                $user = new user($row["user_id"]);
            } else {
                if (DEBUG) {
                    echo "No valid user found... trying old_password...<br>\n";
                }
                /*
                 * No valid user has been found. It could be that we've upgraded
                 * MySQL to a post-4.1 version and the password is still in 
                 * the old format. 
                 * Let's find out, first, we will determine if we're indeed
                 * running running a newer version than 4.1:
                 */
            
                $mysqlver=mysql_get_server_info();
                list($mysqlmaj, $mysqlmin) = split("\.", $mysqlver, 2);
                if (($mysqlmaj == 4 && $mysqlmin >= 1) || ($mysqlmaj >= 5) ) {
                    if (DEBUG) { 
                        echo "Yep, we're running MySQL 4.1 or later<br>\n"; 
                    }
                
                    $query =
                        "select user_id from " . DB_PREFIX . "users where " .
                        "user_name = '" .  escape_string($this->username) . 
                        "' and " .  "password = old_password('" . 
                        escape_string($this->password) . "')";
                    
                    $result = mysql_query($query);

                    if (mysql_num_rows($result) == 1) {
                        $row = mysql_fetch_array($result);
                        $user = new user($row["user_id"]);
                        /* Ok...we found the user, let's make sure 
                         * this won't happen again...
                         */
                     
                        $query = 
                            "update " . DB_PREFIX . "users " .
                            "set password=password('" . 
                            escape_string($this->password) . "') " .
                            "where user_name = '" . 
                            escape_string($this->username) . "' and " .
                            "password = old_password('" . 
                            escape_string($this->password) . "')";
                        
                        $result = mysql_query($query);
                    }
                }
            }
        }
            return $user;
    }

    /*
     * Validate users using Zoph's database against the
     * PHP_AUTH_USER and PHP_AUTH_PW variables.
     *
     * Contributed by Samuel Keim
     */
    function php_validate() {
        $user = null;

        if (empty($this->username)) {
            $this->username = $_SERVER['PHP_AUTH_USER'];

            if (empty($pword)) {
                $this->password = $_SERVER['PHP_AUTH_PW'];
            }
        }

        return $this->default_validate();
    }

    /*
     * Validate using an htpasswd file.
     * (C) and GPL Asheesh Laroia, 2002
     * Uses code by Jason Geiger and include()s "cdi@thewebmasters.net"'s
     * Htpasswd PHP class
     */
    function htpasswd_validate() {
        /*
            Due to licensing issues, the Htpaswd class is not distrubuted with
            Zoph.  You must download it before making use of this feature.
        */
        $user = null;
        include("Passwd.php");
        $htpass = new File_Passwd(HTPASS_FILE);

        if ($htpass->verifyPassword($this->username, $this->password)) {
            $query =
                "select user_id from " . DB_PREFIX . "users where " .
                "user_name = '" .  escape_string($this->username) . "'";

            $result = mysql_query($query);

            if (mysql_num_rows($result) == 0) {
                // make a new user
                $tmpUser = new user();
                $tmpUser->set('user_name', $this->username);
                $tmpUser->set('password', $this->password);

                // make a new person
                $tmpPerson = new person();
                $tmpPerson->set('first_name', $this->username);

                // put him in DB
                $tmpPerson->insert();
                $tmpUser->set('person_id', $tmpPerson->get('person_id'));

                if (DEFAULT_USER) {
                    // Give user same privileges as Guest:
                    $guestUser = new user(DEFAULT_USER);

                    $privNames = array(
                        'browse_people',
                        'browse_places',
                        'detailed_people',
                        'import',
                        'lightbox_id');

                     foreach ($privNames as $q) {
                         $tmpUser->set($q, $guestUser->get($q));
                     }

                     // Now, grant special privileges of being registered
                     $privNames = array(
                         'detailed_people' => 1,
                         'import' => 1);

                     foreach ($privNames as $k => $v) {
                         $tmpUser->set($k, $v);
                     }
                }

                // Put a row in the DB with this cool dude's info
                $tmpUser->insert();

                // And return a new user of that row number
                $user = new user($tmpUser->get('user_id'));
            }
            else if ((mysql_num_rows($result) == 1)) {
                $row = mysql_fetch_array($result);
                $user = new user($row["user_id"]);
            }
        }
        // Fall back to DEFAULT_USER
        // Would it have been better to just fall back to default_validate()?
        // For code reuse's sake, perhaps this should be its own function,
        // and in each we just say "$user = default_user();"
        else if (DEFAULT_USER) {
            $user = new user(DEFAULT_USER);
        }

        return $user;
    }

}

?>