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