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