DISABLE status for user managed
[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') AND (type & CAST (X'%08x' as integer)) = 0;",
90                             $G_dbpfx, escsql($login), USER_FLAG_TY_DISABLE);
91         if (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) != FALSE)
92             if (pg_numrows($user_pg) == 1)
93                 return TRUE;
94         
95         return FALSE;
96     }
97
98     function &getrecord_bylogin($login) {
99         GLOBAL $G_false, $G_dbpfx;
100
101         $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s') AND (type & CAST (X'%08x' as integer)) = 0;",  $G_dbpfx, escsql($login), USER_FLAG_TY_DISABLE);
102         if (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) == FALSE)
103             return $ret;
104         
105         if (pg_numrows($user_pg) != 1)
106             return $ret;
107         
108         $user_obj = pg_fetch_object($user_pg, 0);
109
110         return ($user_obj);
111     }
112
113
114     
115     function &login_verify($login, $pass)
116     {
117         GLOBAL $G_dbpfx, $G_false;
118         
119         $ret = &$G_false;
120         
121         log_main("login_verify: ".$login);
122         
123         
124         //O /* check the existence of the nick in the BriskDB */
125         //O for ($i = 0 ; $i < $this->item_n ; $i++) {
126         //O log_main("login_verify: BEGIN");
127         
128         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
129             return $ret;
130
131         log_main("login[".$user_obj->code."]: ".$user_obj->login);
132         
133         /* if it exists check for a valid challenge */
134         if (($a_sem = Challenges::lock_data()) != FALSE) { 
135             if (($chals = &Challenges::load_data()) != FALSE) {
136                 for ($e = 0 ; $e < $chals->item_n ; $e++) {
137                     
138                     log_main("challenge[".$e."]: ".$chals->item[$e]->login);
139                     if (strcmp($login, $chals->item[$e]->login) == 0) {
140                         log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
141                         
142                         if (strcmp($pass , md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
143                             log_main("login_verify SUCCESS for ".$login);
144                             
145                             $chals->rem($login);
146                             $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
147                             return ($ret);
148                             //O break;
149                         }
150                     }
151                 } // end for ($e = 0 ...
152             }
153             
154             if ($chals->ismod()) {
155                 Challenges::save_data(&$chals);
156             }
157             
158             Challenges::unlock_data($a_sem);
159         }
160         //O break;
161         // O } //  if (strcasecmp($this->item[$i]->login, ...
162         //O }
163     
164         return ($ret);
165     }
166
167     function &getitem_bylogin($login, &$id) {
168         GLOBAL $G_false;
169         
170         $ret = &$G_false;
171         $id = -1;
172         
173         log_main("getitem_bylogin: ".$login);
174         
175         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
176             return $ret;
177
178         $id = $user_obj->code;
179         return (LoginDBItem::LoginDBItemFromRecord($user_obj));
180     }
181     
182     // TODO FOR DB
183     function getmail($login)
184     {
185         log_main("getmail");
186
187         if (($ret = $this->getrecord_bylogin($login)) == FALSE)
188             return FALSE;
189         
190         return ($ret->email);
191     }
192
193     function addusers_from_olddb($olddb, &$cont)
194     {
195         GLOBAL $G_dbpfx;
196
197         for ($i = 0 ; $i < $olddb->count() ; $i++) {
198             $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
199                                 $G_dbpfx, escsql($olddb->item[$i]->login), escsql($olddb->item[$i]->pass),
200                                 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL); 
201             
202             // if ( ! (($user_pg = pg_exec($dbconn,$order_add_sql)) != FALSE && pg_affected_rows($order_pg) == 1) ) {
203
204             if ( ! (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
205                 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
206
207                 return FALSE;
208             }
209         }
210         return TRUE;
211     }
212
213     function &getdbconn()
214     {
215         $ret = $this->dbconn;
216         return ($ret);
217     }
218
219     //   ttok   text UNIQUE,      
220     //   tidx   
221     function bin5_points_save($date, $ttok, $tidx, $ucodes, $pts)
222     {
223         GLOBAL $G_dbpfx;
224
225         $is_trans = FALSE;
226         $ret = FALSE;
227
228         $n = count($ucodes);
229         /* check the existence of the nick in the BriskDB */
230         log_main("bin5_points_save: ");
231         
232         do {
233             if (pg_query($this->dbconn->db(), "BEGIN") == FALSE) {
234                 break;
235             }
236             $is_trans = TRUE;
237
238             /*
239              * matches management
240              */
241             $mtc_sql = sprintf("SELECT * FROM %sbin5_matches WHERE ttok = '%s';", $G_dbpfx, escsql($ttok));
242             if (($mtc_pg  = pg_query($this->dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
243                 // match not exists, insert it
244                 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx) VALUES ('%s', %d) RETURNING *;",
245                                    $G_dbpfx, escsql($ttok), $tidx);
246                 if ( ! (($mtc_pg  = pg_query($this->dbconn->db(), $mtc_sql)) != FALSE && 
247                         pg_affected_rows($mtc_pg) == 1) ) {
248                     log_crit(sprintf("bin5_points_save: failed at insert match [%s]", $mtc_sql));
249                     break;
250                 }
251             }
252             $mtc_obj = pg_fetch_object($mtc_pg,0);
253
254             /*
255              * games management
256              */
257             $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp) 
258                                                VALUES (%d, to_timestamp(%d)) RETURNING *;",
259                                $G_dbpfx, $mtc_obj->code, $date);
260             if ( ! (($gam_pg  = pg_query($this->dbconn->db(), $gam_sql)) != FALSE && 
261                     pg_affected_rows($gam_pg) == 1) ) {
262                 log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
263                 break;                        
264             }
265         
266             $gam_obj = pg_fetch_object($gam_pg,0);
267
268             /*
269              * points management
270              */
271             for ($i = 0 ; $i < $n ; $i++) {
272                 /* put points */
273                 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts) 
274                                                VALUES (%d, %d, %d);",
275                                    $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
276                 if ( ! (($pts_pg  = pg_query($this->dbconn->db(), $pts_sql)) != FALSE && 
277                         pg_affected_rows($pts_pg) == 1) ) {
278                     log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
279                     break;                        
280                 }
281             }
282
283             if ($i < $n)
284                 break;
285             
286             if (pg_query($this->dbconn->db(), "COMMIT") == FALSE) {
287                 break;
288             }
289              
290             $is_trans = FALSE;
291
292             $ret =  TRUE;
293         } while (0);
294         
295         if ($is_trans)
296             pg_query($this->dbconn-db(), "ROLLBACK");
297         
298         return $ret;
299     }
300
301 } // End class BriskDB
302
303 class LoginDBOld 
304 {
305     var $item;
306     var $item_n;
307
308     function LoginDBOld($filename)
309     {
310         GLOBAL $DOCUMENT_ROOT;
311         log_main("LoginDBOld create:start");
312
313         if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
314             require("$DOCUMENT_ROOT/Etc/".$filename);
315         }
316         else {
317             return (FALSE);
318         }
319         $this->item_n = count($this->item);
320         log_main("LoginDBOld create:end");
321     }
322
323     function count()
324     {
325         return ($this->item_n);
326     }
327
328
329
330 }
331
332
333     if (0 == 1) {
334         
335         
336         
337         
338         
339         
340         function count()
341         {
342         // sprintf("select count(code) from %sbrisk");
343         return ($this->item_n);
344     }
345
346     function getlogin_byidx($idx)
347     {
348         if ($idx >= $this->item_n)
349             return FALSE;
350         return ($this->item[$idx]->login);
351     }
352
353     function &getitem_bylogin($login, &$id)
354         {
355             GLOBAL $G_false;
356
357             log_main("login_exists: ".$login);
358     
359             /* check the existence of the nick in the LoginDB */
360             for ($i = 0 ; $i < $this->item_n ; $i++) {
361                 if (strcasecmp($this->item[$i]->login, $login) == 0) {
362                     log_main("login[".$i."]: ".$this->item[$i]->login);
363                     $ret = &$this->item[$i];
364                     $id = $i;
365                     return ($ret);
366                 }
367             }
368             $id = -1;
369             return ($G_false);
370         }
371
372     function getmail($login)
373     {
374         log_main("getmail");
375     
376         /* check the existence of the nick in the LoginDB */
377         for ($i = 0 ; $i < $this->item_n ; $i++) {
378             if (strcasecmp($this->item[$i]->login, $login) == 0) {
379                 log_main("login[".$i."]: ".$this->item[$i]->login);
380                 return ($this->item[$i]->email);
381             }
382         }
383         return (FALSE);
384     }
385
386     function gettype($login)
387     {
388         log_main("getmail");
389     
390         /* check the existence of the nick in the LoginDB */
391         for ($i = 0 ; $i < $this->item_n ; $i++) {
392             if (strcasecmp($this->item[$i]->login, $login) == 0) {
393                 log_main("login[".$i."]: ".$this->item[$i]->login);
394                 return ($this->item[$i]->type);
395             }
396         }
397         return (FALSE);
398     }
399
400     function &login_verify($login, $pass)
401     {
402         GLOBAL $G_false;
403         
404         $ret = &$G_false;
405         
406         log_main("login_verify: ".$login);
407         
408         /* check the existence of the nick in the LoginDB */
409         for ($i = 0 ; $i < $this->item_n ; $i++) {
410             log_main("login_verify: LOOP");
411             if (strcasecmp($this->item[$i]->login, $login) == 0) {
412                 log_main("login[".$i."]: ".$this->item[$i]->login);
413                 
414                 /* if it exists check for a valid challenge */
415                 if (($a_sem = Challenges::lock_data()) != FALSE) { 
416                     
417                     if (($chals = &Challenges::load_data()) != FALSE) {
418                         for ($e = 0 ; $e < $chals->item_n ; $e++) {
419                             
420                             log_main("challenge[".$i."]: ".$chals->item[$e]->login);
421                             if (strcmp($login, $chals->item[$e]->login) == 0) {
422                                 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$this->item[$i]->pass)."]");
423                                 
424                                 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
425                                     log_main("login_verify SUCCESS for ".$login);
426                                     
427                                     $chals->rem($login);
428                                     $ret = &$this->item[$i];
429                                     break;
430                                 }
431                             }
432                         } // end for ($e = 0 ...
433                     }
434                     
435                     if ($chals->ismod()) {
436                         Challenges::save_data(&$chals);
437                     }
438                     
439                     Challenges::unlock_data($a_sem);
440                 }
441                 break;
442             } //  if (strcasecmp($this->item[$i]->login, ...
443         }
444         
445         return ($ret);
446     }
447
448  } // if (0 == 1) {
449
450
451 ?>