LoginDB renamed to more general BriskDB and separate the creation of the class with...
[brisk.git] / web / Obj / dbase_pgsql.phh
1 <?php
2   /*
3    *  brisk - dbase_pgsql.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 $escsql_from = array( "\\",   "'"   );
28 $escsql_to   = array( "\\\\", "\\'" );
29
30 function escsql($s)
31 {
32     GLOBAL $escsql_from, $escsql_to;
33
34     return str_replace($escsql_from, $escsql_to, $s);
35 }
36
37 class DBConn 
38 {
39     static $dbcnnx = FALSE;
40     var $db = FALSE;
41     
42     function DBConn()
43     {
44         GLOBAL $G_dbauth;
45         
46         if (DBConn::$dbcnnx == FALSE) {
47             if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth))) {
48                 echo "DB connection failed.";
49                 exit;
50             }
51         }
52         $this->db = DBConn::$dbcnnx;
53
54         return;
55     }
56     function db()
57     {
58         return ($this->db);
59     }
60 }
61
62 class BriskDB
63 {
64     var $dbconn;
65     var $item;
66     var $item_n;
67     
68     function BriskDB()
69     {
70         GLOBAL $DOCUMENT_ROOT, $G_dbpfx, $G_false;
71         log_main("BriskDB create:start");
72         
73         $this->dbconn = new DBConn();
74         
75         log_main("BriskDB create:end");
76     }
77
78     function users_load()
79     {
80     }
81     
82     function login_exists($login)
83     {
84         GLOBAL $G_dbpfx;
85
86         /* check the existence of the nick in the BriskDB */
87         log_main("login_exists: ".$login);
88         
89         $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s');",  $G_dbpfx, escsql($login));
90         if (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) != FALSE)
91             if (pg_numrows($user_pg) == 1)
92                 return TRUE;
93         
94         return FALSE;
95     }
96
97     function &getrecord_bylogin($login) {
98         GLOBAL $G_false, $G_dbpfx;
99
100         $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s');",  $G_dbpfx, escsql($login));
101         if (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) == FALSE)
102             return $ret;
103         
104         if (pg_numrows($user_pg) != 1)
105             return $ret;
106         
107         $user_obj = pg_fetch_object($user_pg, 0);
108
109         return ($user_obj);
110     }
111
112
113     
114     function &login_verify($login, $pass)
115     {
116         GLOBAL $G_dbpfx, $G_false;
117         
118         $ret = &$G_false;
119         
120         log_main("login_verify: ".$login);
121         
122         
123         //O /* check the existence of the nick in the BriskDB */
124         //O for ($i = 0 ; $i < $this->item_n ; $i++) {
125         //O log_main("login_verify: BEGIN");
126         
127         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
128             return $ret;
129
130         log_main("login[".$user_obj->code."]: ".$user_obj->login);
131         
132         /* if it exists check for a valid challenge */
133         if (($a_sem = Challenges::lock_data()) != FALSE) { 
134             if (($chals = &Challenges::load_data()) != FALSE) {
135                 for ($e = 0 ; $e < $chals->item_n ; $e++) {
136                     
137                     log_main("challenge[".$e."]: ".$chals->item[$e]->login);
138                     if (strcmp($login, $chals->item[$e]->login) == 0) {
139                         log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
140                         
141                         if (strcmp($pass , md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
142                             log_main("login_verify SUCCESS for ".$login);
143                             
144                             $chals->rem($login);
145                             $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
146                             return ($ret);
147                             //O break;
148                         }
149                     }
150                 } // end for ($e = 0 ...
151             }
152             
153             if ($chals->ismod()) {
154                 Challenges::save_data(&$chals);
155             }
156             
157             Challenges::unlock_data($a_sem);
158         }
159         //O break;
160         // O } //  if (strcasecmp($this->item[$i]->login, ...
161         //O }
162     
163         return ($ret);
164     }
165
166     function &getitem_bylogin($login, &$id) {
167         GLOBAL $G_false;
168         
169         $ret = &$G_false;
170         $id = -1;
171         
172         log_main("getitem_bylogin: ".$login);
173         
174         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
175             return $ret;
176
177         $id = $user_obj->code;
178         return (LoginDBItem::LoginDBItemFromRecord($user_obj));
179     }
180     
181     // TODO FOR DB
182     function getmail($login)
183     {
184         log_main("getmail");
185
186         if (($ret = $this->getrecord_bylogin($login)) == FALSE)
187             return FALSE;
188         
189         return ($ret->email);
190     }
191
192     function addusers_from_olddb($olddb, &$cont)
193     {
194         GLOBAL $G_dbpfx;
195
196         for ($i = 0 ; $i < $olddb->count() ; $i++) {
197             $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
198                                 $G_dbpfx, escsql($olddb->item[$i]->login), escsql($olddb->item[$i]->pass),
199                                 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL); 
200             
201             // if ( ! (($user_pg = pg_exec($dbconn,$order_add_sql)) != FALSE && pg_affected_rows($order_pg) == 1) ) {
202
203             if ( ! (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
204                 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
205
206                 return FALSE;
207             }
208         }
209         return TRUE;
210     }
211 } // End class LoginDB
212
213 class LoginDBOld 
214 {
215     var $item;
216     var $item_n;
217
218     function LoginDBOld($filename)
219     {
220         GLOBAL $DOCUMENT_ROOT;
221         log_main("LoginDBOld create:start");
222
223         if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
224             require("$DOCUMENT_ROOT/Etc/".$filename);
225         }
226         else {
227             return (FALSE);
228         }
229         $this->item_n = count($this->item);
230         log_main("LoginDBOld create:end");
231     }
232
233     function count()
234     {
235         return ($this->item_n);
236     }
237
238
239
240 }
241
242
243     if (0 == 1) {
244         
245         
246         
247         
248         
249         
250         function count()
251         {
252         // sprintf("select count(code) from %sbrisk");
253         return ($this->item_n);
254     }
255
256     function getlogin_byidx($idx)
257     {
258         if ($idx >= $this->item_n)
259             return FALSE;
260         return ($this->item[$idx]->login);
261     }
262
263     function &getitem_bylogin($login, &$id)
264         {
265             GLOBAL $G_false;
266
267             log_main("login_exists: ".$login);
268     
269             /* check the existence of the nick in the LoginDB */
270             for ($i = 0 ; $i < $this->item_n ; $i++) {
271                 if (strcasecmp($this->item[$i]->login, $login) == 0) {
272                     log_main("login[".$i."]: ".$this->item[$i]->login);
273                     $ret = &$this->item[$i];
274                     $id = $i;
275                     return ($ret);
276                 }
277             }
278             $id = -1;
279             return ($G_false);
280         }
281
282     function getmail($login)
283     {
284         log_main("getmail");
285     
286         /* check the existence of the nick in the LoginDB */
287         for ($i = 0 ; $i < $this->item_n ; $i++) {
288             if (strcasecmp($this->item[$i]->login, $login) == 0) {
289                 log_main("login[".$i."]: ".$this->item[$i]->login);
290                 return ($this->item[$i]->email);
291             }
292         }
293         return (FALSE);
294     }
295
296     function gettype($login)
297     {
298         log_main("getmail");
299     
300         /* check the existence of the nick in the LoginDB */
301         for ($i = 0 ; $i < $this->item_n ; $i++) {
302             if (strcasecmp($this->item[$i]->login, $login) == 0) {
303                 log_main("login[".$i."]: ".$this->item[$i]->login);
304                 return ($this->item[$i]->type);
305             }
306         }
307         return (FALSE);
308     }
309
310     function &login_verify($login, $pass)
311     {
312         GLOBAL $G_false;
313         
314         $ret = &$G_false;
315         
316         log_main("login_verify: ".$login);
317         
318         /* check the existence of the nick in the LoginDB */
319         for ($i = 0 ; $i < $this->item_n ; $i++) {
320             log_main("login_verify: LOOP");
321             if (strcasecmp($this->item[$i]->login, $login) == 0) {
322                 log_main("login[".$i."]: ".$this->item[$i]->login);
323                 
324                 /* if it exists check for a valid challenge */
325                 if (($a_sem = Challenges::lock_data()) != FALSE) { 
326                     
327                     if (($chals = &Challenges::load_data()) != FALSE) {
328                         for ($e = 0 ; $e < $chals->item_n ; $e++) {
329                             
330                             log_main("challenge[".$i."]: ".$chals->item[$e]->login);
331                             if (strcmp($login, $chals->item[$e]->login) == 0) {
332                                 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$this->item[$i]->pass)."]");
333                                 
334                                 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
335                                     log_main("login_verify SUCCESS for ".$login);
336                                     
337                                     $chals->rem($login);
338                                     $ret = &$this->item[$i];
339                                     break;
340                                 }
341                             }
342                         } // end for ($e = 0 ...
343                     }
344                     
345                     if ($chals->ismod()) {
346                         Challenges::save_data(&$chals);
347                     }
348                     
349                     Challenges::unlock_data($a_sem);
350                 }
351                 break;
352             } //  if (strcasecmp($this->item[$i]->login, ...
353         }
354         
355         return ($ret);
356     }
357
358  } // if (0 == 1) {
359
360
361 ?>