pull conflicts solved
[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         $this->db = DBConn::$dbcnnx;        
45     }
46
47     static function create()
48     {
49         GLOBAL $G_dbauth;
50         
51         if (DBConn::$dbcnnx == FALSE) {
52             if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth))) {
53                 return (FALSE);
54             }
55         }
56
57         $out = new DBConn();
58         
59         return $out;
60     }
61     function db()
62     {
63         return ($this->db);
64     }
65 }
66
67 class BriskDB
68 {
69     var $dbconn;
70     var $item;
71     var $item_n;
72     
73     function BriskDB($dbconn)
74     {
75         $this->dbconn = $dbconn;
76     }
77
78     static function create()
79     {
80         GLOBAL $DOCUMENT_ROOT, $G_dbpfx;
81
82         $ret = FALSE;
83
84         log_main("BriskDB create:start");
85         
86         do {
87             if (($dbconn = DBConn::create()) == FALSE) 
88                 break;
89             
90             $ret = new BriskDB($dbconn);
91         } while (0);
92         
93         return ($ret);
94     }
95
96     function users_load()
97     {
98     }
99     
100     function login_exists($login)
101     {
102         GLOBAL $G_dbpfx;
103
104         /* check the existence of the nick in the BriskDB */
105         log_main("login_exists: ".$login);
106         
107         $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s') AND (type & CAST (X'%08x' as integer)) = 0;",
108                             $G_dbpfx, escsql($login), USER_FLAG_TY_DISABLE);
109         if (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) != FALSE)
110             if (pg_numrows($user_pg) == 1)
111                 return TRUE;
112         
113         return FALSE;
114     }
115
116     function getrecord_bylogin($login) {
117         GLOBAL $G_dbpfx;
118
119         $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);
120         if (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) == FALSE)
121             return FALSE;
122         
123         if (pg_numrows($user_pg) != 1)
124             return FALSE;
125         
126         $user_obj = pg_fetch_object($user_pg, 0);
127
128         return ($user_obj);
129     }
130
131
132     
133     function login_verify($login, $pass)
134     {
135         GLOBAL $G_dbpfx;
136         
137         $ret = FALSE;
138         
139         log_main("login_verify: ".$login);
140         
141         
142         //O /* check the existence of the nick in the BriskDB */
143         //O for ($i = 0 ; $i < $this->item_n ; $i++) {
144         //O log_main("login_verify: BEGIN");
145         
146         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE) {
147             return FALSE;
148         }
149
150         log_main("login[".$user_obj->code."]: ".$user_obj->login);
151         
152         /* if it exists check for a valid challenge */
153         if (($a_sem = Challenges::lock_data()) != FALSE) { 
154             // error_log("loop-2", 0);
155             if (($chals = &Challenges::load_data()) != FALSE) {
156                 // error_log("loop-1", 0);
157                 for ($e = 0 ; $e < $chals->item_n ; $e++) {
158                     // error_log("loop", 0);
159                     log_main("challenge[".$e."]: ".$chals->item[$e]->login);
160                     if (strcmp($login, $chals->item[$e]->login) == 0) {
161                         // error_log("loop2", 0);
162                         log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
163                         
164                         if (strcmp($pass, md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
165                             // error_log("loop3", 0);
166                             log_main("login_verify SUCCESS for ".$login);
167                             
168                             $chals->rem($login);
169                             $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
170                             // error_log("dbitem: ".print_r($ret, TRUE));
171                             break;
172                         }
173                     }
174                 } // end for ($e = 0 ...
175                 // error_log("end loop-1", 0);
176             }
177             
178             if ($chals->ismod()) {
179                 Challenges::save_data(&$chals);
180             }
181             
182             Challenges::unlock_data($a_sem);
183         }
184         //O break;
185         // O } //  if (strcasecmp($this->item[$i]->login, ...
186         //O }
187     
188         return ($ret);
189     }
190
191     function getitem_bylogin($login, &$id) {
192         $ret = FALSE;
193         $id = -1;
194         
195         log_main("getitem_bylogin: ".$login);
196         
197         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
198             return $ret;
199
200         $id = $user_obj->code;
201         return (LoginDBItem::LoginDBItemFromRecord($user_obj));
202     }
203     
204     // TODO FOR DB
205     function getmail($login)
206     {
207         log_main("getmail");
208
209         if (($ret = $this->getrecord_bylogin($login)) == FALSE)
210             return FALSE;
211         
212         return ($ret->email);
213     }
214
215     function addusers_from_olddb($olddb, &$cont)
216     {
217         GLOBAL $G_dbpfx;
218
219         for ($i = 0 ; $i < $olddb->count() ; $i++) {
220             $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
221                                 $G_dbpfx, escsql(strtolower($olddb->item[$i]->login)), escsql($olddb->item[$i]->pass),
222                                 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL); 
223             
224             // if ( ! (($user_pg = pg_exec($dbconn,$order_add_sql)) != FALSE && pg_affected_rows($order_pg) == 1) ) {
225
226             if ( ! (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
227                 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
228
229                 return FALSE;
230             }
231         }
232         return TRUE;
233     }
234
235     function getdbconn()
236     {
237         return ($this->dbconn);
238     }
239
240     //   ttok   text UNIQUE,      
241     //   tidx   
242     function bin5_points_save($date, $ttok, $tidx, $ucodes, $pts)
243     {
244         GLOBAL $G_dbpfx;
245
246         $is_trans = FALSE;
247         $ret = FALSE;
248
249         $n = count($ucodes);
250         /* check the existence of the nick in the BriskDB */
251         log_main("bin5_points_save: ");
252         
253         do {
254             if (pg_query($this->dbconn->db(), "BEGIN") == FALSE) {
255                 break;
256             }
257             $is_trans = TRUE;
258
259             /*
260              * matches management
261              */
262             $mtc_sql = sprintf("SELECT * FROM %sbin5_matches WHERE ttok = '%s';", $G_dbpfx, escsql($ttok));
263             if (($mtc_pg  = pg_query($this->dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
264                 // match not exists, insert it
265                 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx) VALUES ('%s', %d) RETURNING *;",
266                                    $G_dbpfx, escsql($ttok), $tidx);
267                 if ( ! (($mtc_pg  = pg_query($this->dbconn->db(), $mtc_sql)) != FALSE && 
268                         pg_affected_rows($mtc_pg) == 1) ) {
269                     log_crit(sprintf("bin5_points_save: failed at insert match [%s]", $mtc_sql));
270                     break;
271                 }
272             }
273             $mtc_obj = pg_fetch_object($mtc_pg,0);
274
275             /*
276              * games management
277              */
278             $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp) 
279                                                VALUES (%d, to_timestamp(%d)) RETURNING *;",
280                                $G_dbpfx, $mtc_obj->code, $date);
281             if ( ! (($gam_pg  = pg_query($this->dbconn->db(), $gam_sql)) != FALSE && 
282                     pg_affected_rows($gam_pg) == 1) ) {
283                 log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
284                 break;                        
285             }
286         
287             $gam_obj = pg_fetch_object($gam_pg,0);
288
289             /*
290              * points management
291              */
292             for ($i = 0 ; $i < $n ; $i++) {
293                 /* put points */
294                 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts) 
295                                                VALUES (%d, %d, %d);",
296                                    $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
297                 if ( ! (($pts_pg  = pg_query($this->dbconn->db(), $pts_sql)) != FALSE && 
298                         pg_affected_rows($pts_pg) == 1) ) {
299                     log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
300                     break;                        
301                 }
302             }
303
304             if ($i < $n)
305                 break;
306             
307             if (pg_query($this->dbconn->db(), "COMMIT") == FALSE) {
308                 break;
309             }
310              
311             $is_trans = FALSE;
312
313             $ret =  TRUE;
314         } while (0);
315         
316         if ($is_trans)
317             pg_query($this->dbconn-db(), "ROLLBACK");
318         
319         return $ret;
320     }
321
322 } // End class BriskDB
323
324 class LoginDBOld 
325 {
326     var $item;
327     var $item_n;
328
329     function LoginDBOld($filename)
330     {
331         GLOBAL $DOCUMENT_ROOT;
332         log_main("LoginDBOld create:start");
333
334         if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
335             require("$DOCUMENT_ROOT/Etc/".$filename);
336         }
337         else {
338             return (FALSE);
339         }
340         $this->item_n = count($this->item);
341         log_main("LoginDBOld create:end");
342     }
343
344     function count()
345     {
346         return ($this->item_n);
347     }
348
349 } // End class LoginDBOld
350
351 ?>