bin5_points_save function added
[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
212     function &getdbconn()
213     {
214         $ret = $this->dbconn;
215         return ($ret);
216     }
217
218     //   ttok   text UNIQUE,      
219     //   tidx   
220     function bin5_points_save($date, $ttok, $tidx, $ucodes, $pts)
221     {
222         GLOBAL $G_dbpfx;
223
224         $is_trans = FALSE;
225         $ret = FALSE;
226
227         $n = count($ucodes);
228         /* check the existence of the nick in the BriskDB */
229         log_main("bin5_points_save: ");
230         
231         do {
232             if (pg_query($this->dbconn->db(), "BEGIN") == FALSE) {
233                 break;
234             }
235             $is_trans = TRUE;
236
237             /*
238              * matches management
239              */
240             $mtc_sql = sprintf("SELECT * FROM %sbin5_matches WHERE ttok = '%s';", $G_dbpfx, escsql($ttok));
241             if (($mtc_pg  = pg_query($this->dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
242                 // match not exists, insert it
243                 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx) VALUES ('%s', %d) RETURNING *;",
244                                    $G_dbpfx, escsql($ttok), $tidx);
245                 if ( ! (($mtc_pg  = pg_query($this->dbconn->db(), $mtc_sql)) != FALSE && 
246                         pg_affected_rows($mtc_pg) == 1) ) {
247                     log_crit(sprintf("bin5_points_save: failed at insert match [%s]", $mtc_sql));
248                     break;
249                 }
250             }
251             $mtc_obj = pg_fetch_object($mtc_pg,0);
252
253             /*
254              * games management
255              */
256             $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp) 
257                                                VALUES (%d, to_timestamp(%d)) RETURNING *;",
258                                $G_dbpfx, $mtc_obj->code, $date);
259             if ( ! (($gam_pg  = pg_query($this->dbconn->db(), $gam_sql)) != FALSE && 
260                     pg_affected_rows($gam_pg) == 1) ) {
261                 log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
262                 break;                        
263             }
264         
265             $gam_obj = pg_fetch_object($gam_pg,0);
266
267             /*
268              * points management
269              */
270             for ($i = 0 ; $i < $n ; $i++) {
271                 /* put points */
272                 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts) 
273                                                VALUES (%d, %d, %d);",
274                                    $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
275                 if ( ! (($pts_pg  = pg_query($this->dbconn->db(), $pts_sql)) != FALSE && 
276                         pg_affected_rows($pts_pg) == 1) ) {
277                     log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
278                     break;                        
279                 }
280             }
281
282             if ($i < $n)
283                 break;
284             
285             if (pg_query($this->dbconn->db(), "COMMIT") == FALSE) {
286                 break;
287             }
288              
289             $is_trans = FALSE;
290
291             $ret =  TRUE;
292         } while (0);
293         
294         if ($is_trans)
295             pg_query($this->dbconn-db(), "ROLLBACK");
296         
297         return $ret;
298     }
299
300 } // End class BriskDB
301
302 class LoginDBOld 
303 {
304     var $item;
305     var $item_n;
306
307     function LoginDBOld($filename)
308     {
309         GLOBAL $DOCUMENT_ROOT;
310         log_main("LoginDBOld create:start");
311
312         if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
313             require("$DOCUMENT_ROOT/Etc/".$filename);
314         }
315         else {
316             return (FALSE);
317         }
318         $this->item_n = count($this->item);
319         log_main("LoginDBOld create:end");
320     }
321
322     function count()
323     {
324         return ($this->item_n);
325     }
326
327
328
329 }
330
331
332     if (0 == 1) {
333         
334         
335         
336         
337         
338         
339         function count()
340         {
341         // sprintf("select count(code) from %sbrisk");
342         return ($this->item_n);
343     }
344
345     function getlogin_byidx($idx)
346     {
347         if ($idx >= $this->item_n)
348             return FALSE;
349         return ($this->item[$idx]->login);
350     }
351
352     function &getitem_bylogin($login, &$id)
353         {
354             GLOBAL $G_false;
355
356             log_main("login_exists: ".$login);
357     
358             /* check the existence of the nick in the LoginDB */
359             for ($i = 0 ; $i < $this->item_n ; $i++) {
360                 if (strcasecmp($this->item[$i]->login, $login) == 0) {
361                     log_main("login[".$i."]: ".$this->item[$i]->login);
362                     $ret = &$this->item[$i];
363                     $id = $i;
364                     return ($ret);
365                 }
366             }
367             $id = -1;
368             return ($G_false);
369         }
370
371     function getmail($login)
372     {
373         log_main("getmail");
374     
375         /* check the existence of the nick in the LoginDB */
376         for ($i = 0 ; $i < $this->item_n ; $i++) {
377             if (strcasecmp($this->item[$i]->login, $login) == 0) {
378                 log_main("login[".$i."]: ".$this->item[$i]->login);
379                 return ($this->item[$i]->email);
380             }
381         }
382         return (FALSE);
383     }
384
385     function gettype($login)
386     {
387         log_main("getmail");
388     
389         /* check the existence of the nick in the LoginDB */
390         for ($i = 0 ; $i < $this->item_n ; $i++) {
391             if (strcasecmp($this->item[$i]->login, $login) == 0) {
392                 log_main("login[".$i."]: ".$this->item[$i]->login);
393                 return ($this->item[$i]->type);
394             }
395         }
396         return (FALSE);
397     }
398
399     function &login_verify($login, $pass)
400     {
401         GLOBAL $G_false;
402         
403         $ret = &$G_false;
404         
405         log_main("login_verify: ".$login);
406         
407         /* check the existence of the nick in the LoginDB */
408         for ($i = 0 ; $i < $this->item_n ; $i++) {
409             log_main("login_verify: LOOP");
410             if (strcasecmp($this->item[$i]->login, $login) == 0) {
411                 log_main("login[".$i."]: ".$this->item[$i]->login);
412                 
413                 /* if it exists check for a valid challenge */
414                 if (($a_sem = Challenges::lock_data()) != FALSE) { 
415                     
416                     if (($chals = &Challenges::load_data()) != FALSE) {
417                         for ($e = 0 ; $e < $chals->item_n ; $e++) {
418                             
419                             log_main("challenge[".$i."]: ".$chals->item[$e]->login);
420                             if (strcmp($login, $chals->item[$e]->login) == 0) {
421                                 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$this->item[$i]->pass)."]");
422                                 
423                                 if (strcmp($pass , md5($chals->item[$e]->token.$this->item[$i]->pass)) == 0) {
424                                     log_main("login_verify SUCCESS for ".$login);
425                                     
426                                     $chals->rem($login);
427                                     $ret = &$this->item[$i];
428                                     break;
429                                 }
430                             }
431                         } // end for ($e = 0 ...
432                     }
433                     
434                     if ($chals->ismod()) {
435                         Challenges::save_data(&$chals);
436                     }
437                     
438                     Challenges::unlock_data($a_sem);
439                 }
440                 break;
441             } //  if (strcasecmp($this->item[$i]->login, ...
442         }
443         
444         return ($ret);
445     }
446
447  } // if (0 == 1) {
448
449
450 ?>