G_base var added to fix relative path problem in include of includes
[brisk.git] / web / Obj / dbase_file.phh
1 <?php
2   /*
3    *  brisk - dbase_file.phh
4    *
5    *  Copyright (C) 2006-2011 Matteo Nastasi
6    *                          mailto: nastasi@alternativeoutput.it 
7    *                                  matteo.nastasi@milug.org
8    *                          web: http://www.alternativeoutput.it
9    *
10    * This program is free software; you can redistribute it and/or modify
11    * it under the terms of the GNU General Public License as published by
12    * the Free Software Foundation; either version 2 of the License, or
13    * (at your option) any later version.
14    *
15    * This program is distributed in the hope that it will be useful, but
16    * WITHOUT ANY WARRANTY; without even the implied warranty of
17    * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    * General Public License for more details. You should have received a
19    * copy of the GNU General Public License along with this program; if
20    * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21    * Suite 330, Boston, MA 02111-1307, USA.
22    *
23    */
24
25 require_once("${G_base}Obj/dbase_base.phh");
26
27 define(BRISK_AUTH_CONF,   "brisk_auth.conf.pho");
28
29 class LoginDB {
30     var $item;
31     var $item_n;
32
33   
34     function LoginDB()
35     {
36         GLOBAL $DOCUMENT_ROOT;
37         log_main("LoginDB create:start");
38
39         if (file_exists("$DOCUMENT_ROOT/Etc/".BRISK_AUTH_CONF)) {
40             require("$DOCUMENT_ROOT/Etc/".BRISK_AUTH_CONF);
41         }
42         else {
43             $this->item = array( new LoginDBItem(1, "uno", md5("one"),   "pippo@pluto.com", USER_FLAG_TY_SUPER),
44                                  new LoginDBItem(2, "due", md5("two"),   "pippo@pluto.com", USER_FLAG_TY_NORM),
45                                  new LoginDBItem(3, "a_b", md5("abb"),   "pippo@pluto.com", USER_FLAG_TY_NORM),
46                                  new LoginDBItem(4, "tre", md5("three"), "pippo@pluto.com", USER_FLAG_TY_NORM) );
47         }
48         $this->item_n = count($this->item);
49         log_main("LoginDB create:end");
50     }
51
52     function count()
53     {
54         return ($this->item_n);
55     }
56
57     function login_exists($login)
58     {
59         log_main("login_exists: ".$login);
60     
61         /* check the existence of the nick in the LoginDB */
62         for ($i = 0 ; $i < $this->item_n ; $i++) {
63             if (strcasecmp($this->item[$i]->login, $login) == 0) {
64                 log_main("login[".$i."]: ".$this->item[$i]->login);
65                 return (TRUE);
66             }
67         }
68         return (FALSE);
69     }
70
71     function getlogin_byidx($idx)
72     {
73         if ($idx >= $this->item_n)
74             return FALSE;
75         return ($this->item[$idx]->login);
76     }
77
78     function &getitem_bylogin($login, &$id)
79         {
80             GLOBAL $G_false;
81
82             log_main("login_exists: ".$login);
83     
84             /* check the existence of the nick in the LoginDB */
85             for ($i = 0 ; $i < $this->item_n ; $i++) {
86                 if (strcasecmp($this->item[$i]->login, $login) == 0) {
87                     log_main("login[".$i."]: ".$this->item[$i]->login);
88                     $ret = &$this->item[$i];
89                     $id = $i;
90                     return ($ret);
91                 }
92             }
93             $id = -1;
94             return ($G_false);
95         }
96
97     function getmail($login)
98     {
99         log_main("getmail");
100     
101         /* check the existence of the nick in the LoginDB */
102         for ($i = 0 ; $i < $this->item_n ; $i++) {
103             if (strcasecmp($this->item[$i]->login, $login) == 0) {
104                 log_main("login[".$i."]: ".$this->item[$i]->login);
105                 return ($this->item[$i]->email);
106             }
107         }
108         return (FALSE);
109     }
110
111     function gettype($login)
112     {
113         log_main("getmail");
114     
115         /* check the existence of the nick in the LoginDB */
116         for ($i = 0 ; $i < $this->item_n ; $i++) {
117             if (strcasecmp($this->item[$i]->login, $login) == 0) {
118                 log_main("login[".$i."]: ".$this->item[$i]->login);
119                 return ($this->item[$i]->type);
120             }
121         }
122         return (FALSE);
123     }
124
125     function &login_verify($login, $pass)
126     {
127         GLOBAL $G_false;
128         
129         $ret = &$G_false;
130         
131         log_main("login_verify: ".$login);
132         
133         /* check the existence of the nick in the LoginDB */
134         for ($i = 0 ; $i < $this->item_n ; $i++) {
135             log_main("login_verify: LOOP");
136             if (strcasecmp($this->item[$i]->login, $login) == 0) {
137                 log_main("login[".$i."]: ".$this->item[$i]->login);
138                 
139                 /* if it exists check for a valid challenge */
140                 if (($a_sem = Challenges::lock_data()) != FALSE) { 
141                     
142                     if (($chals = &Challenges::load_data()) != FALSE) {
143                         for ($e = 0 ; $e < $chals->item_n ; $e++) {
144                             
145                             log_main("challenge[".$i."]: ".$chals->item[$e]->login);
146                             if (strcmp($login, $chals->item[$e]->login) == 0) {
147                                 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$this->item[$i]->pass)."]");
148                                 
149                                 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
150                                     log_main("login_verify SUCCESS for ".$login);
151                                     
152                                     $chals->rem($login);
153                                     $ret = &$this->item[$i];
154                                     break;
155                                 }
156                             }
157                         } // end for ($e = 0 ...
158                     }
159                     
160                     if ($chals->ismod()) {
161                         Challenges::save_data(&$chals);
162                     }
163                     
164                     Challenges::unlock_data($a_sem);
165                 }
166                 break;
167             } //  if (strcasecmp($this->item[$i]->login, ...
168         }
169         
170         return ($ret);
171     }
172     
173 } // End class LoginDB
174
175 ?>