root/trunk/wifidog-auth/wifidog/classes/Authenticator.php @ 794

Revision 760, 6.9 KB (checked in by fproulx, 8 years ago)

2005-09-17 Francois Proulx <francois.proulx@…>

  • Fixed a few bugs in Radius class
  • Improved syntax and completed 100% of French translations
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2/********************************************************************\
3 * This program is free software; you can redistribute it and/or    *
4 * modify it under the terms of the GNU General Public License as   *
5 * published by the Free Software Foundation; either version 2 of   *
6 * the License, or (at your option) any later version.              *
7 *                                                                  *
8 * This program is distributed in the hope that it will be useful,  *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
11 * GNU General Public License for more details.                     *
12 *                                                                  *
13 * You should have received a copy of the GNU General Public License*
14 * along with this program; if not, contact:                        *
15 *                                                                  *
16 * Free Software Foundation           Voice:  +1-617-542-5942       *
17 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
18 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
19 *                                                                  *
20 \********************************************************************/
21/**@file Authenticator.php
22 * @author Copyright (C) 2005 Benoit Grégoire <bock@step.polymtl.ca>,
23 * Technologies Coeus inc.
24 */
25
26require_once BASEPATH.'classes/Network.php';
27
28/** Abstract class to represent an authentication source */
29abstract class Authenticator
30{
31        private $mNetwork;
32
33        function __construct($network_id)
34        {
35                $this->mNetwork = Network::getObject($network_id);
36        }
37
38        public function getNetwork()
39        {
40                return $this->mNetwork;
41        }
42
43        /** Attempts to login a user against the authentication source.  If successfull, returns a User object */
44        function login()
45        {
46        }
47
48        /** Logs out the user
49         * $conn_id:  The connection id for the connection to work on.  Optionnal.
50         *  If  it is not present, the behaviour depends if the network supports
51         * multiple logins.  If it does not, all connections associated with the
52         * current user will be destroyed.  If it does, only the connections
53         * tied to the current node will be destroyed */
54        function logout($conn_id = null)
55        {
56                global $db;
57                $conn_id = $db->escapeString($conn_id);
58                if (!empty ($conn_id))
59                {
60                        $db->ExecSqlUniqueRes("SELECT NOW(), *, CASE WHEN ((NOW() - reg_date) > networks.validation_grace_time) THEN true ELSE false END AS validation_grace_time_expired FROM connections JOIN users ON (users.user_id=connections.user_id) JOIN networks ON (users.account_origin = networks.network_id) WHERE connections.conn_id='$conn_id'", $info, false);
61
62                        $user = User :: getObject($info['user_id']);
63                        $network = $user->getNetwork();
64                        $splash_user_id = $network->getSplashOnlyUser()->getId();
65                        $this->acctStop($conn_id);
66                }
67                else
68                {
69                        $user = User :: getCurrentUser();
70                        $network = $user->getNetwork();
71                        $splash_user_id = $network->getSplashOnlyUser()->getId();
72                        if ($splash_user_id != $user->getId() && $node = Node :: getCurrentNode())
73                        {
74                                //Try to destroy all connections tied to the current node
75                                $sql = "SELECT conn_id FROM connections WHERE user_id = '{$user->getId()}' AND node_id='{$node->getId()}' AND token_status='".TOKEN_INUSE."';\n";
76                                $conn_rows = null;
77                                $db->ExecSql($sql, $conn_rows, false);
78                                if ($conn_rows)
79                                {
80                                        foreach ($conn_rows as $conn_row)
81                                        {
82                                                $this->acctStop($conn_row['conn_id']);
83                                        }
84                                }
85                        }
86                }
87
88                if ($splash_user_id != $user->getId() && $network->getMultipleLoginAllowed() == false)
89                {
90                        /* The user isn't the splash_only user and the network config does not allow multiple logins. 
91                         * Logging in with a new token implies that all other active tokens should expire */
92                        $sql = "SELECT conn_id FROM connections WHERE user_id = '{$user->getId()}' AND token_status='".TOKEN_INUSE."';\n";
93                        $conn_rows = null;
94                        $db->ExecSql($sql, $conn_rows, false);
95                        if ($conn_rows)
96                        {
97                                foreach ($conn_rows as $conn_row)
98                                {
99                                        $this->acctStop($conn_row['conn_id']);
100                                }
101                        }
102                }
103                global $session;
104                $session->destroy();
105
106        }
107
108        /** Start accounting traffic for the user
109         * $conn_id:  The connection id for the connection to work on */
110        function acctStart($conn_id)
111        {
112                global $db;
113                $conn_id = $db->escapeString($conn_id);
114                $db->ExecSqlUniqueRes("SELECT NOW(), *, CASE WHEN ((NOW() - reg_date) > networks.validation_grace_time) THEN true ELSE false END AS validation_grace_time_expired FROM connections JOIN users ON (users.user_id=connections.user_id) JOIN networks ON (users.account_origin = networks.network_id) WHERE connections.conn_id='$conn_id'", $info, false);
115                $network = Network :: getObject($info['network_id']);
116                $splash_user_id = $network->getSplashOnlyUser()->getId();
117                $auth_response = $info['account_status'];
118                /* Login the user */
119                $mac = $db->EscapeString($_REQUEST['mac']);
120                $ip = $db->EscapeString($_REQUEST['ip']);
121                $sql = "UPDATE connections SET "."token_status='".TOKEN_INUSE."',"."user_mac='$mac',"."user_ip='$ip',"."last_updated=NOW()"."WHERE conn_id='{$conn_id}';\n";
122                $db->ExecSqlUpdate($sql, false);
123                if ($splash_user_id != $info['user_id'] && $network->getMultipleLoginAllowed() == false)
124                {
125                        /* The user isn't the splash_only user and the network config does not allow multiple logins. 
126                         * Logging in with a new token implies that all other active tokens should expire */
127                        $token = $db->EscapeString($_REQUEST['token']);
128                        $sql = "SELECT * FROM connections WHERE user_id = '{$info['user_id']}' AND token_status='".TOKEN_INUSE."' AND token!='$token';\n";
129                        $conn_rows = array ();
130                        $db->ExecSql($sql, $conn_rows, false);
131                        if (isset ($conn_rows))
132                        {
133                                foreach ($conn_rows as $conn_row)
134                                {
135                                        $this->acctStop($conn_row['conn_id']);
136                                }
137                        }
138                }
139
140                /* Delete all unused tokens for this user, so we don't fill the database with them */
141                $sql = "DELETE FROM connections "."WHERE token_status='".TOKEN_UNUSED."' AND user_id = '{$info['user_id']}';\n";
142                $db->ExecSqlUpdate($sql, false);
143        }
144
145        /** Update traffic counters
146         * $conn_id: The connection id for the connection to work on */
147        function acctUpdate($conn_id, $incoming, $outgoing)
148        {
149                // Write traffic counters to database
150                global $db;
151                $conn_id = $db->escapeString($conn_id);
152                $db->ExecSqlUpdate("UPDATE connections SET "."incoming='$incoming',"."outgoing='$outgoing',"."last_updated=NOW() "."WHERE conn_id='{$conn_id}'");
153        }
154
155        /** Final update and stop accounting
156         * $conn_id:  The connection id (the token id) for the connection to work on
157         * */
158        function acctStop($conn_id)
159        {
160                // Stop traffic counters update
161                global $db;
162                $conn_id = $db->escapeString($conn_id);
163                $db->ExecSqlUpdate("UPDATE connections SET "."timestamp_out=NOW(),"."token_status='".TOKEN_USED."' "."WHERE conn_id='{$conn_id}';\n", false);
164        }
165
166        /**
167         * Property method that tells if the class allows registration
168         */
169        function isRegistrationPermitted()
170        {
171                return false;
172        }
173
174} // End class
175?>
Note: See TracBrowser for help on using the browser.