6801031f93a8be3a4acf0b3329ea8963b9a30ff3
[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 BriskDB {
30     var $item;
31     var $item_n;
32
33   
34     function BriskDB()
35     {
36         log_main("BriskDB create:start");
37
38         log_main("BriskDB create:end");
39     }
40
41     function users_load() 
42     {
43         GLOBAL $DOCUMENT_ROOT;
44
45         if (file_exists("$DOCUMENT_ROOT/Etc/".BRISK_AUTH_CONF)) {
46             require("$DOCUMENT_ROOT/Etc/".BRISK_AUTH_CONF);
47         }
48         else {
49             $this->item = array( new LoginDBItem(1, "uno", md5("one"),   "pippo@pluto.com", USER_FLAG_TY_SUPER),
50                                  new LoginDBItem(2, "due", md5("two"),   "pippo@pluto.com", USER_FLAG_TY_NORM),
51                                  new LoginDBItem(3, "a_b", md5("abb"),   "pippo@pluto.com", USER_FLAG_TY_NORM),
52                                  new LoginDBItem(4, "tre", md5("three"), "pippo@pluto.com", USER_FLAG_TY_NORM) );
53         }
54         $this->item_n = count($this->item);
55     }
56
57     function count()
58     {
59         return ($this->item_n);
60     }
61
62     function login_exists($login)
63     {
64         log_main("login_exists: ".$login);
65     
66         /* check the existence of the nick in the BriskDB */
67         for ($i = 0 ; $i < $this->item_n ; $i++) {
68             if (strcasecmp($this->item[$i]->login, $login) == 0) {
69                 log_main("login[".$i."]: ".$this->item[$i]->login);
70                 return (TRUE);
71             }
72         }
73         return (FALSE);
74     }
75
76     function getlogin_byidx($idx)
77     {
78         if ($idx >= $this->item_n)
79             return FALSE;
80         return ($this->item[$idx]->login);
81     }
82
83     function &getitem_bylogin($login, &$id)
84         {
85             GLOBAL $G_false;
86
87             log_main("login_exists: ".$login);
88     
89             /* check the existence of the nick in the BriskDB */
90             for ($i = 0 ; $i < $this->item_n ; $i++) {
91                 if (strcasecmp($this->item[$i]->login, $login) == 0) {
92                     log_main("login[".$i."]: ".$this->item[$i]->login);
93                     $ret = &$this->item[$i];
94                     $id = $i;
95                     return ($ret);
96                 }
97             }
98             $id = -1;
99             return ($G_false);
100         }
101
102     function getmail($login)
103     {
104         log_main("getmail");
105     
106         /* check the existence of the nick in the BriskDB */
107         for ($i = 0 ; $i < $this->item_n ; $i++) {
108             if (strcasecmp($this->item[$i]->login, $login) == 0) {
109                 log_main("login[".$i."]: ".$this->item[$i]->login);
110                 return ($this->item[$i]->email);
111             }
112         }
113         return (FALSE);
114     }
115
116     function gettype($login)
117     {
118         log_main("getmail");
119     
120         /* check the existence of the nick in the BriskDB */
121         for ($i = 0 ; $i < $this->item_n ; $i++) {
122             if (strcasecmp($this->item[$i]->login, $login) == 0) {
123                 log_main("login[".$i."]: ".$this->item[$i]->login);
124                 return ($this->item[$i]->type);
125             }
126         }
127         return (FALSE);
128     }
129
130     function &login_verify($login, $pass)
131     {
132         GLOBAL $G_false;
133         
134         $ret = &$G_false;
135         
136         log_main("login_verify: ".$login);
137         
138         /* check the existence of the nick in the BriskDB */
139         for ($i = 0 ; $i < $this->item_n ; $i++) {
140             log_main("login_verify: LOOP");
141             if (strcasecmp($this->item[$i]->login, $login) == 0) {
142                 log_main("login[".$i."]: ".$this->item[$i]->login);
143                 
144                 /* if it exists check for a valid challenge */
145                 if (($a_sem = Challenges::lock_data()) != FALSE) { 
146                     
147                     if (($chals = &Challenges::load_data()) != FALSE) {
148                         for ($e = 0 ; $e < $chals->item_n ; $e++) {
149                             
150                             log_main("challenge[".$i."]: ".$chals->item[$e]->login);
151                             if (strcmp($login, $chals->item[$e]->login) == 0) {
152                                 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$this->item[$i]->pass)."]");
153                                 
154                                 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
155                                     log_main("login_verify SUCCESS for ".$login);
156                                     
157                                     $chals->rem($login);
158                                     $ret = &$this->item[$i];
159                                     break;
160                                 }
161                             }
162                         } // end for ($e = 0 ...
163                     }
164                     
165                     if ($chals->ismod()) {
166                         Challenges::save_data(&$chals);
167                     }
168                     
169                     Challenges::unlock_data($a_sem);
170                 }
171                 break;
172             } //  if (strcasecmp($this->item[$i]->login, ...
173         }
174         
175         return ($ret);
176     }
177
178     function bin5_points_save($date, $ttok, $tidx, $codes, $pts)
179     {
180         return TRUE;
181     }
182 } // End class BriskDB
183
184 ?>