b882c63a13c35fa0b19fac0196cde0162487ce40
[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, PGSQL_CONNECT_FORCE_NEW))) {
53                 return (FALSE);
54             }
55         }
56
57         $out = new DBConn();
58
59         return $out;
60     }
61
62     static function destroy()
63     {
64         if (DBConn::$dbcnnx != FALSE) {
65             DBConn::$dbcnnx = FALSE;
66             return (pg_close(DBConn::$dbcnnx));
67         }
68         return TRUE;
69     }
70
71     static function recover()
72     {
73         self::destroy();
74         return (self::create());
75     }
76
77     function db()
78     {
79         return ($this->db);
80     }
81 }
82
83 class BriskDB
84 {
85     var $dbconn;
86     var $item;
87     var $item_n;
88
89     function BriskDB($dbconn)
90     {
91         $this->dbconn = $dbconn;
92     }
93
94     static function create()
95     {
96         GLOBAL $DOCUMENT_ROOT, $G_dbpfx;
97
98         $ret = FALSE;
99
100         log_main("BriskDB create:start");
101
102         do {
103             if (($dbconn = DBConn::create()) == FALSE) {
104                 break;
105             }
106
107             $ret = new BriskDB($dbconn);
108         } while (0);
109
110         return ($ret);
111     }
112
113     function query($sql)
114     {
115         if (($res = pg_query($this->dbconn->db(), $sql)) == FALSE) {
116             // try to recover the connection
117             if (($this->dbconn = DBConn::recover()) == FALSE)
118                 return FALSE;
119             return (pg_query($this->dbconn->db(), $sql));
120         }
121
122         return ($res);
123     }
124
125     function last_error()
126     {
127         return pg_last_error($this->dbconn->db);
128     }
129
130     function users_load()
131     {
132     }
133
134     function login_exists($login)
135     {
136         GLOBAL $G_dbpfx;
137
138         /* check the existence of the nick in the BriskDB */
139         log_main("login_exists: ".$login);
140
141         $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s')",
142                             $G_dbpfx, escsql($login));
143         if (($user_pg = $this->query($user_sql)) != FALSE)
144             if (pg_numrows($user_pg) == 1)
145                 return TRUE;
146
147         return FALSE;
148     }
149
150     function getrecord_bylogin($login) {
151         GLOBAL $G_dbpfx;
152
153         $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);
154         if (($user_pg  = $this->query($user_sql)) == FALSE) {
155             return FALSE;
156         }
157         if (pg_numrows($user_pg) != 1)
158             return FALSE;
159
160         $user_obj = pg_fetch_object($user_pg, 0);
161
162         return ($user_obj);
163     }
164
165     function user_add($login, $pass, $email, $type, $disa_reas, $guar_code) {
166         GLOBAL $G_dbpfx;
167
168         $usr_sql = sprintf("INSERT INTO %susers (login, pass, email, type, disa_reas, guar_code, lintm)
169                             VALUES ('%s', '%s', '%s', %d, %d, %d, now()) RETURNING *;",
170                            $G_dbpfx, escsql(strtolower($login)), escsql($pass), escsql($email),
171                            $type, $disa_reas, $guar_code);
172
173         if (! (($usr_pg  = $this->query($usr_sql)) != FALSE && pg_affected_rows($usr_pg) == 1) ) {
174             return FALSE;
175         }
176         $usr_obj = pg_fetch_object($usr_pg, 0);
177
178         return $usr_obj;
179     }
180
181     function transaction($cmd) {
182         if ($cmd != "BEGIN" && $cmd != "COMMIT" && $cmd != "ROLLBACK")
183             return FALSE;
184
185         $trans_sql = sprintf("%s;", $cmd);
186         if (($trans_pg  = $this->query($trans_sql)) == FALSE) {
187             return FALSE;
188         }
189
190         return (TRUE);
191     }
192
193     /*
194       to be able to add mail record code into the record itself I must reserve it before.
195      */
196     function mail_reserve_code() {
197         GLOBAL $G_dbpfx;
198
199         $mail_sql = sprintf("SELECT nextval('%smails_code_seq'::regclass) AS nextval;", $G_dbpfx);
200         if (($mail_pg  = $this->query($mail_sql)) == FALSE) {
201             return FALSE;
202         }
203         if (pg_numrows($mail_pg) != 1)
204             return FALSE;
205
206         $mail_obj = pg_fetch_object($mail_pg, 0);
207
208         return ($mail_obj->nextval);
209     }
210
211     function check_record_by_login_or_email($login, $email) {
212         GLOBAL $G_dbpfx;
213
214         $arr_fie = array('login', 'email');
215         $arr_val = array($login, $email);
216
217         for ($i = 0 ; $i < 2 ; $i++) {
218             $user_sql = sprintf("SELECT * FROM %susers WHERE %s = lower('%s');",
219                                 $G_dbpfx, $arr_fie[$i], escsql($arr_val[$i]));
220             if (($user_pg  = $this->query($user_sql)) == FALSE) {
221                 fprintf(STDERR, "QUERY [%s]_ FALSE", $user_sql);
222                 return (3);
223             }
224             if (pg_numrows($user_pg) == 1) {
225                 return ($i + 1);
226             }
227         }
228
229         return (0);
230     }
231
232     function getrecord_bycode($code) {
233         GLOBAL $G_dbpfx;
234
235         $user_sql = sprintf("SELECT * FROM %susers WHERE code = %d;",  $G_dbpfx, $code);
236         if (($user_pg  = $this->query($user_sql)) == FALSE) {
237             return FALSE;
238         }
239         if (pg_numrows($user_pg) != 1)
240             return FALSE;
241
242         $user_obj = pg_fetch_object($user_pg, 0);
243
244         return ($user_obj);
245     }
246
247     function user_update_login_time($code, $lintm)
248     {
249         GLOBAL $G_dbpfx;
250
251         $user_sql = sprintf("UPDATE %susers SET (lintm) = (date 'epoch' + %d * INTERVAL '1 second') WHERE code = %d;", $G_dbpfx, $lintm, $code);
252
253         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
254              return FALSE;
255         }
256
257         return TRUE;
258     }
259
260     function user_update_flag_ty($code, $old_type, $old_reas, $type, $reas)
261     {
262         GLOBAL $G_dbpfx;
263
264         // (u.type & (CAST (X'00ff0000' as integer)))
265
266         $user_sql = sprintf("UPDATE %susers SET (type, disa_reas) = ((%d & (CAST (X'00ff0000' as integer))), %d)
267 WHERE code = %d AND (type & CAST (X'%08x' as integer)) != 0 AND disa_reas = %d;",
268                             $G_dbpfx, $type, $reas, $code, $old_type, $old_reas);
269
270         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
271              return FALSE;
272         }
273
274         return TRUE;
275     }
276
277     function user_update_passwd($code, $passwd)
278     {
279         GLOBAL $G_dbpfx;
280
281         $user_sql = sprintf("UPDATE %susers SET (pass) = (md5('%s')) WHERE code = %d;",
282                             $G_dbpfx, $passwd, $code);
283
284         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
285              return FALSE;
286         }
287
288         return TRUE;
289     }
290
291     function user_prefs_update($code, $flags, $supp_comp)
292     {
293         GLOBAL $G_dbpfx;
294
295         $user_sql = sprintf("UPDATE %susers SET (type, supp_comp) = (%d, '%s') WHERE code = %d;",
296                             $G_dbpfx, $flags, escsql($supp_comp), $code);
297         fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
298         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
299              return FALSE;
300         }
301         fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
302
303         return TRUE;
304     }
305
306     function user_state_update($code, $flags, $disa_reas)
307     {
308         GLOBAL $G_dbpfx;
309
310         $user_sql = sprintf("UPDATE %susers SET (type, disa_reas) = (%d, %d) WHERE code = %d;",
311                             $G_dbpfx, $flags, $disa_reas, $code);
312         fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
313         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
314              return FALSE;
315         }
316         fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
317
318         return TRUE;
319     }
320
321     function user_tos_update($code, $tos_vers)
322     {
323         GLOBAL $G_dbpfx;
324
325         $user_sql = sprintf("UPDATE %susers SET (tos_vers) = ('%s') WHERE code = %d;",
326                             $G_dbpfx, escsql($tos_vers), $code);
327         fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
328         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
329              return FALSE;
330         }
331         fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
332
333         return TRUE;
334     }
335
336     /*
337       if success return a LoginDBItem object
338      */
339     function login_verify($login, $pass)
340     {
341         GLOBAL $G_dbpfx;
342
343         $ret = FALSE;
344
345         log_main("login_verify: ".$login);
346
347         //O /* check the existence of the nick in the BriskDB */
348         //O for ($i = 0 ; $i < $this->item_n ; $i++) {
349         //O log_main("login_verify: BEGIN");
350
351         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE) {
352             return FALSE;
353         }
354
355         log_main("login[".$user_obj->code."]: ".$user_obj->login);
356
357         /* if it exists check for a valid challenge */
358         if (($a_sem = Challenges::lock_data(TRUE)) != FALSE) {
359             if (($chals = &Challenges::load_data()) != FALSE) {
360                 for ($e = 0 ; $e < $chals->item_n ; $e++) {
361                     log_main("challenge[".$e."]: ".$chals->item[$e]->login);
362                     if (strcmp($login, $chals->item[$e]->login) == 0) {
363                         log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
364
365                         if (strcmp($pass, md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
366                             log_main("login_verify SUCCESS for ".$login);
367
368                             $chals->rem($login);
369                             $this->user_update_login_time($user_obj->code, time());
370                             $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
371                             break;
372                         }
373                     }
374                 } // end for ($e = 0 ...
375             }
376
377             if ($chals->ismod()) {
378                 Challenges::save_data(&$chals);
379             }
380
381             Challenges::unlock_data($a_sem);
382         }
383         //O break;
384         // O } //  if (strcasecmp($this->item[$i]->login, ...
385         //O }
386
387         return ($ret);
388     }
389
390     function getitem_bylogin($login, &$id) {
391         $ret = FALSE;
392         $id = -1;
393
394         log_main("getitem_bylogin: ".$login);
395
396         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
397             return $ret;
398
399         $id = $user_obj->code;
400         return (LoginDBItem::LoginDBItemFromRecord($user_obj));
401     }
402
403     function getitem_bycode($code) {
404         $ret = FALSE;
405
406         log_main("getitem_bycode: ".$code);
407
408         if (($user_obj = $this->getrecord_bycode($code)) == FALSE)
409             return $ret;
410
411         return (LoginDBItem::LoginDBItemFromRecord($user_obj));
412     }
413
414     // TODO FOR DB
415     function getmail($login)
416     {
417         log_main("getmail");
418
419         if (($ret = $this->getrecord_bylogin($login)) == FALSE)
420             return FALSE;
421
422         return ($ret->email);
423     }
424
425     function addusers_from_olddb($olddb, &$cont)
426     {
427         GLOBAL $G_dbpfx;
428
429         for ($i = 0 ; $i < $olddb->count() ; $i++) {
430             $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
431                                 $G_dbpfx, escsql(strtolower($olddb->item[$i]->login)), escsql($olddb->item[$i]->pass),
432                                 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL);
433
434             if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
435                 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
436
437                 return FALSE;
438             }
439         }
440         return TRUE;
441     }
442
443     function getdbconn()
444     {
445         return ($this->dbconn);
446     }
447
448     // return array of array('code', 'login' [, 'first', 'last', 'tidx']) ordered by table position
449     function users_get($match_code, $with_minmaxtidx, $is_newmatch)
450     {
451         GLOBAL $G_dbpfx;
452
453         if ($is_newmatch) { // is new
454             $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s
455                                   FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p,
456                                        %susers AS u, %sbin5_table_orders AS o
457                                   WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode
458                                        AND m.code = o.mcode AND u.code = o.ucode AND m.code = %d
459                                   GROUP BY u.code, u.login%s, o.pos
460                                   ORDER BY o.pos;",
461                                ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""),
462                                $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code,
463                                ($with_minmaxtidx ? ", m.tidx" : ""));
464         }
465         else { // is old
466             $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s
467                                   FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u
468                                   WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d
469                                   GROUP BY u.code, u.login%s;",
470                                ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""),
471                                $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code,
472                                ($with_minmaxtidx ? ", m.tidx" : ""));
473         }
474
475         if (($usr_pg  = pg_query($this->dbconn->db(), $usr_sql)) == FALSE ) {
476             log_crit(sprintf("%s::%s: pg_query usr_sql failed [%s]", __CLASS__, __FUNCTION__, $usr_sql));
477             return (FALSE);
478         }
479         $usr_n = pg_numrows($usr_pg);
480         if ($usr_n != BIN5_PLAYERS_N) {
481             log_crit(sprintf("%s::%s: wrong number of players [%s] %d", __CLASS__, __FUNCTION__, $usr_sql, $usr_n));
482             return (FALSE);
483         }
484         $users = array();
485
486         if ($with_minmaxtidx)
487             $fields = array('code', 'login', 'first', 'last', 'tidx');
488         else
489             $fields = array('code', 'login');
490
491         for ($u = 0 ; $u < $usr_n ; $u++) {
492             $usr_obj = pg_fetch_object($usr_pg, $u);
493             $users[$u] = array();
494             foreach($fields as $field) {
495                 $users[$u][$field] = $usr_obj->$field;
496             }
497         }
498         return ($users);
499     }
500
501     // out: tab->{points,points_n,old_reason}, in: tab->ttok
502     function match_continue($match_code, $table, $tidx)
503     {
504         GLOBAL $G_dbpfx;
505         $sql_ttok = escsql($table->table_token);
506
507         if (($users = $this->users_get($match_code, FALSE /*without minmaxidx*/, TRUE /*new game*/)) == FALSE) {
508             log_crit(sprintf("%s::%s: retrieve users fails", __CLASS__, __FUNCTION__));
509             return (FALSE);
510         }
511
512         $num_sql = sprintf("SELECT count(*) AS points_n FROM %sbin5_games WHERE mcode = %d;", $G_dbpfx, $match_code);
513         if (($num_pg  = $this->query($num_sql)) == FALSE || pg_numrows($num_pg) != 1) {
514             log_crit(sprintf("%s::%s: get games number fails", __CLASS__, __FUNCTION__));
515             return (FALSE);
516         }
517         $num_obj = pg_fetch_object($num_pg, 0);
518         $table->points_n = $num_obj->points_n;
519
520         // TAG: POINTS_MANAGEMENT
521         $tot_sql = sprintf("SELECT sum(p.pts * (2^g.mult)) AS pts
522                             FROM %sbin5_games AS g, %sbin5_points AS p, %susers AS u,
523                                  %sbin5_table_orders AS o
524                             WHERE g.mcode = %d AND g.code = p.gcode AND p.ucode = u.code
525                                   AND p.ucode = o.ucode AND g.mcode = o.mcode
526                             GROUP BY p.ucode, o.pos
527                             ORDER BY o.pos;",
528                            $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code);
529         if (($tot_pg  = pg_query($this->dbconn->db(), $tot_sql)) == FALSE
530             || pg_numrows($tot_pg) != BIN5_PLAYERS_N) {
531             log_crit(sprintf("%s::%s: get games totals fails", __CLASS__, __FUNCTION__));
532             return(FALSE);
533         }
534
535         $u = 0;
536         foreach ($users as $user) {
537             // TAG: POINTS_MANAGEMENT
538             $pts_sql = sprintf("SELECT p.pts AS pts, g.mult AS mult
539                                     FROM %sbin5_points as p, %sbin5_games as g
540                                     WHERE p.gcode = g.code AND g.mcode = %d AND p.ucode = %d
541                                     ORDER BY g.tstamp ASC
542                                     LIMIT %d OFFSET %d;",
543                                $G_dbpfx, $G_dbpfx, $match_code, $user['code'],
544                                MAX_POINTS,
545                                ($num_obj->points_n < MAX_POINTS ? 0 : $num_obj->points_n - MAX_POINTS));
546
547             // points of the match for each user
548             if (($pts_pg  = $this->query($pts_sql)) == FALSE) {
549                 log_crit(sprintf("%s::%s: get points fails", __CLASS__, __FUNCTION__));
550                 return (FALSE);
551             }
552             $pts_n = pg_numrows($pts_pg);
553             if ($pts_n > $table->points_n) {
554                 // inconsistent scenario number of points great than number of games
555                 log_crit(sprintf("%s::%s: number of points great than number of games", __CLASS__, __FUNCTION__));
556                 return (FALSE);
557             }
558             // TAG: POINTS_MANAGEMENT
559             for ($i = 0 , $ct = $table->points_n - $pts_n; $ct < $table->points_n ; $ct++, $i++) {
560                 $pts_obj = pg_fetch_object($pts_pg, $i);
561                 $table->points[$ct % MAX_POINTS][$u] = $pts_obj->pts * pow(2, $pts_obj->mult);
562             }
563             $tot_obj = pg_fetch_object($tot_pg, $u);
564             $table->total[$u] = $tot_obj->pts;
565
566             $u++;
567         }
568
569         $gam_sql = sprintf("SELECT * FROM %sbin5_games WHERE mcode = %d ORDER BY tstamp DESC LIMIT 1;", $G_dbpfx, $match_code);
570         if (($gam_pg  = $this->query($gam_sql)) == FALSE || pg_numrows($gam_pg) != 1) {
571             log_crit(sprintf("%s::%s: get last game fails", __CLASS__, __FUNCTION__));
572             return (FALSE);
573         }
574         $gam_obj = pg_fetch_object($gam_pg, 0);
575
576         $table->old_reason = game_description($gam_obj->act, 'html', $gam_obj->mult,
577                                               $gam_obj->asta_win, ($gam_obj->asta_win != -1 ?
578                                                                    $users[$gam_obj->asta_win]['login'] : ""),
579                                               $gam_obj->friend, ($gam_obj->friend != -1 ?
580                                                                  $users[$gam_obj->friend]['login'] : ""),
581                                               $gam_obj->pnt, $gam_obj->asta_pnt);
582
583         // update matches with new ttok and table idx
584         $mtc_sql = sprintf("UPDATE %sbin5_matches SET (ttok, tidx) = ('%s', %d) WHERE code = %d RETURNING *;",
585                            $G_dbpfx, $sql_ttok, $tidx, $match_code);
586         if (($mtc_pg  = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
587             log_crit(sprintf("%s::%s: update matches table failed", __CLASS__, __FUNCTION__));
588             return (FALSE);
589         }
590
591         return (TRUE);
592     }
593
594     function match_order_get(&$match_data, $match_code, $exp_num)
595     {
596         GLOBAL $G_dbpfx;
597
598         $ord_sql = sprintf("SELECT ucode FROM %sbin5_table_orders WHERE mcode = %d ORDER BY pos ASC;",
599                            $G_dbpfx, $match_code);
600
601         if (($ord_pg  = $this->query($ord_sql)) == FALSE || pg_numrows($ord_pg) != $exp_num) {
602             log_crit(sprintf("%s: fails for id or users number", __FUNCTION__));
603             return (FALSE);
604         }
605
606         $ucodes = array();
607         for ($i = 0 ; $i < $exp_num ; $i++) {
608             $ord_obj = pg_fetch_object($ord_pg, $i);
609             $ucodes[$i] = $ord_obj->ucode;
610         }
611
612         if ($match_data !== NULL) {
613             $mtdt_sql = sprintf("SELECT * FROM %sbin5_matches WHERE code = %d;",
614                                 $G_dbpfx, $match_code);
615
616             if (($mtdt_pg  = $this->query($mtdt_sql)) == FALSE || pg_numrows($mtdt_pg) != 1) {
617                 log_crit(sprintf("%s: fails retrieve match_data values [%d]", __FUNCTION__, $match_code));
618                 return (FALSE);
619             }
620
621             $mtdt_obj = pg_fetch_object($mtdt_pg, 0);
622
623             foreach (array('ttok', 'tidx', 'mult_next', 'mazzo_next', 'tcode') as $match_name) {
624                 $match_data[$match_name] = $mtdt_obj->$match_name;
625             }
626         }
627
628         return ($ucodes);
629     }
630
631     //   ttok   text UNIQUE,
632     //   tidx
633     function bin5_points_save($date, $table, $tidx, $action, $ucodes, $pts)
634     {
635         GLOBAL $G_dbpfx;
636         $sql_ttok = escsql($table->table_token);
637
638         $is_trans = FALSE;
639         $ret = FALSE;
640
641         $n = count($ucodes);
642         /* check the existence of the nick in the BriskDB */
643         log_main("bin5_points_save: ");
644
645         do {
646             if ($this->query("BEGIN") == FALSE) {
647                 break;
648             }
649             $is_trans = TRUE;
650
651             /*
652              * matches management
653              */
654             $mtc_sql = sprintf("UPDATE %sbin5_matches SET (mazzo_next, mult_next) = (%d, %d) WHERE ttok = '%s' RETURNING *;",
655                                $G_dbpfx, $table->mazzo, $table->mult, $sql_ttok);
656             if (($mtc_pg  = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
657
658                 // match not exists, insert it
659                 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx, mazzo_next, mult_next) VALUES ('%s', %d, %d, %d) RETURNING *;",
660                                    $G_dbpfx, $sql_ttok, $tidx, $table->mazzo, $table->mult);
661                 if (($mtc_pg  = $this->query($mtc_sql)) == FALSE || pg_affected_rows($mtc_pg) != 1) {
662                     log_crit(sprintf("bin5_points_save: failed at insert match [%s]", $mtc_sql));
663                     break;
664                 }
665                 $mtc_obj = pg_fetch_object($mtc_pg, 0);
666
667                 for ($i = 0 ; $i < $n ; $i++) {
668                     $ord_sql = sprintf("INSERT INTO %sbin5_table_orders (mcode, ucode, pos) VALUES (%d, %d, %d);",
669                                        $G_dbpfx, $mtc_obj->code, $ucodes[$i], $i);
670                     if (($ord_pg = $this->query($ord_sql)) == FALSE || pg_affected_rows($ord_pg) != 1 ) {
671                         log_crit(sprintf("bin5_points_save: failed at insert table order [%s]", $ord_sql));
672                         break;
673                     }
674                 }
675                 if ($i < $n)
676                     break;
677             }
678             else {
679                 $mtc_obj = pg_fetch_object($mtc_pg,0);
680             }
681
682             /*
683              * games management
684              */
685             $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp, act, asta_pnt, pnt, asta_win, friend, mazzo, mult)
686                                                VALUES (%d, to_timestamp(%d), %d, %d, %d, %d, %d, %d, %d) RETURNING *;",
687                                $G_dbpfx, $mtc_obj->code, $date, $action,
688                                $table->old_asta_pnt, $table->old_pnt,
689                                $table->old_asta_win,
690                                $table->old_friend,
691                                $table->old_mazzo, $table->old_mult);
692             if (($gam_pg  = $this->query($gam_sql)) == FALSE || pg_affected_rows($gam_pg) != 1) {
693                 log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
694                 break;
695             }
696
697             $gam_obj = pg_fetch_object($gam_pg,0);
698
699             /*
700              * points management
701              */
702             for ($i = 0 ; $i < $n ; $i++) {
703                 /* put points */
704                 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts)
705                                                VALUES (%d, %d, %d);",
706                                    $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
707                 if (($pts_pg  = $this->query($pts_sql)) == FALSE || pg_affected_rows($pts_pg) != 1) {
708                     log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
709                     break;
710                 }
711             }
712             if ($i < $n)
713                 break;
714
715             if ($this->query("COMMIT") == FALSE) {
716                 break;
717             }
718
719             $is_trans = FALSE;
720
721             $table->match_id = $mtc_obj->code;
722             $ret = TRUE;
723         } while (0);
724
725         if ($is_trans)
726             $this->query("ROLLBACK");
727
728         return $ret;
729     }
730
731     function mail_add_fromitem($mail) {
732         GLOBAL $G_dbpfx;
733
734         $usr_sql = sprintf("
735 INSERT INTO %smails (code, ucode, type, tstamp, subj, body_txt, body_htm, hash)
736             VALUES (%d, %d, %d, to_timestamp(%d), '%s', '%s', '%s', '%s') RETURNING *;",
737                            $G_dbpfx, $mail->code, $mail->ucode, $mail->type, $mail->tstamp,
738                            escsql($mail->subj), escsql($mail->body_txt), escsql($mail->body_htm),
739                            ($mail->hash == NULL ? "" : escsql($mail->hash))
740                            );
741
742         if (! (($usr_pg  = $this->query($usr_sql)) != FALSE && pg_affected_rows($usr_pg) == 1) ) {
743             return FALSE;
744         }
745         $usr_obj = pg_fetch_object($usr_pg, 0);
746
747         return $usr_obj;
748     }
749
750     function mail_check($code, $type, $hash)
751     {
752         GLOBAL $G_dbpfx;
753
754         $mai_sql = sprintf("SELECT * FROM %smails WHERE code = %d AND type = %d AND hash = '%s';",
755                            $G_dbpfx, $code, $type, escsql($hash));
756         if (($mai_pg  = $this->query($mai_sql)) == FALSE || pg_numrows($mai_pg) != 1) {
757             // check failed
758             return (FALSE);
759         }
760
761         $mai_obj = pg_fetch_object($mai_pg, 0);
762         return ($mai_obj);
763     }
764
765     function mail_delete($code)
766     {
767         GLOBAL $G_dbpfx;
768
769         $mai_sql = sprintf("DELETE FROM %smails WHERE code = %d;", $G_dbpfx, $code);
770
771         if (($mai_pg = $this->query($mai_sql)) == FALSE || pg_affected_rows($mai_pg) != 1) {
772             return (FALSE);
773         }
774         return (TRUE);
775     }
776
777
778 } // End class BriskDB
779
780 class LoginDBOld
781 {
782     var $item;
783     var $item_n;
784
785     function LoginDBOld($filename)
786     {
787         GLOBAL $DOCUMENT_ROOT;
788         log_main("LoginDBOld create:start");
789
790         if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
791             require("$DOCUMENT_ROOT/Etc/".$filename);
792         }
793         else {
794             return (FALSE);
795         }
796         $this->item_n = count($this->item);
797         log_main("LoginDBOld create:end");
798     }
799
800     function count()
801     {
802         return ($this->item_n);
803     }
804
805 } // End class LoginDBOld
806
807 ?>