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